alibicer commited on
Commit
caf5169
Β·
verified Β·
1 Parent(s): 3538cf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -26,29 +26,35 @@ def respond(user_message, history, selected_method):
26
 
27
  valid_methods = ["bar model", "double number line", "equation"]
28
 
 
 
 
 
 
29
  # βœ… If user selects a method, store it and provide the method-specific prompt
30
  if user_message in valid_methods:
31
  selected_method = user_message # Store the method
32
  method_prompt = get_prompt_for_method(user_message)
33
- history.append((user_message, method_prompt)) # Ensure tuple format
34
  return method_prompt, history, selected_method
35
 
36
  # βœ… If a method has already been selected, provide feedback
37
  if selected_method:
38
  feedback = get_feedback_for_method(selected_method, user_message)
39
- history.append((user_message, feedback)) # Ensure tuple format
40
  return feedback, history, selected_method
41
 
 
42
  error_msg = "❌ Please select a method first (Bar Model, Double Number Line, or Equation)."
43
- history.append((user_message, error_msg)) # Ensure tuple format
44
  return error_msg, history, selected_method
45
 
46
  # βœ… Gradio UI Setup
47
  with gr.Blocks() as demo:
48
  gr.Markdown("## πŸ€– AI-Guided Math PD Chatbot")
49
 
50
- chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
51
- state_history = gr.State([(INITIAL_PROMPT, "")])
52
  state_selected_method = gr.State(None) # βœ… New state to track selected method
53
 
54
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
 
26
 
27
  valid_methods = ["bar model", "double number line", "equation"]
28
 
29
+ # βœ… Ensure history is a list of strictly two-element tuples
30
+ if not isinstance(history, list):
31
+ history = []
32
+ history = [(str(h[0]), str(h[1])) for h in history if isinstance(h, tuple) and len(h) == 2]
33
+
34
  # βœ… If user selects a method, store it and provide the method-specific prompt
35
  if user_message in valid_methods:
36
  selected_method = user_message # Store the method
37
  method_prompt = get_prompt_for_method(user_message)
38
+ history.append((user_message, method_prompt)) # Ensure correct format
39
  return method_prompt, history, selected_method
40
 
41
  # βœ… If a method has already been selected, provide feedback
42
  if selected_method:
43
  feedback = get_feedback_for_method(selected_method, user_message)
44
+ history.append((user_message, feedback)) # Ensure correct format
45
  return feedback, history, selected_method
46
 
47
+ # βœ… Ensure chatbot always responds with a valid tuple
48
  error_msg = "❌ Please select a method first (Bar Model, Double Number Line, or Equation)."
49
+ history.append((user_message, error_msg)) # Ensure correct format
50
  return error_msg, history, selected_method
51
 
52
  # βœ… Gradio UI Setup
53
  with gr.Blocks() as demo:
54
  gr.Markdown("## πŸ€– AI-Guided Math PD Chatbot")
55
 
56
+ chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "Hello! Please select a method to begin.")], height=500)
57
+ state_history = gr.State([(INITIAL_PROMPT, "Hello! Please select a method to begin.")])
58
  state_selected_method = gr.State(None) # βœ… New state to track selected method
59
 
60
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")