Tonic commited on
Commit
81dcc03
1 Parent(s): 14f0305

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -140,6 +140,7 @@ def process_speech(input_language, audio_input):
140
  return f"{e}"
141
 
142
 
 
143
  def convert_text_to_speech(input_text: str, source_language: str, target_language: str) -> tuple[str, str]:
144
  client = Client("https://facebook-seamless-m4t.hf.space/--replicas/8cllp/")
145
 
@@ -156,11 +157,10 @@ def convert_text_to_speech(input_text: str, source_language: str, target_languag
156
  )
157
 
158
  # Initialize variables
159
- original_audio_file = None
160
  translated_text = ""
161
  new_file_path = ""
162
 
163
- # Iterate over the result to find the text and the first audio file
164
  for item in result:
165
  if isinstance(item, str):
166
  # Check if the item is likely a URL
@@ -169,19 +169,23 @@ def convert_text_to_speech(input_text: str, source_language: str, target_languag
169
  # Assign the first non-URL string as the translated text
170
  if not translated_text:
171
  translated_text = item
172
- elif os.path.isfile(item):
173
- original_audio_file = item
174
- break # Stop after finding the first audio file
175
-
176
- if original_audio_file:
177
- new_file_name = f"audio_output_{uuid.uuid4()}.wav"
178
- new_file_path = os.path.join(os.path.dirname(original_audio_file), new_file_name)
179
- os.rename(original_audio_file, new_file_path)
 
 
 
180
 
181
  return new_file_path, translated_text
182
 
183
  except Exception as e:
184
  return f"Error in text-to-speech conversion: {str(e)}", ""
 
185
 
186
 
187
 
 
140
  return f"{e}"
141
 
142
 
143
+
144
  def convert_text_to_speech(input_text: str, source_language: str, target_language: str) -> tuple[str, str]:
145
  client = Client("https://facebook-seamless-m4t.hf.space/--replicas/8cllp/")
146
 
 
157
  )
158
 
159
  # Initialize variables
 
160
  translated_text = ""
161
  new_file_path = ""
162
 
163
+ # Process each item in the result
164
  for item in result:
165
  if isinstance(item, str):
166
  # Check if the item is likely a URL
 
169
  # Assign the first non-URL string as the translated text
170
  if not translated_text:
171
  translated_text = item
172
+ elif os.path.isdir(item):
173
+ # Process the directory to find the first audio file
174
+ for file in os.listdir(item):
175
+ file_path = os.path.join(item, file)
176
+ if os.path.isfile(file_path) and file_path.endswith('.wav'):
177
+ new_file_name = f"audio_output_{uuid.uuid4()}.wav"
178
+ new_file_path = os.path.join(os.path.dirname(file_path), new_file_name)
179
+ os.rename(file_path, new_file_path)
180
+ break # Stop after finding the first audio file
181
+ if new_file_path:
182
+ break # Exit if an audio file has been processed
183
 
184
  return new_file_path, translated_text
185
 
186
  except Exception as e:
187
  return f"Error in text-to-speech conversion: {str(e)}", ""
188
+
189
 
190
 
191