Spaces:
Running
Running
added api calls
Browse files- app/main.py +20 -4
app/main.py
CHANGED
|
@@ -24,6 +24,24 @@ IMAGE_TYPES = {"image/jpeg", "image/jpg", "image/png", "image/webp"}
|
|
| 24 |
VIDEO_TYPES = {"video/mp4", "video/quicktime", "video/webm", "video/x-matroska"}
|
| 25 |
GIF_TYPES = {"image/gif"}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
HF_REPORT_REPO = os.environ.get("HF_REPORT_REPO", "ComplexDataLab/openfake-reports")
|
| 28 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 29 |
|
|
@@ -83,8 +101,7 @@ def _predict_with_preprocess(image: Image.Image) -> dict:
|
|
| 83 |
@app.post("/api/predict")
|
| 84 |
async def predict(file: UploadFile = File(...)):
|
| 85 |
content_type = (file.content_type or "").lower()
|
| 86 |
-
|
| 87 |
-
content_type = mimetypes.guess_type(file.filename)[0] or content_type
|
| 88 |
raw = await file.read()
|
| 89 |
size_mb = len(raw) / (1024 * 1024)
|
| 90 |
|
|
@@ -174,8 +191,7 @@ async def report(
|
|
| 174 |
# Read the uploaded file
|
| 175 |
raw = await file.read()
|
| 176 |
content_type = (file.content_type or "").lower()
|
| 177 |
-
|
| 178 |
-
content_type = mimetypes.guess_type(file.filename)[0] or content_type
|
| 179 |
if content_type not in IMAGE_TYPES | VIDEO_TYPES | GIF_TYPES:
|
| 180 |
raise HTTPException(415, "Unsupported file type for reporting.")
|
| 181 |
|
|
|
|
| 24 |
VIDEO_TYPES = {"video/mp4", "video/quicktime", "video/webm", "video/x-matroska"}
|
| 25 |
GIF_TYPES = {"image/gif"}
|
| 26 |
|
| 27 |
+
_EXT_MIME = {
|
| 28 |
+
".jpg": "image/jpeg", ".jpeg": "image/jpeg",
|
| 29 |
+
".png": "image/png",
|
| 30 |
+
".webp": "image/webp",
|
| 31 |
+
".gif": "image/gif",
|
| 32 |
+
".mp4": "video/mp4",
|
| 33 |
+
".mov": "video/quicktime",
|
| 34 |
+
".webm": "video/webm",
|
| 35 |
+
".mkv": "video/x-matroska",
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def _resolve_content_type(content_type: str, filename: str | None) -> str:
|
| 40 |
+
if content_type not in ("", "application/octet-stream") or not filename:
|
| 41 |
+
return content_type
|
| 42 |
+
suffix = Path(filename).suffix.lower()
|
| 43 |
+
return _EXT_MIME.get(suffix) or mimetypes.guess_type(filename)[0] or content_type
|
| 44 |
+
|
| 45 |
HF_REPORT_REPO = os.environ.get("HF_REPORT_REPO", "ComplexDataLab/openfake-reports")
|
| 46 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 47 |
|
|
|
|
| 101 |
@app.post("/api/predict")
|
| 102 |
async def predict(file: UploadFile = File(...)):
|
| 103 |
content_type = (file.content_type or "").lower()
|
| 104 |
+
content_type = _resolve_content_type(content_type, file.filename)
|
|
|
|
| 105 |
raw = await file.read()
|
| 106 |
size_mb = len(raw) / (1024 * 1024)
|
| 107 |
|
|
|
|
| 191 |
# Read the uploaded file
|
| 192 |
raw = await file.read()
|
| 193 |
content_type = (file.content_type or "").lower()
|
| 194 |
+
content_type = _resolve_content_type(content_type, file.filename)
|
|
|
|
| 195 |
if content_type not in IMAGE_TYPES | VIDEO_TYPES | GIF_TYPES:
|
| 196 |
raise HTTPException(415, "Unsupported file type for reporting.")
|
| 197 |
|