Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from openai import OpenAI
|
|
5 |
from prompts.initial_prompt import INITIAL_PROMPT
|
6 |
from prompts.main_prompt import MAIN_PROMPT
|
7 |
|
8 |
-
# .env
|
9 |
if os.path.exists(".env"):
|
10 |
load_dotenv(".env")
|
11 |
|
@@ -13,33 +13,22 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
13 |
|
14 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
def gpt_call(history, user_message,
|
19 |
-
model="gpt-4o-mini",
|
20 |
-
max_tokens=512,
|
21 |
-
temperature=0.7,
|
22 |
-
top_p=0.95):
|
23 |
"""
|
24 |
-
OpenAI
|
25 |
- history: [(user_text, assistant_text), ...]
|
26 |
-
- user_message:
|
27 |
"""
|
28 |
-
# 1) ์์คํ
๋ฉ์์ง(=MAIN_PROMPT)๋ฅผ ๊ฐ์ฅ ์์ ์ถ๊ฐ
|
29 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
30 |
-
|
31 |
-
# 2) ๊ธฐ์กด ๋ํ ๊ธฐ๋ก(history)์ OpenAI ํ์์ผ๋ก ๋ณํ
|
32 |
-
# user_text -> 'user' / assistant_text -> 'assistant'
|
33 |
for user_text, assistant_text in history:
|
34 |
if user_text:
|
35 |
messages.append({"role": "user", "content": user_text})
|
36 |
if assistant_text:
|
37 |
messages.append({"role": "assistant", "content": assistant_text})
|
38 |
|
39 |
-
# 3) ๋ง์ง๋ง์ ์ด๋ฒ ์ฌ์ฉ์์ ์
๋ ฅ์ ์ถ๊ฐ
|
40 |
messages.append({"role": "user", "content": user_message})
|
41 |
|
42 |
-
# 4) OpenAI API ํธ์ถ
|
43 |
completion = client.chat.completions.create(
|
44 |
model=model,
|
45 |
messages=messages,
|
@@ -51,59 +40,53 @@ def gpt_call(history, user_message,
|
|
51 |
|
52 |
def respond(user_message, history):
|
53 |
"""
|
54 |
-
|
55 |
-
-
|
56 |
-
-
|
57 |
"""
|
58 |
-
# ์ฌ์ฉ์๊ฐ ๋น ๋ฌธ์์ด์ ๋ณด๋๋ค๋ฉด ์๋ฌด ์ผ๋ ํ์ง ์์
|
59 |
if not user_message:
|
60 |
return "", history
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
# Gradio์์๋ (์๋ก ๋น์์ง ์
๋ ฅ์ฐฝ, ๊ฐฑ์ ๋ history)๋ฅผ ๋ฐํ
|
69 |
return "", history
|
70 |
|
71 |
##############################
|
72 |
# Gradio Blocks UI
|
73 |
##############################
|
74 |
with gr.Blocks() as demo:
|
75 |
-
gr.Markdown("##
|
76 |
|
77 |
-
# Chatbot ์ด๊ธฐ ์ํ๋ฅผ ์ค์
|
78 |
-
# ์ฒซ ๋ฒ์งธ ๋ฉ์์ง๋ (user="", assistant=INITIAL_PROMPT) ํํ๋ก ๋ฃ์ด
|
79 |
-
# ํ๋ฉด์์์ 'assistant'๊ฐ INITIAL_PROMPT๋ฅผ ๋งํ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ฒ ํจ
|
80 |
chatbot = gr.Chatbot(
|
81 |
-
value=[("", INITIAL_PROMPT)],
|
82 |
height=500
|
83 |
)
|
84 |
|
85 |
-
# (user, assistant) ์์ ์ ์ฅํ ํ์คํ ๋ฆฌ ์ํ
|
86 |
-
# ์ฌ๊ธฐ์๋ ๋์ผํ ์ด๊ธฐ ์ํ๋ฅผ ๋ฃ์ด์ค
|
87 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
88 |
|
89 |
-
# ์ฌ์ฉ์ ์
๋ ฅ
|
90 |
user_input = gr.Textbox(
|
91 |
placeholder="Type your message here...",
|
92 |
label="Your Input"
|
93 |
)
|
94 |
|
95 |
-
# ์
๋ ฅ์ด submit๋๋ฉด respond() ํธ์ถ โ ์ถ๋ ฅ์ (์ ์
๋ ฅ์ฐฝ, ๊ฐฑ์ ๋ chatbot)
|
96 |
user_input.submit(
|
97 |
respond,
|
98 |
inputs=[user_input, state_history],
|
99 |
outputs=[user_input, chatbot]
|
100 |
).then(
|
101 |
-
# respond ๋๋ ๋ค, ์ต์ history๋ฅผ state_history์ ๋ฐ์
|
102 |
fn=lambda _, h: h,
|
103 |
inputs=[user_input, chatbot],
|
104 |
outputs=[state_history]
|
105 |
)
|
106 |
|
107 |
-
# ๋ฉ์ธ ์คํ
|
108 |
if __name__ == "__main__":
|
109 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
5 |
from prompts.initial_prompt import INITIAL_PROMPT
|
6 |
from prompts.main_prompt import MAIN_PROMPT
|
7 |
|
8 |
+
# Load API key from .env file
|
9 |
if os.path.exists(".env"):
|
10 |
load_dotenv(".env")
|
11 |
|
|
|
13 |
|
14 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
15 |
|
16 |
+
def gpt_call(history, user_message, model="gpt-4o-mini", max_tokens=512, temperature=0.7, top_p=0.95):
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
"""
|
18 |
+
Calls OpenAI API to generate a response based on conversation history.
|
19 |
- history: [(user_text, assistant_text), ...]
|
20 |
+
- user_message: The latest user input
|
21 |
"""
|
|
|
22 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
23 |
+
|
|
|
|
|
24 |
for user_text, assistant_text in history:
|
25 |
if user_text:
|
26 |
messages.append({"role": "user", "content": user_text})
|
27 |
if assistant_text:
|
28 |
messages.append({"role": "assistant", "content": assistant_text})
|
29 |
|
|
|
30 |
messages.append({"role": "user", "content": user_message})
|
31 |
|
|
|
32 |
completion = client.chat.completions.create(
|
33 |
model=model,
|
34 |
messages=messages,
|
|
|
40 |
|
41 |
def respond(user_message, history):
|
42 |
"""
|
43 |
+
Handles chatbot responses.
|
44 |
+
- Ensures teachers must explain their reasoning before AI provides hints or feedback.
|
45 |
+
- Guides the conversation to include CCSS practice standards, problem-posing, creativity-directed practices, and summary.
|
46 |
"""
|
|
|
47 |
if not user_message:
|
48 |
return "", history
|
49 |
|
50 |
+
# Extract the last interaction
|
51 |
+
last_message = history[-1][0] if history else ""
|
52 |
|
53 |
+
if "problem" in last_message.lower() and "solve" in last_message.lower():
|
54 |
+
# If the bot is expecting an explanation, store the response and move forward
|
55 |
+
history.append((user_message, "Thanks for sharing your reasoning! Let's analyze your response."))
|
56 |
+
else:
|
57 |
+
# Regular OpenAI GPT response
|
58 |
+
assistant_reply = gpt_call(history, user_message)
|
59 |
+
history.append((user_message, assistant_reply))
|
60 |
|
|
|
61 |
return "", history
|
62 |
|
63 |
##############################
|
64 |
# Gradio Blocks UI
|
65 |
##############################
|
66 |
with gr.Blocks() as demo:
|
67 |
+
gr.Markdown("## AI-Guided Math PD Chatbot")
|
68 |
|
|
|
|
|
|
|
69 |
chatbot = gr.Chatbot(
|
70 |
+
value=[("", INITIAL_PROMPT)],
|
71 |
height=500
|
72 |
)
|
73 |
|
|
|
|
|
74 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
75 |
|
|
|
76 |
user_input = gr.Textbox(
|
77 |
placeholder="Type your message here...",
|
78 |
label="Your Input"
|
79 |
)
|
80 |
|
|
|
81 |
user_input.submit(
|
82 |
respond,
|
83 |
inputs=[user_input, state_history],
|
84 |
outputs=[user_input, chatbot]
|
85 |
).then(
|
|
|
86 |
fn=lambda _, h: h,
|
87 |
inputs=[user_input, chatbot],
|
88 |
outputs=[state_history]
|
89 |
)
|
90 |
|
|
|
91 |
if __name__ == "__main__":
|
92 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|