Spaces:
Running
Running
removed the wav concertor
Browse files
app.py
CHANGED
@@ -88,27 +88,16 @@ def health_check():
|
|
88 |
# os.remove(temp_audio_path)
|
89 |
# print(f"Temporary WAV file deleted: {temp_audio_path}")
|
90 |
|
91 |
-
def
|
92 |
-
"""Download
|
93 |
response = requests.get(url, stream=True)
|
94 |
if response.status_code == 200:
|
95 |
-
with open(
|
96 |
for chunk in response.iter_content(chunk_size=1024):
|
97 |
f.write(chunk)
|
98 |
-
print(f"
|
99 |
else:
|
100 |
-
raise Exception(f"Failed to download
|
101 |
-
|
102 |
-
def convert_to_wav(video_path, wav_path):
|
103 |
-
"""Convert video file to WAV format using FFmpeg."""
|
104 |
-
try:
|
105 |
-
ffmpeg_command = [
|
106 |
-
"ffmpeg", "-i", video_path, "-q:a", "0", "-map", "a", wav_path
|
107 |
-
]
|
108 |
-
subprocess.run(ffmpeg_command, check=True)
|
109 |
-
print(f"Video converted to WAV successfully: {wav_path}")
|
110 |
-
except subprocess.CalledProcessError as e:
|
111 |
-
raise Exception(f"FFmpeg conversion failed: {e}")
|
112 |
|
113 |
@app.route('/process-audio', methods=['POST'])
|
114 |
def process_audio():
|
@@ -116,26 +105,21 @@ def process_audio():
|
|
116 |
return jsonify({"error": "No audio URL provided"}), 400
|
117 |
|
118 |
audio_url = request.json['audioUrl']
|
119 |
-
|
120 |
-
temp_wav_path = None
|
121 |
|
122 |
try:
|
123 |
-
# Step 1: Download the
|
124 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
# Step 2: Convert the downloaded video to WAV format
|
129 |
-
temp_wav_path = temp_video_path.replace(".mp4", ".wav")
|
130 |
-
convert_to_wav(temp_video_path, temp_wav_path)
|
131 |
|
132 |
-
# Step
|
133 |
-
transcription = transcribe_audio(
|
134 |
|
135 |
if not transcription:
|
136 |
return jsonify({"error": "Audio transcription failed"}), 500
|
137 |
|
138 |
-
# Step
|
139 |
structured_data = query_gemini_api(transcription)
|
140 |
|
141 |
return jsonify(structured_data)
|
@@ -144,15 +128,10 @@ def process_audio():
|
|
144 |
return jsonify({"error": str(e)}), 500
|
145 |
|
146 |
finally:
|
147 |
-
# Clean up temporary
|
148 |
-
if
|
149 |
-
os.remove(
|
150 |
-
print(f"Temporary
|
151 |
-
|
152 |
-
if temp_wav_path and os.path.exists(temp_wav_path):
|
153 |
-
os.remove(temp_wav_path)
|
154 |
-
print(f"Temporary WAV file deleted: {temp_wav_path}")
|
155 |
-
|
156 |
|
157 |
|
158 |
|
|
|
88 |
# os.remove(temp_audio_path)
|
89 |
# print(f"Temporary WAV file deleted: {temp_audio_path}")
|
90 |
|
91 |
+
def download_audio(url, temp_audio_path):
|
92 |
+
"""Download audio (WAV format) from the given URL and save it to temp_audio_path."""
|
93 |
response = requests.get(url, stream=True)
|
94 |
if response.status_code == 200:
|
95 |
+
with open(temp_audio_path, 'wb') as f:
|
96 |
for chunk in response.iter_content(chunk_size=1024):
|
97 |
f.write(chunk)
|
98 |
+
print(f"Audio downloaded successfully to {temp_audio_path}")
|
99 |
else:
|
100 |
+
raise Exception(f"Failed to download audio, status code: {response.status_code}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
@app.route('/process-audio', methods=['POST'])
|
103 |
def process_audio():
|
|
|
105 |
return jsonify({"error": "No audio URL provided"}), 400
|
106 |
|
107 |
audio_url = request.json['audioUrl']
|
108 |
+
temp_audio_path = None
|
|
|
109 |
|
110 |
try:
|
111 |
+
# Step 1: Download the WAV file from the provided URL
|
112 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
113 |
+
temp_audio_path = temp_audio_file.name
|
114 |
+
download_audio(audio_url, temp_audio_path)
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
# Step 2: Transcribe the downloaded WAV file synchronously
|
117 |
+
transcription = transcribe_audio(temp_audio_path)
|
118 |
|
119 |
if not transcription:
|
120 |
return jsonify({"error": "Audio transcription failed"}), 500
|
121 |
|
122 |
+
# Step 3: Generate structured recipe information using Gemini API synchronously
|
123 |
structured_data = query_gemini_api(transcription)
|
124 |
|
125 |
return jsonify(structured_data)
|
|
|
128 |
return jsonify({"error": str(e)}), 500
|
129 |
|
130 |
finally:
|
131 |
+
# Clean up temporary audio file
|
132 |
+
if temp_audio_path and os.path.exists(temp_audio_path):
|
133 |
+
os.remove(temp_audio_path)
|
134 |
+
print(f"Temporary audio file deleted: {temp_audio_path}")
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
|
137 |
|