Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import base64
|
| 2 |
-
import
|
| 3 |
-
from fastapi import FastAPI, HTTPException, Depends, Header, Body
|
| 4 |
from schemas import AudioInput, DetectionResult
|
| 5 |
from model_service import get_model_service, ModelService
|
| 6 |
|
|
@@ -23,34 +22,25 @@ async def verify_api_key(x_api_key: str = Header(...)):
|
|
| 23 |
|
| 24 |
@app.post("/detect", response_model=DetectionResult)
|
| 25 |
async def detect_voice(
|
| 26 |
-
|
| 27 |
service: ModelService = Depends(get_model_service),
|
| 28 |
api_key: str = Depends(verify_api_key)
|
| 29 |
):
|
| 30 |
try:
|
| 31 |
-
audio_b64 =
|
| 32 |
-
possible_keys = ["audio_base64", "audio", "data", "file", "encoded_audio", "mp3"]
|
| 33 |
-
for k in possible_keys:
|
| 34 |
-
if k in data and data[k]:
|
| 35 |
-
audio_b64 = data[k]
|
| 36 |
-
break
|
| 37 |
if not audio_b64:
|
| 38 |
-
|
| 39 |
-
if isinstance(v, str) and len(v) > 100:
|
| 40 |
-
audio_b64 = v
|
| 41 |
-
break
|
| 42 |
-
if not audio_b64:
|
| 43 |
-
raise HTTPException(status_code=422, detail="No valid audio data found.")
|
| 44 |
if "," in audio_b64:
|
| 45 |
audio_b64 = audio_b64.split(",")[1]
|
| 46 |
audio_bytes = base64.b64decode(audio_b64)
|
| 47 |
except Exception as e:
|
| 48 |
-
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
| 49 |
try:
|
| 50 |
label, confidence = service.predict(audio_bytes)
|
| 51 |
return DetectionResult(label=label, confidence=confidence, message="Analysis successful")
|
| 52 |
except Exception as e:
|
| 53 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 54 |
|
| 55 |
@app.get("/")
|
| 56 |
def read_root():
|
|
|
|
| 1 |
import base64
|
| 2 |
+
from fastapi import FastAPI, HTTPException, Depends, Header
|
|
|
|
| 3 |
from schemas import AudioInput, DetectionResult
|
| 4 |
from model_service import get_model_service, ModelService
|
| 5 |
|
|
|
|
| 22 |
|
| 23 |
@app.post("/detect", response_model=DetectionResult)
|
| 24 |
async def detect_voice(
|
| 25 |
+
input_data: AudioInput,
|
| 26 |
service: ModelService = Depends(get_model_service),
|
| 27 |
api_key: str = Depends(verify_api_key)
|
| 28 |
):
|
| 29 |
try:
|
| 30 |
+
audio_b64 = input_data.audio_base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"Parsing error: {str(e)}")
|
| 38 |
+
|
| 39 |
try:
|
| 40 |
label, confidence = service.predict(audio_bytes)
|
| 41 |
return DetectionResult(label=label, confidence=confidence, message="Analysis successful")
|
| 42 |
except Exception as e:
|
| 43 |
+
raise HTTPException(status_code=500, detail=f"Model error: {str(e)}")
|
| 44 |
|
| 45 |
@app.get("/")
|
| 46 |
def read_root():
|