Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,9 @@ import gradio as gr
|
|
3 |
from dotenv import load_dotenv
|
4 |
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 |
|
@@ -14,32 +14,30 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
14 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
15 |
|
16 |
|
17 |
-
|
18 |
def gpt_call(history, user_message,
|
19 |
-
model="gpt-4o
|
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)
|
29 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
30 |
-
|
31 |
-
# 2)
|
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,
|
@@ -47,63 +45,63 @@ def gpt_call(history, user_message,
|
|
47 |
temperature=temperature,
|
48 |
top_p=top_p
|
49 |
)
|
|
|
50 |
return completion.choices[0].message.content
|
51 |
|
|
|
52 |
def respond(user_message, history):
|
53 |
"""
|
54 |
-
|
55 |
-
- user_message:
|
56 |
-
- history:
|
57 |
"""
|
58 |
-
# ์ฌ์ฉ์๊ฐ ๋น ๋ฌธ์์ด์ ๋ณด๋๋ค๋ฉด ์๋ฌด ์ผ๋ ํ์ง ์์
|
59 |
if not user_message:
|
60 |
return "", history
|
61 |
|
62 |
-
#
|
63 |
-
|
|
|
|
|
|
|
64 |
|
65 |
-
#
|
66 |
history.append((user_message, assistant_reply))
|
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 |
-
#
|
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 |
-
#
|
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)
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
from openai import OpenAI
|
5 |
from prompts.initial_prompt import INITIAL_PROMPT
|
6 |
+
from prompts.main_prompt import MAIN_PROMPT, PROBLEM_SOLUTIONS_PROMPT # Ensure both are imported
|
7 |
|
8 |
+
# Load the API key from the .env file if available
|
9 |
if os.path.exists(".env"):
|
10 |
load_dotenv(".env")
|
11 |
|
|
|
14 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
15 |
|
16 |
|
|
|
17 |
def gpt_call(history, user_message,
|
18 |
+
model="gpt-4o",
|
19 |
max_tokens=512,
|
20 |
temperature=0.7,
|
21 |
top_p=0.95):
|
22 |
"""
|
23 |
+
Calls the OpenAI API to generate a response.
|
24 |
- history: [(user_text, assistant_text), ...]
|
25 |
+
- user_message: The latest user message
|
26 |
"""
|
27 |
+
# 1) Start with the system message (MAIN_PROMPT) for context
|
28 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
29 |
+
|
30 |
+
# 2) Append conversation history
|
|
|
31 |
for user_text, assistant_text in history:
|
32 |
if user_text:
|
33 |
messages.append({"role": "user", "content": user_text})
|
34 |
if assistant_text:
|
35 |
messages.append({"role": "assistant", "content": assistant_text})
|
36 |
|
37 |
+
# 3) Add the user's new message
|
38 |
messages.append({"role": "user", "content": user_message})
|
39 |
|
40 |
+
# 4) Call OpenAI API
|
41 |
completion = client.chat.completions.create(
|
42 |
model=model,
|
43 |
messages=messages,
|
|
|
45 |
temperature=temperature,
|
46 |
top_p=top_p
|
47 |
)
|
48 |
+
|
49 |
return completion.choices[0].message.content
|
50 |
|
51 |
+
|
52 |
def respond(user_message, history):
|
53 |
"""
|
54 |
+
Handles user input and gets GPT-generated response.
|
55 |
+
- user_message: The message from the user
|
56 |
+
- history: List of (user, assistant) conversation history
|
57 |
"""
|
|
|
58 |
if not user_message:
|
59 |
return "", history
|
60 |
|
61 |
+
# If the user asks for a solution, inject PROBLEM_SOLUTIONS_PROMPT
|
62 |
+
if "solution" in user_message.lower():
|
63 |
+
assistant_reply = gpt_call(history, PROBLEM_SOLUTIONS_PROMPT)
|
64 |
+
else:
|
65 |
+
assistant_reply = gpt_call(history, user_message)
|
66 |
|
67 |
+
# Add conversation turn to history
|
68 |
history.append((user_message, assistant_reply))
|
69 |
|
|
|
70 |
return "", history
|
71 |
|
72 |
+
|
73 |
##############################
|
74 |
# Gradio Blocks UI
|
75 |
##############################
|
76 |
with gr.Blocks() as demo:
|
77 |
+
gr.Markdown("## AI-Guided Math PD Chatbot")
|
78 |
|
79 |
+
# Chatbot initialization with the first AI message
|
|
|
|
|
80 |
chatbot = gr.Chatbot(
|
81 |
+
value=[("", INITIAL_PROMPT)], # Initial system prompt
|
82 |
height=500
|
83 |
)
|
84 |
|
85 |
+
# Stores the chat history
|
|
|
86 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
87 |
|
88 |
+
# User input field
|
89 |
user_input = gr.Textbox(
|
90 |
placeholder="Type your message here...",
|
91 |
label="Your Input"
|
92 |
)
|
93 |
|
94 |
+
# Submit action
|
95 |
user_input.submit(
|
96 |
respond,
|
97 |
inputs=[user_input, state_history],
|
98 |
outputs=[user_input, chatbot]
|
99 |
).then(
|
|
|
100 |
fn=lambda _, h: h,
|
101 |
inputs=[user_input, chatbot],
|
102 |
outputs=[state_history]
|
103 |
)
|
104 |
|
105 |
+
# Run the Gradio app
|
106 |
if __name__ == "__main__":
|
107 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|