Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,10 +40,13 @@ async def process_audio(
|
|
| 40 |
file_url: Optional[str] = Form(None),
|
| 41 |
file: Optional[UploadFile] = File(None)
|
| 42 |
):
|
| 43 |
-
|
|
|
|
|
|
|
| 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:
|
|
@@ -55,26 +58,36 @@ async def process_audio(
|
|
| 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 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return ProcessResponse(
|
| 67 |
user_id=user_id,
|
| 68 |
summary="تم تحليل المقابلة بنجاح.",
|
| 69 |
-
json_url=f"{BASE_URL}/static/outputs/json/{
|
| 70 |
-
pdf_url=f"{BASE_URL}/static/outputs/pdf/{
|
| 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):
|
|
|
|
| 40 |
file_url: Optional[str] = Form(None),
|
| 41 |
file: Optional[UploadFile] = File(None)
|
| 42 |
):
|
| 43 |
+
# إنشاء أسماء فريدة للملفات النهائية
|
| 44 |
+
unique_id = uuid.uuid4().hex
|
| 45 |
+
local_filename = f"{user_id}_{unique_id}"
|
| 46 |
local_path = os.path.join(TEMP_DIR, local_filename)
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
# 1. تحميل الملف
|
| 50 |
if file_url:
|
| 51 |
resp = requests.get(file_url, stream=True, timeout=30)
|
| 52 |
if resp.status_code != 200:
|
|
|
|
| 58 |
shutil.copyfileobj(file.file, buffer)
|
| 59 |
else:
|
| 60 |
raise HTTPException(status_code=400, detail="يجب توفير رابط صوتي أو رفع ملف.")
|
| 61 |
+
|
| 62 |
+
# 2. المعالجة (تأكدي أن process_interview تعيد مسارات الملفات الناتجة)
|
| 63 |
result = process_interview(local_path)
|
| 64 |
|
| 65 |
+
# 3. نقل الملفات من مكانها الأصلي (processed_audio) إلى مجلد static العام
|
| 66 |
+
final_json_name = f"{local_filename}.json"
|
| 67 |
+
final_pdf_name = f"{local_filename}.pdf"
|
| 68 |
+
|
| 69 |
+
json_dest_path = os.path.join(JSON_DIR, final_json_name)
|
| 70 |
+
pdf_dest_path = os.path.join(PDF_DIR, final_pdf_name)
|
| 71 |
|
| 72 |
+
# النقل: من مسار النتيجة إلى المسار المتاح للمتصفح
|
| 73 |
+
shutil.move(result['json_path'], json_dest_path)
|
| 74 |
+
shutil.move(result['pdf_path'], pdf_dest_path)
|
| 75 |
+
|
| 76 |
+
# تنظيف الملف المؤقت
|
| 77 |
+
if os.path.exists(local_path):
|
| 78 |
+
os.remove(local_path)
|
| 79 |
+
|
| 80 |
return ProcessResponse(
|
| 81 |
user_id=user_id,
|
| 82 |
summary="تم تحليل المقابلة بنجاح.",
|
| 83 |
+
json_url=f"{BASE_URL}/static/outputs/json/{final_json_name}",
|
| 84 |
+
pdf_url=f"{BASE_URL}/static/outputs/pdf/{final_pdf_name}"
|
| 85 |
)
|
| 86 |
+
|
| 87 |
except Exception as e:
|
| 88 |
if os.path.exists(local_path):
|
| 89 |
os.remove(local_path)
|
| 90 |
raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء المعالجة: {str(e)}")
|
|
|
|
| 91 |
# مسارات عرض الملفات
|
| 92 |
@app.get("/static/outputs/json/{filename}")
|
| 93 |
async def get_json_file(filename: str):
|