Update app.py
Browse files
app.py
CHANGED
@@ -4,18 +4,16 @@ import requests
|
|
4 |
import json
|
5 |
|
6 |
def create_deepseek_interface():
|
7 |
-
# ์ฑํ
๊ธฐ๋ก
|
8 |
-
chat_history = []
|
9 |
-
|
10 |
-
# DeepSeek API ํธ์ถ ํจ์
|
11 |
def query_deepseek(message, history, api_key):
|
12 |
if not api_key:
|
13 |
return history, "Fireworks AI API ํค๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
|
14 |
|
15 |
# API ์์ฒญ์ ์ํ ๋ํ ๊ธฐ๋ก ์ค๋น
|
16 |
messages = []
|
17 |
-
for
|
18 |
-
messages.append(
|
|
|
19 |
|
20 |
# ์ ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
21 |
messages.append({"role": "user", "content": message})
|
@@ -47,14 +45,12 @@ def create_deepseek_interface():
|
|
47 |
result = response.json()
|
48 |
assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
49 |
|
50 |
-
# ๋ํ ๊ธฐ๋ก ์
๋ฐ์ดํธ (
|
51 |
-
|
52 |
-
|
53 |
-
new_history.append({"role": "assistant", "content": assistant_response})
|
54 |
-
return new_history, ""
|
55 |
except requests.exceptions.RequestException as e:
|
56 |
error_msg = f"API ์ค๋ฅ: {str(e)}"
|
57 |
-
if hasattr(
|
58 |
error_msg = "์ธ์ฆ ์คํจ. API ํค๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
59 |
return history, error_msg
|
60 |
|
@@ -68,9 +64,6 @@ def create_deepseek_interface():
|
|
68 |
"""
|
69 |
)
|
70 |
|
71 |
-
# ์ค๋ฅ ๋ฉ์์ง ์ํ
|
72 |
-
error_state = gr.State("")
|
73 |
-
|
74 |
# ๋ฉ์ธ ๋ ์ด์์ (๋ ๊ฐ์ ์ด)
|
75 |
with gr.Row():
|
76 |
# ์ฌ์ด๋๋ฐ - ๋ชจ๋ธ ์ ๋ณด ๋ฐ API ํค
|
@@ -115,8 +108,7 @@ def create_deepseek_interface():
|
|
115 |
chatbot = gr.Chatbot(
|
116 |
height=500,
|
117 |
show_label=False,
|
118 |
-
container=True
|
119 |
-
type="messages"
|
120 |
)
|
121 |
|
122 |
# ์
๋ ฅ ์์ญ
|
@@ -146,22 +138,20 @@ def create_deepseek_interface():
|
|
146 |
# ํผ ์ ์ถ ์ฒ๋ฆฌ
|
147 |
def process_query(message, history, api_key):
|
148 |
if not message.strip():
|
149 |
-
return history
|
150 |
|
151 |
updated_history, error = query_deepseek(message, history, api_key)
|
152 |
|
153 |
if error:
|
154 |
-
|
155 |
-
return history
|
156 |
else:
|
157 |
-
|
158 |
-
return updated_history
|
159 |
|
160 |
# ๋ฒํผ๊ณผ ๊ธฐ๋ฅ ์ฐ๊ฒฐ
|
161 |
submit.click(
|
162 |
process_query,
|
163 |
inputs=[msg, chatbot, api_key],
|
164 |
-
outputs=[chatbot]
|
165 |
).then(
|
166 |
lambda: "",
|
167 |
None,
|
@@ -172,7 +162,7 @@ def create_deepseek_interface():
|
|
172 |
msg.submit(
|
173 |
process_query,
|
174 |
inputs=[msg, chatbot, api_key],
|
175 |
-
outputs=[chatbot]
|
176 |
).then(
|
177 |
lambda: "",
|
178 |
None,
|
|
|
4 |
import json
|
5 |
|
6 |
def create_deepseek_interface():
|
7 |
+
# ์ฑํ
๊ธฐ๋ก ์ํ๋ฅผ ์ ์ฅํ State ๊ฐ์ฒด
|
|
|
|
|
|
|
8 |
def query_deepseek(message, history, api_key):
|
9 |
if not api_key:
|
10 |
return history, "Fireworks AI API ํค๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
|
11 |
|
12 |
# API ์์ฒญ์ ์ํ ๋ํ ๊ธฐ๋ก ์ค๋น
|
13 |
messages = []
|
14 |
+
for user, assistant in history:
|
15 |
+
messages.append({"role": "user", "content": user})
|
16 |
+
messages.append({"role": "assistant", "content": assistant})
|
17 |
|
18 |
# ์ ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
19 |
messages.append({"role": "user", "content": message})
|
|
|
45 |
result = response.json()
|
46 |
assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
47 |
|
48 |
+
# ๋ํ ๊ธฐ๋ก ์
๋ฐ์ดํธ (Gradio chatbot ํ์์ผ๋ก)
|
49 |
+
history.append((message, assistant_response))
|
50 |
+
return history, ""
|
|
|
|
|
51 |
except requests.exceptions.RequestException as e:
|
52 |
error_msg = f"API ์ค๋ฅ: {str(e)}"
|
53 |
+
if hasattr(e, 'response') and e.response and e.response.status_code == 401:
|
54 |
error_msg = "์ธ์ฆ ์คํจ. API ํค๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
55 |
return history, error_msg
|
56 |
|
|
|
64 |
"""
|
65 |
)
|
66 |
|
|
|
|
|
|
|
67 |
# ๋ฉ์ธ ๋ ์ด์์ (๋ ๊ฐ์ ์ด)
|
68 |
with gr.Row():
|
69 |
# ์ฌ์ด๋๋ฐ - ๋ชจ๋ธ ์ ๋ณด ๋ฐ API ํค
|
|
|
108 |
chatbot = gr.Chatbot(
|
109 |
height=500,
|
110 |
show_label=False,
|
111 |
+
container=True
|
|
|
112 |
)
|
113 |
|
114 |
# ์
๋ ฅ ์์ญ
|
|
|
138 |
# ํผ ์ ์ถ ์ฒ๋ฆฌ
|
139 |
def process_query(message, history, api_key):
|
140 |
if not message.strip():
|
141 |
+
return history, ""
|
142 |
|
143 |
updated_history, error = query_deepseek(message, history, api_key)
|
144 |
|
145 |
if error:
|
146 |
+
return history, f"**์ค๋ฅ:** {error}"
|
|
|
147 |
else:
|
148 |
+
return updated_history, ""
|
|
|
149 |
|
150 |
# ๋ฒํผ๊ณผ ๊ธฐ๋ฅ ์ฐ๊ฒฐ
|
151 |
submit.click(
|
152 |
process_query,
|
153 |
inputs=[msg, chatbot, api_key],
|
154 |
+
outputs=[chatbot, error_box]
|
155 |
).then(
|
156 |
lambda: "",
|
157 |
None,
|
|
|
162 |
msg.submit(
|
163 |
process_query,
|
164 |
inputs=[msg, chatbot, api_key],
|
165 |
+
outputs=[chatbot, error_box]
|
166 |
).then(
|
167 |
lambda: "",
|
168 |
None,
|