Daemontatox commited on
Commit
f339f73
·
verified ·
1 Parent(s): b728ffa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -109
app.py CHANGED
@@ -134,7 +134,6 @@ def convert_history_to_cohere_format(history: List[List[str]]) -> List[dict]:
134
  def chat_response(
135
  message: str,
136
  history: List[List[str]],
137
- chat_display: str,
138
  system_prompt: str,
139
  temperature: float = 0.3,
140
  max_new_tokens: int = 8192,
@@ -170,19 +169,21 @@ def chat_response(
170
  buffer += event.text
171
  formatted_buffer = format_text(buffer)
172
  history[-1][1] = formatted_buffer
173
- chat_display = format_chat_history(history)
174
- yield history, chat_display
175
 
176
  except Exception as e:
177
  error_message = f"Error: {str(e)}"
178
  history[-1][1] = error_message
179
- chat_display = format_chat_history(history)
180
- yield history, chat_display
181
 
182
  def process_example(example: str) -> tuple:
183
  """Process example query and return empty history and updated display"""
184
  return [], f"User: {example}\n\n"
185
 
 
 
 
 
186
  def main():
187
  """Main function to set up and launch the Gradio interface"""
188
  with gr.Blocks(css=CSS, theme="soft") as demo:
@@ -192,121 +193,89 @@ def main():
192
  elem_classes="duplicate-button"
193
  )
194
 
 
 
 
 
 
 
 
195
  with gr.Row():
196
- with gr.Column():
197
- chat_history = gr.State([])
198
- chat_display = gr.TextArea(
199
- value="",
200
- label="Chat History",
201
- interactive=False,
202
- elem_classes=["chat-area"],
203
- )
204
-
205
- message = gr.TextArea(
206
  placeholder=PLACEHOLDER,
207
  label="Your message",
208
- lines=3
 
209
  )
210
-
211
  with gr.Row():
212
- submit = gr.Button("Send")
213
- clear = gr.Button("Clear")
214
-
215
- with gr.Accordion("⚙️ Advanced Settings", open=False):
216
- system_prompt = gr.TextArea(
217
- value=DEFAULT_SYSTEM_PROMPT,
218
- label="System Prompt",
219
- lines=5,
220
- )
221
- temperature = gr.Slider(
222
- minimum=0,
223
- maximum=1,
224
- step=0.1,
225
- value=0.3,
226
- label="Temperature",
227
- )
228
- max_tokens = gr.Slider(
229
- minimum=128,
230
- maximum=32000,
231
- step=128,
232
- value=8192,
233
- label="Max Tokens",
234
- )
235
- top_p = gr.Slider(
236
- minimum=0.1,
237
- maximum=1.0,
238
- step=0.1,
239
- value=0.8,
240
- label="Top-p",
241
- )
242
- top_k = gr.Slider(
243
- minimum=1,
244
- maximum=100,
245
- step=1,
246
- value=40,
247
- label="Top-k",
248
- )
249
- penalty = gr.Slider(
250
- minimum=1.0,
251
- maximum=2.0,
252
- step=0.1,
253
- value=1.2,
254
- label="Repetition Penalty",
255
- )
256
-
257
- examples = gr.Examples(
258
- examples=create_examples(),
259
- inputs=[message],
260
- outputs=[chat_history, chat_display],
261
- fn=process_example,
262
- cache_examples=False,
263
- )
264
 
265
- # Set up event handlers
266
- submit_click = submit.click(
267
- chat_response,
268
- inputs=[
269
- message,
270
- chat_history,
271
- chat_display,
272
- system_prompt,
273
- temperature,
274
- max_tokens,
275
- top_p,
276
- top_k,
277
- penalty,
278
- ],
279
- outputs=[chat_history, chat_display],
280
- show_progress=True,
281
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
 
283
- # Bind Enter key to submit
284
- message.submit(
285
- chat_response,
286
- inputs=[
287
- message,
288
- chat_history,
289
- chat_display,
290
- system_prompt,
291
- temperature,
292
- max_tokens,
293
- top_p,
294
- top_k,
295
- penalty,
296
- ],
297
- outputs=[chat_history, chat_display],
298
- show_progress=True,
299
  )
300
 
 
 
 
 
 
 
 
 
 
301
  clear.click(
302
- lambda: ([], ""),
303
- outputs=[chat_history, chat_display],
304
- show_progress=True,
305
  )
306
-
307
- # Clear input after submission
308
- submit_click.then(lambda: "", outputs=message)
309
- message.submit(lambda: "", outputs=message)
310
 
311
  return demo
312
 
 
134
  def chat_response(
135
  message: str,
136
  history: List[List[str]],
 
137
  system_prompt: str,
138
  temperature: float = 0.3,
139
  max_new_tokens: int = 8192,
 
169
  buffer += event.text
170
  formatted_buffer = format_text(buffer)
171
  history[-1][1] = formatted_buffer
172
+ yield history, history[-1][1]
 
173
 
174
  except Exception as e:
175
  error_message = f"Error: {str(e)}"
176
  history[-1][1] = error_message
177
+ yield history, history[-1][1]
 
178
 
179
  def process_example(example: str) -> tuple:
180
  """Process example query and return empty history and updated display"""
181
  return [], f"User: {example}\n\n"
182
 
183
+ def clear_chat() -> tuple:
184
+ """Clear chat history and input"""
185
+ return [], ""
186
+
187
  def main():
188
  """Main function to set up and launch the Gradio interface"""
189
  with gr.Blocks(css=CSS, theme="soft") as demo:
 
193
  elem_classes="duplicate-button"
194
  )
195
 
196
+ chatbot = gr.Chatbot(
197
+ height=750,
198
+ show_label=False,
199
+ bubble_full_width=False,
200
+ elem_classes=["chat-area"],
201
+ )
202
+
203
  with gr.Row():
204
+ with gr.Column(scale=20):
205
+ message = gr.Textbox(
 
 
 
 
 
 
 
 
206
  placeholder=PLACEHOLDER,
207
  label="Your message",
208
+ lines=3,
209
+ show_label=False
210
  )
211
+ with gr.Column(scale=4):
212
  with gr.Row():
213
+ submit = gr.Button("Send", variant="primary", size="sm")
214
+ clear = gr.Button("Clear Chat", size="sm")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
217
+ system_prompt = gr.TextArea(
218
+ value=DEFAULT_SYSTEM_PROMPT,
219
+ label="System Prompt",
220
+ lines=5,
221
+ )
222
+ temperature = gr.Slider(
223
+ minimum=0,
224
+ maximum=1,
225
+ step=0.1,
226
+ value=0.3,
227
+ label="Temperature",
228
+ )
229
+ max_tokens = gr.Slider(
230
+ minimum=128,
231
+ maximum=32000,
232
+ step=128,
233
+ value=8192,
234
+ label="Max Tokens",
235
+ )
236
+ top_p = gr.Slider(
237
+ minimum=0.1,
238
+ maximum=1.0,
239
+ step=0.1,
240
+ value=0.8,
241
+ label="Top-p",
242
+ )
243
+ top_k = gr.Slider(
244
+ minimum=1,
245
+ maximum=100,
246
+ step=1,
247
+ value=40,
248
+ label="Top-k",
249
+ )
250
+ penalty = gr.Slider(
251
+ minimum=1.0,
252
+ maximum=2.0,
253
+ step=0.1,
254
+ value=1.2,
255
+ label="Repetition Penalty",
256
+ )
257
 
258
+ examples = gr.Examples(
259
+ examples=create_examples(),
260
+ inputs=[message],
261
+ outputs=[chatbot],
262
+ fn=process_example,
263
+ cache_examples=False,
 
 
 
 
 
 
 
 
 
 
264
  )
265
 
266
+ # Handle both submit events (Enter key and Send button)
267
+ submit_events = [message.submit, submit.click]
268
+ for submit_event in submit_events:
269
+ submit_event(
270
+ chat_response,
271
+ inputs=[message, chatbot, system_prompt, temperature, max_tokens, top_p, top_k, penalty],
272
+ outputs=[chatbot, chatbot],
273
+ )
274
+
275
  clear.click(
276
+ clear_chat,
277
+ outputs=[chatbot, message]
 
278
  )
 
 
 
 
279
 
280
  return demo
281