Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -157,20 +157,35 @@ def process_audio_to_text(audio_path, inputlanguage="English", outputlanguage="E
|
|
157 |
def process_text_to_audio(text, translatefrom="English", translateto="English", filename_prefix="audio"):
|
158 |
"""
|
159 |
Convert text input to audio using the Gradio client.
|
160 |
-
Ensure the audio file is correctly saved and returned as a file path.
|
161 |
"""
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
def save_audio_data_to_file(audio_data, directory="audio_files", filename="output_audio.wav"):
|
176 |
"""
|
@@ -328,17 +343,17 @@ outputs = [
|
|
328 |
]
|
329 |
|
330 |
def update_outputs(inputlanguage, target_language, audio, image, text, file):
|
331 |
-
processed_text,
|
332 |
image=image, file=file, audio=audio, text=text,
|
333 |
translateto=target_language, translatefrom=inputlanguage
|
334 |
)
|
335 |
-
|
336 |
-
audio_outputs_components = [(
|
337 |
-
output_tuple = (processed_text,
|
338 |
for i in range(len(top_phrases)):
|
339 |
output_tuple += (top_phrases[i], translations[i]) + audio_outputs_components[i]
|
340 |
while len(output_tuple) < 14:
|
341 |
-
output_tuple += ("", "",
|
342 |
return output_tuple
|
343 |
|
344 |
def interface_func(inputlanguage, target_language, audio, image, text, file):
|
|
|
157 |
def process_text_to_audio(text, translatefrom="English", translateto="English", filename_prefix="audio"):
|
158 |
"""
|
159 |
Convert text input to audio using the Gradio client.
|
160 |
+
Ensure the audio file is correctly saved and returned as a file path or binary data.
|
161 |
"""
|
162 |
+
try:
|
163 |
+
# Generate audio from text
|
164 |
+
audio_response = audio_client.predict(
|
165 |
+
text,
|
166 |
+
translatefrom,
|
167 |
+
translateto,
|
168 |
+
api_name="/t2st"
|
169 |
+
)
|
170 |
+
if "error" in audio_response:
|
171 |
+
raise ValueError(f"API Error: {audio_response['error']}")
|
172 |
+
|
173 |
+
# Assuming audio_response[0] is a URL or file path to the generated audio
|
174 |
+
audio_url = audio_response[0]
|
175 |
+
response = requests.get(audio_url)
|
176 |
+
audio_data = response.content # This should be binary data
|
177 |
+
|
178 |
+
# Generate a unique filename based on the text's hash
|
179 |
+
text_hash = hashlib.md5(text.encode('utf-8')).hexdigest()
|
180 |
+
filename = f"{filename_prefix}_{text_hash}.wav"
|
181 |
+
# Save the audio data to a new file
|
182 |
+
new_audio_file_path = save_audio_data_to_file(audio_data, filename=filename)
|
183 |
+
|
184 |
+
# Return the path to the saved audio file
|
185 |
+
return new_audio_file_path
|
186 |
+
except Exception as e:
|
187 |
+
print(f"Error processing text to audio: {e}")
|
188 |
+
return None
|
189 |
|
190 |
def save_audio_data_to_file(audio_data, directory="audio_files", filename="output_audio.wav"):
|
191 |
"""
|
|
|
343 |
]
|
344 |
|
345 |
def update_outputs(inputlanguage, target_language, audio, image, text, file):
|
346 |
+
processed_text, audio_output_path, top_phrases, translations, audio_outputs = process_input(
|
347 |
image=image, file=file, audio=audio, text=text,
|
348 |
translateto=target_language, translatefrom=inputlanguage
|
349 |
)
|
350 |
+
|
351 |
+
audio_outputs_components = [(ao[0], ao[1]) for ao in audio_outputs]
|
352 |
+
output_tuple = (processed_text, audio_output_path)
|
353 |
for i in range(len(top_phrases)):
|
354 |
output_tuple += (top_phrases[i], translations[i]) + audio_outputs_components[i]
|
355 |
while len(output_tuple) < 14:
|
356 |
+
output_tuple += ("", "", "", "")
|
357 |
return output_tuple
|
358 |
|
359 |
def interface_func(inputlanguage, target_language, audio, image, text, file):
|