JAMESPARK3 commited on
Commit
3c82310
ยท
verified ยท
1 Parent(s): c8de837

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
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)