akpande2 commited on
Commit
15c1fed
·
verified ·
1 Parent(s): c9b0098

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +64 -0
main.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import uvicorn
4
+ from fastapi import FastAPI, UploadFile, File, HTTPException
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from kid_coach_pipeline import KidCoachEngine
7
+
8
+ # Initialize App
9
+ app = FastAPI(title="Kid-Speech Coach API")
10
+
11
+ # Allow your future Flutter app to access this server
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"],
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Global Engine Variable
20
+ engine = None
21
+
22
+
23
+ @app.on_event("startup")
24
+ async def startup_event():
25
+ global engine
26
+ print("🚀 Server starting... Loading Whisper Model...")
27
+ # Initialize Engine (Using 'base' for speed, or 'small'/'medium' based on your GPU)
28
+ engine = KidCoachEngine(model_size="base")
29
+ print("✅ Kid Coach Engine Ready!")
30
+
31
+ @app.post("/coach")
32
+ async def coach_audio(file: UploadFile = File(...)):
33
+ """
34
+ Endpoint: Accepts audio file, returns coaching analytics JSON.
35
+ """
36
+ if not engine:
37
+ raise HTTPException(status_code=500, detail="Engine not ready")
38
+
39
+ # Save uploaded file temporarily
40
+ temp_filename = f"temp_{file.filename}"
41
+ try:
42
+ with open(temp_filename, "wb") as buffer:
43
+ shutil.copyfileobj(file.file, buffer)
44
+
45
+ # Run Analysis
46
+ result = engine.analyze_audio(temp_filename)
47
+
48
+ # Check for logic errors passed back from the engine
49
+ if "error" in result:
50
+ raise HTTPException(status_code=400, detail=result["error"])
51
+
52
+ return result
53
+
54
+ except Exception as e:
55
+ import traceback
56
+ traceback.print_exc()
57
+ raise HTTPException(status_code=500, detail=str(e))
58
+ finally:
59
+ # Cleanup
60
+ if os.path.exists(temp_filename):
61
+ os.remove(temp_filename)
62
+
63
+ if __name__ == "__main__":
64
+ uvicorn.run(app, host="0.0.0.0", port=8000)