Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ import re
|
|
| 14 |
import signal
|
| 15 |
from starlette.middleware import Middleware
|
| 16 |
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
| 17 |
|
| 18 |
# Setup logging
|
| 19 |
logging.basicConfig(
|
|
@@ -37,7 +38,11 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
|
| 37 |
response = await call_next(request)
|
| 38 |
logger.info(f"Response status: {response.status_code}")
|
| 39 |
return response
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Initialize FastAPI app with lifecycle hooks
|
| 42 |
app = FastAPI(
|
| 43 |
title="EvalBot Interview Analysis API",
|
|
@@ -241,24 +246,27 @@ async def analyze_multiple_audios(files: List[UploadFile] = File(...)):
|
|
| 241 |
logger.error(f"Error in analyze_multiple_audios: {e}")
|
| 242 |
raise HTTPException(status_code=500, detail=str(e))
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
try:
|
| 248 |
# Validate user_id (alphanumeric, underscores, hyphens)
|
| 249 |
-
if not re.match(r'^[a-zA-Z0-9_-]+$', user_id):
|
| 250 |
raise HTTPException(status_code=400, detail="Invalid user_id. Use alphanumeric characters, underscores, or hyphens.")
|
| 251 |
|
| 252 |
# Create temporary directory
|
| 253 |
temp_dir = tempfile.mkdtemp()
|
| 254 |
|
| 255 |
# Download audio file
|
| 256 |
-
file_path = download_audio(audio_url, temp_dir)
|
| 257 |
|
| 258 |
# Process audio
|
| 259 |
-
result = process_single_audio(file_path, user_id)
|
| 260 |
|
| 261 |
-
# Clean up temporary
|
| 262 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 263 |
|
| 264 |
if "error" in result:
|
|
@@ -270,12 +278,16 @@ async def analyze_audio_url(audio_url: str = Form(...), user_id: str = Form(...)
|
|
| 270 |
"summary": result["summary"],
|
| 271 |
"json_data": result["json_data"],
|
| 272 |
"pdf_url": f"/download_pdf/{os.path.basename(pdf_path)}",
|
| 273 |
-
"user_id": user_id
|
| 274 |
}
|
| 275 |
except Exception as e:
|
| 276 |
-
logger.error(f"Error in
|
| 277 |
raise HTTPException(status_code=500, detail=str(e))
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
@app.get("/download_pdf/{pdf_filename}")
|
| 280 |
async def download_pdf(pdf_filename: str):
|
| 281 |
"""Download a generated PDF report."""
|
|
|
|
| 14 |
import signal
|
| 15 |
from starlette.middleware import Middleware
|
| 16 |
from starlette.middleware.base import BaseHTTPMiddleware
|
| 17 |
+
from pydantic import BaseModel
|
| 18 |
|
| 19 |
# Setup logging
|
| 20 |
logging.basicConfig(
|
|
|
|
| 38 |
response = await call_next(request)
|
| 39 |
logger.info(f"Response status: {response.status_code}")
|
| 40 |
return response
|
| 41 |
+
|
| 42 |
+
class AudioRequest(BaseModel):
|
| 43 |
+
audio_url: str
|
| 44 |
+
user_id: str
|
| 45 |
+
|
| 46 |
# Initialize FastAPI app with lifecycle hooks
|
| 47 |
app = FastAPI(
|
| 48 |
title="EvalBot Interview Analysis API",
|
|
|
|
| 246 |
logger.error(f"Error in analyze_multiple_audios: {e}")
|
| 247 |
raise HTTPException(status_code=500, detail=str(e))
|
| 248 |
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
# الـ endpoint الجديد لدعم JSON
|
| 252 |
+
@app.post("/analyze_audio_url_json/")
|
| 253 |
+
async def analyze_audio_url_json(request: AudioRequest):
|
| 254 |
+
"""Analyze a single audio file from a URL with user_id using JSON body."""
|
| 255 |
try:
|
| 256 |
# Validate user_id (alphanumeric, underscores, hyphens)
|
| 257 |
+
if not re.match(r'^[a-zA-Z0-9_-]+$', request.user_id):
|
| 258 |
raise HTTPException(status_code=400, detail="Invalid user_id. Use alphanumeric characters, underscores, or hyphens.")
|
| 259 |
|
| 260 |
# Create temporary directory
|
| 261 |
temp_dir = tempfile.mkdtemp()
|
| 262 |
|
| 263 |
# Download audio file
|
| 264 |
+
file_path = download_audio(request.audio_url, temp_dir)
|
| 265 |
|
| 266 |
# Process audio
|
| 267 |
+
result = process_single_audio(file_path, request.user_id)
|
| 268 |
|
| 269 |
+
# Clean up temporary directory
|
| 270 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 271 |
|
| 272 |
if "error" in result:
|
|
|
|
| 278 |
"summary": result["summary"],
|
| 279 |
"json_data": result["json_data"],
|
| 280 |
"pdf_url": f"/download_pdf/{os.path.basename(pdf_path)}",
|
| 281 |
+
"user_id": request.user_id
|
| 282 |
}
|
| 283 |
except Exception as e:
|
| 284 |
+
logger.error(f"Error in analyze_audio_url_json: {e}")
|
| 285 |
raise HTTPException(status_code=500, detail=str(e))
|
| 286 |
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
|
| 291 |
@app.get("/download_pdf/{pdf_filename}")
|
| 292 |
async def download_pdf(pdf_filename: str):
|
| 293 |
"""Download a generated PDF report."""
|