Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,10 @@ import requests
|
|
| 5 |
import json
|
| 6 |
from process_interview import process_interview
|
| 7 |
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
@@ -23,12 +27,14 @@ def process_audio():
|
|
| 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 |
|
|
@@ -36,13 +42,16 @@ def process_audio():
|
|
| 36 |
for chunk in response.iter_content(chunk_size=8192):
|
| 37 |
f.write(chunk)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 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'])}"
|
|
@@ -54,6 +63,7 @@ def process_audio():
|
|
| 54 |
})
|
| 55 |
|
| 56 |
except Exception as e:
|
|
|
|
| 57 |
return jsonify({"error": str(e)}), 500
|
| 58 |
|
| 59 |
@app.route("/download", methods=["GET"])
|
|
@@ -65,4 +75,5 @@ def download_file():
|
|
| 65 |
return send_file(file_path, as_attachment=True)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
from process_interview import process_interview
|
| 7 |
from urllib.parse import urlparse
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
# Setup logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
|
|
|
| 27 |
return jsonify({"error": "Missing user_id or audio_url"}), 400
|
| 28 |
|
| 29 |
# Generate unique filename per user
|
| 30 |
+
file_ext = os.path.splitext(urlparse(audio_url).path)[1] or ".wav"
|
| 31 |
filename = f"{user_id}_{uuid.uuid4()}{file_ext}"
|
| 32 |
local_file_path = os.path.join(OUTPUT_DIR, filename)
|
| 33 |
|
| 34 |
+
logging.info(f"Downloading audio from {audio_url} to {local_file_path}")
|
| 35 |
+
|
| 36 |
# Download the audio file
|
| 37 |
+
response = requests.get(audio_url, stream=True, timeout=60)
|
| 38 |
if response.status_code != 200:
|
| 39 |
return jsonify({"error": "Failed to download audio file"}), 400
|
| 40 |
|
|
|
|
| 42 |
for chunk in response.iter_content(chunk_size=8192):
|
| 43 |
f.write(chunk)
|
| 44 |
|
| 45 |
+
if not os.path.exists(local_file_path) or os.path.getsize(local_file_path) == 0:
|
| 46 |
+
return jsonify({"error": "Downloaded file is empty"}), 500
|
| 47 |
+
|
| 48 |
+
logging.info("Starting interview processing...")
|
| 49 |
result = process_interview(local_file_path)
|
| 50 |
+
logging.info("Processing finished.")
|
| 51 |
|
| 52 |
if not result or 'pdf_path' not in result or 'json_path' not in result:
|
| 53 |
return jsonify({"error": "Processing failed"}), 500
|
| 54 |
|
|
|
|
| 55 |
server_url = request.host_url.rstrip('/')
|
| 56 |
pdf_url = f"{server_url}/download?file={os.path.basename(result['pdf_path'])}"
|
| 57 |
json_url = f"{server_url}/download?file={os.path.basename(result['json_path'])}"
|
|
|
|
| 63 |
})
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
+
logging.exception("Error while processing audio")
|
| 67 |
return jsonify({"error": str(e)}), 500
|
| 68 |
|
| 69 |
@app.route("/download", methods=["GET"])
|
|
|
|
| 75 |
return send_file(file_path, as_attachment=True)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
port = int(os.getenv("PORT", 7860)) # Hugging Face passes port in PORT env variable
|
| 79 |
+
app.run(host="0.0.0.0", port=port)
|