Update app.py
Browse files
app.py
CHANGED
|
@@ -4,24 +4,24 @@ import requests
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
def create_deepseek_interface():
|
| 7 |
-
#
|
| 8 |
chat_history = []
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def query_deepseek(message, history, api_key):
|
| 12 |
if not api_key:
|
| 13 |
-
return history, "
|
| 14 |
|
| 15 |
-
#
|
| 16 |
messages = []
|
| 17 |
for h in history:
|
| 18 |
messages.append({"role": "user", "content": h[0]})
|
| 19 |
messages.append({"role": "assistant", "content": h[1]})
|
| 20 |
|
| 21 |
-
#
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
-
#
|
| 25 |
url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 26 |
payload = {
|
| 27 |
"model": "accounts/fireworks/models/deepseek-v3-0324",
|
|
@@ -40,74 +40,78 @@ def create_deepseek_interface():
|
|
| 40 |
}
|
| 41 |
|
| 42 |
try:
|
| 43 |
-
#
|
| 44 |
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
|
| 45 |
-
response.raise_for_status() #
|
| 46 |
|
| 47 |
-
#
|
| 48 |
result = response.json()
|
| 49 |
assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
history.
|
| 53 |
-
|
|
|
|
| 54 |
except requests.exceptions.RequestException as e:
|
| 55 |
-
error_msg = f"API
|
| 56 |
-
if response.status_code == 401:
|
| 57 |
-
error_msg = "
|
| 58 |
return history, error_msg
|
| 59 |
|
| 60 |
-
#
|
| 61 |
with gr.Blocks(theme="soft", fill_height=True) as demo:
|
| 62 |
-
#
|
| 63 |
gr.Markdown(
|
| 64 |
"""
|
| 65 |
-
# ๐ค DeepSeek V3
|
| 66 |
-
###
|
| 67 |
"""
|
| 68 |
)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
|
| 73 |
-
#
|
| 74 |
with gr.Row():
|
| 75 |
-
#
|
| 76 |
with gr.Column(scale=1):
|
| 77 |
gr.Markdown(
|
| 78 |
"""
|
| 79 |
-
## ๐
|
| 80 |
-
###
|
| 81 |
-
|
| 82 |
|
| 83 |
-
####
|
| 84 |
-
-
|
| 85 |
-
-
|
| 86 |
"""
|
| 87 |
)
|
| 88 |
|
| 89 |
-
# API
|
| 90 |
api_key = gr.Textbox(
|
| 91 |
-
label="Fireworks AI API
|
| 92 |
-
placeholder="
|
| 93 |
type="password"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
gr.Markdown(
|
| 98 |
"""
|
| 99 |
-
### ๐
|
| 100 |
-
-
|
| 101 |
-
-
|
| 102 |
-
-
|
| 103 |
-
-
|
| 104 |
-
-
|
| 105 |
"""
|
| 106 |
)
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
| 109 |
with gr.Column(scale=2):
|
| 110 |
-
#
|
| 111 |
chatbot = gr.Chatbot(
|
| 112 |
height=500,
|
| 113 |
show_label=False,
|
|
@@ -115,64 +119,69 @@ def create_deepseek_interface():
|
|
| 115 |
type="messages"
|
| 116 |
)
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
error_display = gr.Markdown("")
|
| 120 |
-
|
| 121 |
-
# Input area
|
| 122 |
with gr.Row():
|
| 123 |
msg = gr.Textbox(
|
| 124 |
-
label="
|
| 125 |
-
placeholder="
|
| 126 |
show_label=False,
|
| 127 |
scale=9
|
| 128 |
)
|
| 129 |
-
submit = gr.Button("
|
| 130 |
|
| 131 |
-
#
|
| 132 |
with gr.Row():
|
| 133 |
-
clear = gr.ClearButton([msg, chatbot], value="๐งน
|
| 134 |
|
| 135 |
-
#
|
| 136 |
gr.Examples(
|
| 137 |
examples=[
|
| 138 |
-
"
|
| 139 |
-
"
|
| 140 |
-
"
|
| 141 |
],
|
| 142 |
inputs=msg
|
| 143 |
)
|
| 144 |
|
| 145 |
-
#
|
| 146 |
-
def
|
| 147 |
if not message.strip():
|
| 148 |
-
return history
|
| 149 |
|
| 150 |
-
updated_history, error = query_deepseek(message, history
|
| 151 |
|
| 152 |
if error:
|
| 153 |
-
|
|
|
|
| 154 |
else:
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
-
#
|
| 158 |
submit.click(
|
| 159 |
-
|
| 160 |
inputs=[msg, chatbot, api_key],
|
| 161 |
-
outputs=[chatbot]
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
)
|
| 164 |
|
| 165 |
-
#
|
| 166 |
msg.submit(
|
| 167 |
-
|
| 168 |
inputs=[msg, chatbot, api_key],
|
| 169 |
-
outputs=[chatbot]
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
| 171 |
)
|
| 172 |
|
| 173 |
return demo
|
| 174 |
|
| 175 |
-
#
|
| 176 |
if __name__ == "__main__":
|
| 177 |
demo = create_deepseek_interface()
|
| 178 |
demo.launch(debug=True)
|
|
|
|
| 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 h in history:
|
| 18 |
messages.append({"role": "user", "content": h[0]})
|
| 19 |
messages.append({"role": "assistant", "content": h[1]})
|
| 20 |
|
| 21 |
+
# ์ ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
+
# API ์์ฒญ ์ค๋น
|
| 25 |
url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 26 |
payload = {
|
| 27 |
"model": "accounts/fireworks/models/deepseek-v3-0324",
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
try:
|
| 43 |
+
# API ์์ฒญ ์ ์ก
|
| 44 |
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
|
| 45 |
+
response.raise_for_status() # HTTP ์ค๋ฅ ๋ฐ์ ์ ์์ธ ๋ฐ์
|
| 46 |
|
| 47 |
+
# ์๋ต ์ถ์ถ
|
| 48 |
result = response.json()
|
| 49 |
assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 50 |
|
| 51 |
+
# ๋ํ ๊ธฐ๋ก ์
๋ฐ์ดํธ
|
| 52 |
+
new_history = history.copy()
|
| 53 |
+
new_history.append((message, assistant_response))
|
| 54 |
+
return new_history, ""
|
| 55 |
except requests.exceptions.RequestException as e:
|
| 56 |
+
error_msg = f"API ์ค๋ฅ: {str(e)}"
|
| 57 |
+
if hasattr(response, 'status_code') and response.status_code == 401:
|
| 58 |
+
error_msg = "์ธ์ฆ ์คํจ. API ํค๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
| 59 |
return history, error_msg
|
| 60 |
|
| 61 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 62 |
with gr.Blocks(theme="soft", fill_height=True) as demo:
|
| 63 |
+
# ํค๋ ์น์
|
| 64 |
gr.Markdown(
|
| 65 |
"""
|
| 66 |
+
# ๐ค DeepSeek V3 ์ถ๋ก ์ธํฐํ์ด์ค
|
| 67 |
+
### Fireworks AI๊ฐ ์ ๊ณตํ๋ ๊ณ ๊ธ AI ๋ชจ๋ธ
|
| 68 |
"""
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# ์ค๋ฅ ๋ฉ์์ง ์ํ
|
| 72 |
+
error_state = gr.State("")
|
| 73 |
|
| 74 |
+
# ๋ฉ์ธ ๋ ์ด์์ (๋ ๊ฐ์ ์ด)
|
| 75 |
with gr.Row():
|
| 76 |
+
# ์ฌ์ด๋๋ฐ - ๋ชจ๋ธ ์ ๋ณด ๋ฐ API ํค
|
| 77 |
with gr.Column(scale=1):
|
| 78 |
gr.Markdown(
|
| 79 |
"""
|
| 80 |
+
## ๐ ์ ๊ทผ ์ ์ด
|
| 81 |
+
### ์ถ๋ก ์ ๊ณต์
|
| 82 |
+
์ด ์ธํฐํ์ด์ค๋ Fireworks AI API๋ฅผ ํตํด ์ ๊ณต๋๋ DeepSeek-V3 ๋ชจ๋ธ์ ์ฐ๊ฒฐ๋ฉ๋๋ค.
|
| 83 |
|
| 84 |
+
#### ์ธ์ฆ
|
| 85 |
+
- ์๋์ Fireworks AI API ํค๋ฅผ ์
๋ ฅํ์ธ์
|
| 86 |
+
- ์๋-ํฌ-์๋ ์ํธํ๋ก ์์ ํ API ์ ๊ทผ
|
| 87 |
"""
|
| 88 |
)
|
| 89 |
|
| 90 |
+
# API ํค ์
๋ ฅ
|
| 91 |
api_key = gr.Textbox(
|
| 92 |
+
label="Fireworks AI API ํค",
|
| 93 |
+
placeholder="API ํค๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 94 |
type="password"
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# ๋ชจ๋ธ ์ธ๋ถ ์ ๋ณด ์น์
|
| 98 |
gr.Markdown(
|
| 99 |
"""
|
| 100 |
+
### ๐ ๋ชจ๋ธ ์ธ๋ถ ์ ๋ณด
|
| 101 |
+
- **๋ชจ๋ธ**: DeepSeek-V3-0324
|
| 102 |
+
- **์ ๊ณต์**: Fireworks AI
|
| 103 |
+
- **์ต๋ ํ ํฐ**: 20,480
|
| 104 |
+
- **์จ๋**: 0.6
|
| 105 |
+
- **๊ธฐ๋ฅ**: ๊ณ ๊ธ ์ธ์ด ์ดํด
|
| 106 |
"""
|
| 107 |
)
|
| 108 |
+
|
| 109 |
+
# ์ค๋ฅ ๋ฉ์์ง ํ์
|
| 110 |
+
error_box = gr.Markdown("")
|
| 111 |
|
| 112 |
+
# ๋ฉ์ธ ์ฝํ
์ธ ์์ญ
|
| 113 |
with gr.Column(scale=2):
|
| 114 |
+
# ์ฑํ
์ธํฐํ์ด์ค
|
| 115 |
chatbot = gr.Chatbot(
|
| 116 |
height=500,
|
| 117 |
show_label=False,
|
|
|
|
| 119 |
type="messages"
|
| 120 |
)
|
| 121 |
|
| 122 |
+
# ์
๋ ฅ ์์ญ
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
msg = gr.Textbox(
|
| 125 |
+
label="๋ฉ์์ง",
|
| 126 |
+
placeholder="์ฌ๊ธฐ์ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 127 |
show_label=False,
|
| 128 |
scale=9
|
| 129 |
)
|
| 130 |
+
submit = gr.Button("์ ์ก", variant="primary", scale=1)
|
| 131 |
|
| 132 |
+
# ๋ํ ์ด๊ธฐํ ๋ฒํผ
|
| 133 |
with gr.Row():
|
| 134 |
+
clear = gr.ClearButton([msg, chatbot], value="๐งน ๋ํ ์ด๊ธฐํ")
|
| 135 |
|
| 136 |
+
# ์์ ์ฟผ๋ฆฌ
|
| 137 |
gr.Examples(
|
| 138 |
examples=[
|
| 139 |
+
"๋ฅ๋ฌ๋์์ ํธ๋์คํฌ๋จธ์ RNN์ ์ฐจ์ด์ ์ ์ค๋ช
ํด์ฃผ์ธ์.",
|
| 140 |
+
"ํน์ ๋ฒ์ ๋ด์ ์์๋ฅผ ์ฐพ๋ ํ์ด์ฌ ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.",
|
| 141 |
+
"๊ฐํํ์ต์ ์ฃผ์ ๊ฐ๋
์ ์์ฝํด์ฃผ์ธ์."
|
| 142 |
],
|
| 143 |
inputs=msg
|
| 144 |
)
|
| 145 |
|
| 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 |
+
error_box.value = f"**์ค๋ฅ:** {error}"
|
| 155 |
+
return history
|
| 156 |
else:
|
| 157 |
+
error_box.value = ""
|
| 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,
|
| 168 |
+
[msg]
|
| 169 |
)
|
| 170 |
|
| 171 |
+
# Enter ํค ์ ์ถ ํ์ฉ
|
| 172 |
msg.submit(
|
| 173 |
+
process_query,
|
| 174 |
inputs=[msg, chatbot, api_key],
|
| 175 |
+
outputs=[chatbot]
|
| 176 |
+
).then(
|
| 177 |
+
lambda: "",
|
| 178 |
+
None,
|
| 179 |
+
[msg]
|
| 180 |
)
|
| 181 |
|
| 182 |
return demo
|
| 183 |
|
| 184 |
+
# ์ธํฐํ์ด์ค ์คํ
|
| 185 |
if __name__ == "__main__":
|
| 186 |
demo = create_deepseek_interface()
|
| 187 |
demo.launch(debug=True)
|