Spaces:
Sleeping
Sleeping
Commit ·
053568e
1
Parent(s): 99f8a05
feat: add /predict_frames endpoint and frame-based preprocessing to FastAPI service
Browse files- app.py +126 -21
- model.py +78 -3
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -2,9 +2,11 @@
|
|
| 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
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI(
|
| 10 |
title="ISL Recognition API",
|
|
@@ -12,50 +14,153 @@ app = FastAPI(
|
|
| 12 |
version="1.0.0"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Allow all origins
|
| 16 |
app.add_middleware(
|
| 17 |
-
CORSMiddleware,
|
|
|
|
| 18 |
allow_methods=["*"],
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# Global
|
| 23 |
model = None
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
| 25 |
@app.on_event("startup")
|
| 26 |
async def startup_event():
|
| 27 |
-
global model
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
@app.get("/")
|
| 33 |
def root():
|
| 34 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
async def predict_sign(file: UploadFile = File(...), top_k: int = 5):
|
| 38 |
-
# Validate
|
| 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 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
start_time = time.time()
|
| 49 |
video_bytes = await file.read()
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 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 |
-
|
| 61 |
-
uvicorn.run("app.py:app", host="0.0.0.0", port=7860)
|
|
|
|
| 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, predict_from_frames
|
| 6 |
import time
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from typing import List
|
| 9 |
+
import base64
|
| 10 |
|
| 11 |
app = FastAPI(
|
| 12 |
title="ISL Recognition API",
|
|
|
|
| 14 |
version="1.0.0"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Allow all origins (for Flutter / frontend apps)
|
| 18 |
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"],
|
| 21 |
allow_methods=["*"],
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Global state
|
| 26 |
model = None
|
| 27 |
+
model_loaded = False
|
| 28 |
+
model_error = None
|
| 29 |
|
| 30 |
+
|
| 31 |
+
# STARTUP
|
| 32 |
@app.on_event("startup")
|
| 33 |
async def startup_event():
|
| 34 |
+
global model, model_loaded, model_error
|
| 35 |
+
try:
|
| 36 |
+
model = load_model()
|
| 37 |
+
model_loaded = True
|
| 38 |
+
model_error = None
|
| 39 |
+
print("Model loaded and API is ready!")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
model_loaded = False
|
| 42 |
+
model_error = str(e)
|
| 43 |
+
print("Model failed to load:", e)
|
| 44 |
+
|
| 45 |
|
| 46 |
+
# ROOT
|
| 47 |
@app.get("/")
|
| 48 |
def root():
|
| 49 |
+
return {
|
| 50 |
+
"status": "ISL API is running",
|
| 51 |
+
"message": "Send a POST request to /predict (video) or /predict_frames (frames list)"
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# HEALTH
|
| 56 |
+
@app.get("/health")
|
| 57 |
+
def health():
|
| 58 |
+
if not model_loaded or model is None:
|
| 59 |
+
return {
|
| 60 |
+
"status": "error",
|
| 61 |
+
"model_loaded": False,
|
| 62 |
+
"error": model_error
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
"status": "ok",
|
| 67 |
+
"model_loaded": True,
|
| 68 |
+
"device": str(next(model.parameters()).device)
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# DEEP HEALTH
|
| 73 |
+
@app.get("/health/deep")
|
| 74 |
+
def health_deep():
|
| 75 |
+
if not model_loaded or model is None:
|
| 76 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
import torch
|
| 80 |
+
|
| 81 |
+
dummy = torch.zeros(1, 3, 16, 224, 224).to(
|
| 82 |
+
next(model.parameters()).device
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
_ = model(dummy)
|
| 87 |
+
|
| 88 |
+
return {
|
| 89 |
+
"status": "ok",
|
| 90 |
+
"inference": "working"
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
raise HTTPException(
|
| 95 |
+
status_code=500,
|
| 96 |
+
detail=f"Inference failed: {str(e)}"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
class FramesPayload(BaseModel):
|
| 100 |
+
frames: List[str] # List of base64 encoded JPEG/PNG images
|
| 101 |
+
top_k: int = 5
|
| 102 |
+
|
| 103 |
+
@app.post("/predict_frames")
|
| 104 |
+
async def predict_frames_api(payload: FramesPayload):
|
| 105 |
+
if not model_loaded or model is None:
|
| 106 |
+
raise HTTPException(status_code=503, detail="Model is not ready")
|
| 107 |
+
|
| 108 |
+
if not payload.frames or len(payload.frames) != 16:
|
| 109 |
+
raise HTTPException(status_code=400, detail="Exactly 16 frames required")
|
| 110 |
|
| 111 |
+
start_time = time.time()
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
# Convert base64 strings to bytes
|
| 115 |
+
frames_bytes = [base64.b64decode(f) for f in payload.frames]
|
| 116 |
+
result = predict_from_frames(model, frames_bytes, top_k=payload.top_k)
|
| 117 |
+
except Exception as e:
|
| 118 |
+
raise HTTPException(
|
| 119 |
+
status_code=500,
|
| 120 |
+
detail=f"Inference error: {str(e)}"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Standardized response format as per checklist
|
| 124 |
+
return {
|
| 125 |
+
"prediction": result["prediction"],
|
| 126 |
+
"confidence": result["confidence"],
|
| 127 |
+
"inference_time_ms": round((time.time() - start_time) * 1000, 2)
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# PREDICT
|
| 132 |
+
@app.post("/predict")
|
| 133 |
async def predict_sign(file: UploadFile = File(...), top_k: int = 5):
|
| 134 |
+
# Validate file type
|
| 135 |
if not file.filename.lower().endswith(('.mp4', '.mov', '.avi', '.mkv')):
|
| 136 |
raise HTTPException(
|
| 137 |
status_code=400,
|
| 138 |
detail="Invalid file type. Please upload a video (.mp4, .mov, etc.)"
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# Ensure model is ready
|
| 142 |
+
if not model_loaded or model is None:
|
| 143 |
+
raise HTTPException(
|
| 144 |
+
status_code=503,
|
| 145 |
+
detail="Model is not ready"
|
| 146 |
+
)
|
| 147 |
|
| 148 |
start_time = time.time()
|
| 149 |
video_bytes = await file.read()
|
| 150 |
+
|
| 151 |
+
try:
|
| 152 |
+
result = predict(model, video_bytes, top_k=top_k)
|
| 153 |
+
except Exception as e:
|
| 154 |
+
raise HTTPException(
|
| 155 |
+
status_code=500,
|
| 156 |
+
detail=f"Inference error: {str(e)}"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
result["inference_time_ms"] = round((time.time() - start_time) * 1000, 2)
|
| 160 |
result["filename"] = file.filename
|
| 161 |
+
|
| 162 |
return result
|
| 163 |
|
| 164 |
+
|
| 165 |
if __name__ == "__main__":
|
| 166 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
|
|
model.py
CHANGED
|
@@ -8,6 +8,8 @@ from decord.bridge import set_bridge
|
|
| 8 |
import gc
|
| 9 |
import tempfile
|
| 10 |
import os
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Exactly 76 classes from your notebook metadata
|
| 13 |
CLASSES = [
|
|
@@ -90,12 +92,13 @@ def preprocess_video(video_bytes: bytes, clip_length: int = 16):
|
|
| 90 |
|
| 91 |
# Ensure video is a torch tensor in (Frames, Channels, Height, Width)
|
| 92 |
video = vr.get_batch(indices)
|
| 93 |
-
video = video.permute(0, 3, 1, 2).
|
| 94 |
|
| 95 |
# Pass as a list of Tensors
|
| 96 |
processed = image_processor(
|
| 97 |
-
list(video),
|
| 98 |
-
return_tensors='pt'
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
pixel_values = processed['pixel_values'].squeeze(0) # (T, C, H, W)
|
|
@@ -106,6 +109,78 @@ def preprocess_video(video_bytes: bytes, clip_length: int = 16):
|
|
| 106 |
if os.path.exists(tmp_path):
|
| 107 |
os.remove(tmp_path)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def predict(model, video_bytes: bytes, top_k: int = 5):
|
| 110 |
"""Runs inference and returns the top results"""
|
| 111 |
pixel_values = preprocess_video(video_bytes).to(DEVICE)
|
|
|
|
| 8 |
import gc
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
+
import cv2
|
| 12 |
+
import numpy as np
|
| 13 |
|
| 14 |
# Exactly 76 classes from your notebook metadata
|
| 15 |
CLASSES = [
|
|
|
|
| 92 |
|
| 93 |
# Ensure video is a torch tensor in (Frames, Channels, Height, Width)
|
| 94 |
video = vr.get_batch(indices)
|
| 95 |
+
video = video.permute(0, 3, 1, 2).to(torch.uint8) # Convert to Float for the processor
|
| 96 |
|
| 97 |
# Pass as a list of Tensors
|
| 98 |
processed = image_processor(
|
| 99 |
+
list(video),
|
| 100 |
+
return_tensors='pt',
|
| 101 |
+
input_data_format='channels_first'
|
| 102 |
)
|
| 103 |
|
| 104 |
pixel_values = processed['pixel_values'].squeeze(0) # (T, C, H, W)
|
|
|
|
| 109 |
if os.path.exists(tmp_path):
|
| 110 |
os.remove(tmp_path)
|
| 111 |
|
| 112 |
+
def preprocess_frames(frames_list_bytes: list[bytes], clip_length: int = 16):
|
| 113 |
+
"""
|
| 114 |
+
Processes a list of raw frame bytes into the Swin3D model input format.
|
| 115 |
+
Following the exact 'no-BS' checklist implementation.
|
| 116 |
+
"""
|
| 117 |
+
image_processor = VivitImageProcessor(
|
| 118 |
+
do_resize=True,
|
| 119 |
+
size={"shortest_edge": 224},
|
| 120 |
+
do_center_crop=True,
|
| 121 |
+
crop_size={"height": 224, "width": 224},
|
| 122 |
+
do_rescale=True,
|
| 123 |
+
rescale_factor=1/255,
|
| 124 |
+
do_normalize=True,
|
| 125 |
+
image_mean=[0.5, 0.5, 0.5],
|
| 126 |
+
image_std=[0.5, 0.5, 0.5],
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# 1. Decode bytes to PIL Images
|
| 130 |
+
from io import BytesIO
|
| 131 |
+
from PIL import Image
|
| 132 |
+
|
| 133 |
+
decoded_frames = []
|
| 134 |
+
for f_bytes in frames_list_bytes:
|
| 135 |
+
img = Image.open(BytesIO(f_bytes)).convert("RGB")
|
| 136 |
+
decoded_frames.append(img)
|
| 137 |
+
|
| 138 |
+
if len(decoded_frames) != clip_length:
|
| 139 |
+
raise ValueError(f"Exactly {clip_length} frames required, got {len(decoded_frames)}")
|
| 140 |
+
|
| 141 |
+
# 2. Convert to tensor stack (T, C, H, W)
|
| 142 |
+
# Note: User's snippet used torch.from_numpy(np.array(img)).permute(2, 0, 1)
|
| 143 |
+
video = torch.stack([
|
| 144 |
+
torch.from_numpy(np.array(img)).permute(2, 0, 1)
|
| 145 |
+
for img in decoded_frames
|
| 146 |
+
])
|
| 147 |
+
|
| 148 |
+
# 3. Apply ImageProcessor
|
| 149 |
+
processed = image_processor(
|
| 150 |
+
list(video),
|
| 151 |
+
return_tensors='pt',
|
| 152 |
+
input_data_format='channels_first'
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# 4. Standardize dimensions for Swin3D: (Batch, Channels, Time, Height, Width)
|
| 156 |
+
pixel_values = processed['pixel_values'].squeeze(0) # (T, C, H, W)
|
| 157 |
+
pixel_values = pixel_values.permute(1, 0, 2, 3) # (C, T, H, W)
|
| 158 |
+
|
| 159 |
+
return pixel_values.unsqueeze(0) # (1, C, T, H, W)
|
| 160 |
+
|
| 161 |
+
def predict_from_frames(model, frames_list_bytes: list[bytes], top_k: int = 5):
|
| 162 |
+
"""Runs inference from raw frame bytes"""
|
| 163 |
+
pixel_values = preprocess_frames(frames_list_bytes).to(DEVICE)
|
| 164 |
+
|
| 165 |
+
with torch.no_grad():
|
| 166 |
+
outputs = model(pixel_values)
|
| 167 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=-1)[0]
|
| 168 |
+
|
| 169 |
+
top_probs, top_indices = torch.topk(probabilities, k=top_k)
|
| 170 |
+
|
| 171 |
+
results = []
|
| 172 |
+
for i in range(top_k):
|
| 173 |
+
results.append({
|
| 174 |
+
"class": CLASSES[top_indices[i].item()],
|
| 175 |
+
"confidence": float(top_probs[i].item())
|
| 176 |
+
})
|
| 177 |
+
|
| 178 |
+
return {
|
| 179 |
+
"prediction": results[0]["class"],
|
| 180 |
+
"confidence": results[0]["confidence"],
|
| 181 |
+
"top_k": results
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
def predict(model, video_bytes: bytes, top_k: int = 5):
|
| 185 |
"""Runs inference and returns the top results"""
|
| 186 |
pixel_values = preprocess_video(video_bytes).to(DEVICE)
|
requirements.txt
CHANGED
|
@@ -6,4 +6,5 @@ torchvision
|
|
| 6 |
transformers
|
| 7 |
decord
|
| 8 |
huggingface_hub
|
| 9 |
-
python-multipart
|
|
|
|
|
|
| 6 |
transformers
|
| 7 |
decord
|
| 8 |
huggingface_hub
|
| 9 |
+
python-multipart
|
| 10 |
+
opencv-python
|