|
import gradio as gr |
|
import os |
|
import requests |
|
import json |
|
|
|
def create_deepseek_interface(): |
|
|
|
def query_deepseek(message, history, api_key): |
|
if not api_key: |
|
return history, "Fireworks AI 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}) |
|
|
|
|
|
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 |
|
} |
|
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)) |
|
response.raise_for_status() |
|
|
|
|
|
result = response.json() |
|
assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "") |
|
|
|
|
|
history.append((message, assistant_response)) |
|
return 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 = "์ธ์ฆ ์คํจ. API ํค๋ฅผ ํ์ธํด์ฃผ์ธ์." |
|
return 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( |
|
""" |
|
## ๐ ์ ๊ทผ ์ ์ด |
|
### ์ถ๋ก ์ ๊ณต์ |
|
์ด ์ธํฐํ์ด์ค๋ Fireworks AI API๋ฅผ ํตํด ์ ๊ณต๋๋ DeepSeek-V3 ๋ชจ๋ธ์ ์ฐ๊ฒฐ๋ฉ๋๋ค. |
|
|
|
#### ์ธ์ฆ |
|
- ์๋์ Fireworks AI API ํค๋ฅผ ์
๋ ฅํ์ธ์ |
|
- ์๋-ํฌ-์๋ ์ํธํ๋ก ์์ ํ API ์ ๊ทผ |
|
""" |
|
) |
|
|
|
|
|
api_key = gr.Textbox( |
|
label="Fireworks AI API ํค", |
|
placeholder="API ํค๋ฅผ ์
๋ ฅํ์ธ์...", |
|
type="password" |
|
) |
|
|
|
|
|
gr.Markdown( |
|
""" |
|
### ๐ ๋ชจ๋ธ ์ธ๋ถ ์ ๋ณด |
|
- **๋ชจ๋ธ**: DeepSeek-V3-0324 |
|
- **์ ๊ณต์**: Fireworks AI |
|
- **์ต๋ ํ ํฐ**: 20,480 |
|
- **์จ๋**: 0.6 |
|
- **๊ธฐ๋ฅ**: ๊ณ ๊ธ ์ธ์ด ์ดํด |
|
""" |
|
) |
|
|
|
|
|
error_box = gr.Markdown("") |
|
|
|
|
|
with gr.Column(scale=2): |
|
|
|
chatbot = gr.Chatbot( |
|
height=500, |
|
show_label=False, |
|
container=True |
|
) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
def process_query(message, history, api_key): |
|
if not message.strip(): |
|
return history, "" |
|
|
|
updated_history, error = query_deepseek(message, history, api_key) |
|
|
|
if error: |
|
return history, f"**์ค๋ฅ:** {error}" |
|
else: |
|
return updated_history, "" |
|
|
|
|
|
submit.click( |
|
process_query, |
|
inputs=[msg, chatbot, api_key], |
|
outputs=[chatbot, error_box] |
|
).then( |
|
lambda: "", |
|
None, |
|
[msg] |
|
) |
|
|
|
|
|
msg.submit( |
|
process_query, |
|
inputs=[msg, chatbot, api_key], |
|
outputs=[chatbot, error_box] |
|
).then( |
|
lambda: "", |
|
None, |
|
[msg] |
|
) |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
demo = create_deepseek_interface() |
|
demo.launch(debug=True) |