IAMTFRMZA commited on
Commit
f736f6b
·
verified ·
1 Parent(s): 8971a48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -39
app.py CHANGED
@@ -90,15 +90,18 @@ def clear_transcript(cid):
90
  return ""
91
 
92
  def format_response(content, prompt):
93
- header = f"<div class='response-card'><h3>❓ {prompt}</h3><p><b>🧠 In summary:</b></p><p>{content}</p>"
94
- image_urls = re.findall(r'https://raw\.githubusercontent\.com/[^\s)]+\.png', content)
95
- if image_urls:
96
- header += "<div class='source-grid'><h4>📎 Sources:</h4>"
97
- for url in image_urls:
98
- header += f"<img src='{url}' class='thumbnail' />"
99
- header += "</div>"
100
- header += "</div>"
101
- return header
 
 
 
102
 
103
  def handle_chat(prompt, thread_id):
104
  if not OPENAI_API_KEY or not ASSISTANT_ID:
@@ -129,86 +132,96 @@ def feed_transcript(transcript, thread_id, cid):
129
  connections[cid].transcript = ""
130
  return handle_chat(transcript, thread_id)
131
 
132
- # === Gradio UI ===
133
  with gr.Blocks(css="""
134
  body {
135
  background-color: #0f0f0f;
136
- color: #f1f1f1;
137
  font-family: 'Inter', sans-serif;
138
  }
139
- .response-card {
140
  background: #1a1a1a;
141
  padding: 20px;
 
142
  border-radius: 14px;
143
- margin-top: 16px;
144
- box-shadow: 0 2px 6px #000;
145
  }
146
- .source-grid {
147
  display: flex;
148
- flex-wrap: wrap;
149
  gap: 10px;
150
- margin-top: 10px;
 
151
  }
152
- .thumbnail {
153
  width: 120px;
154
  border-radius: 8px;
155
  border: 1px solid #333;
156
  }
157
- .input-wrap {
158
  position: fixed;
159
- bottom: 20px;
160
  left: 0;
161
  right: 0;
162
  max-width: 700px;
163
  margin: auto;
164
  display: flex;
165
- gap: 10px;
166
- padding: 12px;
167
- background: #1a1a1a;
168
  border-radius: 16px;
 
169
  }
170
- #chat-input {
171
  flex-grow: 1;
172
- padding: 14px;
173
- border-radius: 12px;
174
  background: #2a2a2a;
175
  border: none;
 
176
  color: white;
177
  font-size: 16px;
 
178
  }
179
- #send-btn {
180
- font-size: 18px;
181
  background: #3f3fff;
182
  color: white;
183
  border: none;
184
  padding: 12px 16px;
185
- border-radius: 10px;
 
186
  }
187
  """) as app:
188
 
189
  thread_state = gr.State()
190
  client_id = gr.State()
 
191
 
192
- with gr.Column():
193
- gr.HTML("<h1 style='text-align:center; margin-top:40px;'>How can I help you today?</h1>")
194
- output_md = gr.HTML()
195
 
196
- with gr.Row(elem_id="chat-row", elem_classes="input-wrap"):
197
- user_input = gr.Textbox(elem_id="chat-input", show_label=False, placeholder="Ask something...")
198
  send_btn = gr.Button("➤", elem_id="send-btn")
 
199
 
200
- with gr.Column():
201
- gr.Markdown("🎙️ Real-time Voice Input")
202
- mic_audio = gr.Audio(label="Tap to Speak", streaming=True, type="numpy")
203
  mic_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
204
- mic_send = gr.Button("Send Voice", elem_classes="big-btn")
205
- mic_clear = gr.Button("Clear Transcript", elem_classes="big-btn")
206
 
207
  # Bindings
208
  send_btn.click(fn=handle_chat,
209
  inputs=[user_input, thread_state],
210
  outputs=[output_md, thread_state])
211
 
 
 
 
 
 
 
 
 
 
212
  mic_audio.stream(fn=send_audio,
213
  inputs=[mic_audio, client_id],
214
  outputs=mic_transcript,
 
90
  return ""
91
 
92
  def format_response(content, prompt):
93
+ summary = f"""<div class="card">
94
+ <h3>❓ {prompt}</h3>
95
+ <p><b>🧠 In summary:</b></p>
96
+ <p>{content}</p>"""
97
+ thumbnails = re.findall(r'https://raw\.githubusercontent\.com/[^\s)]+\.png', content)
98
+ if thumbnails:
99
+ summary += "<h4>📎 Sources:</h4><div class='thumb-grid'>"
100
+ for url in thumbnails:
101
+ summary += f"<img src='{url}' class='thumb' />"
102
+ summary += "</div>"
103
+ summary += "</div>"
104
+ return summary
105
 
106
  def handle_chat(prompt, thread_id):
107
  if not OPENAI_API_KEY or not ASSISTANT_ID:
 
132
  connections[cid].transcript = ""
133
  return handle_chat(transcript, thread_id)
134
 
135
+ # Gradio App
136
  with gr.Blocks(css="""
137
  body {
138
  background-color: #0f0f0f;
139
+ color: white;
140
  font-family: 'Inter', sans-serif;
141
  }
142
+ .card {
143
  background: #1a1a1a;
144
  padding: 20px;
145
+ margin-top: 24px;
146
  border-radius: 14px;
147
+ box-shadow: 0 2px 8px #000;
 
148
  }
149
+ .thumb-grid {
150
  display: flex;
 
151
  gap: 10px;
152
+ flex-wrap: wrap;
153
+ margin-top: 12px;
154
  }
155
+ .thumb {
156
  width: 120px;
157
  border-radius: 8px;
158
  border: 1px solid #333;
159
  }
160
+ .input-box {
161
  position: fixed;
162
+ bottom: 16px;
163
  left: 0;
164
  right: 0;
165
  max-width: 700px;
166
  margin: auto;
167
  display: flex;
168
+ gap: 8px;
169
+ background: #1f1f1f;
170
+ padding: 14px;
171
  border-radius: 16px;
172
+ justify-content: space-between;
173
  }
174
+ #main-input {
175
  flex-grow: 1;
 
 
176
  background: #2a2a2a;
177
  border: none;
178
+ padding: 12px;
179
  color: white;
180
  font-size: 16px;
181
+ border-radius: 12px;
182
  }
183
+ #send-btn, #mic-btn {
 
184
  background: #3f3fff;
185
  color: white;
186
  border: none;
187
  padding: 12px 16px;
188
+ border-radius: 12px;
189
+ font-size: 16px;
190
  }
191
  """) as app:
192
 
193
  thread_state = gr.State()
194
  client_id = gr.State()
195
+ voice_visible = gr.State(False)
196
 
197
+ gr.HTML("<h1 style='text-align:center; margin-top:40px;'>How can I help you today?</h1>")
198
+ output_md = gr.HTML()
 
199
 
200
+ with gr.Row(elem_classes="input-box"):
201
+ user_input = gr.Textbox(elem_id="main-input", show_label=False, placeholder="Ask a question...")
202
  send_btn = gr.Button("➤", elem_id="send-btn")
203
+ mic_toggle = gr.Button("🎙", elem_id="mic-btn")
204
 
205
+ with gr.Column(visible=False) as voice_area:
206
+ mic_audio = gr.Audio(label="Record", streaming=True, type="numpy")
 
207
  mic_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
208
+ mic_send = gr.Button("Send Voice")
209
+ mic_clear = gr.Button("Clear Transcript")
210
 
211
  # Bindings
212
  send_btn.click(fn=handle_chat,
213
  inputs=[user_input, thread_state],
214
  outputs=[output_md, thread_state])
215
 
216
+ mic_toggle.click(fn=lambda v: not v,
217
+ inputs=voice_visible,
218
+ outputs=voice_visible)
219
+
220
+ voice_visible.change(fn=None,
221
+ inputs=voice_visible,
222
+ outputs=voice_area,
223
+ show_progress=False)
224
+
225
  mic_audio.stream(fn=send_audio,
226
  inputs=[mic_audio, client_id],
227
  outputs=mic_transcript,