|
import gradio as gr |
|
import os |
|
import requests |
|
import json |
|
import time |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
def create_deepseek_interface(): |
|
|
|
api_key = os.getenv("FW_API_KEY") |
|
if not api_key: |
|
print("๊ฒฝ๊ณ : FW_API_KEY ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.") |
|
|
|
|
|
def query_deepseek_streaming(message, history): |
|
if not api_key: |
|
yield history, "ํ๊ฒฝ ๋ณ์ FW_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ์๋ฒ์์ ํ๊ฒฝ ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์." |
|
return |
|
|
|
|
|
messages = [] |
|
for user, assistant in history: |
|
messages.append({"role": "user", "content": user}) |
|
messages.append({"role": "assistant", "content": assistant}) |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
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() |
|
|
|
|
|
new_history = history.copy() |
|
new_history.append((message, "")) |
|
|
|
|
|
full_response = "" |
|
|
|
|
|
for line in response.iter_lines(): |
|
if line: |
|
line_text = line.decode('utf-8') |
|
|
|
|
|
if line_text.startswith("data: "): |
|
line_text = line_text[6:] |
|
|
|
|
|
if line_text == "[DONE]": |
|
break |
|
|
|
try: |
|
|
|
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 |
|
|
|
|
|
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] |
|
) |
|
|
|
|
|
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) |