Saad110jb commited on
Commit
f2a3546
·
verified ·
1 Parent(s): e8cb105

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +72 -0
main.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import anyio
4
+ from fastapi import FastAPI, UploadFile, File, HTTPException
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from scripts.worker import RegentsAIEngine
7
+
8
+ # 1. Initialize FastAPI
9
+ app = FastAPI(title="Regents AI Worker")
10
+
11
+ # 2. ENABLE CORS (Crucial for Mobile App Connection)
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"], # In production, replace with your specific domain
15
+ allow_credentials=True,
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+ # 3. Load Engine once at startup
21
+ engine = RegentsAIEngine()
22
+
23
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24
+ TEMP_DIR = os.path.join(BASE_DIR, "temp_uploads")
25
+ os.makedirs(TEMP_DIR, exist_ok=True)
26
+
27
+ @app.get("/")
28
+ def read_root():
29
+ return {"status": "Regents AI Engine Online", "mode": "Production"}
30
+
31
+ @app.post("/process-video")
32
+ async def process_video(file: UploadFile = File(...)):
33
+ """
34
+ Receives video, saves temporarily, runs the 4-Expert sequence,
35
+ and returns results.
36
+ """
37
+ # Validate file type
38
+ if not file.filename.lower().endswith(('.mp4', '.mov', '.avi')):
39
+ raise HTTPException(status_code=400, detail="Invalid video format")
40
+
41
+ temp_filename = f"{uuid.uuid4()}_{file.filename}"
42
+ temp_path = os.path.join(TEMP_DIR, temp_filename)
43
+ output_path = os.path.join(TEMP_DIR, f"annotated_{temp_filename}")
44
+
45
+ try:
46
+ # Save uploaded file
47
+ with open(temp_path, "wb") as buffer:
48
+ buffer.write(await file.read())
49
+
50
+ # Execute AI Logic in a separate thread
51
+ results = await anyio.to_thread.run_sync(
52
+ engine.analyze_video, temp_path, output_path
53
+ )
54
+
55
+ return {
56
+ "status": "success",
57
+ "results": results,
58
+ "video_id": temp_filename
59
+ }
60
+
61
+ except Exception as e:
62
+ raise HTTPException(status_code=500, detail=str(e))
63
+
64
+ finally:
65
+ # Cleanup original upload to save disk space
66
+ if os.path.exists(temp_path):
67
+ os.remove(temp_path)
68
+ # Note: You might want to keep output_path if you plan to serve the video back
69
+
70
+ @app.get("/health")
71
+ def health():
72
+ return {"status": "ready", "models_loaded": True}