Creator-090 commited on
Commit
c126626
·
1 Parent(s): ebfa9f1

add: app.py API calls

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from fastapi import FastAPI, File, UploadFile, HTTPException
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import uvicorn
5
+ from model import load_model, predict
6
+ import time
7
+ import os
8
+
9
+ app = FastAPI(
10
+ title="ISL Recognition API",
11
+ description="Indian Sign Language recognition using Swin3D-S",
12
+ version="1.0.0"
13
+ )
14
+
15
+ # Allow all origins so your Flutter app can talk to it
16
+ app.add_middleware(
17
+ CORSMiddleware, allow_origins=["*"],
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ # Global variable for the model
23
+ model = None
24
+
25
+ @app.on_event("startup")
26
+ async def startup_event():
27
+ global model
28
+ # This calls the function in your model.py to download and load the .pt file
29
+ model = load_model()
30
+ print("Model loaded and API is ready!")
31
+
32
+ @app.get("/")
33
+ def root():
34
+ return {"status": "ISL API is running", "message": "Send a POST request to /predict"}
35
+
36
+ @app.post("/predict")
37
+ async def predict_sign(file: UploadFile = File(...), top_k: int = 5):
38
+ # Validate that it's a video
39
+ if not file.filename.lower().endswith(('.mp4', '.mov', '.avi', '.mkv')):
40
+ raise HTTPException(
41
+ status_code=400,
42
+ detail="Invalid file type. Please upload a video (.mp4, .mov, etc.)"
43
+ )
44
+
45
+ if model is None:
46
+ raise HTTPException(status_code=503, detail="Model is still loading...")
47
+
48
+ start_time = time.time()
49
+ video_bytes = await file.read()
50
+
51
+ # This calls the prediction logic in your model.py
52
+ result = predict(model, video_bytes, top_k=top_k)
53
+
54
+ result["inference_time_ms"] = round((time.time() - start_time) * 1000, 2)
55
+ result["filename"] = file.filename
56
+
57
+ return result
58
+
59
+ if __name__ == "__main__":
60
+ # Hugging Face Spaces usually look for port 7860
61
+ uvicorn.run("app.py:app", host="0.0.0.0", port=7860)