Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,68 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
-
import
|
| 4 |
import json
|
| 5 |
-
import shutil
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
import tempfile
|
| 8 |
-
import gradio as gr
|
| 9 |
from process_interview import process_interview
|
| 10 |
-
from
|
| 11 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 12 |
-
import requests
|
| 13 |
-
logging.basicConfig(level=logging.INFO)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
TEMP_DIR = "./temp_files"
|
| 17 |
-
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
except Exception as e:
|
| 29 |
-
logging.error(f"Health check failed: {e}")
|
| 30 |
-
return "System unhealthy"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
try:
|
| 35 |
-
# تحميل الملف
|
| 36 |
-
filename = f"{user_id}_{os.path.basename(file_url)}"
|
| 37 |
-
local_path = os.path.join(TEMP_DIR, filename)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
f.write(chunk)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
-
return
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
result = process_interview(
|
| 56 |
-
if not result:
|
| 57 |
-
return "❌ Processing failed.", "", ""
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
analysis_data = json.load(f)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
f"Speakers: {', '.join(analysis_data['speakers'])}\n"
|
| 68 |
-
f"Duration: {analysis_data['text_analysis']['total_duration']:.2f} sec\n"
|
| 69 |
-
f"Confidence: {voice['confidence_level']}\n"
|
| 70 |
-
f"Anxiety: {voice['anxiety_level']}"
|
| 71 |
-
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
|
| 78 |
-
return f"❌ Internal Error: {str(e)}", "", ""
|
| 79 |
-
|
| 80 |
-
# Gradio Interface
|
| 81 |
-
with gr.Blocks(title="EvalBot Audio Analysis") as demo:
|
| 82 |
-
gr.Markdown("# 🎙️ EvalBot Audio Analysis (Single URL Mode)")
|
| 83 |
-
|
| 84 |
-
file_url = gr.Textbox(label="Audio File URL (single)")
|
| 85 |
-
user_id = gr.Textbox(label="User ID")
|
| 86 |
-
|
| 87 |
-
analyze_btn = gr.Button("Analyze Audio")
|
| 88 |
-
|
| 89 |
-
output_summary = gr.Textbox(label="Summary")
|
| 90 |
-
output_json = gr.Textbox(label="JSON Output")
|
| 91 |
-
output_pdf = gr.Textbox(label="PDF Path")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
import os
|
| 3 |
import uuid
|
| 4 |
+
import requests
|
| 5 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from process_interview import process_interview
|
| 7 |
+
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Create folders to store the files
|
| 12 |
+
OUTPUT_DIR = "./processed_audio"
|
| 13 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 14 |
|
| 15 |
+
@app.route("/process-audio", methods=["POST"])
|
| 16 |
+
def process_audio():
|
| 17 |
try:
|
| 18 |
+
data = request.json
|
| 19 |
+
user_id = data.get("user_id")
|
| 20 |
+
audio_url = data.get("audio_url")
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
if not user_id or not audio_url:
|
| 23 |
+
return jsonify({"error": "Missing user_id or audio_url"}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Generate unique filename per user
|
| 26 |
+
file_ext = os.path.splitext(urlparse(audio_url).path)[1]
|
| 27 |
+
filename = f"{user_id}_{uuid.uuid4()}{file_ext}"
|
| 28 |
+
local_file_path = os.path.join(OUTPUT_DIR, filename)
|
|
|
|
| 29 |
|
| 30 |
+
# Download the audio file
|
| 31 |
+
response = requests.get(audio_url, stream=True)
|
| 32 |
+
if response.status_code != 200:
|
| 33 |
+
return jsonify({"error": "Failed to download audio file"}), 400
|
| 34 |
|
| 35 |
+
with open(local_file_path, "wb") as f:
|
| 36 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 37 |
+
f.write(chunk)
|
| 38 |
|
| 39 |
+
# Call your processing pipeline
|
| 40 |
+
result = process_interview(local_file_path)
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
if not result or 'pdf_path' not in result or 'json_path' not in result:
|
| 43 |
+
return jsonify({"error": "Processing failed"}), 500
|
|
|
|
| 44 |
|
| 45 |
+
# Return file URLs (assuming you deploy this API somewhere publicly)
|
| 46 |
+
server_url = request.host_url.rstrip('/')
|
| 47 |
+
pdf_url = f"{server_url}/download?file={os.path.basename(result['pdf_path'])}"
|
| 48 |
+
json_url = f"{server_url}/download?file={os.path.basename(result['json_path'])}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
return jsonify({
|
| 51 |
+
"message": "Processing complete",
|
| 52 |
+
"pdf_url": pdf_url,
|
| 53 |
+
"json_url": json_url
|
| 54 |
+
})
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
@app.route("/download", methods=["GET"])
|
| 60 |
+
def download_file():
|
| 61 |
+
filename = request.args.get("file")
|
| 62 |
+
file_path = os.path.join(OUTPUT_DIR, filename)
|
| 63 |
+
if not os.path.exists(file_path):
|
| 64 |
+
return jsonify({"error": "File not found"}), 404
|
| 65 |
+
return send_file(file_path, as_attachment=True)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
app.run(host="0.0.0.0", port=5000)
|