IAMTFRMZA commited on
Commit
5338b7d
·
verified ·
1 Parent(s): bafde5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -36,7 +36,7 @@ class WebSocketClient:
36
  await self.websocket.send(f.read())
37
  await asyncio.gather(self.receive_messages(), self.send_audio_chunks())
38
  except Exception as e:
39
- print(f"🔴 WebSocket Connection Failed: {e}")
40
 
41
  def run(self):
42
  asyncio.set_event_loop(self.loop)
@@ -115,7 +115,7 @@ def handle_chat(user_input, history, thread_id, image_url):
115
  content = msg.content[0].text.value
116
  history.append((user_input, content))
117
  match = re.search(
118
- r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
119
  content
120
  )
121
  if match: image_url = match.group(0)
@@ -150,21 +150,48 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
150
 
151
  with gr.Accordion("🎤 Voice Transcription", open=False) as voice_section:
152
  with gr.Row():
153
- voice_input = gr.Audio(label="Mic", streaming=True)
154
  voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
155
- clear_btn = gr.Button("🧹 Clear Transcript")
 
 
156
 
157
  # Functional bindings
158
  def toggle_voice(curr):
159
  return not curr, gr.update(visible=not curr)
160
 
 
 
 
 
 
 
 
 
 
 
161
  mic_toggle_btn.click(fn=toggle_voice, inputs=voice_enabled, outputs=[voice_enabled, voice_section])
162
  send_btn.click(fn=handle_chat,
163
  inputs=[user_prompt, chat_state, thread_state, image_state],
164
  outputs=[user_prompt, chat, thread_state, image_state])
165
  image_state.change(fn=lambda x: x, inputs=image_state, outputs=image_display)
166
  voice_input.stream(fn=send_audio, inputs=[voice_input, client_id], outputs=voice_transcript, stream_every=0.5)
167
- clear_btn.click(fn=clear_transcript, inputs=[client_id], outputs=voice_transcript)
 
 
 
 
 
168
  app.load(fn=create_ws, outputs=[client_id])
169
 
170
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
36
  await self.websocket.send(f.read())
37
  await asyncio.gather(self.receive_messages(), self.send_audio_chunks())
38
  except Exception as e:
39
+ print(f"\U0001F534 WebSocket Connection Failed: {e}")
40
 
41
  def run(self):
42
  asyncio.set_event_loop(self.loop)
 
115
  content = msg.content[0].text.value
116
  history.append((user_input, content))
117
  match = re.search(
118
+ r'https://raw\\.githubusercontent\\.com/AndrewLORTech/surgical-pathology-manual/main/[\\w\\-/]*\\.png',
119
  content
120
  )
121
  if match: image_url = match.group(0)
 
150
 
151
  with gr.Accordion("🎤 Voice Transcription", open=False) as voice_section:
152
  with gr.Row():
153
+ voice_input = gr.Audio(label="🎙️ Record", streaming=True, elem_classes="record-button")
154
  voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
155
+ with gr.Row():
156
+ ask_btn = gr.Button("🟢 Ask", elem_id="ask-btn")
157
+ clear_chat_btn = gr.Button("🧹 Clear Chat", elem_id="clear-chat-btn")
158
 
159
  # Functional bindings
160
  def toggle_voice(curr):
161
  return not curr, gr.update(visible=not curr)
162
 
163
+ def send_transcript_to_assistant(transcript, history, thread_id, image_url):
164
+ if not transcript.strip():
165
+ return gr.update(), history, thread_id, image_url
166
+ return handle_chat(transcript, history, thread_id, image_url)
167
+
168
+ def clear_chat_and_transcript(client_id):
169
+ if client_id in connections:
170
+ connections[client_id].transcript = ""
171
+ return [], "", None, None
172
+
173
  mic_toggle_btn.click(fn=toggle_voice, inputs=voice_enabled, outputs=[voice_enabled, voice_section])
174
  send_btn.click(fn=handle_chat,
175
  inputs=[user_prompt, chat_state, thread_state, image_state],
176
  outputs=[user_prompt, chat, thread_state, image_state])
177
  image_state.change(fn=lambda x: x, inputs=image_state, outputs=image_display)
178
  voice_input.stream(fn=send_audio, inputs=[voice_input, client_id], outputs=voice_transcript, stream_every=0.5)
179
+ ask_btn.click(fn=send_transcript_to_assistant,
180
+ inputs=[voice_transcript, chat_state, thread_state, image_state],
181
+ outputs=[user_prompt, chat, thread_state, image_state])
182
+ clear_chat_btn.click(fn=clear_chat_and_transcript,
183
+ inputs=[client_id],
184
+ outputs=[chat, voice_transcript, thread_state, image_state])
185
  app.load(fn=create_ws, outputs=[client_id])
186
 
187
+ app.launch(css="""
188
+ #ask-btn, #clear-chat-btn {
189
+ font-size: 16px !important;
190
+ padding: 10px 20px !important;
191
+ }
192
+ .record-button button {
193
+ font-size: 16px !important;
194
+ padding: 12px 24px !important;
195
+ background-color: #f2f2f2;
196
+ }
197
+ """)