awacke1 commited on
Commit
260707c
·
verified ·
1 Parent(s): 309d3fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -213,13 +213,21 @@ async def async_edge_tts_generate(text, voice, username, rate=0, pitch=0, file_f
213
  return st.session_state['audio_cache'][cache_key], 0
214
  start_time = time.time()
215
  text = clean_text_for_tts(text)
216
- if not text:
 
217
  return None, 0
218
  filename = f"{format_timestamp_prefix(username)}-{hashlib.md5(text.encode()).hexdigest()[:8]}.{file_format}"
219
- communicate = edge_tts.Communicate(text, voice, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz")
220
- await communicate.save(filename)
221
- st.session_state['audio_cache'][cache_key] = filename
222
- return filename, time.time() - start_time
 
 
 
 
 
 
 
223
 
224
  def play_and_download_audio(file_path):
225
  if file_path and os.path.exists(file_path):
@@ -245,6 +253,8 @@ async def save_chat_entry(username, message, is_markdown=False):
245
  with open(HISTORY_FILE, 'a') as f:
246
  f.write(f"[{timestamp}] {username}: Audio - {audio_file}\n")
247
  st.session_state['mp3_files'][os.path.basename(audio_file)] = audio_file
 
 
248
  await broadcast_message(f"{username}|{message}", "chat")
249
  st.session_state.last_chat_update = time.time()
250
  st.session_state.chat_history.append(entry)
 
213
  return st.session_state['audio_cache'][cache_key], 0
214
  start_time = time.time()
215
  text = clean_text_for_tts(text)
216
+ if not text or text == "No text":
217
+ print(f"Skipping audio generation for empty/invalid text: '{text}'")
218
  return None, 0
219
  filename = f"{format_timestamp_prefix(username)}-{hashlib.md5(text.encode()).hexdigest()[:8]}.{file_format}"
220
+ try:
221
+ communicate = edge_tts.Communicate(text, voice, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz")
222
+ await communicate.save(filename)
223
+ st.session_state['audio_cache'][cache_key] = filename
224
+ return filename, time.time() - start_time
225
+ except edge_tts.exceptions.NoAudioReceived as e:
226
+ print(f"No audio received for text: '{text}' with voice: {voice}. Error: {e}")
227
+ return None, 0
228
+ except Exception as e:
229
+ print(f"Error generating audio for text: '{text}' with voice: {voice}. Error: {e}")
230
+ return None, 0
231
 
232
  def play_and_download_audio(file_path):
233
  if file_path and os.path.exists(file_path):
 
253
  with open(HISTORY_FILE, 'a') as f:
254
  f.write(f"[{timestamp}] {username}: Audio - {audio_file}\n")
255
  st.session_state['mp3_files'][os.path.basename(audio_file)] = audio_file
256
+ else:
257
+ print(f"No audio generated for message: '{message}' by {username}")
258
  await broadcast_message(f"{username}|{message}", "chat")
259
  st.session_state.last_chat_update = time.time()
260
  st.session_state.chat_history.append(entry)