Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ 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, get_prompt_for_method, get_feedback_for_method
|
| 6 |
from prompts.initial_prompt import INITIAL_PROMPT
|
|
|
|
| 7 |
|
| 8 |
# β
Load API key from .env file
|
| 9 |
if os.path.exists(".env"):
|
|
@@ -17,46 +17,29 @@ if not OPENAI_API_KEY:
|
|
| 17 |
|
| 18 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 19 |
|
| 20 |
-
# β
Track the selected method
|
| 21 |
-
selected_method = None # This will store which method the teacher selects
|
| 22 |
-
|
| 23 |
# β
Chatbot Response Function
|
| 24 |
def respond(user_message, history):
|
| 25 |
-
|
| 26 |
-
Handles user input for the chatbot.
|
| 27 |
-
- Step 1: Teacher selects a method (Bar Model, Double Number Line, or Equation).
|
| 28 |
-
- Step 2: AI asks teacher to explain their reasoning before giving guidance.
|
| 29 |
-
- Step 3: AI listens to teacher's explanation and provides feedback.
|
| 30 |
-
"""
|
| 31 |
-
global selected_method # Store the selected method so AI remembers it
|
| 32 |
-
|
| 33 |
-
user_message = user_message.strip().lower()
|
| 34 |
-
|
| 35 |
-
# β
Step 1: Check if user selected a method (but hasn't explained yet)
|
| 36 |
-
if user_message in ["bar model", "double number line", "equation"]:
|
| 37 |
-
selected_method = user_message # Store selected method
|
| 38 |
-
response = get_prompt_for_method(user_message) # Ask for reasoning
|
| 39 |
-
history.append((user_message, response))
|
| 40 |
return "", history
|
| 41 |
|
| 42 |
-
# β
|
| 43 |
-
if
|
| 44 |
-
|
| 45 |
-
history.append((user_message, response))
|
| 46 |
-
return "", history
|
| 47 |
|
| 48 |
-
# β
|
| 49 |
-
if
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
return "
|
| 53 |
|
| 54 |
# β
Gradio UI Setup
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
gr.Markdown("## π€ AI-Guided Math PD Chatbot")
|
| 57 |
|
| 58 |
-
chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
|
| 59 |
-
state_history = gr.State([(INITIAL_PROMPT, "")])
|
| 60 |
|
| 61 |
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
| 62 |
|
|
|
|
| 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"):
|
|
|
|
| 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 |
+
# β
Check if user selected a method
|
| 26 |
+
if user_message.lower() in ["bar model", "double number line", "equation"]:
|
| 27 |
+
return get_prompt_for_method(user_message), history + [(user_message, get_prompt_for_method(user_message))]
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# β
Process feedback based on last recorded method
|
| 30 |
+
if history and history[-1][0].lower() in ["bar model", "double number line", "equation"]:
|
| 31 |
+
selected_method = history[-1][0]
|
| 32 |
+
feedback = get_feedback_for_method(selected_method, user_message)
|
| 33 |
+
return feedback, history + [(user_message, feedback)]
|
| 34 |
|
| 35 |
+
return "I didnβt understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
|
| 36 |
|
| 37 |
# β
Gradio UI Setup
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("## π€ AI-Guided Math PD Chatbot")
|
| 40 |
|
| 41 |
+
chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
|
| 42 |
+
state_history = gr.State([(INITIAL_PROMPT, "")])
|
| 43 |
|
| 44 |
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
| 45 |
|