IAMTFRMZA commited on
Commit
65aa40f
Β·
verified Β·
1 Parent(s): 7d28c06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # top of the file
2
  import gradio as gr
3
  import os, time, re, json, base64, asyncio, threading, uuid, io
4
  import numpy as np
@@ -36,7 +35,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"\U0001F534 WebSocket Connection Failed: {e}")
40
 
41
  def run(self):
42
  asyncio.set_event_loop(self.loop)
@@ -91,7 +90,7 @@ def clear_transcript(cid):
91
  connections[cid].transcript = ""
92
  return ""
93
 
94
- # ============ Chat Assistant ============
95
  def handle_chat(user_input, history, thread_id, image_url):
96
  if not OPENAI_API_KEY or not ASSISTANT_ID:
97
  return "❌ Missing secrets!", history, thread_id, image_url
@@ -116,7 +115,7 @@ def handle_chat(user_input, history, thread_id, image_url):
116
  history.append({"role": "user", "content": user_input})
117
  history.append({"role": "assistant", "content": content})
118
  match = re.search(
119
- r'https://raw\\.githubusercontent\\.com/AndrewLORTech/surgical-pathology-manual/main/[\\w\\-/]*\\.png',
120
  content
121
  )
122
  if match: image_url = match.group(0)
@@ -127,7 +126,17 @@ def handle_chat(user_input, history, thread_id, image_url):
127
  except Exception as e:
128
  return f"❌ {e}", history, thread_id, image_url
129
 
130
- # ============ Gradio UI ============
 
 
 
 
 
 
 
 
 
 
131
  with gr.Blocks(theme=gr.themes.Soft()) as app:
132
  gr.Markdown("# πŸ“„ Document AI Assistant")
133
 
@@ -135,10 +144,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
135
  <style>
136
  #ask-btn, #clear-chat-btn, #record-btn {
137
  font-size: 16px !important;
138
- padding: 10px 20px !important;
 
139
  }
140
- #voice-transcript-box textarea {
141
- width: 100% !important;
142
  }
143
  </style>
144
  """)
@@ -155,31 +165,20 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
155
 
156
  with gr.Column(scale=2):
157
  chat = gr.Chatbot(label="πŸ’¬ Chat", height=460, type="messages")
158
-
159
  with gr.Row():
160
  user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False, scale=6)
161
  send_btn = gr.Button("Send", variant="primary", scale=2)
162
 
163
- with gr.Accordion("🎀 Voice Transcription", open=True) as voice_section:
164
- with gr.Row():
165
- voice_input = gr.Audio(label="", streaming=True, elem_id="record-btn")
166
- voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False, elem_id="voice-transcript-box")
167
  with gr.Row():
168
- record_label = gr.Label("Record", visible=False)
169
  ask_btn = gr.Button("🟒 Ask", elem_id="ask-btn")
170
  clear_chat_btn = gr.Button("🧹 Clear Chat", elem_id="clear-chat-btn")
 
171
 
172
  # Functional bindings
173
- def send_transcript_to_assistant(transcript, history, thread_id, image_url):
174
- if not transcript.strip():
175
- return gr.update(), history, thread_id, image_url
176
- return handle_chat(transcript, history, thread_id, image_url)
177
-
178
- def clear_chat_and_transcript(client_id):
179
- if client_id in connections:
180
- connections[client_id].transcript = ""
181
- return [], "", None, None
182
-
183
  send_btn.click(fn=handle_chat,
184
  inputs=[user_prompt, chat_state, thread_state, image_state],
185
  outputs=[user_prompt, chat, thread_state, image_state])
@@ -193,4 +192,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
193
  outputs=[chat, voice_transcript, thread_state, image_state])
194
  app.load(fn=create_ws, outputs=[client_id])
195
 
196
- app.launch()
 
 
1
  import gradio as gr
2
  import os, time, re, json, base64, asyncio, threading, uuid, io
3
  import numpy as np
 
35
  await self.websocket.send(f.read())
36
  await asyncio.gather(self.receive_messages(), self.send_audio_chunks())
37
  except Exception as e:
38
+ print(f"πŸ”΄ WebSocket Connection Failed: {e}")
39
 
40
  def run(self):
41
  asyncio.set_event_loop(self.loop)
 
90
  connections[cid].transcript = ""
91
  return ""
92
 
93
+ # Chat assistant logic
94
  def handle_chat(user_input, history, thread_id, image_url):
95
  if not OPENAI_API_KEY or not ASSISTANT_ID:
96
  return "❌ Missing secrets!", history, thread_id, image_url
 
115
  history.append({"role": "user", "content": user_input})
116
  history.append({"role": "assistant", "content": 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)
 
126
  except Exception as e:
127
  return f"❌ {e}", history, thread_id, image_url
128
 
129
+ def send_transcript_to_assistant(transcript, history, thread_id, image_url):
130
+ if not transcript.strip():
131
+ return gr.update(), history, thread_id, image_url
132
+ return handle_chat(transcript, history, thread_id, image_url)
133
+
134
+ def clear_chat_and_transcript(client_id):
135
+ if client_id in connections:
136
+ connections[client_id].transcript = ""
137
+ return [], "", None, None
138
+
139
+ # UI
140
  with gr.Blocks(theme=gr.themes.Soft()) as app:
141
  gr.Markdown("# πŸ“„ Document AI Assistant")
142
 
 
144
  <style>
145
  #ask-btn, #clear-chat-btn, #record-btn {
146
  font-size: 16px !important;
147
+ padding: 10px 24px !important;
148
+ margin-top: 6px;
149
  }
150
+ .record-button button {
151
+ display: none !important;
152
  }
153
  </style>
154
  """)
 
165
 
166
  with gr.Column(scale=2):
167
  chat = gr.Chatbot(label="πŸ’¬ Chat", height=460, type="messages")
 
168
  with gr.Row():
169
  user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False, scale=6)
170
  send_btn = gr.Button("Send", variant="primary", scale=2)
171
 
172
+ with gr.Accordion("🎀 Voice Transcription", open=False) as voice_section:
173
+ voice_input = gr.Audio(label="πŸŽ™οΈ Record", streaming=True, elem_classes="record-button")
174
+ voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
175
+
176
  with gr.Row():
 
177
  ask_btn = gr.Button("🟒 Ask", elem_id="ask-btn")
178
  clear_chat_btn = gr.Button("🧹 Clear Chat", elem_id="clear-chat-btn")
179
+ record_btn = gr.Button("πŸŽ™οΈ Record", elem_id="record-btn")
180
 
181
  # Functional bindings
 
 
 
 
 
 
 
 
 
 
182
  send_btn.click(fn=handle_chat,
183
  inputs=[user_prompt, chat_state, thread_state, image_state],
184
  outputs=[user_prompt, chat, thread_state, image_state])
 
192
  outputs=[chat, voice_transcript, thread_state, image_state])
193
  app.load(fn=create_ws, outputs=[client_id])
194
 
195
+ app.launch()