IAMTFRMZA commited on
Commit
ec26bc2
·
verified ·
1 Parent(s): f4c7018

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -17,7 +17,6 @@ HEADERS = {"Authorization": f"Bearer {OPENAI_API_KEY}", "OpenAI-Beta": "realtime
17
  WS_URI = "wss://api.openai.com/v1/realtime?intent=transcription"
18
  connections = {}
19
 
20
- # WebSocket Client
21
  class WebSocketClient:
22
  def __init__(self, uri, headers, client_id):
23
  self.uri = uri
@@ -70,7 +69,6 @@ class WebSocketClient:
70
  if data["type"] == "conversation.item.input_audio_transcription.delta":
71
  self.transcript += data["delta"]
72
 
73
- # WebSocket Connection Manager
74
  def create_ws():
75
  cid = str(uuid.uuid4())
76
  client = WebSocketClient(WS_URI, HEADERS, cid)
@@ -93,7 +91,6 @@ def clear_transcript_only(cid):
93
  def clear_chat_only():
94
  return [], None, None
95
 
96
- # Assistant chat handler
97
  def handle_chat(user_input, history, thread_id, image_url):
98
  if not OPENAI_API_KEY or not ASSISTANT_ID:
99
  return "❌ Missing secrets!", history, thread_id, image_url
@@ -116,6 +113,9 @@ def handle_chat(user_input, history, thread_id, image_url):
116
  for msg in reversed(msgs.data):
117
  if msg.role == "assistant":
118
  content = msg.content[0].text.value
 
 
 
119
  history.append((user_input, content))
120
  match = re.search(
121
  r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
@@ -130,7 +130,6 @@ def handle_chat(user_input, history, thread_id, image_url):
130
  except Exception as e:
131
  return f"❌ {e}", history, thread_id, image_url
132
 
133
- # Feed transcript as assistant input
134
  def feed_transcript(transcript, history, thread_id, image_url, cid):
135
  if not transcript.strip():
136
  return gr.update(), history, thread_id, image_url
@@ -138,7 +137,6 @@ def feed_transcript(transcript, history, thread_id, image_url, cid):
138
  connections[cid].transcript = ""
139
  return handle_chat(transcript, history, thread_id, image_url)
140
 
141
- # Fallback for image display
142
  def update_image_display(image_url):
143
  if image_url and isinstance(image_url, str) and image_url.startswith("http"):
144
  return image_url
@@ -157,6 +155,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
157
  border-radius: 8px !important;
158
  width: 100% !important;
159
  margin-top: 10px;
 
160
  }
161
  .voice-area {
162
  padding-top: 12px;
@@ -172,20 +171,27 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
172
  client_id = gr.State()
173
 
174
  with gr.Row(equal_height=True):
175
- with gr.Column(scale=0.8): # thinner image column
176
- image_display = gr.Image(label="🖼️ Document", type="filepath", show_download_button=False)
 
 
 
 
 
177
 
178
  with gr.Column(elem_classes="voice-area"):
179
  gr.Markdown("### 🎙️ Voice Input")
180
  voice_input = gr.Audio(label="Tap to Record", streaming=True, type="numpy", show_label=True)
181
  voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
182
 
183
- with gr.Row():
184
- voice_send_btn = gr.Button("🟢 Send Voice to Assistant", elem_classes="big-btn")
185
- clear_transcript_btn = gr.Button("🧹 Clear Transcript", elem_classes="big-btn")
 
 
186
 
187
- with gr.Column(scale=2): # wider chat column
188
- chat = gr.Chatbot(label="💬 Chat", height=460)
189
 
190
  with gr.Row():
191
  user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False, scale=8)
 
17
  WS_URI = "wss://api.openai.com/v1/realtime?intent=transcription"
18
  connections = {}
19
 
 
20
  class WebSocketClient:
21
  def __init__(self, uri, headers, client_id):
22
  self.uri = uri
 
69
  if data["type"] == "conversation.item.input_audio_transcription.delta":
70
  self.transcript += data["delta"]
71
 
 
72
  def create_ws():
73
  cid = str(uuid.uuid4())
74
  client = WebSocketClient(WS_URI, HEADERS, cid)
 
91
  def clear_chat_only():
92
  return [], None, None
93
 
 
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
 
113
  for msg in reversed(msgs.data):
114
  if msg.role == "assistant":
115
  content = msg.content[0].text.value
116
+ # Optional: prevent repeating fallback messages
117
+ # if history and content == history[-1][1]:
118
+ # content += "\n🔁 Try asking a different type of question!"
119
  history.append((user_input, content))
120
  match = re.search(
121
  r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
 
130
  except Exception as e:
131
  return f"❌ {e}", history, thread_id, image_url
132
 
 
133
  def feed_transcript(transcript, history, thread_id, image_url, cid):
134
  if not transcript.strip():
135
  return gr.update(), history, thread_id, image_url
 
137
  connections[cid].transcript = ""
138
  return handle_chat(transcript, history, thread_id, image_url)
139
 
 
140
  def update_image_display(image_url):
141
  if image_url and isinstance(image_url, str) and image_url.startswith("http"):
142
  return image_url
 
155
  border-radius: 8px !important;
156
  width: 100% !important;
157
  margin-top: 10px;
158
+ white-space: nowrap;
159
  }
160
  .voice-area {
161
  padding-top: 12px;
 
171
  client_id = gr.State()
172
 
173
  with gr.Row(equal_height=True):
174
+ with gr.Column(scale=0.8):
175
+ image_display = gr.Image(
176
+ label="🖼️ Document",
177
+ type="filepath",
178
+ show_download_button=False,
179
+ height=480 # taller preview
180
+ )
181
 
182
  with gr.Column(elem_classes="voice-area"):
183
  gr.Markdown("### 🎙️ Voice Input")
184
  voice_input = gr.Audio(label="Tap to Record", streaming=True, type="numpy", show_label=True)
185
  voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
186
 
187
+ with gr.Row(equal_height=True):
188
+ with gr.Column(scale=1):
189
+ voice_send_btn = gr.Button("🟢 Send Voice to Assistant", elem_classes="big-btn")
190
+ with gr.Column(scale=1):
191
+ clear_transcript_btn = gr.Button("🧹 Clear Transcript", elem_classes="big-btn")
192
 
193
+ with gr.Column(scale=2):
194
+ chat = gr.Chatbot(label="💬 Chat", height=480)
195
 
196
  with gr.Row():
197
  user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False, scale=8)