Update app.py
Browse files1 unmodified line234567897 unmodified lines171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061621 unmodified lineimport gradio as grfrom dotenv import load_dotenvfrom openai import OpenAIfrom prompts.main_prompt import MAIN_PROMPT, get_prompt_for_method, get_feedback_for_methodfrom prompts.initial_prompt import INITIAL_PROMPT# β Load API key from .env fileif os.path.exists(".env"):7 unmodified linesclient = OpenAI(api_key=OPENAI_API_KEY)# β Track the selected methodselected_method = None # This will store which method the teacher selects# β Chatbot Response Functiondef respond(user_message, history):"""Handles user input for the chatbot.- Step 1: Teacher selects a method (Bar Model, Double Number Line, or Equation).- Step 2: AI asks teacher to explain their reasoning before giving guidance.- Step 3: AI listens to teacher's explanation and provides feedback."""global selected_method # Store the selected method so AI remembers ituser_message = user_message.strip().lower()# β Step 1: Check if user selected a method (but hasn't explained yet)if user_message in ["bar model", "double number line", "equation"]:selected_method = user_message # Store selected methodresponse = get_prompt_for_method(user_message) # Ask for reasoninghistory.append((user_message, response))return "", history# β Step 2: If a method was selected, process teacher's explanationif selected_method:response = get_feedback_for_method(selected_method, user_message) # Give feedbackhistory.append((user_message, response))return "", history# β If no method is selected yet, ask the user to select oneif not selected_method:return "I didnβt understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", historyreturn "", history # β Maintain conversation flow# β 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")1 unmodified line234567897 unmodified lines17181920212223242526272829303132333435363738394041424344451 unmodified lineimport gradio as grfrom dotenv import load_dotenvfrom openai import OpenAIfrom prompts.initial_prompt import INITIAL_PROMPTfrom prompts.main_prompt import MAIN_PROMPT, get_prompt_for_method, get_feedback_for_method# β Load API key from .env fileif os.path.exists(".env"):7 unmodified linesclient = OpenAI(api_key=OPENAI_API_KEY)# β Chatbot Response Functiondef respond(user_message, history):if 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")
app.py+13-30