Update app.py
Browse files
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
|
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
|
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
|
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")
|