Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -156,8 +156,7 @@ def process_audio_to_text(audio_path, inputlanguage="English", outputlanguage="E
|
|
| 156 |
|
| 157 |
def process_text_to_audio(text, translatefrom="English", translateto="English", filename_prefix="audio"):
|
| 158 |
"""
|
| 159 |
-
Convert text input to audio
|
| 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
|
|
@@ -170,18 +169,20 @@ def process_text_to_audio(text, translatefrom="English", translateto="English",
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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}")
|
|
@@ -196,6 +197,7 @@ def save_audio_data_to_file(audio_data, directory="audio_files", filename="outpu
|
|
| 196 |
with open(file_path, 'wb') as file:
|
| 197 |
file.write(audio_data)
|
| 198 |
return file_path
|
|
|
|
| 199 |
# Ensure the function that reads the audio file checks if the path is a file
|
| 200 |
def read_audio_file(file_path):
|
| 201 |
"""
|
|
|
|
| 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
|
|
|
|
| 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")
|
| 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 |
|
|
|
|
| 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}")
|
|
|
|
| 197 |
with open(file_path, 'wb') as file:
|
| 198 |
file.write(audio_data)
|
| 199 |
return file_path
|
| 200 |
+
|
| 201 |
# Ensure the function that reads the audio file checks if the path is a file
|
| 202 |
def read_audio_file(file_path):
|
| 203 |
"""
|