IAMTFRMZA commited on
Commit
b3b692e
Β·
verified Β·
1 Parent(s): a63ec01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -69,7 +69,7 @@ def save_message(role, content):
69
  # πŸ’¬ Display chat history
70
  def display_chat_history():
71
  messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream()
72
- assistant_icon_html = "<img src='https://huggingface.co/spaces/IAMTFRMZA/lortechassistant/blob/main/lorain.jpg' width='20' style='vertical-align:middle;'/>"
73
  for msg in list(messages)[::-1]:
74
  data = msg.to_dict()
75
  if data["role"] == "user":
@@ -77,7 +77,7 @@ def display_chat_history():
77
  else:
78
  st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LORAIN:</strong> {data['content']}</div>", unsafe_allow_html=True)
79
 
80
- # πŸš€ Main Chat UI
81
  input_col, clear_col = st.columns([9, 1])
82
  with input_col:
83
  user_input = st.chat_input("Type your message here...")
@@ -100,22 +100,24 @@ display_chat_history()
100
  if "mute_voice" not in st.session_state:
101
  st.session_state["mute_voice"] = False
102
 
103
- def synthesize_and_play(text, mute):
104
- if not mute:
105
- # Only call OpenAI TTS if unmuted
 
 
 
 
106
  with st.spinner("Synthesizing voice with GPT-4o..."):
107
  speech_response = client.audio.speech.create(
108
- model="tts-1", # 'tts-1' or 'tts-1-hd'
109
- voice="nova", # or "alloy", "echo", "fable", etc.
110
  input=text,
111
  response_format="mp3"
112
  )
113
- audio_path = f"output_{user_id}.mp3"
114
  with open(audio_path, "wb") as f:
115
  f.write(speech_response.content)
116
- st.audio(audio_path, format="audio/mp3", autoplay=True)
117
- # Optionally remove the file after play to save disk
118
- # os.remove(audio_path)
119
 
120
  if user_input:
121
  # Send user message to OpenAI thread
@@ -135,20 +137,24 @@ if user_input:
135
  assistant_message = latest_response.content[0].text.value
136
  save_message("assistant", assistant_message)
137
 
138
- # πŸ‘‡ Voice controls (auto-speak enabled, can mute)
139
- col1, col2 = st.columns([1, 1])
140
- with col1:
141
- if st.button("πŸ”Š Play Voice", key="unmute"):
142
- st.session_state["mute_voice"] = False
143
- synthesize_and_play(assistant_message, False)
144
- with col2:
145
- if st.button("πŸ”‡ Mute Voice", key="mute"):
146
- st.session_state["mute_voice"] = True
147
- st.info("Voice output muted for this and future messages.")
148
 
149
- # Play voice unless muted
150
- synthesize_and_play(assistant_message, st.session_state["mute_voice"])
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
- # Force Streamlit to rerun so chat refreshes and you get a new prompt
153
  time.sleep(0.5)
154
  st.rerun()
 
69
  # πŸ’¬ Display chat history
70
  def display_chat_history():
71
  messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream()
72
+ assistant_icon_html = "<img src='https://lortechnologies.com/wp-content/uploads/2023/03/LOR-Online-Logo.svg' width='20' style='vertical-align:middle;'/>"
73
  for msg in list(messages)[::-1]:
74
  data = msg.to_dict()
75
  if data["role"] == "user":
 
77
  else:
78
  st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LORAIN:</strong> {data['content']}</div>", unsafe_allow_html=True)
79
 
80
+ # --- Main Chat UI ---
81
  input_col, clear_col = st.columns([9, 1])
82
  with input_col:
83
  user_input = st.chat_input("Type your message here...")
 
100
  if "mute_voice" not in st.session_state:
101
  st.session_state["mute_voice"] = False
102
 
103
+ if "last_tts_text" not in st.session_state:
104
+ st.session_state["last_tts_text"] = ""
105
+
106
+ def synthesize_voice(text, user_id):
107
+ audio_path = f"output_{user_id}.mp3"
108
+ # Only synthesize if text changed or file doesn't exist
109
+ if st.session_state["last_tts_text"] != text or not os.path.exists(audio_path):
110
  with st.spinner("Synthesizing voice with GPT-4o..."):
111
  speech_response = client.audio.speech.create(
112
+ model="tts-1",
113
+ voice="nova",
114
  input=text,
115
  response_format="mp3"
116
  )
 
117
  with open(audio_path, "wb") as f:
118
  f.write(speech_response.content)
119
+ st.session_state["last_tts_text"] = text
120
+ return audio_path
 
121
 
122
  if user_input:
123
  # Send user message to OpenAI thread
 
137
  assistant_message = latest_response.content[0].text.value
138
  save_message("assistant", assistant_message)
139
 
140
+ # Voice autoplay unless muted
141
+ mute_voice = st.session_state.get("mute_voice", False)
142
+ audio_path = synthesize_voice(assistant_message, user_id) if not mute_voice else None
 
 
 
 
 
 
 
143
 
144
+ if not mute_voice and audio_path:
145
+ st.audio(audio_path, format="audio/mp3", autoplay=True)
146
+ elif mute_voice:
147
+ st.info("πŸ”‡ Voice is muted. To enable assistant speech, click 'Unmute Voice' below and ask another question.")
148
+
149
+ # Single mute/unmute button
150
+ if not mute_voice:
151
+ if st.button("πŸ”‡ Mute Voice"):
152
+ st.session_state["mute_voice"] = True
153
+ st.rerun()
154
+ else:
155
+ if st.button("πŸ”Š Unmute Voice"):
156
+ st.session_state["mute_voice"] = False
157
+ st.rerun()
158
 
 
159
  time.sleep(0.5)
160
  st.rerun()