Spestly commited on
Commit
0fa7b48
·
verified ·
1 Parent(s): 6971847

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -15
app.py CHANGED
@@ -120,15 +120,14 @@ def format_response_with_thinking(response):
120
 
121
  def chat_submit(message, history, conversation_state, model_name, max_length, temperature):
122
  """Process a new message and update the chat history"""
123
- if not message.strip():
124
  return "", history, conversation_state
125
 
 
 
 
126
  model_id = MODELS.get(model_name, MODELS["Athena-R3X 4B"])
127
  try:
128
- # Print debug info to help diagnose issues
129
- print(f"Processing message: {message}")
130
- print(f"Selected model: {model_name} ({model_id})")
131
-
132
  response, load_time, generation_time = generate_response(
133
  model_id, conversation_state, message, max_length, temperature
134
  )
@@ -225,8 +224,9 @@ css = """
225
  }
226
  """
227
 
228
- # Add JavaScript to make the thinking buttons work
229
  js = """
 
230
  function setupThinkingToggle() {
231
  document.querySelectorAll('.thinking-toggle').forEach(button => {
232
  if (!button.hasEventListener) {
@@ -252,9 +252,45 @@ const observer = new MutationObserver(function(mutations) {
252
  setupThinkingToggle();
253
  });
254
 
255
- // Start observing the document body for changes
256
- document.addEventListener('DOMContentLoaded', () => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  setupThinkingToggle();
 
 
 
 
 
 
 
 
 
258
  setTimeout(() => {
259
  const chatbot = document.querySelector('.chatbot');
260
  if (chatbot) {
@@ -270,6 +306,9 @@ document.addEventListener('DOMContentLoaded', () => {
270
  subtree: true
271
  });
272
  }
 
 
 
273
  }, 1000);
274
  });
275
  """
@@ -286,8 +325,22 @@ with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as d
286
  chatbot = gr.Chatbot(height=500, label="Athena", render_markdown=True, elem_classes=["chatbot"])
287
 
288
  with gr.Row():
289
- user_input = gr.Textbox(label="Your message", scale=8, autofocus=True, placeholder="Type your message here...")
290
- send_btn = gr.Button(value="Send", scale=1, variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
  # Clear button for resetting the conversation
293
  clear_btn = gr.Button("Clear Conversation")
@@ -318,20 +371,26 @@ with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as d
318
 
319
  # Connect the interface components - note the specific ordering
320
  user_input.submit(
321
- chat_submit,
322
  inputs=[user_input, chatbot, conversation_state, model_choice, max_length, temperature],
323
- outputs=[user_input, chatbot, conversation_state]
 
324
  )
325
 
326
  # Make sure send button uses the exact same function with the same parameter ordering
327
  send_btn.click(
328
- chat_submit,
329
  inputs=[user_input, chatbot, conversation_state, model_choice, max_length, temperature],
330
- outputs=[user_input, chatbot, conversation_state]
 
331
  )
332
 
333
  # Connect clear button
334
- clear_btn.click(clear_conversation, outputs=[chatbot, conversation_state])
 
 
 
 
335
 
336
  # Add examples if desired
337
  gr.Examples(
@@ -351,4 +410,5 @@ with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as d
351
  """)
352
 
353
  if __name__ == "__main__":
 
354
  demo.launch(debug=True) # Enable debug mode for better error reporting
 
120
 
121
  def chat_submit(message, history, conversation_state, model_name, max_length, temperature):
122
  """Process a new message and update the chat history"""
123
+ if not message or not message.strip():
124
  return "", history, conversation_state
125
 
126
+ # Debug print to check function execution
127
+ print(f"Processing message in chat_submit: {message}")
128
+
129
  model_id = MODELS.get(model_name, MODELS["Athena-R3X 4B"])
130
  try:
 
 
 
 
131
  response, load_time, generation_time = generate_response(
132
  model_id, conversation_state, message, max_length, temperature
133
  )
 
224
  }
225
  """
226
 
227
+ # Add JavaScript to make the thinking buttons work and fix enter key issues
228
  js = """
229
+ // Function to handle thinking toggle buttons
230
  function setupThinkingToggle() {
231
  document.querySelectorAll('.thinking-toggle').forEach(button => {
232
  if (!button.hasEventListener) {
 
252
  setupThinkingToggle();
253
  });
254
 
255
+ // Function to ensure the textbox and submit button work correctly
256
+ function fixChatInputs() {
257
+ const textbox = document.querySelector('textarea[data-testid="textbox"]');
258
+ const submitBtn = document.querySelector('button[data-testid="send-btn"]');
259
+
260
+ if (textbox && !textbox.hasEnterListener) {
261
+ console.log("Setting up enter key handler");
262
+ textbox.addEventListener('keydown', function(e) {
263
+ if (e.key === 'Enter' && !e.shiftKey) {
264
+ e.preventDefault();
265
+ if (textbox.value.trim() !== '') {
266
+ submitBtn.click();
267
+ }
268
+ }
269
+ });
270
+ textbox.hasEnterListener = true;
271
+ }
272
+
273
+ if (submitBtn && !submitBtn.hasClickFix) {
274
+ console.log("Enhancing submit button");
275
+ submitBtn.addEventListener('click', function() {
276
+ console.log("Submit button clicked");
277
+ });
278
+ submitBtn.hasClickFix = true;
279
+ }
280
+ }
281
+
282
+ // Function to run all UI fixes
283
+ function setupUI() {
284
  setupThinkingToggle();
285
+ fixChatInputs();
286
+ }
287
+
288
+ // Initial setup
289
+ document.addEventListener('DOMContentLoaded', () => {
290
+ console.log("DOM loaded, setting up UI");
291
+ setTimeout(setupUI, 1000);
292
+
293
+ // Set up observer after a delay
294
  setTimeout(() => {
295
  const chatbot = document.querySelector('.chatbot');
296
  if (chatbot) {
 
306
  subtree: true
307
  });
308
  }
309
+
310
+ // Run UI fixes periodically
311
+ setInterval(setupUI, 2000);
312
  }, 1000);
313
  });
314
  """
 
325
  chatbot = gr.Chatbot(height=500, label="Athena", render_markdown=True, elem_classes=["chatbot"])
326
 
327
  with gr.Row():
328
+ user_input = gr.Textbox(
329
+ label="Your message",
330
+ scale=8,
331
+ autofocus=True,
332
+ placeholder="Type your message here...",
333
+ elem_id="chat-input",
334
+ lines=2,
335
+ max_lines=10,
336
+ )
337
+ send_btn = gr.Button(
338
+ value="Send",
339
+ scale=1,
340
+ variant="primary",
341
+ elem_id="send-btn",
342
+ min_width=100
343
+ )
344
 
345
  # Clear button for resetting the conversation
346
  clear_btn = gr.Button("Clear Conversation")
 
371
 
372
  # Connect the interface components - note the specific ordering
373
  user_input.submit(
374
+ fn=chat_submit,
375
  inputs=[user_input, chatbot, conversation_state, model_choice, max_length, temperature],
376
+ outputs=[user_input, chatbot, conversation_state],
377
+ api_name="submit_message"
378
  )
379
 
380
  # Make sure send button uses the exact same function with the same parameter ordering
381
  send_btn.click(
382
+ fn=chat_submit,
383
  inputs=[user_input, chatbot, conversation_state, model_choice, max_length, temperature],
384
+ outputs=[user_input, chatbot, conversation_state],
385
+ api_name="send_message"
386
  )
387
 
388
  # Connect clear button
389
+ clear_btn.click(
390
+ fn=clear_conversation,
391
+ outputs=[chatbot, conversation_state],
392
+ api_name="clear_chat"
393
+ )
394
 
395
  # Add examples if desired
396
  gr.Examples(
 
410
  """)
411
 
412
  if __name__ == "__main__":
413
+ demo.queue() # Enable queuing for smoother experience
414
  demo.launch(debug=True) # Enable debug mode for better error reporting