Deepseek-v3-0324-Research / app-backup.py
openfree's picture
Rename app.py to app-backup.py
73fe1ee verified
raw
history blame
6.36 kB
import gradio as gr
import os
import requests
import json
def create_deepseek_interface():
# ์ฑ„ํŒ… ๊ธฐ๋ก ์ƒํƒœ๋ฅผ ์ €์žฅํ•  State ๊ฐ์ฒด
def query_deepseek(message, history, api_key):
if not api_key:
return history, "Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
# 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
}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
try:
# API ์š”์ฒญ ์ „์†ก
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
response.raise_for_status() # HTTP ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
# ์‘๋‹ต ์ถ”์ถœ
result = response.json()
assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
# ๋Œ€ํ™” ๊ธฐ๋ก ์—…๋ฐ์ดํŠธ (Gradio chatbot ํ˜•์‹์œผ๋กœ)
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
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
with gr.Blocks(theme="soft", fill_height=True) as demo:
# ํ—ค๋” ์„น์…˜
gr.Markdown(
"""
# ๐Ÿค– DeepSeek V3 ์ถ”๋ก  ์ธํ„ฐํŽ˜์ด์Šค
### Fireworks AI๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณ ๊ธ‰ AI ๋ชจ๋ธ
"""
)
# ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ (๋‘ ๊ฐœ์˜ ์—ด)
with gr.Row():
# ์‚ฌ์ด๋“œ๋ฐ” - ๋ชจ๋ธ ์ •๋ณด ๋ฐ API ํ‚ค
with gr.Column(scale=1):
gr.Markdown(
"""
## ๐Ÿ”‘ ์ ‘๊ทผ ์ œ์–ด
### ์ถ”๋ก  ์ œ๊ณต์ž
์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” Fireworks AI API๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋˜๋Š” DeepSeek-V3 ๋ชจ๋ธ์— ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
#### ์ธ์ฆ
- ์•„๋ž˜์— Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
- ์—”๋“œ-ํˆฌ-์—”๋“œ ์•”ํ˜ธํ™”๋กœ ์•ˆ์ „ํ•œ 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]
)
# Enter ํ‚ค ์ œ์ถœ ํ—ˆ์šฉ
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)