Update app.py
Browse files21 unmodified lines222324252627282930313233343536373839404142434445464748495051525354555621 unmodified linesif not user_message:return "", history# ✅ Check if user selected a methodif user_message.lower() in ["bar model", "double number line", "equation"]:return get_prompt_for_method(user_message), history + [(user_message, get_prompt_for_method(user_message))]# ✅ Process feedback based on last recorded methodif history and history[-1][0].lower() in ["bar model", "double number line", "equation"]:selected_method = history[-1][0]feedback = get_feedback_for_method(selected_method, user_message)return feedback, history + [(user_message, feedback)]return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history# ✅ Gradio UI Setupwith gr.Blocks() as demo:gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)state_history = gr.State([(INITIAL_PROMPT, "")])user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")user_input.submit(respond,inputs=[user_input, state_history],outputs=[user_input, chatbot]).then(fn=lambda _, h: h,inputs=[user_input, chatbot],outputs=[state_history])if __name__ == "__main__":21 unmodified lines2223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697021 unmodified linesif not user_message:return "", historyuser_message = user_message.strip().lower() # Normalize input for better handling# ✅ Extract previous user inputlast_user_input = history[-1][0].strip().lower() if history else ""# ✅ Check if user selected a methodvalid_methods = ["bar model", "double number line", "equation"]if user_message in valid_methods:prompt = get_prompt_for_method(user_message)history.append((user_message, prompt)) # Update historyreturn prompt, history# ✅ Ensure the user has selected a method before receiving feedbackif last_user_input in valid_methods:selected_method = last_user_input # Keep track of last selected methodfeedback = get_feedback_for_method(selected_method, user_message)history.append((user_message, feedback)) # Update historyreturn feedback, historyreturn "❌ I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history# ✅ Gradio UI Setupwith gr.Blocks() as demo:gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")chatbot = gr.Chatbot(height=500)state_history = gr.State([])user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")# ✅ Button for first-time users to start the chatdef start_chat():return [(INITIAL_PROMPT, "")]start_button = gr.Button("Start Chat")start_button.click(start_chat, outputs=[chatbot, state_history])# ✅ Handling user input and response logicuser_input.submit(respond,inputs=[user_input, state_history],outputs=[chatbot, state_history])if __name__ == "__main__":
app.py+28-14