alibicer commited on
Commit
8bcaf24
·
verified ·
1 Parent(s): 73b1050

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -22,35 +22,49 @@ def respond(user_message, history):
22
  if not user_message:
23
  return "", history
24
 
 
 
 
 
 
25
  # ✅ Check if user selected a method
26
- if user_message.lower() in ["bar model", "double number line", "equation"]:
27
- return get_prompt_for_method(user_message), history + [(user_message, get_prompt_for_method(user_message))]
 
 
 
 
28
 
29
- # ✅ Process feedback based on last recorded method
30
- if history and history[-1][0].lower() in ["bar model", "double number line", "equation"]:
31
- selected_method = history[-1][0]
32
  feedback = get_feedback_for_method(selected_method, user_message)
33
- return feedback, history + [(user_message, feedback)]
 
34
 
35
- return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
36
 
37
  # ✅ Gradio UI Setup
38
  with gr.Blocks() as demo:
39
  gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")
40
 
41
- chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
42
- state_history = gr.State([(INITIAL_PROMPT, "")])
43
 
44
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
45
 
 
 
 
 
 
 
 
 
46
  user_input.submit(
47
  respond,
48
  inputs=[user_input, state_history],
49
- outputs=[user_input, chatbot]
50
- ).then(
51
- fn=lambda _, h: h,
52
- inputs=[user_input, chatbot],
53
- outputs=[state_history]
54
  )
55
 
56
  if __name__ == "__main__":
 
22
  if not user_message:
23
  return "", history
24
 
25
+ user_message = user_message.strip().lower() # Normalize input for better handling
26
+
27
+ # ✅ Extract previous user input
28
+ last_user_input = history[-1][0].strip().lower() if history else ""
29
+
30
  # ✅ Check if user selected a method
31
+ valid_methods = ["bar model", "double number line", "equation"]
32
+
33
+ if user_message in valid_methods:
34
+ prompt = get_prompt_for_method(user_message)
35
+ history.append((user_message, prompt)) # Update history
36
+ return prompt, history
37
 
38
+ # ✅ Ensure the user has selected a method before receiving feedback
39
+ if last_user_input in valid_methods:
40
+ selected_method = last_user_input # Keep track of last selected method
41
  feedback = get_feedback_for_method(selected_method, user_message)
42
+ history.append((user_message, feedback)) # Update history
43
+ return feedback, history
44
 
45
+ return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
46
 
47
  # ✅ Gradio UI Setup
48
  with gr.Blocks() as demo:
49
  gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")
50
 
51
+ chatbot = gr.Chatbot(height=500)
52
+ state_history = gr.State([])
53
 
54
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
55
 
56
+ # ✅ Button for first-time users to start the chat
57
+ def start_chat():
58
+ return [(INITIAL_PROMPT, "")]
59
+
60
+ start_button = gr.Button("Start Chat")
61
+ start_button.click(start_chat, outputs=[chatbot, state_history])
62
+
63
+ # ✅ Handling user input and response logic
64
  user_input.submit(
65
  respond,
66
  inputs=[user_input, state_history],
67
+ outputs=[chatbot, state_history]
 
 
 
 
68
  )
69
 
70
  if __name__ == "__main__":