IAMTFRMZA commited on
Commit
1acdb78
·
verified ·
1 Parent(s): a4eaf13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -5
app.py CHANGED
@@ -113,9 +113,44 @@ if user_input:
113
  messages_response = client.beta.threads.messages.list(thread_id=thread_id)
114
  latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
115
  assistant_message = latest_response.content[0].text.value
116
-
117
- save_message("assistant", assistant_message)
118
- time.sleep(0.5)
119
- st.rerun()
120
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
 
113
  messages_response = client.beta.threads.messages.list(thread_id=thread_id)
114
  latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
115
  assistant_message = latest_response.content[0].text.value
116
+ save_message("assistant", assistant_message)
117
+
118
+ # 👇 Voice controls (after displaying the message)
119
+ st.components.v1.html(f"""
120
+ <div id="voice-controls" style="margin: 10px 0;">
121
+ <button id="speak-btn" style="margin-right:10px;">🔊 Speak</button>
122
+ <button id="mute-btn">🔇 Mute</button>
123
+ </div>
124
+ <script>
125
+ var utterance;
126
+ var isSpeaking = false;
127
+ var synth = window.speechSynthesis;
128
+ var text = `{assistant_message.replace("`", "\\`")}`;
129
+ var voices = [];
130
+ function getFemaleVoice() {{
131
+ voices = synth.getVoices();
132
+ let female = voices.find(v => v.name.includes("Female") || (v.name.includes("en") && v.gender !== "male"));
133
+ return female || voices.find(v => v.lang.startsWith('en') && v.name.toLowerCase().includes('female')) || voices[0];
134
+ }}
135
+
136
+ document.getElementById('speak-btn').onclick = function() {{
137
+ if (isSpeaking) return;
138
+ utterance = new SpeechSynthesisUtterance(text);
139
+ utterance.voice = getFemaleVoice();
140
+ utterance.rate = 1;
141
+ utterance.pitch = 1.1;
142
+ synth.speak(utterance);
143
+ isSpeaking = true;
144
+ utterance.onend = function() {{ isSpeaking = false; }};
145
+ }};
146
+
147
+ document.getElementById('mute-btn').onclick = function() {{
148
+ synth.cancel();
149
+ isSpeaking = false;
150
+ }};
151
+ </script>
152
+ """, height=60)
153
+
154
+ time.sleep(0.5)
155
+ st.rerun()
156