Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from duckduckgo_search import DDGS
|
| 3 |
+
from swarm import Swarm, Agent
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
current_date = datetime.now().strftime("%Y-%m")
|
| 8 |
+
client = Swarm()
|
| 9 |
+
|
| 10 |
+
def contains_chinese(text):
|
| 11 |
+
"""์ค๊ตญ์ด ๋ฌธ์ ํฌํจ ์ฌ๋ถ ํ์ธ"""
|
| 12 |
+
return any('\u4e00' <= char <= '\u9fff' for char in text)
|
| 13 |
+
|
| 14 |
+
def get_news_articles(topic, date=None):
|
| 15 |
+
print(f"Running DuckDuckGo news search for {topic}...")
|
| 16 |
+
ddg_api = DDGS()
|
| 17 |
+
search_query = f"{topic} {date if date else current_date}"
|
| 18 |
+
results = ddg_api.news(search_query, max_results=10)
|
| 19 |
+
if results:
|
| 20 |
+
# "https://www.msn.com" URL์ ํฌํจํ์ง ์์ ๋ด์ค๋ง ํํฐ๋ง
|
| 21 |
+
filtered_results = [
|
| 22 |
+
result for result in results
|
| 23 |
+
if "https://www.msn.com" not in result['url']
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
if filtered_results:
|
| 27 |
+
news_results = "\n\n".join([
|
| 28 |
+
f"**์ ๋ชฉ**: {result['title']}\n**๋งํฌ**: {result['url']}\n**๋ด์ฉ**: {result['body']}"
|
| 29 |
+
for result in filtered_results
|
| 30 |
+
])
|
| 31 |
+
return news_results
|
| 32 |
+
else:
|
| 33 |
+
return f"{topic}์ ๋ํ ๋ด์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 34 |
+
else:
|
| 35 |
+
return f"{topic}์ ๋ํ ๋ด์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 36 |
+
|
| 37 |
+
news_agent = Agent(
|
| 38 |
+
name="News Assistant",
|
| 39 |
+
instructions="""
|
| 40 |
+
๋น์ ์ DuckDuckGo ๊ฒ์์ ์ฌ์ฉํ์ฌ ์ฃผ์ด์ง ์ฃผ์ ์ ๋ํ ์ค๋์ ์ต์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ์ ๊ณตํฉ๋๋ค.
|
| 41 |
+
๋ฐ๋์ ํ๊ตญ์ด๋ก๋ง ์๋ตํ์ญ์์ค.
|
| 42 |
+
์ค๊ตญ์ด๊ฐ ํฌํจ๋ ๋ด์ฉ์ด ์๋ค๋ฉด ํ๊ตญ์ด๋ก ๋ฒ์ญํ์ฌ ์ ๊ณตํ์ญ์์ค.
|
| 43 |
+
ํญ์ ์๋ต์ ์ ์ฒด URL์ ํฌํจํ์ญ์์ค.
|
| 44 |
+
๊ฐ ๋ด์ค ๊ธฐ์ฌ์ ๋ํ ๋ ์ง๋ฅผ ํฌํจํ์ญ์์ค.
|
| 45 |
+
""",
|
| 46 |
+
functions=[get_news_articles],
|
| 47 |
+
model="qwen2.5:latest"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
editor_agent = Agent(
|
| 51 |
+
name="Editor Assistant",
|
| 52 |
+
instructions="""
|
| 53 |
+
๋น์ ์ ๋ด์ค ํธ์ง์์
๋๋ค. ๋ค์ ๊ท์น์ ๋ฐ๋ผ ๋ด์ค๋ฅผ ํ๊ตญ์ด๋ก ํธ์งํด์ฃผ์ธ์:
|
| 54 |
+
1. ๊ฐ ๋ด์ค๋ ๋ณ๋์ ์น์
์ผ๋ก ๊ตฌ๋ถํ์ฌ ์์ฑ
|
| 55 |
+
2. ๋ชจ๋ ์๋ณธ URL์ ๋ฐ๋์ ํฌํจ
|
| 56 |
+
3. ๊ฐ ๊ธฐ์ฌ์ ๋ ์ง ํฌํจ
|
| 57 |
+
4. ์ค์ ๋ด์ฉ์ ** ๋ณผ๋์ฒด ์ฒ๋ฆฌ
|
| 58 |
+
5. URL์ [์ ๋ชฉ](URL) ํ์์ผ๋ก ํฌํจ
|
| 59 |
+
6. ๋ฐ๋์ ํ๊ตญ์ด๋ก ์์ฑํ๊ณ , ์ค๊ตญ์ด๊ฐ ํฌํจ๋ ๋ด์ฉ์ ํ๊ตญ์ด๋ก ๋ฒ์ญํ์ฌ ์ ๊ณต
|
| 60 |
+
7. ๊ฐ ๊ธฐ์ฌ๋ 200๋จ์ด ์ด๋ด๋ก ์์ฝ
|
| 61 |
+
""",
|
| 62 |
+
model="qwen2.5:latest"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def process_news_request(topic, progress=gr.Progress(track_tqdm=True)):
|
| 66 |
+
try:
|
| 67 |
+
progress(0, desc="์์ํ๋ ์ค...")
|
| 68 |
+
|
| 69 |
+
progress(0.3, desc="๋ด์ค ๊ฒ์ ์ค...")
|
| 70 |
+
news_response = client.run(
|
| 71 |
+
agent=news_agent,
|
| 72 |
+
messages=[{"role": "user", "content": f"ํ๊ตญ์ด๋ก {topic}์ ๋ํ ์ต์ ๋ด์ค๋ฅผ ์ฐพ์์ฃผ์ธ์."}],
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
raw_news = news_response.messages[-1]["content"]
|
| 76 |
+
|
| 77 |
+
# ์๋ณธ ๋ด์ค์์ ์ค๊ตญ์ด๊ฐ ํฌํจ๋ ํญ๋ชฉ ์ ๊ฑฐ
|
| 78 |
+
raw_news_list = raw_news.split("\n\n")
|
| 79 |
+
filtered_news_list = [news for news in raw_news_list if not contains_chinese(news)]
|
| 80 |
+
|
| 81 |
+
# MSN URL์ด ํฌํจ๋ ํญ๋ชฉ๋ ์ ๊ฑฐ
|
| 82 |
+
filtered_news_list = [news for news in filtered_news_list if "https://www.msn.com" not in news]
|
| 83 |
+
|
| 84 |
+
filtered_news = "\n\n".join(filtered_news_list)
|
| 85 |
+
|
| 86 |
+
progress(0.6, desc="๋ด์ค ํธ์ง ์ค...")
|
| 87 |
+
|
| 88 |
+
edited_news_response = client.run(
|
| 89 |
+
agent=editor_agent,
|
| 90 |
+
messages=[{"role": "user", "content": f"๋ค์ ๋ด์ค๋ฅผ ํ๊ตญ์ด๋ก ํธ์งํด์ฃผ์ธ์:\n{filtered_news}"}],
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
progress(1.0, desc="์๋ฃ!")
|
| 94 |
+
return edited_news_response.messages[-1]["content"]
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}\n\n๋๋ฒ๊ทธ ์ ๋ณด: {type(e).__name__}"
|
| 97 |
+
|
| 98 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 99 |
+
gr.Markdown("# ๐ค Swarm AI ๋ด์ค ์์ด์ ํธ")
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
topic_input = gr.Textbox(
|
| 103 |
+
label="๊ฒ์ํ ์ฃผ์ ๋ฅผ ์
๋ ฅํ์ธ์",
|
| 104 |
+
placeholder="์: AI, SpaceX, Blockchain...",
|
| 105 |
+
lines=1
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
submit_btn = gr.Button("๊ฒ์", variant="primary")
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
result_output = gr.Markdown(
|
| 113 |
+
value="Swarm AI๋ฅผ ํ์ฉํด์ Ollama qwen2.5 ๋ชจ๋ธ์ด ์ฌ์ฉ์๊ฐ ์์ฒญํ ์ต์ ๋ด์ค๋ฅผ ๊ฒ์ํ๊ณ ํ๊ตญ์ด๋ก ์์ฝํด๋๋ฆฝ๋๋ค.",
|
| 114 |
+
label="AI๊ฐ ์ ๋ฆฌํ ๋ด์ค"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
examples = gr.Examples(
|
| 118 |
+
examples=[
|
| 119 |
+
["์ธ๊ณต์ง๋ฅ"],
|
| 120 |
+
["ํ
์ฌ๋ผ"],
|
| 121 |
+
["OpenAI"],
|
| 122 |
+
["๊ธฐํ๋ณํ"]
|
| 123 |
+
],
|
| 124 |
+
inputs=topic_input
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
submit_btn.click(
|
| 128 |
+
fn=process_news_request,
|
| 129 |
+
inputs=topic_input,
|
| 130 |
+
outputs=result_output
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
topic_input.submit(
|
| 134 |
+
fn=process_news_request,
|
| 135 |
+
inputs=topic_input,
|
| 136 |
+
outputs=result_output
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
demo.queue().launch(share=True)
|