Update app.py
Browse files1 unmodified line2345567897 unmodified lines171819202122202125262728293031323334353637383922232442434445462526272848495029303132333452353637383940585941424344451 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_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 lines
client = 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 it
user_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 method response = get_prompt_for_method(user_message) # Ask for reasoning history.append((user_message, response)) if not user_message: return "", history
# ✅ Step 2: If a method was selected, process teacher's explanation if selected_method: response = get_feedback_for_method(selected_method, user_message) # Give feedback history.append((user_message, response)) return "", history # ✅ 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))]
# ✅ If no method is selected yet, ask the user to select one if not selected_method: return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", 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] feedback = get_feedback_for_method(selected_method, user_message) return feedback, history + [(user_message, feedback)]
return "", history # ✅ Maintain conversation flow 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(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