openfree's picture
Update app.py
dc92774 verified
raw
history blame
9.3 kB
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")
serphouse_api_key = os.getenv("SERPHOUSE_API_KEY")
if not api_key:
print("๊ฒฝ๊ณ : FW_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
if not serphouse_api_key:
print("๊ฒฝ๊ณ : SERPHOUSE_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
# SerpHouse API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰ ํ•จ์ˆ˜
def search_with_serphouse(query):
if not serphouse_api_key:
return "SERPHOUSE_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
url = "https://api.serphouse.com/serp/live"
payload = {
"q": query,
"domain": "google.com",
"loc": "us",
"lang": "en",
"device": "desktop",
"serp_type": "web",
"page": 1,
"num": 5 # ์ƒ์œ„ 5๊ฐœ ๊ฒฐ๊ณผ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {serphouse_api_key}"
}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
search_results = response.json()
# ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํŒŒ์‹ฑ ๋ฐ ํฌ๋งทํŒ…
formatted_results = []
if "organic" in search_results and len(search_results["organic"]) > 0:
for result in search_results["organic"][:5]: # ์ƒ์œ„ 5๊ฐœ ๊ฒฐ๊ณผ๋งŒ ์‚ฌ์šฉ
title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
link = result.get("link", "#")
formatted_results.append(f"์ œ๋ชฉ: {title}\n๋‚ด์šฉ: {snippet}\n๋งํฌ: {link}\n\n")
return "".join(formatted_results)
else:
return "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
except Exception as e:
return f"๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
# ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์‹์œผ๋กœ DeepSeek API ํ˜ธ์ถœ ํ•จ์ˆ˜
def query_deepseek_streaming(message, history, use_deep_research):
if not api_key:
yield history, "ํ™˜๊ฒฝ ๋ณ€์ˆ˜ FW_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
return
search_context = ""
if use_deep_research:
# ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰
search_results = search_with_serphouse(message)
if not search_results.startswith("๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ") and not search_results.startswith("SERPHOUSE_API_KEY"):
search_context = f"๋‹ค์Œ์€ ์‚ฌ์šฉ์ž ์งˆ๋ฌธ๊ณผ ๊ด€๋ จ๋œ ์ตœ์‹  ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ •ํ™•ํ•˜๊ณ  ์ตœ์‹  ์ •๋ณด๊ฐ€ ๋ฐ˜์˜๋œ ์‘๋‹ต์„ ์ œ๊ณตํ•˜์„ธ์š”:\n\n{search_results}\n\n์‚ฌ์šฉ์ž ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜์„ธ์š”.\n\n"
# API ์š”์ฒญ์„ ์œ„ํ•œ ๋Œ€ํ™” ๊ธฐ๋ก ์ค€๋น„
messages = []
for user, assistant in history:
messages.append({"role": "user", "content": user})
messages.append({"role": "assistant", "content": assistant})
# ๊ฒ€์ƒ‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์žˆ์œผ๋ฉด ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
if search_context:
# DeepSeek ๋ชจ๋ธ์€ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
messages.insert(0, {"role": "system", "content": search_context})
# ์ƒˆ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
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():
# ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
chatbot = gr.Chatbot(
height=500,
show_label=False,
container=True
)
# Deep Research ํ† ๊ธ€ ์ถ”๊ฐ€
with gr.Row():
use_deep_research = gr.Checkbox(
label="Deep Research ํ™œ์„ฑํ™”",
info="์ตœ์‹  ์›น ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ํ™œ์šฉํ•œ ์‘๋‹ต ์ƒ์„ฑ"
)
# ์ž…๋ ฅ ์˜์—ญ
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
)
# ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
error_box = gr.Markdown("")
# ๋ฒ„ํŠผ๊ณผ ๊ธฐ๋Šฅ ์—ฐ๊ฒฐ
submit.click(
query_deepseek_streaming,
inputs=[msg, chatbot, use_deep_research],
outputs=[chatbot, error_box]
).then(
lambda: "",
None,
[msg]
)
# Enter ํ‚ค ์ œ์ถœ ํ—ˆ์šฉ
msg.submit(
query_deepseek_streaming,
inputs=[msg, chatbot, use_deep_research],
outputs=[chatbot, error_box]
).then(
lambda: "",
None,
[msg]
)
return demo
# ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
if __name__ == "__main__":
demo = create_deepseek_interface()
demo.launch(debug=True)