Dmang69 commited on
Commit
7829d42
·
verified ·
1 Parent(s): 024be8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -56,16 +56,37 @@ def chat_handler(message, history):
56
 
57
  # UI wiring for the Chat tab
58
  with gr.Tab("Chat"):
59
- chatbot = gr.Chatbot(label="Programming Assistant")
60
- chat_input = gr.Textbox(label="Ask anything about programming...", placeholder="e.g. 'generate python: http scraper'")
 
61
  send_btn = gr.Button("Send")
62
  clear_btn = gr.Button("Clear")
63
 
64
- # Note: the callback must return the updated chat history (single output mapped to chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  send_btn.click(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot)
66
- # Allow pressing Enter in textbox to send
67
  chat_input.submit(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot)
68
- # Clear chat: set chatbot to empty list
69
  clear_btn.click(lambda: [], None, chatbot, queue=False)
70
 
71
 
 
56
 
57
  # UI wiring for the Chat tab
58
  with gr.Tab("Chat"):
59
+ # Chatbot using new 'messages' format
60
+ chatbot = gr.Chatbot(label="Programming Assistant", type="messages")
61
+ chat_input = gr.Textbox(label="Ask anything about programming...", placeholder="e.g. 'generate python: scraper'")
62
  send_btn = gr.Button("Send")
63
  clear_btn = gr.Button("Clear")
64
 
65
+ # Callback for chat
66
+ def chat_handler(message, history):
67
+ history = history or []
68
+ msg = (message or "").strip()
69
+ if not msg:
70
+ history.append({"role": "assistant", "content": "Please type a question!"})
71
+ return history
72
+
73
+ # Simple AI placeholder logic
74
+ low = msg.lower()
75
+ if "generate" in low:
76
+ reply = "Tell me the language and a short description, e.g. 'python: web scraper'"
77
+ elif "fix" in low:
78
+ reply = "Paste your code and error message; I can suggest fixes."
79
+ else:
80
+ reply = "I can generate, explain, and fix code. Try: generate, fix, explain."
81
+
82
+ # Use 'messages' format
83
+ history.append({"role": "user", "content": message})
84
+ history.append({"role": "assistant", "content": reply})
85
+ return history
86
+
87
+ # Wire buttons and submit inside Blocks context
88
  send_btn.click(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot)
 
89
  chat_input.submit(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot)
 
90
  clear_btn.click(lambda: [], None, chatbot, queue=False)
91
 
92