Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
|
@@ -17,7 +18,7 @@ if not OPENAI_API_KEY:
|
|
| 17 |
|
| 18 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 19 |
|
| 20 |
-
# β
Chatbot Response Function
|
| 21 |
def respond(user_message, history, selected_method):
|
| 22 |
if not user_message:
|
| 23 |
return "", history, selected_method
|
|
@@ -29,24 +30,31 @@ def respond(user_message, history, selected_method):
|
|
| 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
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 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
|
|
|
|
| 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
|