Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,13 @@ 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()
|
| 13 |
|
| 14 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
|
| 16 |
-
|
| 17 |
TEMP_DIR = "./temp_files"
|
| 18 |
OUTPUT_DIR = "./static/outputs"
|
| 19 |
|
|
@@ -23,6 +22,8 @@ os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
| 23 |
VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac')
|
| 24 |
MAX_FILE_SIZE_MB = 300
|
| 25 |
|
|
|
|
|
|
|
| 26 |
class ProcessResponse(BaseModel):
|
| 27 |
summary: str
|
| 28 |
json_url: str
|
|
@@ -36,15 +37,15 @@ async def root():
|
|
| 36 |
async def process_audio(file_url: str = Query(..., description="URL of the audio file"),
|
| 37 |
user_id: str = Query(..., description="User ID")):
|
| 38 |
try:
|
| 39 |
-
#
|
| 40 |
file_ext = os.path.splitext(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 |
-
# تحميل الملف
|
| 48 |
resp = requests.get(file_url, stream=True, timeout=30)
|
| 49 |
if resp.status_code != 200:
|
| 50 |
raise HTTPException(status_code=400, detail="Could not download the file from URL")
|
|
@@ -54,23 +55,22 @@ async def process_audio(file_url: str = Query(..., description="URL of the audio
|
|
| 54 |
if chunk:
|
| 55 |
f.write(chunk)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
file_size_mb = os.path.getsize(local_path) / (1024 * 1024)
|
| 59 |
if file_size_mb > MAX_FILE_SIZE_MB:
|
| 60 |
os.remove(local_path)
|
| 61 |
raise HTTPException(status_code=400, detail=f"File too large: {file_size_mb:.2f} MB")
|
| 62 |
|
| 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 |
-
# نسخ ملفات
|
| 70 |
json_src = result['json_path']
|
| 71 |
pdf_src = result['pdf_path']
|
| 72 |
|
| 73 |
-
# أسماء جديدة مع UUID
|
| 74 |
json_dest_name = f"{user_id}_{uuid.uuid4().hex}.json"
|
| 75 |
pdf_dest_name = f"{user_id}_{uuid.uuid4().hex}.pdf"
|
| 76 |
|
|
@@ -80,7 +80,7 @@ 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,12 +96,11 @@ 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 |
-
|
| 101 |
-
|
| 102 |
-
pdf_url = f"{base_url}/{pdf_dest_name}"
|
| 103 |
|
| 104 |
-
#
|
| 105 |
os.remove(local_path)
|
| 106 |
|
| 107 |
return ProcessResponse(summary=summary, json_url=json_url, pdf_url=pdf_url)
|
|
|
|
| 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()
|
| 13 |
|
| 14 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
|
|
|
|
| 16 |
TEMP_DIR = "./temp_files"
|
| 17 |
OUTPUT_DIR = "./static/outputs"
|
| 18 |
|
|
|
|
| 22 |
VALID_EXTENSIONS = ('.wav', '.mp3', '.m4a', '.flac')
|
| 23 |
MAX_FILE_SIZE_MB = 300
|
| 24 |
|
| 25 |
+
BASE_URL = os.getenv("BASE_URL", "http://localhost:7860/static/outputs")
|
| 26 |
+
|
| 27 |
class ProcessResponse(BaseModel):
|
| 28 |
summary: str
|
| 29 |
json_url: str
|
|
|
|
| 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")
|
|
|
|
| 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 |
|
|
|
|
| 74 |
json_dest_name = f"{user_id}_{uuid.uuid4().hex}.json"
|
| 75 |
pdf_dest_name = f"{user_id}_{uuid.uuid4().hex}.pdf"
|
| 76 |
|
|
|
|
| 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 |
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)
|