alibicer commited on
Commit
46431bd
Β·
verified Β·
1 Parent(s): 72068ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
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 strictly two-element tuples
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
- print("\nDEBUG: Current History:", history) # πŸ›  Debugging step
 
 
 
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)) # Ensure correct format
44
- print("\nDEBUG: Method Selected:", selected_method) # πŸ›  Debugging
 
 
 
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)) # Ensure correct format
51
- print("\nDEBUG: Providing Feedback:", feedback) # πŸ›  Debugging
 
 
 
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)) # Ensure correct format
57
- print("\nDEBUG: Error Message Triggered") # πŸ›  Debugging
 
 
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