bradnow commited on
Commit
ddbdc31
·
1 Parent(s): 3f99cb6

Restructure to allow clearing of the chat when switching models

Browse files
Files changed (1) hide show
  1. app.py +59 -14
app.py CHANGED
@@ -8,7 +8,7 @@ from utils import COMMUNITY_POSTFIX_URL, get_model_config, log_message, check_fo
8
  print(f"Gradio version: {gr.__version__}")
9
 
10
  DEFAULT_MODEL_NAME = "Apriel-Nemotron-15b-Thinker"
11
-
12
 
13
  chat_start_count = 0
14
  model_config = None
@@ -41,6 +41,11 @@ def chat_fn(message, history):
41
  log_message(f"chat_fn() --> Message: {message}")
42
  log_message(f"chat_fn() --> History: {history}")
43
 
 
 
 
 
 
44
  global chat_start_count
45
  chat_start_count = chat_start_count + 1
46
  print(
@@ -146,6 +151,13 @@ description = None
146
  with gr.Blocks(theme=gr.themes.Default(primary_hue="green")) as demo:
147
  gr.HTML("""
148
  <style>
 
 
 
 
 
 
 
149
  .model-message {
150
  text-align: end;
151
  }
@@ -194,7 +206,14 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="green")) as demo:
194
  max-height: 400px;
195
  }
196
  }
197
- """)
 
 
 
 
 
 
 
198
 
199
  with gr.Row(variant="panel", elem_classes="responsive-row"):
200
  with gr.Column(scale=1, min_width=400, elem_classes="model-dropdown-container"):
@@ -216,30 +235,56 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="green")) as demo:
216
  elem_classes="chatbot",
217
  )
218
 
219
- chat_interface = gr.ChatInterface(
220
- chat_fn,
221
- description="",
222
- type="messages",
223
- chatbot=chatbot,
224
- fill_height=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  )
 
226
 
227
- # Add this line to ensure the model is reset to default on page reload
 
 
 
 
 
 
 
228
  demo.load(lambda: setup_model(DEFAULT_MODEL_NAME, intial=False), [], [description_html])
229
 
230
 
231
  def update_model_and_clear(model_name):
232
- # Remove the "Model: " prefix to get the actual model name
233
  actual_model_name = model_name.replace("Model: ", "")
234
  desc = setup_model(actual_model_name)
235
- chatbot.clear() # Critical line
236
- return desc
237
-
238
 
239
  model_dropdown.change(
240
  fn=update_model_and_clear,
241
  inputs=[model_dropdown],
242
- outputs=[description_html]
243
  )
244
 
245
  demo.launch(ssr_mode=False)
 
8
  print(f"Gradio version: {gr.__version__}")
9
 
10
  DEFAULT_MODEL_NAME = "Apriel-Nemotron-15b-Thinker"
11
+ BUTTON_WIDTH = 160
12
 
13
  chat_start_count = 0
14
  model_config = None
 
41
  log_message(f"chat_fn() --> Message: {message}")
42
  log_message(f"chat_fn() --> History: {history}")
43
 
44
+ # Check if the message is empty
45
+ if not message.strip():
46
+ gr.Warning("Please enter a message before sending.")
47
+ return history
48
+
49
  global chat_start_count
50
  chat_start_count = chat_start_count + 1
51
  print(
 
151
  with gr.Blocks(theme=gr.themes.Default(primary_hue="green")) as demo:
152
  gr.HTML("""
153
  <style>
154
+ .html-container:has(.css-styles) {
155
+ padding: 0;
156
+ margin: 0;
157
+ }
158
+
159
+ .css-styles { height: 0; }
160
+
161
  .model-message {
162
  text-align: end;
163
  }
 
206
  max-height: 400px;
207
  }
208
  }
209
+ """ + f"""
210
+ @media (min-width: 1024px) {{
211
+ .send-button-container, .clear-button-container {{
212
+ max-width: {BUTTON_WIDTH}px;
213
+ }}
214
+ }}
215
+ </style>
216
+ """, elem_classes="css-styles")
217
 
218
  with gr.Row(variant="panel", elem_classes="responsive-row"):
219
  with gr.Column(scale=1, min_width=400, elem_classes="model-dropdown-container"):
 
235
  elem_classes="chatbot",
236
  )
237
 
238
+ # chat_interface = gr.ChatInterface(
239
+ # chat_fn,
240
+ # description="",
241
+ # type="messages",
242
+ # chatbot=chatbot,
243
+ # fill_height=True,
244
+ # )
245
+
246
+ with gr.Row():
247
+ with gr.Column(scale=10, min_width=400, elem_classes="user-input-container"):
248
+ user_input = gr.Textbox(
249
+ show_label=False,
250
+ placeholder="Type your message here and press Enter",
251
+ container=False,
252
+ )
253
+ with gr.Column(scale=1, min_width=BUTTON_WIDTH * 2 + 20):
254
+ with gr.Row():
255
+ with gr.Column(scale=1, min_width=BUTTON_WIDTH, elem_classes="send-button-container"):
256
+ send_btn = gr.Button("Send", variant="primary")
257
+ with gr.Column(scale=1, min_width=BUTTON_WIDTH, elem_classes="clear-button-container"):
258
+ clear_btn = gr.ClearButton(chatbot, value="New Chat", variant="secondary")
259
+
260
+ # on Enter: stream into the chatbot, then clear the textbox
261
+ user_input.submit(
262
+ fn=chat_fn,
263
+ inputs=[user_input, chatbot],
264
+ outputs=[chatbot]
265
  )
266
+ user_input.submit(lambda: "", None, user_input, queue=False)
267
 
268
+ send_btn.click(
269
+ fn=chat_fn,
270
+ inputs=[user_input, chatbot],
271
+ outputs=[chatbot]
272
+ )
273
+ send_btn.click(lambda: "", None, user_input, queue=False)
274
+
275
+ # Ensure the model is reset to default on page reload
276
  demo.load(lambda: setup_model(DEFAULT_MODEL_NAME, intial=False), [], [description_html])
277
 
278
 
279
  def update_model_and_clear(model_name):
 
280
  actual_model_name = model_name.replace("Model: ", "")
281
  desc = setup_model(actual_model_name)
282
+ return desc, []
 
 
283
 
284
  model_dropdown.change(
285
  fn=update_model_and_clear,
286
  inputs=[model_dropdown],
287
+ outputs=[description_html, chatbot]
288
  )
289
 
290
  demo.launch(ssr_mode=False)