| import logging |
| from fastapi import FastAPI, HTTPException |
| from pydantic import BaseModel, HttpUrl |
| from typing import List, Dict |
| from concurrent.futures import ThreadPoolExecutor |
| from process_interview import process_interview |
|
|
| logging.basicConfig( |
| level=logging.INFO, |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| logger = logging.getLogger(__name__) |
|
|
| app = FastAPI( |
| title="EvalBot API", |
| description="API to analyze audio interviews.", |
| version="1.0.0" |
| ) |
|
|
|
|
| class AudioItem(BaseModel): |
| url: HttpUrl |
| user_id: str |
|
|
|
|
| class AnalysisRequest(BaseModel): |
| audio_items: List[AudioItem] |
|
|
|
|
| def process_item_wrapper(item: AudioItem) -> Dict: |
| try: |
| logger.info(f"Starting analysis for user '{item.user_id}' with URL: {item.url}") |
| result = process_interview(str(item.url)) |
| return { |
| "user_id": item.user_id, |
| "url": str(item.url), |
| "status": "success", |
| "result": result |
| } |
| except Exception as e: |
| logger.error(f"Failed processing for user '{item.user_id}': {e}", exc_info=True) |
| return { |
| "user_id": item.user_id, |
| "url": str(item.url), |
| "status": "error", |
| "detail": str(e) |
| } |
|
|
|
|
| @app.get("/health", summary="Check API Health") |
| def health_check(): |
| return {"status": "ok"} |
|
|
|
|
| @app.post("/analyze", summary="Analyze a list of audio URLs") |
| def analyze_audios(request: AnalysisRequest): |
| if not request.audio_items: |
| raise HTTPException(status_code=400, detail="No audio items provided.") |
|
|
| logger.info(f"Received request to analyze {len(request.audio_items)} audio items.") |
| results = [] |
|
|
| with ThreadPoolExecutor(max_workers=5) as executor: |
| futures = [executor.submit(process_item_wrapper, item) for item in request.audio_items] |
|
|
| for future in futures: |
| results.append(future.result()) |
|
|
| logger.info("Finished processing all items.") |
| return {"analysis_results": results} |