IAMTFRMZA commited on
Commit
860955f
·
verified ·
1 Parent(s): dacdb50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -82,8 +82,13 @@ def send_audio(chunk, cid):
82
  if not cid or cid not in connections:
83
  return "Connecting..."
84
  sr, arr = chunk
 
 
 
 
 
85
  connections[cid].enqueue_audio_chunk(sr, arr)
86
- return connections[cid].transcript
87
 
88
  def clear_transcript(cid):
89
  if cid in connections:
@@ -126,9 +131,12 @@ def handle_chat(user_input, history, thread_id, image_url):
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):
@@ -142,17 +150,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
142
 
143
  gr.HTML("""
144
  <style>
145
- #ask-btn, #clear-chat-btn {
146
- font-size: 16px !important;
147
- padding: 10px 24px !important;
148
- margin-top: 6px;
149
- }
150
- #record-audio button {
151
  font-size: 16px !important;
152
- padding: 12px 24px !important;
153
- background-color: #f2f2f2 !important;
154
  border-radius: 6px;
155
- margin-top: 6px;
 
 
 
 
 
 
 
156
  }
157
  </style>
158
  """)
@@ -185,13 +194,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
185
  inputs=[user_prompt, chat_state, thread_state, image_state],
186
  outputs=[user_prompt, chat, thread_state, image_state])
187
  image_state.change(fn=lambda x: x, inputs=image_state, outputs=image_display)
 
188
  voice_input.stream(fn=send_audio, inputs=[voice_input, client_id], outputs=voice_transcript, stream_every=0.5)
 
189
  ask_btn.click(fn=send_transcript_to_assistant,
190
- inputs=[voice_transcript, chat_state, thread_state, image_state],
191
  outputs=[user_prompt, chat, thread_state, image_state])
 
192
  clear_chat_btn.click(fn=clear_chat_and_transcript,
193
  inputs=[client_id],
194
  outputs=[chat, voice_transcript, thread_state, image_state])
 
195
  app.load(fn=create_ws, outputs=[client_id])
196
 
197
  app.launch()
 
82
  if not cid or cid not in connections:
83
  return "Connecting..."
84
  sr, arr = chunk
85
+
86
+ # Reset transcript if it's been running long or restarted
87
+ if len(connections[cid].transcript) > 1000:
88
+ connections[cid].transcript = ""
89
+
90
  connections[cid].enqueue_audio_chunk(sr, arr)
91
+ return connections[cid].transcript.strip()
92
 
93
  def clear_transcript(cid):
94
  if cid in connections:
 
131
  except Exception as e:
132
  return f"❌ {e}", history, thread_id, image_url
133
 
134
+ def send_transcript_to_assistant(transcript, history, thread_id, image_url, cid):
135
  if not transcript.strip():
136
  return gr.update(), history, thread_id, image_url
137
+ # Clear transcript after sending
138
+ if cid in connections:
139
+ connections[cid].transcript = ""
140
  return handle_chat(transcript, history, thread_id, image_url)
141
 
142
  def clear_chat_and_transcript(client_id):
 
150
 
151
  gr.HTML("""
152
  <style>
153
+ #ask-btn, #clear-chat-btn, #record-audio button {
 
 
 
 
 
154
  font-size: 16px !important;
155
+ padding: 12px 28px !important;
 
156
  border-radius: 6px;
157
+ margin-top: 10px;
158
+ background-color: #f2f2f2 !important;
159
+ }
160
+ button {
161
+ margin-right: 8px;
162
+ }
163
+ #record-audio button svg {
164
+ display: none !important;
165
  }
166
  </style>
167
  """)
 
194
  inputs=[user_prompt, chat_state, thread_state, image_state],
195
  outputs=[user_prompt, chat, thread_state, image_state])
196
  image_state.change(fn=lambda x: x, inputs=image_state, outputs=image_display)
197
+
198
  voice_input.stream(fn=send_audio, inputs=[voice_input, client_id], outputs=voice_transcript, stream_every=0.5)
199
+
200
  ask_btn.click(fn=send_transcript_to_assistant,
201
+ inputs=[voice_transcript, chat_state, thread_state, image_state, client_id],
202
  outputs=[user_prompt, chat, thread_state, image_state])
203
+
204
  clear_chat_btn.click(fn=clear_chat_and_transcript,
205
  inputs=[client_id],
206
  outputs=[chat, voice_transcript, thread_state, image_state])
207
+
208
  app.load(fn=create_ws, outputs=[client_id])
209
 
210
  app.launch()