File size: 2,216 Bytes
16af0dd ac4efc7 16af0dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | from fastapi import APIRouter, Form, File, UploadFile, HTTPException
from services.voice_analyze_service import process_audio_and_predict
router = APIRouter(
prefix="/analyze",
tags=["analyze"],
)
@router.post("/test")
async def test_endpoint(
age: int = Form(...),
sex: str = Form(...),
test_time: float = Form(...),
audio_file: UploadFile = File(...)):
return {
"received_data": {
"age": age,
"sex": sex,
"test_time": test_time,
"audio_file": {
"filename": audio_file.filename,
"content_type": audio_file.content_type,
"size": len(await audio_file.read()) if audio_file else 0
}
},
"status": "success"
}
@router.post("/voice")
async def analyze_voice(
age: int = Form(..., gt=10, lt=120),
sex: str = Form(..., pattern="^(male|female)$"),
test_time: float = Form(..., gt=0),
audio_file: UploadFile = File(...) ):
# for debugging
print("-" * 20)
print("RECEIVED REQUEST FROM FRONTEND:")
print(f"Age: {age}")
print(f"Sex: {sex}")
print(f"Test Time: {test_time}")
print(f"Audio File: {audio_file.filename} ")
print(f"Audio Content Type: {audio_file.content_type}")
# validation checks
if not audio_file or audio_file.filename == "":
print("---No audio file provided! ---")
raise HTTPException(status_code=400, detail="No audio file provided")
if audio_file.content_type and not audio_file.content_type.startswith(('audio/', 'application/octet-stream')):
print(f"Unusual content type: {audio_file.content_type}")
print("-" * 20)
basic_info = {"age": age, "sex": sex, "test_time": test_time}
print(f"Basic info being passed to service: {basic_info}")
try:
result = await process_audio_and_predict(audio_file, basic_info)
print("\nSENDING RESPONSE TO FRONTEND:")
print(f"Response: {result}\n")
return result
except Exception as e:
print("=" * 50)
print("ERROR OCCURRED:")
print(f"Error: {str(e)}")
print(f"Error type: {type(e).__name__}\n")
raise e |