Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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",
|
| 159 |
"""
|
| 160 |
-
Convert text input to audio, ensuring the audio file is correctly saved and returned as a file path
|
| 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 |
-
|
| 173 |
-
|
| 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"{
|
| 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 |
-
|
| 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
|