Tonic commited on
Commit
278c735
1 Parent(s): 3bfb859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -155,32 +155,27 @@ def process_audio_to_text(audio_path, inputlanguage="English", outputlanguage="E
155
  return result[0]
156
 
157
 
158
- def process_text_to_audio(text, translatefrom="English", translateto="English", directory="audio_files"):
159
  """
160
- Convert text input to audio, ensuring the audio file is correctly saved and returned as a file path.
161
  """
162
  try:
163
- audio_response = audio_client.predict(
 
164
  text,
165
  translatefrom,
166
  translateto,
167
  api_name="/t2st"
168
  )
169
- if "error" in audio_response:
170
- raise ValueError(f"API Error: {audio_response['error']}")
171
- audio_data = audio_response[0]
172
 
173
- text_hash = hashlib.md5(text.encode('utf-8')).hexdigest()
174
- filename = f"{text_hash}.wav"
 
175
 
176
- os.makedirs(directory, exist_ok=True)
177
 
178
- file_path = os.path.join(directory, filename)
179
-
180
- with open(file_path, 'wb') as file:
181
- file.write(audio_data)
182
-
183
- return file_path
184
  except Exception as e:
185
  print(f"Error processing text to audio: {e}")
186
  return None
 
155
  return result[0]
156
 
157
 
158
+ def process_text_to_audio(text, translatefrom="English", translateto="English"):
159
  """
160
+ Convert text input to audio using the Gradio client and return the local file path of the generated audio.
161
  """
162
  try:
163
+ # Call the client's predict method with the text, source language, and target language
164
+ result = audio_client.predict(
165
  text,
166
  translatefrom,
167
  translateto,
168
  api_name="/t2st"
169
  )
 
 
 
170
 
171
+ # The result is expected to be a tuple (audio_file_path, translated_text)
172
+ if not isinstance(result, tuple) or len(result) < 2:
173
+ raise ValueError("Unexpected result format from audio_client.predict")
174
 
175
+ audio_file_path, _ = result # Ignore the translated text for this function's purpose
176
 
177
+ # Return the local file path of the generated audio
178
+ return audio_file_path
 
 
 
 
179
  except Exception as e:
180
  print(f"Error processing text to audio: {e}")
181
  return None