norhan12 commited on
Commit
0b9cdd1
·
verified ·
1 Parent(s): 99c7c90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -19
app.py CHANGED
@@ -1,12 +1,11 @@
 
 
1
  import os
2
  import uuid
3
  import shutil
4
  import json
5
- from fastapi import FastAPI, HTTPException, Query
6
- from fastapi.responses import JSONResponse, FileResponse
7
- from pydantic import BaseModel
8
  import requests
9
- from process_interview import process_interview # لازم يكون ملفك موجود فيه الدالة دي
10
  from fastapi.staticfiles import StaticFiles
11
 
12
  app = FastAPI()
@@ -29,24 +28,23 @@ class ProcessResponse(BaseModel):
29
  json_url: str
30
  pdf_url: str
31
 
32
- @app.get("/")
33
- async def root():
34
- return {"message": "EvalBot API is running"}
35
 
36
  @app.post("/process-audio", response_model=ProcessResponse)
37
- async def process_audio(file_url: str = Query(..., description="URL of the audio file"),
38
- user_id: str = Query(..., description="User ID")):
 
39
  try:
40
- # التحقق من امتداد الملف
41
- file_ext = os.path.splitext(file_url)[1].lower()
42
  if file_ext not in VALID_EXTENSIONS:
43
  raise HTTPException(status_code=400, detail=f"Invalid file extension: {file_ext}")
44
 
45
- # تنزيل الملف مؤقتًا
46
  local_filename = f"{user_id}_{uuid.uuid4().hex}{file_ext}"
47
  local_path = os.path.join(TEMP_DIR, local_filename)
48
 
49
- resp = requests.get(file_url, stream=True, timeout=30)
50
  if resp.status_code != 200:
51
  raise HTTPException(status_code=400, detail="Could not download the file from URL")
52
 
@@ -55,19 +53,16 @@ async def process_audio(file_url: str = Query(..., description="URL of the audio
55
  if chunk:
56
  f.write(chunk)
57
 
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
- # استدعاء دالة المعالجة
65
  result = process_interview(local_path)
66
  if not result:
67
  os.remove(local_path)
68
  raise HTTPException(status_code=500, detail="Processing failed")
69
 
70
- # نسخ ملفات json و pdf للمجلد النهائي
71
  json_src = result['json_path']
72
  pdf_src = result['pdf_path']
73
 
@@ -80,7 +75,6 @@ async def process_audio(file_url: str = Query(..., description="URL of the audio
80
  shutil.copyfile(json_src, json_dest)
81
  shutil.copyfile(pdf_src, pdf_dest)
82
 
83
- # قراءة بيانات JSON لتحضير الملخص
84
  with open(json_src, "r") as jf:
85
  analysis_data = json.load(jf)
86
 
@@ -96,11 +90,9 @@ async def process_audio(file_url: str = Query(..., description="URL of the audio
96
  f"Anxiety: {voice.get('anxiety_level', 'N/A')}"
97
  )
98
 
99
- # بناء روابط الملفات التي ستُعاد
100
  json_url = f"{BASE_URL}/{json_dest_name}"
101
  pdf_url = f"{BASE_URL}/{pdf_dest_name}"
102
 
103
- # حذف الملف الصوتي المؤقت
104
  os.remove(local_path)
105
 
106
  return ProcessResponse(summary=summary, json_url=json_url, pdf_url=pdf_url)
 
1
+ from fastapi import FastAPI, HTTPException, Body
2
+ from pydantic import BaseModel, HttpUrl
3
  import os
4
  import uuid
5
  import shutil
6
  import json
 
 
 
7
  import requests
8
+ from process_interview import process_interview
9
  from fastapi.staticfiles import StaticFiles
10
 
11
  app = FastAPI()
 
28
  json_url: str
29
  pdf_url: str
30
 
31
+ class ProcessAudioRequest(BaseModel):
32
+ file_url: HttpUrl
33
+ user_id: str
34
 
35
  @app.post("/process-audio", response_model=ProcessResponse)
36
+ async def process_audio(request: ProcessAudioRequest = Body(...)):
37
+ file_url = request.file_url
38
+ user_id = request.user_id
39
  try:
40
+ file_ext = os.path.splitext(str(file_url))[1].lower()
 
41
  if file_ext not in VALID_EXTENSIONS:
42
  raise HTTPException(status_code=400, detail=f"Invalid file extension: {file_ext}")
43
 
 
44
  local_filename = f"{user_id}_{uuid.uuid4().hex}{file_ext}"
45
  local_path = os.path.join(TEMP_DIR, local_filename)
46
 
47
+ resp = requests.get(str(file_url), stream=True, timeout=30)
48
  if resp.status_code != 200:
49
  raise HTTPException(status_code=400, detail="Could not download the file from URL")
50
 
 
53
  if chunk:
54
  f.write(chunk)
55
 
 
56
  file_size_mb = os.path.getsize(local_path) / (1024 * 1024)
57
  if file_size_mb > MAX_FILE_SIZE_MB:
58
  os.remove(local_path)
59
  raise HTTPException(status_code=400, detail=f"File too large: {file_size_mb:.2f} MB")
60
 
 
61
  result = process_interview(local_path)
62
  if not result:
63
  os.remove(local_path)
64
  raise HTTPException(status_code=500, detail="Processing failed")
65
 
 
66
  json_src = result['json_path']
67
  pdf_src = result['pdf_path']
68
 
 
75
  shutil.copyfile(json_src, json_dest)
76
  shutil.copyfile(pdf_src, pdf_dest)
77
 
 
78
  with open(json_src, "r") as jf:
79
  analysis_data = json.load(jf)
80
 
 
90
  f"Anxiety: {voice.get('anxiety_level', 'N/A')}"
91
  )
92
 
 
93
  json_url = f"{BASE_URL}/{json_dest_name}"
94
  pdf_url = f"{BASE_URL}/{pdf_dest_name}"
95
 
 
96
  os.remove(local_path)
97
 
98
  return ProcessResponse(summary=summary, json_url=json_url, pdf_url=pdf_url)