Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,71 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
if not user_message:
|
3 |
return "", history
|
4 |
|
5 |
-
# ✅
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
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 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
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)
|
|
|
|