Tonic commited on
Commit
ac0fcbc
·
verified ·
1 Parent(s): 3ef51fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
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
- # Generate audio from text
163
- audio_response = audio_client.predict(
164
- text,
165
- translatefrom,
166
- translateto,
167
- api_name="/t2st"
168
- )
169
- text_hash = hashlib.md5(text.encode('utf-8')).hexdigest()
170
- filename = f"{filename_prefix}_{text_hash}.wav"
171
- audio_file_path = save_audio_data_to_file(audio_response[0], filename=filename)
172
-
173
- return audio_response[0], audio_file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, audio_output, top_phrases, translations, audio_outputs = process_input(
332
  image=image, file=file, audio=audio, text=text,
333
  translateto=target_language, translatefrom=inputlanguage
334
  )
335
- audio_output = gr.Audio(file_path=audio_output_path)
336
- audio_outputs_components = [(gr.Audio(file_path=ao[0]), gr.Audio(file_path=ao[1])) for ao in audio_outputs]
337
- output_tuple = (processed_text, audio_output)
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 += ("", "", gr.Audio(), gr.Audio())
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):