norhan12 commited on
Commit
e73ce26
·
verified ·
1 Parent(s): a70217a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel, HttpUrl
4
+ from typing import List, Dict
5
+ from concurrent.futures import ThreadPoolExecutor
6
+ from process_interview import process_interview
7
+
8
+ logging.basicConfig(
9
+ level=logging.INFO,
10
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
11
+ logger = logging.getLogger(__name__)
12
+
13
+ app = FastAPI(
14
+ title="EvalBot API",
15
+ description="API to analyze audio interviews.",
16
+ version="1.0.0"
17
+ )
18
+
19
+
20
+ class AudioItem(BaseModel):
21
+ url: HttpUrl
22
+ user_id: str
23
+
24
+
25
+ class AnalysisRequest(BaseModel):
26
+ audio_items: List[AudioItem]
27
+
28
+
29
+ def process_item_wrapper(item: AudioItem) -> Dict:
30
+ try:
31
+ logger.info(f"Starting analysis for user '{item.user_id}' with URL: {item.url}")
32
+ result = process_interview(str(item.url))
33
+ return {
34
+ "user_id": item.user_id,
35
+ "url": str(item.url),
36
+ "status": "success",
37
+ "result": result
38
+ }
39
+ except Exception as e:
40
+ logger.error(f"Failed processing for user '{item.user_id}': {e}", exc_info=True)
41
+ return {
42
+ "user_id": item.user_id,
43
+ "url": str(item.url),
44
+ "status": "error",
45
+ "detail": str(e)
46
+ }
47
+
48
+
49
+ @app.get("/health", summary="Check API Health")
50
+ def health_check():
51
+ return {"status": "ok"}
52
+
53
+
54
+ @app.post("/analyze", summary="Analyze a list of audio URLs")
55
+ def analyze_audios(request: AnalysisRequest):
56
+ if not request.audio_items:
57
+ raise HTTPException(status_code=400, detail="No audio items provided.")
58
+
59
+ logger.info(f"Received request to analyze {len(request.audio_items)} audio items.")
60
+ results = []
61
+
62
+ with ThreadPoolExecutor(max_workers=5) as executor:
63
+ futures = [executor.submit(process_item_wrapper, item) for item in request.audio_items]
64
+
65
+ for future in futures:
66
+ results.append(future.result())
67
+
68
+ logger.info("Finished processing all items.")
69
+ return {"analysis_results": results}