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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -73
app.py CHANGED
@@ -120,12 +120,13 @@ 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 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(
@@ -224,9 +225,8 @@ css = """
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,45 +252,9 @@ const observer = new MutationObserver(function(mutations) {
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) {
@@ -300,49 +264,47 @@ document.addEventListener('DOMContentLoaded', () => {
300
  characterData: true
301
  });
302
  } else {
303
- // If chatbot container not found, observe the body
304
  observer.observe(document.body, {
305
  childList: true,
306
  subtree: true
307
  });
308
  }
309
-
310
- // Run UI fixes periodically
311
- setInterval(setupUI, 2000);
312
  }, 1000);
313
  });
314
  """
315
 
316
- theme = gr.themes.Soft()
317
-
318
- with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as demo:
319
  gr.Markdown("# 🚀 Athena Playground Chat")
320
  gr.Markdown("*Powered by HuggingFace ZeroGPU*")
321
 
322
  # State to keep track of the conversation for the model
323
  conversation_state = gr.State([])
324
 
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")
347
 
348
  # Configuration controls
@@ -369,30 +331,27 @@ with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as d
369
  def clear_conversation():
370
  return [], []
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(
397
  examples=[
398
  "What is artificial intelligence?",
@@ -400,7 +359,7 @@ with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as d
400
  "Write a short poem about technology",
401
  "What are some ethical concerns about AI?"
402
  ],
403
- inputs=[user_input]
404
  )
405
 
406
  gr.Markdown("""
@@ -410,5 +369,6 @@ with gr.Blocks(title="Athena Playground Chat", css=css, theme=theme, js=js) as d
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
 
 
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
+ # For debugging - print when the function is called
124
+ print(f"chat_submit function called with message: '{message}'")
125
+
126
  if not message or not message.strip():
127
+ print("Empty message, returning without processing")
128
  return "", history, conversation_state
129
 
 
 
 
130
  model_id = MODELS.get(model_name, MODELS["Athena-R3X 4B"])
131
  try:
132
  response, load_time, generation_time = generate_response(
 
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
  setupThinkingToggle();
253
  });
254
 
255
+ // Start observing after DOM is loaded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  document.addEventListener('DOMContentLoaded', () => {
257
+ setupThinkingToggle();
 
 
 
258
  setTimeout(() => {
259
  const chatbot = document.querySelector('.chatbot');
260
  if (chatbot) {
 
264
  characterData: true
265
  });
266
  } else {
 
267
  observer.observe(document.body, {
268
  childList: true,
269
  subtree: true
270
  });
271
  }
 
 
 
272
  }, 1000);
273
  });
274
  """
275
 
276
+ # Create Gradio interface
277
+ with gr.Blocks(title="Athena Playground Chat", css=css, js=js) as demo:
 
278
  gr.Markdown("# 🚀 Athena Playground Chat")
279
  gr.Markdown("*Powered by HuggingFace ZeroGPU*")
280
 
281
  # State to keep track of the conversation for the model
282
  conversation_state = gr.State([])
283
 
284
+ # Chatbot component
285
+ chatbot = gr.Chatbot(
286
+ height=500,
287
+ label="Athena",
288
+ render_markdown=True,
289
+ elem_classes=["chatbot"]
290
+ )
291
 
292
+ # Input and send button row
293
  with gr.Row():
294
  user_input = gr.Textbox(
295
  label="Your message",
296
  scale=8,
297
  autofocus=True,
298
  placeholder="Type your message here...",
299
+ lines=2
 
 
300
  )
301
  send_btn = gr.Button(
302
  value="Send",
303
  scale=1,
304
+ variant="primary"
 
 
305
  )
306
 
307
+ # Clear button
308
  clear_btn = gr.Button("Clear Conversation")
309
 
310
  # Configuration controls
 
331
  def clear_conversation():
332
  return [], []
333
 
334
+ # Connect the interface components with explicit handlers
335
+ submit_click = user_input.submit(
336
  fn=chat_submit,
337
  inputs=[user_input, chatbot, conversation_state, model_choice, max_length, temperature],
338
+ outputs=[user_input, chatbot, conversation_state]
 
339
  )
340
 
341
+ # Connect send button explicitly
342
+ send_click = send_btn.click(
343
  fn=chat_submit,
344
  inputs=[user_input, chatbot, conversation_state, model_choice, max_length, temperature],
345
+ outputs=[user_input, chatbot, conversation_state]
 
346
  )
347
 
348
+ # Clear conversation
349
  clear_btn.click(
350
  fn=clear_conversation,
351
+ outputs=[chatbot, conversation_state]
 
352
  )
353
 
354
+ # Examples
355
  gr.Examples(
356
  examples=[
357
  "What is artificial intelligence?",
 
359
  "Write a short poem about technology",
360
  "What are some ethical concerns about AI?"
361
  ],
362
+ inputs=user_input
363
  )
364
 
365
  gr.Markdown("""
 
369
  """)
370
 
371
  if __name__ == "__main__":
372
+ # Enable queue and debugging
373
+ demo.queue()
374
+ demo.launch(debug=True)