Update app.py
Browse files
app.py
CHANGED
|
@@ -92,5 +92,27 @@ demo = gr.Interface(
|
|
| 92 |
description="Upload a video to classify it as real or fake."
|
| 93 |
)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if __name__ == "__main__":
|
| 96 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 92 |
description="Upload a video to classify it as real or fake."
|
| 93 |
)
|
| 94 |
|
| 95 |
+
from fastapi import UploadFile, File
|
| 96 |
+
from fastapi.responses import JSONResponse
|
| 97 |
+
import tempfile
|
| 98 |
+
import os
|
| 99 |
+
|
| 100 |
+
@demo.app.post("/api/predict")
|
| 101 |
+
async def api_predict(file: UploadFile = File(...)):
|
| 102 |
+
"""Upload + predict in one call — for ShieldSense app."""
|
| 103 |
+
if not file.filename or not any(file.filename.lower().endswith(ext) for ext in ('.mp4', '.mov', '.avi', '.webm', '.mkv', '.jpg', '.jpeg', '.png')):
|
| 104 |
+
return JSONResponse({"error": "Invalid file type. Use video or image."}, status_code=400)
|
| 105 |
+
try:
|
| 106 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as f:
|
| 107 |
+
f.write(await file.read())
|
| 108 |
+
tmp_path = f.name
|
| 109 |
+
try:
|
| 110 |
+
result = predict_video(tmp_path)
|
| 111 |
+
return JSONResponse(result)
|
| 112 |
+
finally:
|
| 113 |
+
os.unlink(tmp_path)
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
| 116 |
+
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|