Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,23 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
from openai import OpenAI
|
5 |
-
from prompts.main_prompt import MAIN_PROMPT # Ensure this file exists and is fixed
|
6 |
-
|
7 |
-
# Load API key from .env file
|
8 |
-
if os.path.exists(".env"):
|
9 |
-
load_dotenv(".env")
|
10 |
-
|
11 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
12 |
-
|
13 |
-
if not OPENAI_API_KEY:
|
14 |
-
raise ValueError("OpenAI API key is missing! Set it in the .env file.")
|
15 |
-
|
16 |
-
client = OpenAI(api_key=OPENAI_API_KEY)
|
17 |
-
|
18 |
-
# Chatbot Response Function
|
19 |
def respond(user_message, history):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
history.
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
except Exception as e:
|
43 |
-
return f"An error occurred: {str(e)}", history
|
44 |
-
|
45 |
-
# Gradio UI Setup
|
46 |
-
with gr.Blocks() as demo:
|
47 |
-
gr.Markdown("## AI-Guided Math PD Chatbot")
|
48 |
-
|
49 |
-
chatbot = gr.Chatbot(value=[(MAIN_PROMPT, "")], height=500)
|
50 |
-
state_history = gr.State([(MAIN_PROMPT, "")])
|
51 |
-
|
52 |
-
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
53 |
-
|
54 |
-
user_input.submit(
|
55 |
-
respond,
|
56 |
-
inputs=[user_input, state_history],
|
57 |
-
outputs=[user_input, chatbot]
|
58 |
-
).then(
|
59 |
-
fn=lambda _, h: h,
|
60 |
-
inputs=[user_input, chatbot],
|
61 |
-
outputs=[state_history]
|
62 |
-
)
|
63 |
-
|
64 |
-
if __name__ == "__main__":
|
65 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def respond(user_message, history):
|
2 |
+
"""
|
3 |
+
Handles user input and determines how the chatbot responds.
|
4 |
+
- If the teacher selects a method, AI will **first ask them to explain their approach**.
|
5 |
+
- AI will not provide a full solution immediately.
|
6 |
+
- If the teacher already explained, AI will provide feedback and hints.
|
7 |
+
"""
|
8 |
+
user_message = user_message.strip().lower()
|
9 |
+
|
10 |
+
# If the user selects a method, prompt them to explain their approach first
|
11 |
+
if user_message in ["bar model", "double number line", "equation"]:
|
12 |
+
return get_prompt_for_method(user_message), history
|
13 |
+
|
14 |
+
# If AI is expecting an explanation, process teacher's response
|
15 |
+
elif len(history) > 0 and history[-1] in [
|
16 |
+
get_prompt_for_method("bar model"),
|
17 |
+
get_prompt_for_method("double number line"),
|
18 |
+
get_prompt_for_method("equation")
|
19 |
+
]:
|
20 |
+
last_method = history[-1].split("**")[1].split(" ")[0].lower() # Extract method name from the last message
|
21 |
+
return get_feedback_for_method(last_method, user_message), history
|
22 |
+
|
23 |
+
return "I didn’t quite understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|