Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException,
|
| 2 |
-
from pydantic import BaseModel
|
| 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 |
from fastapi.responses import FileResponse
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
@@ -22,103 +22,64 @@ 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', '.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
|
| 42 |
pdf_url: str
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
@app.post("/process-audio", response_model=ProcessResponse)
|
| 49 |
-
async def process_audio(
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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 |
-
|
| 89 |
-
|
| 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 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
f"
|
| 99 |
-
f"
|
| 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 |
-
|
|
|
|
|
|
|
| 111 |
|
|
|
|
| 112 |
@app.get("/static/outputs/json/{filename}")
|
| 113 |
async def get_json_file(filename: str):
|
| 114 |
-
|
| 115 |
-
if not os.path.exists(file_path):
|
| 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 |
-
|
| 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)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
import shutil
|
|
|
|
| 6 |
import requests
|
| 7 |
from process_interview import process_interview
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
from fastapi.responses import FileResponse
|
| 10 |
+
from typing import Optional
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
| 22 |
|
| 23 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 24 |
|
|
|
|
|
|
|
| 25 |
BASE_URL = os.getenv("BASE_URL", "https://evalbot-audio-evalbot.hf.space")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
class ProcessResponse(BaseModel):
|
| 28 |
+
user_id: str
|
| 29 |
summary: str
|
| 30 |
json_url: str
|
| 31 |
pdf_url: str
|
| 32 |
|
| 33 |
+
@app.get("/")
|
| 34 |
+
def read_root():
|
| 35 |
+
return {"message": "EvalBot API is running successfully! 🚀"}
|
| 36 |
|
| 37 |
@app.post("/process-audio", response_model=ProcessResponse)
|
| 38 |
+
async def process_audio(
|
| 39 |
+
user_id: str = Form(...),
|
| 40 |
+
file_url: Optional[str] = Form(None),
|
| 41 |
+
file: Optional[UploadFile] = File(None)
|
| 42 |
+
):
|
| 43 |
+
local_filename = f"{user_id}_{uuid.uuid4().hex}"
|
| 44 |
+
local_path = os.path.join(TEMP_DIR, local_filename)
|
| 45 |
|
| 46 |
try:
|
| 47 |
+
if file_url:
|
| 48 |
+
resp = requests.get(file_url, stream=True, timeout=30)
|
| 49 |
+
if resp.status_code != 200:
|
| 50 |
+
raise HTTPException(status_code=400, detail="تعذر الوصول للرابط المقدم.")
|
| 51 |
+
with open(local_path, "wb") as f:
|
| 52 |
+
shutil.copyfileobj(resp.raw, f)
|
| 53 |
+
elif file:
|
| 54 |
+
with open(local_path, "wb") as buffer:
|
| 55 |
+
shutil.copyfileobj(file.file, buffer)
|
| 56 |
+
else:
|
| 57 |
+
raise HTTPException(status_code=400, detail="يجب توفير رابط صوتي أو رفع ملف.")
|
| 58 |
+
|
| 59 |
+
# 2. المعالجة
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
result = process_interview(local_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# 3. إعداد الروابط
|
| 63 |
+
json_filename = os.path.basename(result['json_path'])
|
| 64 |
+
pdf_filename = os.path.basename(result['pdf_path'])
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
return ProcessResponse(
|
| 67 |
+
user_id=user_id,
|
| 68 |
+
summary="تم تحليل المقابلة بنجاح.",
|
| 69 |
+
json_url=f"{BASE_URL}/static/outputs/json/{json_filename}",
|
| 70 |
+
pdf_url=f"{BASE_URL}/static/outputs/pdf/{pdf_filename}"
|
|
|
|
| 71 |
)
|
| 72 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
+
if os.path.exists(local_path):
|
| 75 |
+
os.remove(local_path)
|
| 76 |
+
raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء المعالجة: {str(e)}")
|
| 77 |
|
| 78 |
+
# مسارات عرض الملفات
|
| 79 |
@app.get("/static/outputs/json/{filename}")
|
| 80 |
async def get_json_file(filename: str):
|
| 81 |
+
return FileResponse(os.path.join(JSON_DIR, filename))
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
@app.get("/static/outputs/pdf/{filename}")
|
| 84 |
async def get_pdf_file(filename: str):
|
| 85 |
+
return FileResponse(os.path.join(PDF_DIR, filename))
|
|
|
|
|
|
|
|
|