Tonic commited on
Commit
181a56d
·
verified ·
1 Parent(s): dd7db97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -172,26 +172,33 @@ def process_text_to_audio(text, translatefrom="English", translateto="English"):
172
  if not isinstance(result, tuple) or len(result) < 2:
173
  raise ValueError("Unexpected result format from audio_client.predict")
174
 
175
- # Extract the local file path from the result
176
- local_file_path, _ = result
177
-
178
- # Ensure that local_file_path points to a file, not a directory
179
- if not os.path.isfile(local_file_path):
180
- raise ValueError(f"The path does not point to a valid file: {local_file_path}")
181
-
182
- # Extract the file name from the local_file_path
183
- file_name = os.path.basename(local_file_path)
184
 
185
- # Construct the URL by appending the file name to the base URL
186
- base_url = "https://multitransformer-ayatonic.hf.space/"
187
- audio_url = os.path.join(base_url, file_name) # This might need adjustment based on how your server serves files
 
 
 
 
 
 
 
 
 
 
 
188
 
189
- audio_url = f"{base_url}{file_name}"
 
 
 
 
 
190
 
191
- return audio_url
192
  except Exception as e:
193
  print(f"Error processing text to audio: {e}")
194
- return None
 
195
 
196
  def save_audio_data_to_file(audio_data, directory="audio_files", filename="output_audio.wav"):
197
  """
 
172
  if not isinstance(result, tuple) or len(result) < 2:
173
  raise ValueError("Unexpected result format from audio_client.predict")
174
 
 
 
 
 
 
 
 
 
 
175
 
176
+ # Print or log the raw API response for inspection
177
+ print("Raw API Response:", result)
178
+
179
+ # Initialize variables
180
+ audio_file_path = ""
181
+
182
+ # Process the result
183
+ if result:
184
+ for item in result:
185
+ if isinstance(item, str):
186
+ # Check if the item is a URL pointing to an audio file or a base64 encoded string
187
+ if any(ext in item.lower() for ext in ['.mp3', '.wav', '.ogg']) or is_base64(item):
188
+ audio_file_path = item
189
+ break
190
 
191
+ if not audio_file_path:
192
+ raise ValueError("No audio file path found in the response")
193
+
194
+ # If the response is a direct file path or a base64 string, handle accordingly
195
+ # For simplicity, we're returning the URL or base64 string directly
196
+ return audio_file_path
197
 
 
198
  except Exception as e:
199
  print(f"Error processing text to audio: {e}")
200
+ return ""
201
+
202
 
203
  def save_audio_data_to_file(audio_data, directory="audio_files", filename="output_audio.wav"):
204
  """