Swarm_AI_Ollama / app.py
JAMESPARK3's picture
Create app.py
3c82310 verified
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)