KingNish commited on
Commit
a6837f3
·
1 Parent(s): 0f2dc9a

little fix

Browse files
Files changed (2) hide show
  1. app.css +4 -0
  2. app.py +4 -30
app.css CHANGED
@@ -29,4 +29,8 @@
29
  flex: 1;
30
  min-height: 100px;
31
  border: 0;
 
 
 
 
32
  }
 
29
  flex: 1;
30
  min-height: 100px;
31
  border: 0;
32
+ }
33
+
34
+ .icon-button-wrapper {
35
+ display: hidden;
36
  }
app.py CHANGED
@@ -26,7 +26,6 @@ def _conv_choices(state_value):
26
  class GradioEvents:
27
  """Event handlers for the chatbot UI."""
28
 
29
- # ------------------------------------------------------------------ stream
30
  @staticmethod
31
  def stream_response(message, state_value):
32
  """Stream a chat completion into the active conversation."""
@@ -34,7 +33,6 @@ class GradioEvents:
34
  yield gr.skip()
35
  return
36
 
37
- # Create new conversation if there isn't an active one
38
  if not state_value.get("conversation_id"):
39
  conv_id = str(uuid.uuid4())
40
  state_value["conversation_id"] = conv_id
@@ -50,16 +48,13 @@ class GradioEvents:
50
 
51
  ctx = state_value["conversation_contexts"][conv_id]
52
 
53
- # Update conversation label if blank
54
  for c in state_value["conversations"]:
55
  if c["key"] == conv_id and not c.get("label"):
56
  c["label"] = message[:30]
57
  break
58
 
59
- # Append user message
60
  ctx["history"].append({"role": "user", "content": message})
61
 
62
- # Initial yield: clear input, show user message
63
  yield { msg: gr.update(value=""), chatbot: gr.update(value=ctx["history"]), state: gr.update(value=state_value), conv_choice: _conv_choices(state_value), send_btn: gr.update(visible=False), stop_btn: gr.update(visible=True)}
64
 
65
  reasoning_content = ""
@@ -122,11 +117,9 @@ class GradioEvents:
122
  state: gr.update(value=state_value),
123
  }
124
 
125
- # Persist assistant messages into history
126
  ctx["history"].extend(partial)
127
  if temp:
128
  ctx["history"].extend(temp)
129
- # Mark last assistant message as final
130
  if ctx["history"] and ctx["history"][-1].get("role") == "assistant":
131
  ctx["history"][-1]["is_final"] = True
132
 
@@ -137,7 +130,7 @@ class GradioEvents:
137
  stop_btn: gr.update(visible=False),
138
  }
139
 
140
- except Exception as exc: # noqa: BLE001
141
  print("model:", model, "-", "Error:", exc)
142
  ctx["history"].append({
143
  "role": "assistant",
@@ -151,7 +144,6 @@ class GradioEvents:
151
  }
152
  raise
153
 
154
- # ------------------------------------------------------------ conversations
155
  @staticmethod
156
  def new_chat(state_value):
157
  state_value["conversation_id"] = ""
@@ -213,8 +205,6 @@ class GradioEvents:
213
  gr.update(value=state_value),
214
  )
215
 
216
- # ---------------------------------------------------------------- settings
217
-
218
  @staticmethod
219
  def clear_history(state_value):
220
  if not state_value.get("conversation_id"):
@@ -238,7 +228,6 @@ class GradioEvents:
238
  gr.update(value=state_value), gr.update(visible=True),
239
  gr.update(visible=False))
240
 
241
- # ----------------------------------------------------------------- state IO
242
  @staticmethod
243
  def save_browser_state(state_value):
244
  return gr.update(value=dict(
@@ -263,7 +252,6 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
263
  })
264
 
265
  with gr.Row(elem_id="main-row"):
266
- # ============== Sidebar ============================================
267
  with gr.Column(scale=0, min_width=260, elem_id="sidebar"):
268
  new_chat_btn = gr.Button(
269
  value="New Conversation",
@@ -280,9 +268,7 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
280
  variant="stop",
281
  )
282
 
283
- # ============== Main chat column ==================================
284
  with gr.Column(scale=1, elem_id="chat-column"):
285
- # Action bar
286
  with gr.Row():
287
  clear_btn = gr.Button(
288
  value="Clear History",
@@ -291,15 +277,12 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
291
  visible=False,
292
  )
293
 
294
- # Chatbot
295
  chatbot = gr.Chatbot(
296
  elem_id="chatbot",
297
  show_label=False,
298
- buttons=None,
299
- layout="panel"
300
  )
301
- # Input area (msg must be defined here so the input lives
302
- # at the bottom of the column)
303
  with gr.Row():
304
  msg = gr.Textbox(
305
  placeholder="Type a message and press Enter...",
@@ -321,37 +304,30 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
321
  visible=False,
322
  )
323
 
324
- # ============== Event wiring ===========================================
325
-
326
- # New chat
327
  new_chat_btn.click(
328
  fn=GradioEvents.new_chat,
329
  inputs=[state],
330
  outputs=[conv_choice, chatbot, state],
331
  )
332
 
333
- # Select conversation
334
  conv_choice.change(
335
  fn=GradioEvents.select_conversation,
336
  inputs=[conv_choice, state],
337
  outputs=[chatbot, state],
338
  )
339
 
340
- # Delete conversation
341
  delete_btn.click(
342
  fn=GradioEvents.delete_selected_conversation,
343
  inputs=[conv_choice, state],
344
  outputs=[conv_choice, chatbot, state],
345
  )
346
 
347
- # Clear history of current conversation
348
  clear_btn.click(
349
  fn=GradioEvents.clear_history,
350
  inputs=[state],
351
  outputs=[chatbot, state],
352
  )
353
 
354
- # Send message
355
  submit_event = send_btn.click(
356
  fn=GradioEvents.stream_response,
357
  inputs=[msg, state],
@@ -363,7 +339,6 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
363
  outputs=[msg, chatbot, state, conv_choice, send_btn, stop_btn],
364
  )
365
 
366
- # Stop button cancels the streaming
367
  stop_btn.click(
368
  fn=GradioEvents.cancel_stream,
369
  inputs=[state],
@@ -376,8 +351,7 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
376
  "conversation_contexts": {},
377
  "conversations": [],
378
  },
379
- storage_key="chat_app_state",
380
- secret="devmode"
381
  )
382
  state.change(
383
  fn=GradioEvents.save_browser_state,
 
26
  class GradioEvents:
27
  """Event handlers for the chatbot UI."""
28
 
 
29
  @staticmethod
30
  def stream_response(message, state_value):
31
  """Stream a chat completion into the active conversation."""
 
33
  yield gr.skip()
34
  return
35
 
 
36
  if not state_value.get("conversation_id"):
37
  conv_id = str(uuid.uuid4())
38
  state_value["conversation_id"] = conv_id
 
48
 
49
  ctx = state_value["conversation_contexts"][conv_id]
50
 
 
51
  for c in state_value["conversations"]:
52
  if c["key"] == conv_id and not c.get("label"):
53
  c["label"] = message[:30]
54
  break
55
 
 
56
  ctx["history"].append({"role": "user", "content": message})
57
 
 
58
  yield { msg: gr.update(value=""), chatbot: gr.update(value=ctx["history"]), state: gr.update(value=state_value), conv_choice: _conv_choices(state_value), send_btn: gr.update(visible=False), stop_btn: gr.update(visible=True)}
59
 
60
  reasoning_content = ""
 
117
  state: gr.update(value=state_value),
118
  }
119
 
 
120
  ctx["history"].extend(partial)
121
  if temp:
122
  ctx["history"].extend(temp)
 
123
  if ctx["history"] and ctx["history"][-1].get("role") == "assistant":
124
  ctx["history"][-1]["is_final"] = True
125
 
 
130
  stop_btn: gr.update(visible=False),
131
  }
132
 
133
+ except Exception as exc:
134
  print("model:", model, "-", "Error:", exc)
135
  ctx["history"].append({
136
  "role": "assistant",
 
144
  }
145
  raise
146
 
 
147
  @staticmethod
148
  def new_chat(state_value):
149
  state_value["conversation_id"] = ""
 
205
  gr.update(value=state_value),
206
  )
207
 
 
 
208
  @staticmethod
209
  def clear_history(state_value):
210
  if not state_value.get("conversation_id"):
 
228
  gr.update(value=state_value), gr.update(visible=True),
229
  gr.update(visible=False))
230
 
 
231
  @staticmethod
232
  def save_browser_state(state_value):
233
  return gr.update(value=dict(
 
252
  })
253
 
254
  with gr.Row(elem_id="main-row"):
 
255
  with gr.Column(scale=0, min_width=260, elem_id="sidebar"):
256
  new_chat_btn = gr.Button(
257
  value="New Conversation",
 
268
  variant="stop",
269
  )
270
 
 
271
  with gr.Column(scale=1, elem_id="chat-column"):
 
272
  with gr.Row():
273
  clear_btn = gr.Button(
274
  value="Clear History",
 
277
  visible=False,
278
  )
279
 
 
280
  chatbot = gr.Chatbot(
281
  elem_id="chatbot",
282
  show_label=False,
283
+ buttons=[],
284
+ layout="bubble"
285
  )
 
 
286
  with gr.Row():
287
  msg = gr.Textbox(
288
  placeholder="Type a message and press Enter...",
 
304
  visible=False,
305
  )
306
 
 
 
 
307
  new_chat_btn.click(
308
  fn=GradioEvents.new_chat,
309
  inputs=[state],
310
  outputs=[conv_choice, chatbot, state],
311
  )
312
 
 
313
  conv_choice.change(
314
  fn=GradioEvents.select_conversation,
315
  inputs=[conv_choice, state],
316
  outputs=[chatbot, state],
317
  )
318
 
 
319
  delete_btn.click(
320
  fn=GradioEvents.delete_selected_conversation,
321
  inputs=[conv_choice, state],
322
  outputs=[conv_choice, chatbot, state],
323
  )
324
 
 
325
  clear_btn.click(
326
  fn=GradioEvents.clear_history,
327
  inputs=[state],
328
  outputs=[chatbot, state],
329
  )
330
 
 
331
  submit_event = send_btn.click(
332
  fn=GradioEvents.stream_response,
333
  inputs=[msg, state],
 
339
  outputs=[msg, chatbot, state, conv_choice, send_btn, stop_btn],
340
  )
341
 
 
342
  stop_btn.click(
343
  fn=GradioEvents.cancel_stream,
344
  inputs=[state],
 
351
  "conversation_contexts": {},
352
  "conversations": [],
353
  },
354
+ storage_key="chat_app_state"
 
355
  )
356
  state.change(
357
  fn=GradioEvents.save_browser_state,