alibicer commited on
Commit
ff6f0c2
·
verified ·
1 Parent(s): 4910ade

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -17,38 +17,39 @@ if not OPENAI_API_KEY:
17
 
18
  client = OpenAI(api_key=OPENAI_API_KEY)
19
 
 
 
 
20
  # ✅ Chatbot Response Function
21
  def respond(user_message, history):
22
  """
23
  Handles user input for the chatbot.
24
- - If a method is selected, AI asks for teacher's reasoning first.
25
- - AI only provides feedback after hearing the teacher's approach.
 
26
  """
 
 
27
  user_message = user_message.strip().lower()
28
 
29
- # ✅ Check if the last message in history was a method selection prompt
30
- if history and history[-1][1] in [
31
- get_prompt_for_method("bar model"),
32
- get_prompt_for_method("double number line"),
33
- get_prompt_for_method("equation")
34
- ]:
35
- # ✅ Retrieve the last selected method
36
- last_method = history[-1][1].split("**")[1].split(" ")[0].lower()
37
- response = get_feedback_for_method(last_method, user_message)
38
  history.append((user_message, response))
39
  return "", history
40
 
41
- # ✅ If user selects a method, ask them to explain their reasoning
42
- if user_message in ["bar model", "double number line", "equation"]:
43
- response = get_prompt_for_method(user_message)
44
  history.append((user_message, response))
45
  return "", history
46
 
47
- # ✅ Default response if input is unclear and no method has been selected
48
- if not history or history[-1][0] not in ["bar model", "double number line", "equation"]:
49
  return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
50
 
51
- return "", history # ✅ Keep the conversation flowing without errors
52
 
53
  # ✅ Gradio UI Setup
54
  with gr.Blocks() as demo:
 
17
 
18
  client = OpenAI(api_key=OPENAI_API_KEY)
19
 
20
+ # ✅ Track the selected method
21
+ selected_method = None # This will store which method the teacher selects
22
+
23
  # ✅ Chatbot Response Function
24
  def respond(user_message, history):
25
  """
26
  Handles user input for the chatbot.
27
+ - Step 1: Teacher selects a method (Bar Model, Double Number Line, or Equation).
28
+ - Step 2: AI asks teacher to explain their reasoning before giving guidance.
29
+ - Step 3: AI listens to teacher's explanation and provides feedback.
30
  """
31
+ global selected_method # Store the selected method so AI remembers it
32
+
33
  user_message = user_message.strip().lower()
34
 
35
+ # ✅ Step 1: Check if user selected a method (but hasn't explained yet)
36
+ if user_message in ["bar model", "double number line", "equation"]:
37
+ selected_method = user_message # Store selected method
38
+ response = get_prompt_for_method(user_message) # Ask for reasoning
 
 
 
 
 
39
  history.append((user_message, response))
40
  return "", history
41
 
42
+ # ✅ Step 2: If a method was selected, process teacher's explanation
43
+ if selected_method:
44
+ response = get_feedback_for_method(selected_method, user_message) # Give feedback
45
  history.append((user_message, response))
46
  return "", history
47
 
48
+ # ✅ If no method is selected yet, ask the user to select one
49
+ if not selected_method:
50
  return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
51
 
52
+ return "", history # ✅ Maintain conversation flow
53
 
54
  # ✅ Gradio UI Setup
55
  with gr.Blocks() as demo: