File size: 4,978 Bytes
3c82310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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)