Spaces:
Sleeping
Sleeping
Upload 40 files
Browse files
ai_voice_detection/api/main.py
CHANGED
|
@@ -43,8 +43,28 @@ class ErrorResponse(BaseModel):
|
|
| 43 |
message: str
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
app = FastAPI(title="AI-Generated Voice Detection API")
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# --- Dependencies ---
|
| 50 |
async def verify_api_key(api_key: str = Security(api_key_header)):
|
|
|
|
| 43 |
message: str
|
| 44 |
|
| 45 |
|
| 46 |
+
from fastapi import FastAPI, HTTPException, Security, Request
|
| 47 |
+
from fastapi.exceptions import RequestValidationError
|
| 48 |
+
from fastapi.responses import JSONResponse
|
| 49 |
+
from fastapi.security import APIKeyHeader
|
| 50 |
+
|
| 51 |
app = FastAPI(title="AI-Generated Voice Detection API")
|
| 52 |
|
| 53 |
+
@app.exception_handler(HTTPException)
|
| 54 |
+
async def http_exception_handler(request: Request, exc: HTTPException):
|
| 55 |
+
return JSONResponse(
|
| 56 |
+
status_code=exc.status_code,
|
| 57 |
+
content={"status": "error", "message": str(exc.detail)},
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
@app.exception_handler(RequestValidationError)
|
| 61 |
+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
| 62 |
+
# Simplify validation error message
|
| 63 |
+
return JSONResponse(
|
| 64 |
+
status_code=400,
|
| 65 |
+
content={"status": "error", "message": "Malformed request or invalid data"},
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
|
| 69 |
# --- Dependencies ---
|
| 70 |
async def verify_api_key(api_key: str = Security(api_key_header)):
|