IAMTFRMZA commited on
Commit
f7b0220
·
verified ·
1 Parent(s): dc8e6e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -4,6 +4,8 @@ import edge_tts
4
  import time
5
  import os
6
  import uuid
 
 
7
  import firebase_admin
8
  from firebase_admin import credentials, firestore
9
  from openai import OpenAI
@@ -64,7 +66,6 @@ header {visibility: hidden;}
64
  margin-top: 0.1em;
65
  position: relative;
66
  }
67
-
68
  .clear-chat-btn-top {
69
  position: absolute;
70
  top: 10px;
@@ -77,21 +78,17 @@ header {visibility: hidden;}
77
  z-index: 1000;
78
  transition: color 0.2s ease;
79
  }
80
-
81
  .clear-chat-btn-top:hover {
82
  color: #fff;
83
  }
84
-
85
  .stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
86
  .stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
87
  .stChatMessage[data-testid="stChatMessage-assistant"] { background: #e3f2fd; color: #000000; }
88
-
89
  .chat-history-wrapper {
90
  margin-top: 0.5em;
91
- padding-bottom: 9em; /* enough space so input does not cover messages */
92
  min-height: 60vh;
93
  }
94
-
95
  .input-bottom-bar {
96
  position: fixed;
97
  bottom: 3.5em;
@@ -180,6 +177,22 @@ def display_chat_history():
180
  st.markdown('<div class="chat-history-wrapper">' + "".join(chat_msgs) + '</div>', unsafe_allow_html=True)
181
  st.markdown('<div id="chat-top-anchor"></div>', unsafe_allow_html=True)
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  # --- Edge TTS synth
184
  async def edge_tts_synthesize(text, voice, user_id):
185
  out_path = f"output_{user_id}.mp3"
@@ -241,7 +254,8 @@ if user_input:
241
  mute_voice = st.session_state.get("mute_voice", False)
242
  audio_path = None
243
  if not mute_voice and assistant_message.strip():
244
- audio_path = synthesize_voice(assistant_message, st.session_state["selected_voice"], user_id)
 
245
  st.session_state["last_audio_path"] = audio_path
246
 
247
  time.sleep(0.2)
 
4
  import time
5
  import os
6
  import uuid
7
+ import re
8
+ import html
9
  import firebase_admin
10
  from firebase_admin import credentials, firestore
11
  from openai import OpenAI
 
66
  margin-top: 0.1em;
67
  position: relative;
68
  }
 
69
  .clear-chat-btn-top {
70
  position: absolute;
71
  top: 10px;
 
78
  z-index: 1000;
79
  transition: color 0.2s ease;
80
  }
 
81
  .clear-chat-btn-top:hover {
82
  color: #fff;
83
  }
 
84
  .stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
85
  .stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
86
  .stChatMessage[data-testid="stChatMessage-assistant"] { background: #e3f2fd; color: #000000; }
 
87
  .chat-history-wrapper {
88
  margin-top: 0.5em;
89
+ padding-bottom: 9em;
90
  min-height: 60vh;
91
  }
 
92
  .input-bottom-bar {
93
  position: fixed;
94
  bottom: 3.5em;
 
177
  st.markdown('<div class="chat-history-wrapper">' + "".join(chat_msgs) + '</div>', unsafe_allow_html=True)
178
  st.markdown('<div id="chat-top-anchor"></div>', unsafe_allow_html=True)
179
 
180
+ # --- TTS sanitize
181
+ def sanitize_for_tts(text):
182
+ text = html.unescape(text)
183
+ text = re.sub(r'[^\x00-\x7F]+', ' ', text)
184
+ text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)
185
+ text = re.sub(r'(\*\*|__)(.*?)\1', r'\2', text)
186
+ text = re.sub(r'(\*|_)(.*?)\1', r'\2', text)
187
+ text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
188
+ text = re.sub(r'^\s*[-*+]\s+', ' • ', text, flags=re.MULTILINE)
189
+ text = re.sub(r'^\s*\d+\.\s+', ' • ', text, flags=re.MULTILINE)
190
+ text = re.sub(r'[!?]{2,}', '.', text)
191
+ text = re.sub(r'\.{3,}', '.', text)
192
+ text = re.sub(r'\n{2,}', '. ', text)
193
+ text = re.sub(r'\s+', ' ', text).strip()
194
+ return text
195
+
196
  # --- Edge TTS synth
197
  async def edge_tts_synthesize(text, voice, user_id):
198
  out_path = f"output_{user_id}.mp3"
 
254
  mute_voice = st.session_state.get("mute_voice", False)
255
  audio_path = None
256
  if not mute_voice and assistant_message.strip():
257
+ clean_text = sanitize_for_tts(assistant_message)
258
+ audio_path = synthesize_voice(clean_text, st.session_state["selected_voice"], user_id)
259
  st.session_state["last_audio_path"] = audio_path
260
 
261
  time.sleep(0.2)