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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -7,21 +7,25 @@ import json
7
  import requests
8
  from process_interview import process_interview
9
  from fastapi.staticfiles import StaticFiles
 
10
 
11
  app = FastAPI()
12
 
13
- app.mount("/static", StaticFiles(directory="static"), name="static")
14
-
15
  TEMP_DIR = "./temp_files"
16
  OUTPUT_DIR = "./static/outputs"
 
 
17
 
18
  os.makedirs(TEMP_DIR, exist_ok=True)
19
- os.makedirs(OUTPUT_DIR, exist_ok=True)
 
 
 
20
 
21
  VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac')
22
  MAX_FILE_SIZE_MB = 300
23
 
24
- BASE_URL = os.getenv("BASE_URL", "http://localhost:7860/static/outputs")
25
 
26
  class ProcessResponse(BaseModel):
27
  summary: str
@@ -63,19 +67,16 @@ async def process_audio(request: ProcessAudioRequest = Body(...)):
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
-
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(OUTPUT_DIR, json_dest_name)
73
- pdf_dest = os.path.join(OUTPUT_DIR, pdf_dest_name)
74
 
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
 
81
  voice = analysis_data.get('voice_analysis', {}).get('interpretation', {})
@@ -90,8 +91,8 @@ async def process_audio(request: ProcessAudioRequest = Body(...)):
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
 
@@ -99,3 +100,18 @@ async def process_audio(request: ProcessAudioRequest = Body(...)):
99
 
100
  except Exception as e:
101
  raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  import requests
8
  from process_interview import process_interview
9
  from fastapi.staticfiles import StaticFiles
10
+ from fastapi.responses import FileResponse
11
 
12
  app = FastAPI()
13
 
 
 
14
  TEMP_DIR = "./temp_files"
15
  OUTPUT_DIR = "./static/outputs"
16
+ JSON_DIR = os.path.join(OUTPUT_DIR, "json")
17
+ PDF_DIR = os.path.join(OUTPUT_DIR, "pdf")
18
 
19
  os.makedirs(TEMP_DIR, exist_ok=True)
20
+ os.makedirs(JSON_DIR, exist_ok=True)
21
+ os.makedirs(PDF_DIR, exist_ok=True)
22
+
23
+ app.mount("/static", StaticFiles(directory="static"), name="static")
24
 
25
  VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac')
26
  MAX_FILE_SIZE_MB = 300
27
 
28
+ BASE_URL = os.getenv("BASE_URL", "https://evalbot-audio-evalbot.hf.space")
29
 
30
  class ProcessResponse(BaseModel):
31
  summary: str
 
67
  os.remove(local_path)
68
  raise HTTPException(status_code=500, detail="Processing failed")
69
 
 
 
 
70
  json_dest_name = f"{user_id}_{uuid.uuid4().hex}.json"
71
  pdf_dest_name = f"{user_id}_{uuid.uuid4().hex}.pdf"
72
 
73
+ json_dest = os.path.join(JSON_DIR, json_dest_name)
74
+ pdf_dest = os.path.join(PDF_DIR, pdf_dest_name)
75
 
76
+ shutil.copyfile(result['json_path'], json_dest)
77
+ shutil.copyfile(result['pdf_path'], pdf_dest)
78
 
79
+ with open(result['json_path'], "r") as jf:
80
  analysis_data = json.load(jf)
81
 
82
  voice = analysis_data.get('voice_analysis', {}).get('interpretation', {})
 
91
  f"Anxiety: {voice.get('anxiety_level', 'N/A')}"
92
  )
93
 
94
+ json_url = f"{BASE_URL}/outputs/json/{json_dest_name}"
95
+ pdf_url = f"{BASE_URL}/outputs/pdf/{pdf_dest_name}"
96
 
97
  os.remove(local_path)
98
 
 
100
 
101
  except Exception as e:
102
  raise HTTPException(status_code=500, detail=str(e))
103
+
104
+
105
+ @app.get("/outputs/json/{filename}")
106
+ async def get_json_file(filename: str):
107
+ file_path = os.path.join(JSON_DIR, filename)
108
+ if not os.path.exists(file_path):
109
+ raise HTTPException(status_code=404, detail="JSON file not found")
110
+ return FileResponse(file_path, media_type="application/json", filename=filename)
111
+
112
+ @app.get("/outputs/pdf/{filename}")
113
+ async def get_pdf_file(filename: str):
114
+ file_path = os.path.join(PDF_DIR, filename)
115
+ if not os.path.exists(file_path):
116
+ raise HTTPException(status_code=404, detail="PDF file not found")
117
+ return FileResponse(file_path, media_type="application/pdf", filename=filename)