alibicer commited on
Commit
b6b4a7e
·
verified ·
1 Parent(s): bf0df63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -16
app.py CHANGED
@@ -1,22 +1,71 @@
1
- def respond(user_message, history, state_history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  if not user_message:
3
  return "", history
4
 
5
- # ✅ Retrieve stored method choice
6
- last_method = state_history.get("selected_method", None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # If the user selects a method, store it
9
- if user_message.lower() in ["bar model", "double number line", "equation"]:
10
- state_history["selected_method"] = user_message.lower() # Store method
11
- response = get_prompt_for_method(user_message)
12
- history.append((user_message, response))
13
- return "", history, state_history
14
 
15
- # ✅ If no method was selected, ask for one
16
- if not last_method:
17
- return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history, state_history
 
 
 
 
 
 
18
 
19
- # Process teacher’s reasoning and give feedback
20
- response = get_feedback_for_method(last_method, user_message)
21
- history.append((user_message, response))
22
- return "", history, state_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
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
+ if not OPENAI_API_KEY:
15
+ raise ValueError("🚨 OpenAI API key is missing! Set it in your .env file.")
16
+
17
+ client = OpenAI(api_key=OPENAI_API_KEY)
18
+
19
+ def respond(user_message, history):
20
  if not user_message:
21
  return "", history
22
 
23
+ # ✅ Ensure proper message handling
24
+ try:
25
+ messages = [{"role": "system", "content": MAIN_PROMPT}]
26
+
27
+ for user_text, assistant_text in history:
28
+ if user_text:
29
+ messages.append({"role": "user", "content": user_text})
30
+ if assistant_text:
31
+ messages.append({"role": "assistant", "content": assistant_text})
32
+
33
+ messages.append({"role": "user", "content": user_message})
34
+
35
+ # ✅ Get AI response
36
+ completion = client.chat.completions.create(
37
+ model="gpt-4o",
38
+ messages=messages,
39
+ max_tokens=512,
40
+ temperature=0.7
41
+ )
42
+
43
+ assistant_reply = completion.choices[0].message.content
44
+ history.append((user_message, assistant_reply))
45
+
46
+ return "", history
47
+
48
+ except Exception as e:
49
+ return f"⚠️ An error occurred: {str(e)}", history
50
+
51
+ # ✅ Gradio UI Setup
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")
54
+
55
+ chatbot = gr.Chatbot(value=[("", INITIAL_PROMPT)], height=500)
56
+ state_history = gr.State([("", INITIAL_PROMPT)])
57
 
58
+ user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
 
 
 
 
 
59
 
60
+ user_input.submit(
61
+ respond,
62
+ inputs=[user_input, state_history],
63
+ outputs=[user_input, chatbot]
64
+ ).then(
65
+ fn=lambda _, h: h,
66
+ inputs=[user_input, chatbot],
67
+ outputs=[state_history]
68
+ )
69
 
70
+ if __name__ == "__main__":
71
+ demo.launch(server_name="0.0.0.0", server_port=7860)