Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import base64
|
| 2 |
-
|
|
|
|
| 3 |
from schemas import AudioInput, DetectionResult
|
| 4 |
from model_service import get_model_service, ModelService
|
| 5 |
|
|
@@ -11,7 +12,8 @@ app = FastAPI(
|
|
| 11 |
|
| 12 |
@app.on_event("startup")
|
| 13 |
async def startup_event():
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
API_KEY = "my_secret_key_123"
|
| 17 |
|
|
@@ -22,23 +24,52 @@ async def verify_api_key(x_api_key: str = Header(...)):
|
|
| 22 |
|
| 23 |
@app.post("/detect", response_model=DetectionResult)
|
| 24 |
async def detect_voice(
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
service: ModelService = Depends(get_model_service),
|
| 27 |
api_key: str = Depends(verify_api_key)
|
| 28 |
):
|
| 29 |
try:
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if not audio_b64:
|
| 32 |
raise HTTPException(status_code=422, detail="No audio data found.")
|
|
|
|
|
|
|
| 33 |
if "," in audio_b64:
|
| 34 |
audio_b64 = audio_b64.split(",")[1]
|
|
|
|
|
|
|
| 35 |
audio_bytes = base64.b64decode(audio_b64)
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
raise HTTPException(status_code=400, detail=f"
|
| 38 |
|
| 39 |
try:
|
|
|
|
| 40 |
label, confidence = service.predict(audio_bytes)
|
| 41 |
-
return DetectionResult(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
raise HTTPException(status_code=500, detail=f"Model error: {str(e)}")
|
| 44 |
|
|
|
|
| 1 |
import base64
|
| 2 |
+
import json
|
| 3 |
+
from fastapi import FastAPI, HTTPException, Depends, Header, Request, Body
|
| 4 |
from schemas import AudioInput, DetectionResult
|
| 5 |
from model_service import get_model_service, ModelService
|
| 6 |
|
|
|
|
| 12 |
|
| 13 |
@app.on_event("startup")
|
| 14 |
async def startup_event():
|
| 15 |
+
# EXTREMELY IMPORTANT: Instant startup to prevent 504 Timeout
|
| 16 |
+
print("API is starting up...")
|
| 17 |
|
| 18 |
API_KEY = "my_secret_key_123"
|
| 19 |
|
|
|
|
| 24 |
|
| 25 |
@app.post("/detect", response_model=DetectionResult)
|
| 26 |
async def detect_voice(
|
| 27 |
+
request: Request,
|
| 28 |
+
# This line ensures the "Request body" box appears in your /docs test page
|
| 29 |
+
payload: dict = Body(..., example={"audioBase64": "PASTE_BASE64_HERE"}),
|
| 30 |
service: ModelService = Depends(get_model_service),
|
| 31 |
api_key: str = Depends(verify_api_key)
|
| 32 |
):
|
| 33 |
try:
|
| 34 |
+
# 1. Flexible parsing for portal compatibility (audioBase64, audio_base_64, etc.)
|
| 35 |
+
body = await request.json()
|
| 36 |
+
audio_b64 = None
|
| 37 |
+
|
| 38 |
+
# Check all possible key variants including camelCase for the portal
|
| 39 |
+
possible_keys = ["audioBase64", "audio_base_64", "audio_base64", "audio", "data", "file"]
|
| 40 |
+
for k in possible_keys:
|
| 41 |
+
if k in body and body[k]:
|
| 42 |
+
audio_b64 = body[k]
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
# Fallback: search for any long string value
|
| 46 |
+
if not audio_b64:
|
| 47 |
+
for v in body.values():
|
| 48 |
+
if isinstance(v, str) and len(v) > 100:
|
| 49 |
+
audio_b64 = v
|
| 50 |
+
break
|
| 51 |
+
|
| 52 |
if not audio_b64:
|
| 53 |
raise HTTPException(status_code=422, detail="No audio data found.")
|
| 54 |
+
|
| 55 |
+
# 2. Cleanup (Remove Data URI prefix like "data:audio/mp3;base64,")
|
| 56 |
if "," in audio_b64:
|
| 57 |
audio_b64 = audio_b64.split(",")[1]
|
| 58 |
+
|
| 59 |
+
# 3. Decode
|
| 60 |
audio_bytes = base64.b64decode(audio_b64)
|
| 61 |
+
|
| 62 |
except Exception as e:
|
| 63 |
+
raise HTTPException(status_code=400, detail=f"Request parsing error: {str(e)}")
|
| 64 |
|
| 65 |
try:
|
| 66 |
+
# 4. AI Inference (Returns label: HUMAN/AI_GENERATED and score)
|
| 67 |
label, confidence = service.predict(audio_bytes)
|
| 68 |
+
return DetectionResult(
|
| 69 |
+
label=label,
|
| 70 |
+
confidence=confidence,
|
| 71 |
+
message="Analysis successful"
|
| 72 |
+
)
|
| 73 |
except Exception as e:
|
| 74 |
raise HTTPException(status_code=500, detail=f"Model error: {str(e)}")
|
| 75 |
|