alibicer commited on
Commit
fba2fd4
·
verified ·
1 Parent(s): 804e705

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -64
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
- if not user_message:
21
- return "", history
22
-
23
- try:
24
- assistant_reply = client.chat.completions.create(
25
- model="gpt-4o",
26
- messages=[
27
- {"role": "system", "content": MAIN_PROMPT},
28
- *[
29
- {"role": "user", "content": u} if i % 2 == 0 else {"role": "assistant", "content": a}
30
- for i, (u, a) in enumerate(history)
31
- ],
32
- {"role": "user", "content": user_message}
33
- ],
34
- max_tokens=512,
35
- temperature=0.7,
36
- ).choices[0].message.content
37
-
38
- history.append((user_message, assistant_reply))
39
-
40
- return "", history
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