File size: 7,009 Bytes
1f1f6a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import gradio as gr
import os
import requests
import json
import time
from dotenv import load_dotenv
# .env ํ์ผ ๋ก๋ (์๋ ๊ฒฝ์ฐ)
load_dotenv()
def create_deepseek_interface():
# ํ๊ฒฝ ๋ณ์์์ API ํค ๊ฐ์ ธ์ค๊ธฐ
api_key = os.getenv("FW_API_KEY")
if not api_key:
print("๊ฒฝ๊ณ : FW_API_KEY ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
# ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก DeepSeek API ํธ์ถ ํจ์
def query_deepseek_streaming(message, history):
if not api_key:
yield history, "ํ๊ฒฝ ๋ณ์ FW_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ์๋ฒ์์ ํ๊ฒฝ ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
return
# API ์์ฒญ์ ์ํ ๋ํ ๊ธฐ๋ก ์ค๋น
messages = []
for user, assistant in history:
messages.append({"role": "user", "content": user})
messages.append({"role": "assistant", "content": assistant})
# ์ ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
messages.append({"role": "user", "content": message})
# API ์์ฒญ ์ค๋น
url = "https://api.fireworks.ai/inference/v1/chat/completions"
payload = {
"model": "accounts/fireworks/models/deepseek-v3-0324",
"max_tokens": 20480,
"top_p": 1,
"top_k": 40,
"presence_penalty": 0,
"frequency_penalty": 0,
"temperature": 0.6,
"messages": messages,
"stream": True # ์คํธ๋ฆฌ๋ฐ ํ์ฑํ
}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
try:
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์์ฒญ
response = requests.request("POST", url, headers=headers, data=json.dumps(payload), stream=True)
response.raise_for_status() # HTTP ์ค๋ฅ ๋ฐ์ ์ ์์ธ ๋ฐ์
# ๋ฉ์์ง๋ฅผ ์ถ๊ฐํ๊ณ ์ด๊ธฐ ๋น ์๋ต์ผ๋ก ์์
new_history = history.copy()
new_history.append((message, ""))
# ์๋ต ์ ์ฒด ํ
์คํธ
full_response = ""
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์ฒ๋ฆฌ
for line in response.iter_lines():
if line:
line_text = line.decode('utf-8')
# 'data: ' ์ ๋์ฌ ์ ๊ฑฐ
if line_text.startswith("data: "):
line_text = line_text[6:]
# ์คํธ๋ฆผ ์ข
๋ฃ ๋ฉ์์ง ํ์ธ
if line_text == "[DONE]":
break
try:
# JSON ํ์ฑ
chunk = json.loads(line_text)
chunk_content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
if chunk_content:
full_response += chunk_content
# ์ฑํ
๊ธฐ๋ก ์
๋ฐ์ดํธ
new_history[-1] = (message, full_response)
yield new_history, ""
except json.JSONDecodeError:
continue
# ์ต์ข
์๋ต ๋ฐํ
yield new_history, ""
except requests.exceptions.RequestException as e:
error_msg = f"API ์ค๋ฅ: {str(e)}"
if hasattr(e, 'response') and e.response and e.response.status_code == 401:
error_msg = "์ธ์ฆ ์คํจ. ํ๊ฒฝ ๋ณ์ FW_API_KEY๋ฅผ ํ์ธํด์ฃผ์ธ์."
yield history, error_msg
# Gradio ์ธํฐํ์ด์ค ์์ฑ
with gr.Blocks(theme="soft", fill_height=True) as demo:
# ํค๋ ์น์
gr.Markdown(
"""
# ๐ค DeepSeek V3 ์คํธ๋ฆฌ๋ฐ ์ธํฐํ์ด์ค
### Fireworks AI๊ฐ ์ ๊ณตํ๋ ๊ณ ๊ธ AI ๋ชจ๋ธ - ์ค์๊ฐ ์๋ต ์ง์
"""
)
# ๋ฉ์ธ ๋ ์ด์์ (๋ ๊ฐ์ ์ด)
with gr.Row():
# ์ฌ์ด๋๋ฐ - ๋ชจ๋ธ ์ ๋ณด
with gr.Column(scale=1):
gr.Markdown(
"""
## ๐ ๋ชจ๋ธ ์ธ๋ถ ์ ๋ณด
- **๋ชจ๋ธ**: DeepSeek-V3-0324
- **์ ๊ณต์**: Fireworks AI
- **์ต๋ ํ ํฐ**: 20,480
- **์จ๋**: 0.6
- **๊ธฐ๋ฅ**: ๊ณ ๊ธ ์ธ์ด ์ดํด์ ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์๋ต
> **์ฐธ๊ณ **: API ํค๋ ํ๊ฒฝ ๋ณ์(FW_API_KEY)์์ ์๋์ผ๋ก ๋ก๋๋ฉ๋๋ค.
"""
)
# ์ค๋ฅ ๋ฉ์์ง ํ์
error_box = gr.Markdown("")
# ๋ฉ์ธ ์ฝํ
์ธ ์์ญ
with gr.Column(scale=2):
# ์ฑํ
์ธํฐํ์ด์ค
chatbot = gr.Chatbot(
height=500,
show_label=False,
container=True,
bubble=True,
avatar_images=("๐ค", "๐ค")
)
# ์
๋ ฅ ์์ญ
with gr.Row():
msg = gr.Textbox(
label="๋ฉ์์ง",
placeholder="์ฌ๊ธฐ์ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์...",
show_label=False,
scale=9
)
submit = gr.Button("์ ์ก", variant="primary", scale=1)
# ๋ํ ์ด๊ธฐํ ๋ฒํผ
with gr.Row():
clear = gr.ClearButton([msg, chatbot], value="๐งน ๋ํ ์ด๊ธฐํ")
# ์์ ์ฟผ๋ฆฌ
gr.Examples(
examples=[
"๋ฅ๋ฌ๋์์ ํธ๋์คํฌ๋จธ์ RNN์ ์ฐจ์ด์ ์ ์ค๋ช
ํด์ฃผ์ธ์.",
"ํน์ ๋ฒ์ ๋ด์ ์์๋ฅผ ์ฐพ๋ ํ์ด์ฌ ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.",
"๊ฐํํ์ต์ ์ฃผ์ ๊ฐ๋
์ ์์ฝํด์ฃผ์ธ์."
],
inputs=msg
)
# ๋ฒํผ๊ณผ ๊ธฐ๋ฅ ์ฐ๊ฒฐ
submit.click(
query_deepseek_streaming,
inputs=[msg, chatbot],
outputs=[chatbot, error_box]
).then(
lambda: "",
None,
[msg]
)
# Enter ํค ์ ์ถ ํ์ฉ
msg.submit(
query_deepseek_streaming,
inputs=[msg, chatbot],
outputs=[chatbot, error_box]
).then(
lambda: "",
None,
[msg]
)
return demo
# ์ธํฐํ์ด์ค ์คํ
if __name__ == "__main__":
demo = create_deepseek_interface()
demo.launch(debug=True) |