Spaces:
Sleeping
Sleeping
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 |
-
#
|
9 |
if os.path.exists(".env"):
|
10 |
load_dotenv(".env")
|
11 |
|
@@ -13,33 +13,30 @@ 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 ChatCompletion API
|
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,
|
@@ -51,21 +48,18 @@ def gpt_call(history, user_message,
|
|
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 |
-
# GPT ๋ชจ๋ธ๋ก๋ถํฐ ์๋ต์ ๋ฐ์
|
63 |
assistant_reply = gpt_call(history, user_message)
|
64 |
|
65 |
-
#
|
66 |
history.append((user_message, assistant_reply))
|
67 |
|
68 |
-
# Gradio์์๋ (์๋ก ๋น์์ง ์
๋ ฅ์ฐฝ, ๊ฐฑ์ ๋ history)๋ฅผ ๋ฐํ
|
69 |
return "", history
|
70 |
|
71 |
##############################
|
@@ -74,38 +68,31 @@ def respond(user_message, history):
|
|
74 |
with gr.Blocks() as demo:
|
75 |
gr.Markdown("## Simple Chat Interface")
|
76 |
|
77 |
-
# Chatbot
|
78 |
-
# ์ฒซ ๋ฒ์งธ ๋ฉ์์ง๋ (user="", assistant=INITIAL_PROMPT) ํํ๋ก ๋ฃ์ด
|
79 |
-
# ํ๋ฉด์์์ 'assistant'๊ฐ INITIAL_PROMPT๋ฅผ ๋งํ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ฒ ํจ
|
80 |
chatbot = gr.Chatbot(
|
81 |
-
value=[
|
82 |
-
height=500
|
83 |
-
type="messages"
|
84 |
)
|
85 |
|
86 |
-
|
87 |
-
# (user, assistant) ์์ ์ ์ฅํ ํ์คํ ๋ฆฌ ์ํ
|
88 |
-
# ์ฌ๊ธฐ์๋ ๋์ผํ ์ด๊ธฐ ์ํ๋ฅผ ๋ฃ์ด์ค
|
89 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
90 |
|
91 |
-
# ์ฌ์ฉ์ ์
๋ ฅ
|
92 |
user_input = gr.Textbox(
|
93 |
placeholder="Type your message here...",
|
94 |
label="Your Input"
|
95 |
)
|
96 |
|
97 |
-
#
|
98 |
user_input.submit(
|
99 |
respond,
|
100 |
inputs=[user_input, state_history],
|
101 |
outputs=[user_input, chatbot]
|
102 |
).then(
|
103 |
-
#
|
104 |
-
|
105 |
-
inputs=[user_input, chatbot],
|
106 |
outputs=[state_history]
|
107 |
)
|
108 |
|
109 |
-
# ๋ฉ์ธ ์คํ
|
110 |
if __name__ == "__main__":
|
111 |
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
|
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,
|
17 |
model="gpt-4o-mini",
|
18 |
max_tokens=512,
|
19 |
temperature=0.7,
|
20 |
top_p=0.95):
|
21 |
"""
|
22 |
+
Generate responses using OpenAI ChatCompletion API.
|
23 |
- history: [(user_text, assistant_text), ...]
|
24 |
+
- user_message: The latest user input.
|
25 |
"""
|
26 |
+
# 1) Add system message
|
27 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
28 |
|
29 |
+
# 2) Convert history into OpenAI message format
|
|
|
30 |
for user_text, assistant_text in history:
|
31 |
if user_text:
|
32 |
messages.append({"role": "user", "content": user_text})
|
33 |
if assistant_text:
|
34 |
messages.append({"role": "assistant", "content": assistant_text})
|
35 |
|
36 |
+
# 3) Add the latest user input
|
37 |
messages.append({"role": "user", "content": user_message})
|
38 |
|
39 |
+
# 4) OpenAI API call
|
40 |
completion = client.chat.completions.create(
|
41 |
model=model,
|
42 |
messages=messages,
|
|
|
48 |
|
49 |
def respond(user_message, history):
|
50 |
"""
|
51 |
+
Handles user input and generates chatbot responses.
|
52 |
+
- user_message: The latest user message.
|
53 |
+
- history: A list of (user, assistant) message tuples.
|
54 |
"""
|
|
|
55 |
if not user_message:
|
56 |
return "", history
|
57 |
|
|
|
58 |
assistant_reply = gpt_call(history, user_message)
|
59 |
|
60 |
+
# Append new message pair to history
|
61 |
history.append((user_message, assistant_reply))
|
62 |
|
|
|
63 |
return "", history
|
64 |
|
65 |
##############################
|
|
|
68 |
with gr.Blocks() as demo:
|
69 |
gr.Markdown("## Simple Chat Interface")
|
70 |
|
71 |
+
# Initialize Chatbot correctly (list of tuples, not dictionaries)
|
|
|
|
|
72 |
chatbot = gr.Chatbot(
|
73 |
+
value=[("", INITIAL_PROMPT)], # Fixed format
|
74 |
+
height=500
|
|
|
75 |
)
|
76 |
|
77 |
+
# Maintain chat history in state
|
|
|
|
|
78 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
79 |
|
|
|
80 |
user_input = gr.Textbox(
|
81 |
placeholder="Type your message here...",
|
82 |
label="Your Input"
|
83 |
)
|
84 |
|
85 |
+
# Handle user input
|
86 |
user_input.submit(
|
87 |
respond,
|
88 |
inputs=[user_input, state_history],
|
89 |
outputs=[user_input, chatbot]
|
90 |
).then(
|
91 |
+
fn=lambda h: h, # Fix: Only update history, not chatbot
|
92 |
+
inputs=[state_history],
|
|
|
93 |
outputs=[state_history]
|
94 |
)
|
95 |
|
|
|
96 |
if __name__ == "__main__":
|
97 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
98 |
+
|