Update app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,52 @@ def chat(message, history):
|
|
| 22 |
history.append((message, reply))
|
| 23 |
return history
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# ✅ UI Layout
|
| 27 |
with gr.Blocks(title="Synthetic AI Coder") as app:
|
|
|
|
| 22 |
history.append((message, reply))
|
| 23 |
return history
|
| 24 |
|
| 25 |
+
def chat_handler(message, history):
|
| 26 |
+
"""
|
| 27 |
+
message: str from textbox
|
| 28 |
+
history: existing chat history list (list of tuples/lists)
|
| 29 |
+
Must return: updated history (list of tuples/lists)
|
| 30 |
+
"""
|
| 31 |
+
history = history or []
|
| 32 |
+
|
| 33 |
+
# sanitize input
|
| 34 |
+
msg = (message or "").strip()
|
| 35 |
+
if not msg:
|
| 36 |
+
history.append(("", "Please type a question or command."))
|
| 37 |
+
return history
|
| 38 |
+
|
| 39 |
+
# simple keyword-driven assistant (expand to real LLM later)
|
| 40 |
+
low = msg.lower()
|
| 41 |
+
if "hello" in low or "hi" in low:
|
| 42 |
+
reply = "Hello! 👋 What would you like to build or debug today?"
|
| 43 |
+
elif "generate" in low or "build" in low:
|
| 44 |
+
reply = "Tell me the language and a short description (e.g., 'python: web scraper that saves to csv')."
|
| 45 |
+
elif "fix" in low or "bug" in low:
|
| 46 |
+
reply = "Paste the code (or error message) and I'll suggest fixes and diagnostics."
|
| 47 |
+
elif "explain" in low or "what does" in low:
|
| 48 |
+
reply = "Paste the code you want explained and I'll walk through it step-by-step."
|
| 49 |
+
else:
|
| 50 |
+
# fallback behavior — useful when switching to a real model later
|
| 51 |
+
reply = "I can generate, explain, and fix code. Try commands like: `generate`, `fix`, or `explain`."
|
| 52 |
+
|
| 53 |
+
# Append user message and bot reply as a tuple (user, bot)
|
| 54 |
+
history.append((message, reply))
|
| 55 |
+
return 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 |
|
| 72 |
# ✅ UI Layout
|
| 73 |
with gr.Blocks(title="Synthetic AI Coder") as app:
|