Spaces:
Runtime error
Runtime error
import gradio as gr | |
from duckduckgo_search import DDGS | |
from swarm import Swarm, Agent | |
from datetime import datetime | |
import re | |
current_date = datetime.now().strftime("%Y-%m") | |
client = Swarm() | |
def contains_chinese(text): | |
"""์ค๊ตญ์ด ๋ฌธ์ ํฌํจ ์ฌ๋ถ ํ์ธ""" | |
return any('\u4e00' <= char <= '\u9fff' for char in text) | |
def get_news_articles(topic, date=None): | |
print(f"Running DuckDuckGo news search for {topic}...") | |
ddg_api = DDGS() | |
search_query = f"{topic} {date if date else current_date}" | |
results = ddg_api.news(search_query, max_results=10) | |
if results: | |
# "https://www.msn.com" URL์ ํฌํจํ์ง ์์ ๋ด์ค๋ง ํํฐ๋ง | |
filtered_results = [ | |
result for result in results | |
if "https://www.msn.com" not in result['url'] | |
] | |
if filtered_results: | |
news_results = "\n\n".join([ | |
f"**์ ๋ชฉ**: {result['title']}\n**๋งํฌ**: {result['url']}\n**๋ด์ฉ**: {result['body']}" | |
for result in filtered_results | |
]) | |
return news_results | |
else: | |
return f"{topic}์ ๋ํ ๋ด์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค." | |
else: | |
return f"{topic}์ ๋ํ ๋ด์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค." | |
news_agent = Agent( | |
name="News Assistant", | |
instructions=""" | |
๋น์ ์ DuckDuckGo ๊ฒ์์ ์ฌ์ฉํ์ฌ ์ฃผ์ด์ง ์ฃผ์ ์ ๋ํ ์ค๋์ ์ต์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ์ ๊ณตํฉ๋๋ค. | |
๋ฐ๋์ ํ๊ตญ์ด๋ก๋ง ์๋ตํ์ญ์์ค. | |
์ค๊ตญ์ด๊ฐ ํฌํจ๋ ๋ด์ฉ์ด ์๋ค๋ฉด ํ๊ตญ์ด๋ก ๋ฒ์ญํ์ฌ ์ ๊ณตํ์ญ์์ค. | |
ํญ์ ์๋ต์ ์ ์ฒด URL์ ํฌํจํ์ญ์์ค. | |
๊ฐ ๋ด์ค ๊ธฐ์ฌ์ ๋ํ ๋ ์ง๋ฅผ ํฌํจํ์ญ์์ค. | |
""", | |
functions=[get_news_articles], | |
model="qwen2.5:latest" | |
) | |
editor_agent = Agent( | |
name="Editor Assistant", | |
instructions=""" | |
๋น์ ์ ๋ด์ค ํธ์ง์์ ๋๋ค. ๋ค์ ๊ท์น์ ๋ฐ๋ผ ๋ด์ค๋ฅผ ํ๊ตญ์ด๋ก ํธ์งํด์ฃผ์ธ์: | |
1. ๊ฐ ๋ด์ค๋ ๋ณ๋์ ์น์ ์ผ๋ก ๊ตฌ๋ถํ์ฌ ์์ฑ | |
2. ๋ชจ๋ ์๋ณธ URL์ ๋ฐ๋์ ํฌํจ | |
3. ๊ฐ ๊ธฐ์ฌ์ ๋ ์ง ํฌํจ | |
4. ์ค์ ๋ด์ฉ์ ** ๋ณผ๋์ฒด ์ฒ๋ฆฌ | |
5. URL์ [์ ๋ชฉ](URL) ํ์์ผ๋ก ํฌํจ | |
6. ๋ฐ๋์ ํ๊ตญ์ด๋ก ์์ฑํ๊ณ , ์ค๊ตญ์ด๊ฐ ํฌํจ๋ ๋ด์ฉ์ ํ๊ตญ์ด๋ก ๋ฒ์ญํ์ฌ ์ ๊ณต | |
7. ๊ฐ ๊ธฐ์ฌ๋ 200๋จ์ด ์ด๋ด๋ก ์์ฝ | |
""", | |
model="qwen2.5:latest" | |
) | |
def process_news_request(topic, progress=gr.Progress(track_tqdm=True)): | |
try: | |
progress(0, desc="์์ํ๋ ์ค...") | |
progress(0.3, desc="๋ด์ค ๊ฒ์ ์ค...") | |
news_response = client.run( | |
agent=news_agent, | |
messages=[{"role": "user", "content": f"ํ๊ตญ์ด๋ก {topic}์ ๋ํ ์ต์ ๋ด์ค๋ฅผ ์ฐพ์์ฃผ์ธ์."}], | |
) | |
raw_news = news_response.messages[-1]["content"] | |
# ์๋ณธ ๋ด์ค์์ ์ค๊ตญ์ด๊ฐ ํฌํจ๋ ํญ๋ชฉ ์ ๊ฑฐ | |
raw_news_list = raw_news.split("\n\n") | |
filtered_news_list = [news for news in raw_news_list if not contains_chinese(news)] | |
# MSN URL์ด ํฌํจ๋ ํญ๋ชฉ๋ ์ ๊ฑฐ | |
filtered_news_list = [news for news in filtered_news_list if "https://www.msn.com" not in news] | |
filtered_news = "\n\n".join(filtered_news_list) | |
progress(0.6, desc="๋ด์ค ํธ์ง ์ค...") | |
edited_news_response = client.run( | |
agent=editor_agent, | |
messages=[{"role": "user", "content": f"๋ค์ ๋ด์ค๋ฅผ ํ๊ตญ์ด๋ก ํธ์งํด์ฃผ์ธ์:\n{filtered_news}"}], | |
) | |
progress(1.0, desc="์๋ฃ!") | |
return edited_news_response.messages[-1]["content"] | |
except Exception as e: | |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}\n\n๋๋ฒ๊ทธ ์ ๋ณด: {type(e).__name__}" | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# ๐ค Swarm AI ๋ด์ค ์์ด์ ํธ") | |
with gr.Row(): | |
topic_input = gr.Textbox( | |
label="๊ฒ์ํ ์ฃผ์ ๋ฅผ ์ ๋ ฅํ์ธ์", | |
placeholder="์: AI, SpaceX, Blockchain...", | |
lines=1 | |
) | |
with gr.Row(): | |
submit_btn = gr.Button("๊ฒ์", variant="primary") | |
with gr.Row(): | |
result_output = gr.Markdown( | |
value="Swarm AI๋ฅผ ํ์ฉํด์ Ollama qwen2.5 ๋ชจ๋ธ์ด ์ฌ์ฉ์๊ฐ ์์ฒญํ ์ต์ ๋ด์ค๋ฅผ ๊ฒ์ํ๊ณ ํ๊ตญ์ด๋ก ์์ฝํด๋๋ฆฝ๋๋ค.", | |
label="AI๊ฐ ์ ๋ฆฌํ ๋ด์ค" | |
) | |
examples = gr.Examples( | |
examples=[ | |
["์ธ๊ณต์ง๋ฅ"], | |
["ํ ์ฌ๋ผ"], | |
["OpenAI"], | |
["๊ธฐํ๋ณํ"] | |
], | |
inputs=topic_input | |
) | |
submit_btn.click( | |
fn=process_news_request, | |
inputs=topic_input, | |
outputs=result_output | |
) | |
topic_input.submit( | |
fn=process_news_request, | |
inputs=topic_input, | |
outputs=result_output | |
) | |
if __name__ == "__main__": | |
demo.queue().launch(share=True) |