Tonic commited on
Commit
7780662
1 Parent(s): 5567a6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -154,9 +154,10 @@ def process_audio_to_text(audio_path, inputlanguage="English", outputlanguage="E
154
  print("Audio Result: ", result)
155
  return result[0]
156
 
157
- def process_text_to_audio(text, translatefrom="English", translateto="English", filename_prefix="audio"):
 
158
  """
159
- Convert text input to audio.
160
  """
161
  try:
162
  # Generate audio from text
@@ -168,7 +169,6 @@ def process_text_to_audio(text, translatefrom="English", translateto="English",
168
  )
169
  if "error" in audio_response:
170
  raise ValueError(f"API Error: {audio_response['error']}")
171
-
172
  audio_url = audio_response[0]
173
  if not audio_url.startswith('http'):
174
  raise ValueError("Invalid URL returned from audio generation API")
@@ -177,17 +177,24 @@ def process_text_to_audio(text, translatefrom="English", translateto="English",
177
  if response.status_code != 200:
178
  raise ValueError("Failed to download audio from URL")
179
 
180
- audio_data = response.content
181
-
182
  text_hash = hashlib.md5(text.encode('utf-8')).hexdigest()
183
  filename = f"{filename_prefix}_{text_hash}.wav"
184
- new_audio_file_path = save_audio_data_to_file(audio_data, filename=filename)
185
-
186
- return new_audio_file_path
 
 
 
 
 
 
 
 
187
  except Exception as e:
188
  print(f"Error processing text to audio: {e}")
189
  return None
190
-
191
  def save_audio_data_to_file(audio_data, directory="audio_files", filename="output_audio.wav"):
192
  """
193
  Save audio data to a file and return the file path.
 
154
  print("Audio Result: ", result)
155
  return result[0]
156
 
157
+
158
+ def process_text_to_audio(text, translatefrom="English", translateto="English", filename_prefix="audio", base_url="https://huggingface.co/spaces/MultiTransformer/AyaTonic"):
159
  """
160
+ Convert text input to audio, ensuring the audio file is correctly saved and returned as a file path or URL.
161
  """
162
  try:
163
  # Generate audio from text
 
169
  )
170
  if "error" in audio_response:
171
  raise ValueError(f"API Error: {audio_response['error']}")
 
172
  audio_url = audio_response[0]
173
  if not audio_url.startswith('http'):
174
  raise ValueError("Invalid URL returned from audio generation API")
 
177
  if response.status_code != 200:
178
  raise ValueError("Failed to download audio from URL")
179
 
180
+ audio_data = response.content
 
181
  text_hash = hashlib.md5(text.encode('utf-8')).hexdigest()
182
  filename = f"{filename_prefix}_{text_hash}.wav"
183
+
184
+ directory = "audio_files"
185
+ os.makedirs(directory, exist_ok=True)
186
+
187
+ file_path = os.path.join(directory, filename)
188
+ with open(file_path, 'wb') as file:
189
+ file.write(audio_data)
190
+
191
+ full_url = f"{base_url}/{directory}/{filename}"
192
+
193
+ return full_url
194
  except Exception as e:
195
  print(f"Error processing text to audio: {e}")
196
  return None
197
+
198
  def save_audio_data_to_file(audio_data, directory="audio_files", filename="output_audio.wav"):
199
  """
200
  Save audio data to a file and return the file path.