alibicer commited on
Commit
73b1050
·
verified ·
1 Parent(s): fd84d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -30
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
- # ✅ Step 2: If a method was selected, process teacher's explanation
43
- if selected_method:
44
- response = get_feedback_for_method(selected_method, user_message) # Give feedback
45
- history.append((user_message, response))
46
- return "", history
47
 
48
- # ✅ If no method is selected yet, ask the user to select one
49
- if not selected_method:
50
- return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
 
 
51
 
52
- return "", history # Maintain conversation flow
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