Update app.py
Browse files21 unmodified lines22232425262728293026273132333435363729303138394041334243443545464748495041425152535455565758596061626364656649505152536768697021 unmodified lines if not user_message: return "", history
user_message = user_message.strip().lower() # Normalize input for better handling
# ✅ Extract previous user input last_user_input = history[-1][0].strip().lower() if history else ""
# ✅ Check if user selected a method if 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))] valid_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 history return prompt, history
# ✅ Process feedback based on last recorded method if history and history[-1][0].lower() in ["bar model", "double number line", "equation"]: selected_method = history[-1][0] # ✅ Ensure the user has selected a method before receiving feedback if last_user_input in valid_methods: selected_method = last_user_input # Keep track of last selected method feedback = get_feedback_for_method(selected_method, user_message) return feedback, history + [(user_message, feedback)] history.append((user_message, feedback)) # Update history return feedback, history
return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history 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, "")]) 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 chat def 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 logic 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] outputs=[chatbot, state_history] )
if __name__ == "__main__":app.py+28-14