norhan12 commited on
Commit
326a734
·
verified ·
1 Parent(s): b5dfad3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -24,8 +24,18 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
24
 
25
  VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac', '.webm', '.ogg', '.aac')
26
  MAX_FILE_SIZE_MB = 300
27
-
28
  BASE_URL = os.getenv("BASE_URL", "https://evalbot-audio-evalbot.hf.space")
 
 
 
 
 
 
 
 
 
 
 
29
  class ProcessResponse(BaseModel):
30
  summary: str
31
  json_url: str
@@ -39,49 +49,49 @@ class ProcessAudioRequest(BaseModel):
39
  async def process_audio(request: ProcessAudioRequest = Body(...)):
40
  file_url = request.file_url
41
  user_id = request.user_id
 
42
  try:
43
  file_ext = os.path.splitext(str(file_url))[1].lower()
44
  if file_ext not in VALID_EXTENSIONS:
45
  raise HTTPException(status_code=400, detail=f"Invalid file extension: {file_ext}")
46
-
47
  local_filename = f"{user_id}_{uuid.uuid4().hex}{file_ext}"
48
  local_path = os.path.join(TEMP_DIR, local_filename)
49
-
50
  resp = requests.get(str(file_url), stream=True, timeout=30)
51
  if resp.status_code != 200:
52
  raise HTTPException(status_code=400, detail="Could not download the file from URL")
53
-
54
  with open(local_path, "wb") as f:
55
  for chunk in resp.iter_content(chunk_size=8192):
56
  if chunk:
57
  f.write(chunk)
58
-
59
  file_size_mb = os.path.getsize(local_path) / (1024 * 1024)
60
  if file_size_mb > MAX_FILE_SIZE_MB:
61
  os.remove(local_path)
62
  raise HTTPException(status_code=400, detail=f"File too large: {file_size_mb:.2f} MB")
63
-
64
  result = process_interview(local_path)
65
  if not result:
66
  os.remove(local_path)
67
  raise HTTPException(status_code=500, detail="Processing failed")
68
-
69
  json_dest_name = f"{user_id}_{uuid.uuid4().hex}.json"
70
  pdf_dest_name = f"{user_id}_{uuid.uuid4().hex}.pdf"
71
-
72
  json_dest = os.path.join(JSON_DIR, json_dest_name)
73
  pdf_dest = os.path.join(PDF_DIR, pdf_dest_name)
74
-
75
  shutil.copyfile(result['json_path'], json_dest)
76
  shutil.copyfile(result['pdf_path'], pdf_dest)
77
-
78
  with open(result['json_path'], "r") as jf:
79
  analysis_data = json.load(jf)
80
-
81
  voice = analysis_data.get('voice_analysis', {}).get('interpretation', {})
82
  speakers = analysis_data.get('speakers', [])
83
  total_duration = analysis_data.get('text_analysis', {}).get('total_duration', 0.0)
84
-
85
  summary = (
86
  f"User ID: {user_id}\n"
87
  f"Speakers: {', '.join(speakers)}\n"
@@ -89,19 +99,16 @@ async def process_audio(request: ProcessAudioRequest = Body(...)):
89
  f"Confidence: {voice.get('confidence_level', 'N/A')}\n"
90
  f"Anxiety: {voice.get('anxiety_level', 'N/A')}"
91
  )
92
-
93
  json_url = f"{BASE_URL}/static/outputs/json/{json_dest_name}"
94
  pdf_url = f"{BASE_URL}/static/outputs/pdf/{pdf_dest_name}"
95
-
96
-
97
  os.remove(local_path)
98
-
99
  return ProcessResponse(summary=summary, json_url=json_url, pdf_url=pdf_url)
100
-
101
  except Exception as e:
102
  raise HTTPException(status_code=500, detail=str(e))
103
 
104
-
105
  @app.get("/static/outputs/json/{filename}")
106
  async def get_json_file(filename: str):
107
  file_path = os.path.join(JSON_DIR, filename)
@@ -109,10 +116,9 @@ async def get_json_file(filename: str):
109
  raise HTTPException(status_code=404, detail="JSON file not found")
110
  return FileResponse(file_path, media_type="application/json", filename=filename)
111
 
112
-
113
  @app.get("/static/outputs/pdf/{filename}")
114
  async def get_pdf_file(filename: str):
115
  file_path = os.path.join(PDF_DIR, filename)
116
  if not os.path.exists(file_path):
117
  raise HTTPException(status_code=404, detail="PDF file not found")
118
- return FileResponse(file_path, media_type="application/pdf", filename=filename)
 
24
 
25
  VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac', '.webm', '.ogg', '.aac')
26
  MAX_FILE_SIZE_MB = 300
 
27
  BASE_URL = os.getenv("BASE_URL", "https://evalbot-audio-evalbot.hf.space")
28
+
29
+ # --- المسارات الجديدة لحل مشكلة 404 والـ Healthcheck ---
30
+ @app.get("/")
31
+ def read_root():
32
+ return {"message": "EvalBot API is running successfully! 🚀"}
33
+
34
+ @app.get("/health")
35
+ def health_check():
36
+ return {"status": "healthy"}
37
+ # -------------------------------------------------------
38
+
39
  class ProcessResponse(BaseModel):
40
  summary: str
41
  json_url: str
 
49
  async def process_audio(request: ProcessAudioRequest = Body(...)):
50
  file_url = request.file_url
51
  user_id = request.user_id
52
+
53
  try:
54
  file_ext = os.path.splitext(str(file_url))[1].lower()
55
  if file_ext not in VALID_EXTENSIONS:
56
  raise HTTPException(status_code=400, detail=f"Invalid file extension: {file_ext}")
57
+
58
  local_filename = f"{user_id}_{uuid.uuid4().hex}{file_ext}"
59
  local_path = os.path.join(TEMP_DIR, local_filename)
60
+
61
  resp = requests.get(str(file_url), stream=True, timeout=30)
62
  if resp.status_code != 200:
63
  raise HTTPException(status_code=400, detail="Could not download the file from URL")
64
+
65
  with open(local_path, "wb") as f:
66
  for chunk in resp.iter_content(chunk_size=8192):
67
  if chunk:
68
  f.write(chunk)
69
+
70
  file_size_mb = os.path.getsize(local_path) / (1024 * 1024)
71
  if file_size_mb > MAX_FILE_SIZE_MB:
72
  os.remove(local_path)
73
  raise HTTPException(status_code=400, detail=f"File too large: {file_size_mb:.2f} MB")
74
+
75
  result = process_interview(local_path)
76
  if not result:
77
  os.remove(local_path)
78
  raise HTTPException(status_code=500, detail="Processing failed")
79
+
80
  json_dest_name = f"{user_id}_{uuid.uuid4().hex}.json"
81
  pdf_dest_name = f"{user_id}_{uuid.uuid4().hex}.pdf"
 
82
  json_dest = os.path.join(JSON_DIR, json_dest_name)
83
  pdf_dest = os.path.join(PDF_DIR, pdf_dest_name)
84
+
85
  shutil.copyfile(result['json_path'], json_dest)
86
  shutil.copyfile(result['pdf_path'], pdf_dest)
87
+
88
  with open(result['json_path'], "r") as jf:
89
  analysis_data = json.load(jf)
90
+
91
  voice = analysis_data.get('voice_analysis', {}).get('interpretation', {})
92
  speakers = analysis_data.get('speakers', [])
93
  total_duration = analysis_data.get('text_analysis', {}).get('total_duration', 0.0)
94
+
95
  summary = (
96
  f"User ID: {user_id}\n"
97
  f"Speakers: {', '.join(speakers)}\n"
 
99
  f"Confidence: {voice.get('confidence_level', 'N/A')}\n"
100
  f"Anxiety: {voice.get('anxiety_level', 'N/A')}"
101
  )
102
+
103
  json_url = f"{BASE_URL}/static/outputs/json/{json_dest_name}"
104
  pdf_url = f"{BASE_URL}/static/outputs/pdf/{pdf_dest_name}"
105
+
 
106
  os.remove(local_path)
 
107
  return ProcessResponse(summary=summary, json_url=json_url, pdf_url=pdf_url)
108
+
109
  except Exception as e:
110
  raise HTTPException(status_code=500, detail=str(e))
111
 
 
112
  @app.get("/static/outputs/json/{filename}")
113
  async def get_json_file(filename: str):
114
  file_path = os.path.join(JSON_DIR, filename)
 
116
  raise HTTPException(status_code=404, detail="JSON file not found")
117
  return FileResponse(file_path, media_type="application/json", filename=filename)
118
 
 
119
  @app.get("/static/outputs/pdf/{filename}")
120
  async def get_pdf_file(filename: str):
121
  file_path = os.path.join(PDF_DIR, filename)
122
  if not os.path.exists(file_path):
123
  raise HTTPException(status_code=404, detail="PDF file not found")
124
+ return FileResponse(file_path, media_type="application/pdf", filename=filename)