alibicer commited on
Commit
866286c
·
verified ·
1 Parent(s): fba2fd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -13
app.py CHANGED
@@ -1,23 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, 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"):
10
+ load_dotenv(".env")
11
+
12
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
+
14
+ # ✅ Ensure API Key is available
15
+ if not OPENAI_API_KEY:
16
+ raise ValueError("🚨 OpenAI API key is missing! Set it in the .env file.")
17
+
18
+ client = OpenAI(api_key=OPENAI_API_KEY)
19
+
20
+ # ✅ Chatbot Response Function
21
  def respond(user_message, history):
22
  """
23
+ Handles user input for the chatbot.
24
+ - If a method is selected, AI asks for teacher's reasoning first.
25
+ - AI only provides feedback after hearing the teacher's approach.
 
26
  """
27
  user_message = user_message.strip().lower()
28
 
29
+ # If user selects a method, ask them to explain their reasoning
30
  if user_message in ["bar model", "double number line", "equation"]:
31
+ response = get_prompt_for_method(user_message)
32
+ history.append((user_message, response))
33
+ return "", history
34
+
35
+ # ✅ If AI expects an explanation, process the response
36
+ elif len(history) > 0 and history[-1][1] in [
37
  get_prompt_for_method("bar model"),
38
  get_prompt_for_method("double number line"),
39
  get_prompt_for_method("equation")
40
  ]:
41
+ last_method = history[-1][1].split("**")[1].split(" ")[0].lower()
42
+ response = get_feedback_for_method(last_method, user_message)
43
+ history.append((user_message, response))
44
+ return "", history
45
+
46
+ # ✅ Default response if the message is unclear
47
+ return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
48
+
49
+ # ✅ Gradio UI Setup
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")
52
+
53
+ chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
54
+ state_history = gr.State([(INITIAL_PROMPT, "")])
55
+
56
+ user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
57
+
58
+ user_input.submit(
59
+ respond,
60
+ inputs=[user_input, state_history],
61
+ outputs=[user_input, chatbot]
62
+ ).then(
63
+ fn=lambda _, h: h,
64
+ inputs=[user_input, chatbot],
65
+ outputs=[state_history]
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)