Tonic commited on
Commit
3bfb859
·
verified ·
1 Parent(s): 31ada78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -17
app.py CHANGED
@@ -155,12 +155,11 @@ 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", filename_prefix="audio", base_url="https://huggingface.co/spaces/MultiTransformer/AyaTonicblob/main/"):
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
164
  audio_response = audio_client.predict(
165
  text,
166
  translatefrom,
@@ -169,28 +168,19 @@ def process_text_to_audio(text, translatefrom="English", translateto="English",
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")
175
-
176
- response = requests.get(audio_url)
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
 
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,
 
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