IAMTFRMZA commited on
Commit
16745ff
·
verified ·
1 Parent(s): 54f692e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -6
app.py CHANGED
@@ -33,6 +33,8 @@ if "last_tts_text" not in st.session_state:
33
  st.session_state["last_tts_text"] = ""
34
  if "last_audio_path" not in st.session_state:
35
  st.session_state["last_audio_path"] = ""
 
 
36
 
37
  # --- Page config ---
38
  st.set_page_config(page_title="LOR Technologies AI Assistant", layout="wide")
@@ -119,7 +121,7 @@ def display_chat_history():
119
 
120
  # --- Edge TTS synth ---
121
  def sanitize_tts_text(text):
122
- text = re.sub(r'[^\w\s\.\,]', '', text) # remove emojis & special chars
123
  text = text.replace('.co.za', 'dot coza')
124
  return text
125
 
@@ -134,15 +136,29 @@ def synthesize_voice(text, user_id):
134
  sanitized = sanitize_tts_text(text)
135
  out_path = f"output_{user_id}.mp3"
136
  if st.session_state["last_tts_text"] != sanitized or not os.path.exists(out_path):
137
- with st.spinner(f"Generating voice ({FIXED_VOICE_NAME})..."):
138
- asyncio.run(edge_tts_synthesize(sanitized, voice, user_id))
139
- st.session_state["last_tts_text"] = sanitized
140
- st.session_state["last_audio_path"] = out_path
 
 
 
 
141
  return out_path
142
 
143
  # --- CHAT: display history ---
144
  display_chat_history()
145
 
 
 
 
 
 
 
 
 
 
 
146
  # --- INPUT BAR (floating at bottom) ---
147
  st.markdown('<div class="chat-input-bar">', unsafe_allow_html=True)
148
  col1, col2 = st.columns([10, 1])
@@ -153,6 +169,8 @@ st.markdown('</div>', unsafe_allow_html=True)
153
 
154
  # --- PROCESS USER INPUT ---
155
  if user_input:
 
 
156
  thread_id = get_or_create_thread_id()
157
  client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
158
  save_message("user", user_input)
@@ -171,8 +189,24 @@ if user_input:
171
  save_message("assistant", assistant_message)
172
 
173
  audio_path = synthesize_voice(assistant_message, user_id)
174
- if os.path.exists(audio_path):
175
  st.audio(audio_path, format="audio/mp3", autoplay=True)
176
 
 
 
177
  time.sleep(0.2)
178
  st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  st.session_state["last_tts_text"] = ""
34
  if "last_audio_path" not in st.session_state:
35
  st.session_state["last_audio_path"] = ""
36
+ if "is_thinking" not in st.session_state:
37
+ st.session_state["is_thinking"] = False
38
 
39
  # --- Page config ---
40
  st.set_page_config(page_title="LOR Technologies AI Assistant", layout="wide")
 
121
 
122
  # --- Edge TTS synth ---
123
  def sanitize_tts_text(text):
124
+ text = re.sub(r'[^\w\s\.\,\!\?\:\;\'\"]', '', text) # keep basic punctuation
125
  text = text.replace('.co.za', 'dot coza')
126
  return text
127
 
 
136
  sanitized = sanitize_tts_text(text)
137
  out_path = f"output_{user_id}.mp3"
138
  if st.session_state["last_tts_text"] != sanitized or not os.path.exists(out_path):
139
+ try:
140
+ with st.spinner(f"Generating voice ({FIXED_VOICE_NAME})..."):
141
+ asyncio.run(edge_tts_synthesize(sanitized, voice, user_id))
142
+ st.session_state["last_tts_text"] = sanitized
143
+ st.session_state["last_audio_path"] = out_path
144
+ except Exception as e:
145
+ st.warning(f"TTS Error: {e}")
146
+ return None
147
  return out_path
148
 
149
  # --- CHAT: display history ---
150
  display_chat_history()
151
 
152
+ # --- LORAIN is thinking indicator ---
153
+ if st.session_state.get("is_thinking", False):
154
+ st.markdown("""
155
+ <div style="
156
+ text-align:center; color:#ddd; font-size: 14px;
157
+ margin-top: -1em; margin-bottom: 0.5em;">
158
+ 🤖 <em>LORAIN is thinking...</em>
159
+ </div>
160
+ """, unsafe_allow_html=True)
161
+
162
  # --- INPUT BAR (floating at bottom) ---
163
  st.markdown('<div class="chat-input-bar">', unsafe_allow_html=True)
164
  col1, col2 = st.columns([10, 1])
 
169
 
170
  # --- PROCESS USER INPUT ---
171
  if user_input:
172
+ st.session_state["is_thinking"] = True # start thinking
173
+
174
  thread_id = get_or_create_thread_id()
175
  client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
176
  save_message("user", user_input)
 
189
  save_message("assistant", assistant_message)
190
 
191
  audio_path = synthesize_voice(assistant_message, user_id)
192
+ if audio_path and os.path.exists(audio_path):
193
  st.audio(audio_path, format="audio/mp3", autoplay=True)
194
 
195
+ st.session_state["is_thinking"] = False # stop thinking
196
+
197
  time.sleep(0.2)
198
  st.rerun()
199
+
200
+ # --- Auto-scroll JS ---
201
+ st.markdown("""
202
+ <script>
203
+ window.onload = function() {
204
+ var chatWrapper = document.querySelector('.chat-history-wrapper');
205
+ if(chatWrapper){ chatWrapper.scrollTop = chatWrapper.scrollHeight; }
206
+ };
207
+ setTimeout(function(){
208
+ var chatWrapper = document.querySelector('.chat-history-wrapper');
209
+ if(chatWrapper){ chatWrapper.scrollTop = chatWrapper.scrollHeight; }
210
+ }, 300);
211
+ </script>
212
+ """, unsafe_allow_html=True)