Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
from dotenv import load_dotenv
|
@@ -12,75 +11,50 @@ if os.path.exists(".env"):
|
|
12 |
|
13 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
|
15 |
-
# β
Ensure API Key is available
|
16 |
if not OPENAI_API_KEY:
|
17 |
-
raise ValueError("π¨ OpenAI API key is missing! Set it in the .env file.")
|
18 |
|
19 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
20 |
|
21 |
-
# β
Chatbot Response Function
|
22 |
-
def respond(user_message, history
|
23 |
if not user_message:
|
24 |
-
return "
|
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 |
-
# β
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
|
|
68 |
|
69 |
# β
Gradio UI Setup
|
70 |
with gr.Blocks() as demo:
|
71 |
gr.Markdown("## π€ AI-Guided Math PD Chatbot")
|
72 |
|
73 |
-
chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "
|
74 |
-
state_history = gr.State([(INITIAL_PROMPT, "
|
75 |
-
state_selected_method = gr.State(None) # β
New state to track selected method
|
76 |
|
77 |
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
78 |
|
79 |
-
# β
Handling user input and response logic
|
80 |
user_input.submit(
|
81 |
respond,
|
82 |
-
inputs=[user_input, state_history
|
83 |
-
outputs=[
|
|
|
|
|
|
|
|
|
84 |
)
|
85 |
|
86 |
if __name__ == "__main__":
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from dotenv import load_dotenv
|
|
|
11 |
|
12 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
13 |
|
14 |
+
# β
Ensure API Key is available, otherwise show an error
|
15 |
if not OPENAI_API_KEY:
|
16 |
+
raise ValueError("π¨ OpenAI API key is missing! Set it in the .env file or hardcode it in app.py.")
|
17 |
|
18 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
19 |
|
20 |
+
# β
Chatbot Response Function
|
21 |
+
def respond(user_message, history):
|
22 |
if not user_message:
|
23 |
+
return "", history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# β
Handle method selection
|
26 |
+
if user_message.lower() in ["bar model", "double number line", "equation"]:
|
27 |
+
response = get_prompt_for_method(user_message)
|
28 |
+
history.append((user_message, response))
|
29 |
+
return "", history
|
30 |
|
31 |
+
# β
Handle teacher response to method
|
32 |
+
last_method = history[-1][0] if history else None
|
33 |
+
if last_method and last_method.lower() in ["bar model", "double number line", "equation"]:
|
34 |
+
response = get_feedback_for_method(last_method, user_message)
|
35 |
+
history.append((user_message, response))
|
36 |
+
return "", history
|
37 |
|
38 |
+
# β
Default response if input is unclear
|
39 |
+
return "I didnβt understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
|
40 |
|
41 |
# β
Gradio UI Setup
|
42 |
with gr.Blocks() as demo:
|
43 |
gr.Markdown("## π€ AI-Guided Math PD Chatbot")
|
44 |
|
45 |
+
chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500) # β
Ensures AI starts with INITIAL_PROMPT
|
46 |
+
state_history = gr.State([(INITIAL_PROMPT, "")]) # β
Sets initial prompt as the first message
|
|
|
47 |
|
48 |
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
49 |
|
|
|
50 |
user_input.submit(
|
51 |
respond,
|
52 |
+
inputs=[user_input, state_history],
|
53 |
+
outputs=[user_input, chatbot]
|
54 |
+
).then(
|
55 |
+
fn=lambda _, h: h,
|
56 |
+
inputs=[user_input, chatbot],
|
57 |
+
outputs=[state_history]
|
58 |
)
|
59 |
|
60 |
if __name__ == "__main__":
|