Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,22 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
from openai import OpenAI
|
5 |
-
from prompts.initial_prompt import INITIAL_PROMPT
|
6 |
-
from prompts.main_prompt import MAIN_PROMPT, get_prompt_for_method, get_feedback_for_method
|
7 |
-
|
8 |
-
# ✅ Load API key from .env file
|
9 |
-
if os.path.exists(".env"):
|
10 |
-
load_dotenv(".env")
|
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 |
-
# ✅
|
|
|
|
|
|
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
outputs=[user_input, chatbot]
|
54 |
-
).then(
|
55 |
-
fn=lambda _, h: h,
|
56 |
-
inputs=[user_input, chatbot],
|
57 |
-
outputs=[state_history]
|
58 |
-
)
|
59 |
|
60 |
-
|
61 |
-
|
|
|
|
|
|
1 |
+
def respond(user_message, history, state_history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
if not user_message:
|
3 |
return "", history
|
4 |
|
5 |
+
# ✅ Retrieve stored method choice
|
6 |
+
last_method = state_history.get("selected_method", None)
|
7 |
+
|
8 |
+
# ✅ If the user selects a method, store it
|
9 |
if user_message.lower() in ["bar model", "double number line", "equation"]:
|
10 |
+
state_history["selected_method"] = user_message.lower() # Store method
|
11 |
response = get_prompt_for_method(user_message)
|
12 |
history.append((user_message, response))
|
13 |
+
return "", history, state_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# ✅ If no method was selected, ask for one
|
16 |
+
if not last_method:
|
17 |
+
return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history, state_history
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# ✅ Process teacher’s reasoning and give feedback
|
20 |
+
response = get_feedback_for_method(last_method, user_message)
|
21 |
+
history.append((user_message, response))
|
22 |
+
return "", history, state_history
|