Update app.py
Browse files
app.py
CHANGED
@@ -18,43 +18,52 @@ if not OPENAI_API_KEY:
|
|
18 |
|
19 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
20 |
|
21 |
-
# β
Chatbot Response Function with Debugging
|
22 |
def respond(user_message, history, selected_method):
|
23 |
if not user_message:
|
24 |
-
return "", history, selected_method
|
25 |
|
26 |
user_message = user_message.strip().lower() # Normalize input
|
27 |
|
28 |
valid_methods = ["bar model", "double number line", "equation"]
|
29 |
|
30 |
-
# β
Ensure history is a list of
|
31 |
if not isinstance(history, list):
|
32 |
history = []
|
33 |
-
|
34 |
-
# β
Convert all history elements to strings and tuples if necessary
|
35 |
history = [(str(h[0]), str(h[1])) for h in history if isinstance(h, tuple) and len(h) == 2]
|
36 |
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
# β
If user selects a method, store it and provide the method-specific prompt
|
40 |
if user_message in valid_methods:
|
41 |
selected_method = user_message # Store the method
|
42 |
method_prompt = get_prompt_for_method(user_message)
|
43 |
-
history.append((user_message, method_prompt)) #
|
44 |
-
|
|
|
|
|
|
|
45 |
return method_prompt, history, selected_method
|
46 |
|
47 |
# β
If a method has already been selected, provide feedback
|
48 |
if selected_method:
|
49 |
feedback = get_feedback_for_method(selected_method, user_message)
|
50 |
-
history.append((user_message, feedback)) #
|
51 |
-
|
|
|
|
|
|
|
52 |
return feedback, history, selected_method
|
53 |
|
54 |
# β
Ensure chatbot always responds with a valid tuple
|
55 |
error_msg = "β Please select a method first (Bar Model, Double Number Line, or Equation)."
|
56 |
-
history.append((user_message, error_msg)) #
|
57 |
-
|
|
|
|
|
58 |
return error_msg, history, selected_method
|
59 |
|
60 |
# β
Gradio UI Setup
|
|
|
18 |
|
19 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
20 |
|
21 |
+
# β
Chatbot Response Function with Full Debugging
|
22 |
def respond(user_message, history, selected_method):
|
23 |
if not user_message:
|
24 |
+
return "β No input received.", history, selected_method
|
25 |
|
26 |
user_message = user_message.strip().lower() # Normalize input
|
27 |
|
28 |
valid_methods = ["bar model", "double number line", "equation"]
|
29 |
|
30 |
+
# β
Ensure history is a list of tuples
|
31 |
if not isinstance(history, list):
|
32 |
history = []
|
|
|
|
|
33 |
history = [(str(h[0]), str(h[1])) for h in history if isinstance(h, tuple) and len(h) == 2]
|
34 |
|
35 |
+
# β
Debug Logs
|
36 |
+
print("\nDEBUG: Incoming User Message:", user_message)
|
37 |
+
print("DEBUG: Current History:", history)
|
38 |
+
print("DEBUG: Selected Method Before Processing:", selected_method)
|
39 |
|
40 |
# β
If user selects a method, store it and provide the method-specific prompt
|
41 |
if user_message in valid_methods:
|
42 |
selected_method = user_message # Store the method
|
43 |
method_prompt = get_prompt_for_method(user_message)
|
44 |
+
history.append((user_message, method_prompt)) # Store correctly formatted tuple
|
45 |
+
|
46 |
+
print("DEBUG: Method Selected:", selected_method)
|
47 |
+
print("DEBUG: Sending Prompt for Method:", method_prompt)
|
48 |
+
|
49 |
return method_prompt, history, selected_method
|
50 |
|
51 |
# β
If a method has already been selected, provide feedback
|
52 |
if selected_method:
|
53 |
feedback = get_feedback_for_method(selected_method, user_message)
|
54 |
+
history.append((user_message, feedback)) # Store correctly formatted tuple
|
55 |
+
|
56 |
+
print("DEBUG: Feedback Given:", feedback)
|
57 |
+
print("DEBUG: Updated History:", history)
|
58 |
+
|
59 |
return feedback, history, selected_method
|
60 |
|
61 |
# β
Ensure chatbot always responds with a valid tuple
|
62 |
error_msg = "β Please select a method first (Bar Model, Double Number Line, or Equation)."
|
63 |
+
history.append((user_message, error_msg)) # Store correctly formatted tuple
|
64 |
+
|
65 |
+
print("DEBUG: Error Triggered, No Method Selected")
|
66 |
+
|
67 |
return error_msg, history, selected_method
|
68 |
|
69 |
# β
Gradio UI Setup
|