Tonic commited on
Commit
a543a78
·
1 Parent(s): e6f14ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -196,13 +196,15 @@ def process_speech(input_language, audio_input):
196
 
197
 
198
  def convert_text_to_speech(input_text: str, source_language: str, target_language: str) -> tuple[str, str]:
 
 
 
199
  client = Client("https://facebook-seamless-m4t.hf.space/--replicas/8cllp/")
200
 
201
  try:
202
- # Make a prediction request to the client
203
  result = client.predict(
204
  "T2ST",
205
- "text", # Since we are doing text-to-speech
206
  None,
207
  None,
208
  input_text,
@@ -210,27 +212,28 @@ def convert_text_to_speech(input_text: str, source_language: str, target_languag
210
  target_language,
211
  api_name="/run"
212
  )
 
 
213
 
214
- # Initialize variables
215
  translated_text = ""
216
  audio_file_path = ""
217
 
218
- # Process the result
219
  if result:
220
  for item in result:
221
  if isinstance(item, str):
222
- # Check if the item is a URL pointing to an audio file
223
- if item.endswith('.mp3'):
224
- if not audio_file_path: # Store only the first audio file path
225
- audio_file_path = item
226
  else:
227
- # Concatenate the translated text
228
  translated_text += item + " "
 
 
229
 
230
- return audio_file_path, translated_text.strip()
 
 
 
231
 
232
- except Exception as e:
233
- return None, f"Error in text-to-speech conversion: {str(e)}"
234
 
235
  def process_image(image_input):
236
  # Initialize the Gradio client with the URL of the Gradio server
 
196
 
197
 
198
  def convert_text_to_speech(input_text: str, source_language: str, target_language: str) -> tuple[str, str]:
199
+ if not input_text or not source_language or not target_language:
200
+ return None, "Invalid input parameters."
201
+
202
  client = Client("https://facebook-seamless-m4t.hf.space/--replicas/8cllp/")
203
 
204
  try:
 
205
  result = client.predict(
206
  "T2ST",
207
+ "text",
208
  None,
209
  None,
210
  input_text,
 
212
  target_language,
213
  api_name="/run"
214
  )
215
+ except Exception as e:
216
+ return None, f"Error during prediction: {str(e)}"
217
 
218
+ try:
219
  translated_text = ""
220
  audio_file_path = ""
221
 
 
222
  if result:
223
  for item in result:
224
  if isinstance(item, str):
225
+ if item.endswith('.mp3') and not audio_file_path:
226
+ audio_file_path = item
 
 
227
  else:
 
228
  translated_text += item + " "
229
+ except Exception as e:
230
+ return None, f"Error processing result: {str(e)}"
231
 
232
+ if not audio_file_path:
233
+ return None, "No audio file path found in the result."
234
+
235
+ return audio_file_path, translated_text.strip()
236
 
 
 
237
 
238
  def process_image(image_input):
239
  # Initialize the Gradio client with the URL of the Gradio server