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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
from datetime import datetime, timedelta
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
|
8 |
+
MAX_COUNTRY_RESULTS = 100 # ๊ตญ๊ฐ๋ณ ์ต๋ ๊ฒฐ๊ณผ ์
|
9 |
+
MAX_GLOBAL_RESULTS = 1000 # ์ ์ธ๊ณ ์ต๋ ๊ฒฐ๊ณผ ์
|
10 |
+
|
11 |
+
def create_article_components(max_results):
|
12 |
+
article_components = []
|
13 |
+
for i in range(max_results):
|
14 |
+
with gr.Group(visible=False) as article_group:
|
15 |
+
title = gr.Markdown()
|
16 |
+
image = gr.Image(width=200, height=150)
|
17 |
+
snippet = gr.Markdown()
|
18 |
+
info = gr.Markdown()
|
19 |
+
|
20 |
+
article_components.append({
|
21 |
+
'group': article_group,
|
22 |
+
'title': title,
|
23 |
+
'image': image,
|
24 |
+
'snippet': snippet,
|
25 |
+
'info': info,
|
26 |
+
'index': i,
|
27 |
+
})
|
28 |
+
return article_components
|
29 |
+
|
30 |
+
API_KEY = os.getenv("SERPHOUSE_API_KEY")
|
31 |
+
# hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
32 |
+
|
33 |
+
# ๊ตญ๊ฐ๋ณ ์ธ์ด ์ฝ๋ ๋งคํ
|
34 |
+
COUNTRY_LANGUAGES = {
|
35 |
+
"United States": "en",
|
36 |
+
"United Kingdom": "en",
|
37 |
+
"Taiwan": "zh-TW", # ๋๋ง์ด(๋ฒ์ฒด ์ค๊ตญ์ด)
|
38 |
+
"Canada": "en",
|
39 |
+
"Australia": "en",
|
40 |
+
"Germany": "de",
|
41 |
+
"France": "fr",
|
42 |
+
"Japan": "ja",
|
43 |
+
"South Korea": "ko",
|
44 |
+
"China": "zh",
|
45 |
+
"India": "hi",
|
46 |
+
"Brazil": "pt",
|
47 |
+
"Mexico": "es",
|
48 |
+
"Russia": "ru",
|
49 |
+
"Italy": "it",
|
50 |
+
"Spain": "es",
|
51 |
+
"Netherlands": "nl",
|
52 |
+
"Singapore": "en",
|
53 |
+
"Hong Kong": "zh-HK",
|
54 |
+
"Indonesia": "id",
|
55 |
+
"Malaysia": "ms",
|
56 |
+
"Philippines": "tl",
|
57 |
+
"Thailand": "th",
|
58 |
+
"Vietnam": "vi",
|
59 |
+
"Belgium": "nl",
|
60 |
+
"Denmark": "da",
|
61 |
+
"Finland": "fi",
|
62 |
+
"Ireland": "en",
|
63 |
+
"Norway": "no",
|
64 |
+
"Poland": "pl",
|
65 |
+
"Sweden": "sv",
|
66 |
+
"Switzerland": "de",
|
67 |
+
"Austria": "de",
|
68 |
+
"Czech Republic": "cs",
|
69 |
+
"Greece": "el",
|
70 |
+
"Hungary": "hu",
|
71 |
+
"Portugal": "pt",
|
72 |
+
"Romania": "ro",
|
73 |
+
"Turkey": "tr",
|
74 |
+
"Israel": "he",
|
75 |
+
"Saudi Arabia": "ar",
|
76 |
+
"United Arab Emirates": "ar",
|
77 |
+
"South Africa": "en",
|
78 |
+
"Argentina": "es",
|
79 |
+
"Chile": "es",
|
80 |
+
"Colombia": "es",
|
81 |
+
"Peru": "es",
|
82 |
+
"Venezuela": "es",
|
83 |
+
"New Zealand": "en",
|
84 |
+
"Bangladesh": "bn",
|
85 |
+
"Pakistan": "ur",
|
86 |
+
"Egypt": "ar",
|
87 |
+
"Morocco": "ar",
|
88 |
+
"Nigeria": "en",
|
89 |
+
"Kenya": "sw",
|
90 |
+
"Ukraine": "uk",
|
91 |
+
"Croatia": "hr",
|
92 |
+
"Slovakia": "sk",
|
93 |
+
"Bulgaria": "bg",
|
94 |
+
"Serbia": "sr",
|
95 |
+
"Estonia": "et",
|
96 |
+
"Latvia": "lv",
|
97 |
+
"Lithuania": "lt",
|
98 |
+
"Slovenia": "sl",
|
99 |
+
"Luxembourg": "fr",
|
100 |
+
"Malta": "mt",
|
101 |
+
"Cyprus": "el",
|
102 |
+
"Iceland": "is"
|
103 |
+
}
|
104 |
+
|
105 |
+
COUNTRY_LOCATIONS = {
|
106 |
+
"United States": "United States",
|
107 |
+
"United Kingdom": "United Kingdom",
|
108 |
+
"Taiwan": "Taiwan", # ๊ตญ๊ฐ๋ช
์ฌ์ฉ
|
109 |
+
"Canada": "Canada",
|
110 |
+
"Australia": "Australia",
|
111 |
+
"Germany": "Germany",
|
112 |
+
"France": "France",
|
113 |
+
"Japan": "Japan",
|
114 |
+
"South Korea": "South Korea",
|
115 |
+
"China": "China",
|
116 |
+
"India": "India",
|
117 |
+
"Brazil": "Brazil",
|
118 |
+
"Mexico": "Mexico",
|
119 |
+
"Russia": "Russia",
|
120 |
+
"Italy": "Italy",
|
121 |
+
"Spain": "Spain",
|
122 |
+
"Netherlands": "Netherlands",
|
123 |
+
"Singapore": "Singapore",
|
124 |
+
"Hong Kong": "Hong Kong",
|
125 |
+
"Indonesia": "Indonesia",
|
126 |
+
"Malaysia": "Malaysia",
|
127 |
+
"Philippines": "Philippines",
|
128 |
+
"Thailand": "Thailand",
|
129 |
+
"Vietnam": "Vietnam",
|
130 |
+
"Belgium": "Belgium",
|
131 |
+
"Denmark": "Denmark",
|
132 |
+
"Finland": "Finland",
|
133 |
+
"Ireland": "Ireland",
|
134 |
+
"Norway": "Norway",
|
135 |
+
"Poland": "Poland",
|
136 |
+
"Sweden": "Sweden",
|
137 |
+
"Switzerland": "Switzerland",
|
138 |
+
"Austria": "Austria",
|
139 |
+
"Czech Republic": "Czech Republic",
|
140 |
+
"Greece": "Greece",
|
141 |
+
"Hungary": "Hungary",
|
142 |
+
"Portugal": "Portugal",
|
143 |
+
"Romania": "Romania",
|
144 |
+
"Turkey": "Turkey",
|
145 |
+
"Israel": "Israel",
|
146 |
+
"Saudi Arabia": "Saudi Arabia",
|
147 |
+
"United Arab Emirates": "United Arab Emirates",
|
148 |
+
"South Africa": "South Africa",
|
149 |
+
"Argentina": "Argentina",
|
150 |
+
"Chile": "Chile",
|
151 |
+
"Colombia": "Colombia",
|
152 |
+
"Peru": "Peru",
|
153 |
+
"Venezuela": "Venezuela",
|
154 |
+
"New Zealand": "New Zealand",
|
155 |
+
"Bangladesh": "Bangladesh",
|
156 |
+
"Pakistan": "Pakistan",
|
157 |
+
"Egypt": "Egypt",
|
158 |
+
"Morocco": "Morocco",
|
159 |
+
"Nigeria": "Nigeria",
|
160 |
+
"Kenya": "Kenya",
|
161 |
+
"Ukraine": "Ukraine",
|
162 |
+
"Croatia": "Croatia",
|
163 |
+
"Slovakia": "Slovakia",
|
164 |
+
"Bulgaria": "Bulgaria",
|
165 |
+
"Serbia": "Serbia",
|
166 |
+
"Estonia": "Estonia",
|
167 |
+
"Latvia": "Latvia",
|
168 |
+
"Lithuania": "Lithuania",
|
169 |
+
"Slovenia": "Slovenia",
|
170 |
+
"Luxembourg": "Luxembourg",
|
171 |
+
"Malta": "Malta",
|
172 |
+
"Cyprus": "Cyprus",
|
173 |
+
"Iceland": "Iceland"
|
174 |
+
}
|
175 |
+
|
176 |
+
MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
|
177 |
+
|
178 |
+
def translate_query(query, country):
|
179 |
+
try:
|
180 |
+
# ์์ด ์
๋ ฅ ํ์ธ
|
181 |
+
if is_english(query):
|
182 |
+
print(f"์์ด ๊ฒ์์ด ๊ฐ์ง - ์๋ณธ ์ฌ์ฉ: {query}")
|
183 |
+
return query
|
184 |
+
|
185 |
+
# ์ ํ๋ ๊ตญ๊ฐ๊ฐ ๋ฒ์ญ ์ง์ ๊ตญ๊ฐ์ธ ๊ฒฝ์ฐ
|
186 |
+
if country in COUNTRY_LANGUAGES:
|
187 |
+
# South Korea ์ ํ์ ํ๊ธ ์
๋ ฅ์ ๊ทธ๋๋ก ์ฌ์ฉ
|
188 |
+
if country == "South Korea":
|
189 |
+
print(f"ํ๊ตญ ์ ํ - ์๋ณธ ์ฌ์ฉ: {query}")
|
190 |
+
return query
|
191 |
+
|
192 |
+
target_lang = COUNTRY_LANGUAGES[country]
|
193 |
+
print(f"๋ฒ์ญ ์๋: {query} -> {country}({target_lang})")
|
194 |
+
|
195 |
+
url = f"https://translate.googleapis.com/translate_a/single"
|
196 |
+
params = {
|
197 |
+
"client": "gtx",
|
198 |
+
"sl": "auto",
|
199 |
+
"tl": target_lang,
|
200 |
+
"dt": "t",
|
201 |
+
"q": query
|
202 |
+
}
|
203 |
+
|
204 |
+
response = requests.get(url, params=params)
|
205 |
+
translated_text = response.json()[0][0][0]
|
206 |
+
print(f"๋ฒ์ญ ์๋ฃ: {query} -> {translated_text} ({country})")
|
207 |
+
return translated_text
|
208 |
+
|
209 |
+
return query
|
210 |
+
|
211 |
+
except Exception as e:
|
212 |
+
print(f"๋ฒ์ญ ์ค๋ฅ: {str(e)}")
|
213 |
+
return query
|
214 |
+
|
215 |
+
def translate_to_korean(text):
|
216 |
+
try:
|
217 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
218 |
+
params = {
|
219 |
+
"client": "gtx",
|
220 |
+
"sl": "auto",
|
221 |
+
"tl": "ko",
|
222 |
+
"dt": "t",
|
223 |
+
"q": text
|
224 |
+
}
|
225 |
+
|
226 |
+
response = requests.get(url, params=params)
|
227 |
+
translated_text = response.json()[0][0][0]
|
228 |
+
return translated_text
|
229 |
+
except Exception as e:
|
230 |
+
print(f"ํ๊ธ ๋ฒ์ญ ์ค๋ฅ: {str(e)}")
|
231 |
+
return text
|
232 |
+
|
233 |
+
def is_english(text):
|
234 |
+
return all(ord(char) < 128 for char in text.replace(' ', '').replace('-', '').replace('_', ''))
|
235 |
+
|
236 |
+
def is_korean(text):
|
237 |
+
return any('\uAC00' <= char <= '\uD7A3' for char in text)
|
238 |
+
|
239 |
+
def search_serphouse(query, country, page=1, num_result=10):
|
240 |
+
url = "https://api.serphouse.com/serp/live"
|
241 |
+
|
242 |
+
now = datetime.utcnow()
|
243 |
+
yesterday = now - timedelta(days=1)
|
244 |
+
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
245 |
+
|
246 |
+
translated_query = translate_query(query, country)
|
247 |
+
print(f"Original query: {query}")
|
248 |
+
print(f"Translated query: {translated_query}")
|
249 |
+
|
250 |
+
payload = {
|
251 |
+
"data": {
|
252 |
+
"q": translated_query,
|
253 |
+
"domain": "google.com",
|
254 |
+
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
255 |
+
"lang": COUNTRY_LANGUAGES.get(country, "en"),
|
256 |
+
"device": "desktop",
|
257 |
+
"serp_type": "news",
|
258 |
+
"page": "1",
|
259 |
+
"num": "10",
|
260 |
+
"date_range": date_range,
|
261 |
+
"sort_by": "date"
|
262 |
+
}
|
263 |
+
}
|
264 |
+
|
265 |
+
headers = {
|
266 |
+
"accept": "application/json",
|
267 |
+
"content-type": "application/json",
|
268 |
+
"authorization": f"Bearer {API_KEY}"
|
269 |
+
}
|
270 |
+
|
271 |
+
try:
|
272 |
+
response = requests.post(url, json=payload, headers=headers)
|
273 |
+
print("Request payload:", json.dumps(payload, indent=2, ensure_ascii=False))
|
274 |
+
print("Response status:", response.status_code)
|
275 |
+
|
276 |
+
response.raise_for_status()
|
277 |
+
return {"results": response.json(), "translated_query": translated_query}
|
278 |
+
except requests.RequestException as e:
|
279 |
+
return {"error": f"Error: {str(e)}", "translated_query": query}
|
280 |
+
|
281 |
+
def format_results_from_raw(response_data):
|
282 |
+
if "error" in response_data:
|
283 |
+
return "Error: " + response_data["error"], []
|
284 |
+
|
285 |
+
try:
|
286 |
+
results = response_data["results"]
|
287 |
+
translated_query = response_data["translated_query"]
|
288 |
+
|
289 |
+
news_results = results.get('results', {}).get('results', {}).get('news', [])
|
290 |
+
if not news_results:
|
291 |
+
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.", []
|
292 |
+
|
293 |
+
articles = []
|
294 |
+
for idx, result in enumerate(news_results, 1):
|
295 |
+
articles.append({
|
296 |
+
"index": idx,
|
297 |
+
"title": result.get("title", "์ ๋ชฉ ์์"),
|
298 |
+
"link": result.get("url", result.get("link", "#")),
|
299 |
+
"snippet": result.get("snippet", "๋ด์ฉ ์์"),
|
300 |
+
"channel": result.get("channel", result.get("source", "์ ์ ์์")),
|
301 |
+
"time": result.get("time", result.get("date", "์ ์ ์๋ ์๊ฐ")),
|
302 |
+
"image_url": result.get("img", result.get("thumbnail", "")),
|
303 |
+
"translated_query": translated_query
|
304 |
+
})
|
305 |
+
return "", articles
|
306 |
+
except Exception as e:
|
307 |
+
return f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}", []
|
308 |
+
|
309 |
+
def serphouse_search(query, country):
|
310 |
+
response_data = search_serphouse(query, country)
|
311 |
+
return format_results_from_raw(response_data)
|
312 |
+
|
313 |
+
css = """
|
314 |
+
footer {visibility: hidden;}
|
315 |
+
"""
|
316 |
+
|
317 |
+
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as iface:
|
318 |
+
with gr.Tabs():
|
319 |
+
# ๊ตญ๊ฐ๋ณ ํญ
|
320 |
+
with gr.Tab("๊ตญ๊ฐ๋ณ"):
|
321 |
+
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ(67๊ฐ๊ตญ)๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 100๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
322 |
+
gr.Markdown("๊ตญ๊ฐ ์ ํํ ๊ฒ์์ด์ 'ํ๊ธ'์ ์
๋ ฅํ๋ฉด ํ์ง ์ธ์ด๋ก ๋ฒ์ญ๋์ด ๊ฒ์ํฉ๋๋ค. ์: 'Taiwan' ๊ตญ๊ฐ ์ ํํ '์ผ์ฑ' ์
๋ ฅ์ 'ไธๆ'์ผ๋ก ์๋ ๊ฒ์")
|
323 |
+
|
324 |
+
with gr.Column():
|
325 |
+
with gr.Row():
|
326 |
+
query = gr.Textbox(label="๊ฒ์์ด")
|
327 |
+
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
328 |
+
|
329 |
+
# ๊ฒ์ ์ํ ๋ฉ์์ง
|
330 |
+
status_message = gr.Markdown("", visible=True)
|
331 |
+
|
332 |
+
# ๋ฒ์ญ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์ปดํฌ๋ํธ
|
333 |
+
translated_query_display = gr.Markdown(visible=False)
|
334 |
+
|
335 |
+
search_button = gr.Button("๊ฒ์", variant="primary")
|
336 |
+
|
337 |
+
progress = gr.Progress()
|
338 |
+
articles_state = gr.State([])
|
339 |
+
|
340 |
+
article_components = []
|
341 |
+
for i in range(100):
|
342 |
+
with gr.Group(visible=False) as article_group:
|
343 |
+
title = gr.Markdown()
|
344 |
+
image = gr.Image(width=200, height=150)
|
345 |
+
snippet = gr.Markdown()
|
346 |
+
info = gr.Markdown()
|
347 |
+
|
348 |
+
article_components.append({
|
349 |
+
'group': article_group,
|
350 |
+
'title': title,
|
351 |
+
'image': image,
|
352 |
+
'snippet': snippet,
|
353 |
+
'info': info,
|
354 |
+
'index': i,
|
355 |
+
})
|
356 |
+
|
357 |
+
# ์ ์ธ๊ณ ํญ
|
358 |
+
with gr.Tab("์ ์ธ๊ณ"):
|
359 |
+
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๋ฉด 67๊ฐ๊ตญ์ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 1000๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
360 |
+
|
361 |
+
with gr.Column():
|
362 |
+
query_global = gr.Textbox(label="๊ฒ์์ด")
|
363 |
+
status_message_global = gr.Markdown("", visible=True)
|
364 |
+
translated_query_display_global = gr.Markdown(visible=False)
|
365 |
+
search_button_global = gr.Button("์ ์ธ๊ณ ๊ฒ์", variant="primary")
|
366 |
+
|
367 |
+
progress_global = gr.Progress()
|
368 |
+
articles_state_global = gr.State([])
|
369 |
+
|
370 |
+
global_article_components = []
|
371 |
+
for i in range(1000):
|
372 |
+
with gr.Group(visible=False) as article_group:
|
373 |
+
title = gr.Markdown()
|
374 |
+
image = gr.Image(width=200, height=150)
|
375 |
+
snippet = gr.Markdown()
|
376 |
+
info = gr.Markdown()
|
377 |
+
|
378 |
+
global_article_components.append({
|
379 |
+
'group': article_group,
|
380 |
+
'title': title,
|
381 |
+
'image': image,
|
382 |
+
'snippet': snippet,
|
383 |
+
'info': info,
|
384 |
+
'index': i,
|
385 |
+
})
|
386 |
+
|
387 |
+
def search_and_display(query, country, articles_state, progress=gr.Progress()):
|
388 |
+
# ๊ฒ์ ์ํ ๋ฉ์์ง ์
๋ฐ์ดํธ
|
389 |
+
status_msg = "๊ฒ์์ ์งํ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ฆฌ์ธ์..."
|
390 |
+
|
391 |
+
progress(0, desc="๊ฒ์์ด ๋ฒ์ญ ์ค...")
|
392 |
+
|
393 |
+
# ๊ฒ์์ด ๋ฒ์ญ
|
394 |
+
translated_query = translate_query(query, country)
|
395 |
+
translated_display = f"**์๋ณธ ๊ฒ์์ด:** {query}\n**๋ฒ์ญ๋ ๊ฒ์์ด:** {translated_query}" if translated_query != query else f"**๊ฒ์์ด:** {query}"
|
396 |
+
|
397 |
+
progress(0.2, desc="๊ฒ์ ์์...")
|
398 |
+
error_message, articles = serphouse_search(query, country)
|
399 |
+
progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค...")
|
400 |
+
|
401 |
+
outputs = []
|
402 |
+
outputs.append(gr.update(value=status_msg, visible=True)) # ์ํ ๋ฉ์์ง
|
403 |
+
outputs.append(gr.update(value=translated_display, visible=True)) # ๋ฒ์ญ ๊ฒฐ๊ณผ
|
404 |
+
|
405 |
+
if error_message:
|
406 |
+
outputs.append(gr.update(value=error_message, visible=True))
|
407 |
+
for comp in article_components:
|
408 |
+
outputs.extend([
|
409 |
+
gr.update(visible=False), gr.update(), gr.update(),
|
410 |
+
gr.update(), gr.update()
|
411 |
+
])
|
412 |
+
articles_state = []
|
413 |
+
else:
|
414 |
+
outputs.append(gr.update(value="", visible=False))
|
415 |
+
total_articles = len(articles)
|
416 |
+
for idx, comp in enumerate(article_components):
|
417 |
+
progress((idx + 1) / total_articles, desc=f"๊ฒฐ๊ณผ ํ์ ์ค... {idx + 1}/{total_articles}")
|
418 |
+
if idx < len(articles):
|
419 |
+
article = articles[idx]
|
420 |
+
image_url = article['image_url']
|
421 |
+
image_update = gr.update(value=image_url, visible=True) if image_url and not image_url.startswith('data:image') else gr.update(value=None, visible=False)
|
422 |
+
|
423 |
+
# ์์ฝ ๋ด์ฉ ํ๊ธ ๋ฒ์ญ
|
424 |
+
korean_summary = translate_to_korean(article['snippet'])
|
425 |
+
|
426 |
+
outputs.extend([
|
427 |
+
gr.update(visible=True),
|
428 |
+
gr.update(value=f"### [{article['title']}]({article['link']})"),
|
429 |
+
image_update,
|
430 |
+
gr.update(value=f"**์์ฝ:** {article['snippet']}\n\n**ํ๊ธ ์์ฝ:** {korean_summary}"),
|
431 |
+
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}")
|
432 |
+
])
|
433 |
+
else:
|
434 |
+
outputs.extend([
|
435 |
+
gr.update(visible=False), gr.update(), gr.update(),
|
436 |
+
gr.update(), gr.update()
|
437 |
+
])
|
438 |
+
articles_state = articles
|
439 |
+
|
440 |
+
progress(1.0, desc="์๋ฃ!")
|
441 |
+
outputs.append(articles_state)
|
442 |
+
|
443 |
+
# ๊ฒ์ ์๋ฃ ํ ์ํ ๋ฉ์์ง ์จ๊น
|
444 |
+
outputs[0] = gr.update(value="", visible=False)
|
445 |
+
|
446 |
+
return outputs
|
447 |
+
|
448 |
+
def search_global(query, articles_state_global, progress=gr.Progress()):
|
449 |
+
status_msg = "์ ์ธ๊ณ ๊ฒ์์ ์งํ์ค์
๋๋ค. ์ค์๊ฐ์ผ๋ก ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค..."
|
450 |
+
all_results = []
|
451 |
+
displayed_count = 0
|
452 |
+
|
453 |
+
# ์ด๊ธฐ ์ถ๋ ฅ ์ค์
|
454 |
+
outputs = []
|
455 |
+
outputs.append(gr.update(value=status_msg, visible=True))
|
456 |
+
outputs.append(gr.update(value=f"**๊ฒ์์ด:** {query}", visible=True))
|
457 |
+
|
458 |
+
# 1000๊ฐ์ ๊ฒฐ๊ณผ ์ปดํฌ๋ํธ๋ฅผ ์ด๊ธฐ์ ๋ชจ๋ ์จ๊น ์ฒ๋ฆฌ
|
459 |
+
for _ in global_article_components:
|
460 |
+
outputs.extend([
|
461 |
+
gr.update(visible=False), gr.update(), gr.update(),
|
462 |
+
gr.update(), gr.update()
|
463 |
+
])
|
464 |
+
outputs.append([]) # articles_state_global
|
465 |
+
|
466 |
+
yield outputs # ์ด๊ธฐ ์ํ ์ถ๋ ฅ
|
467 |
+
|
468 |
+
# ๊ตญ๊ฐ๋ณ ๊ฒ์ ๋ฐ ์ค์๊ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
469 |
+
for idx, (country, location) in enumerate(COUNTRY_LOCATIONS.items()):
|
470 |
+
progress(idx / len(COUNTRY_LOCATIONS), f"{country} ๊ฒ์ ์ค... ({idx + 1}/{len(COUNTRY_LOCATIONS)})")
|
471 |
+
|
472 |
+
try:
|
473 |
+
error_message, articles = serphouse_search(query, country)
|
474 |
+
if not error_message and articles:
|
475 |
+
# ํด๋น ๊ตญ๊ฐ์ ๊ฒฐ๊ณผ์ ๊ตญ๊ฐ ์ ๋ณด ์ถ๊ฐ
|
476 |
+
for article in articles:
|
477 |
+
article['source_country'] = country
|
478 |
+
|
479 |
+
# ์๋ก์ด ๊ฒฐ๊ณผ ์ถ๊ฐ ๋ฐ ์ ๋ ฌ
|
480 |
+
all_results.extend(articles)
|
481 |
+
sorted_results = sorted(all_results, key=lambda x: x.get('time', ''), reverse=True)
|
482 |
+
|
483 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
484 |
+
seen_urls = set()
|
485 |
+
unique_results = []
|
486 |
+
for article in sorted_results:
|
487 |
+
url = article.get('link', '')
|
488 |
+
if url not in seen_urls:
|
489 |
+
seen_urls.add(url)
|
490 |
+
unique_results.append(article)
|
491 |
+
|
492 |
+
# ์ต๋ 1000๊ฐ๊น์ง๋ง ์ ์ง
|
493 |
+
unique_results = unique_results[:1000]
|
494 |
+
|
495 |
+
# ์๋ก์ด ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
496 |
+
outputs = []
|
497 |
+
outputs.append(gr.update(value=f"์ ์ธ๊ณ ๊ฒ์ ์งํ ์ค... ({idx + 1}/{len(COUNTRY_LOCATIONS)} ๊ตญ๊ฐ ์๋ฃ)", visible=True))
|
498 |
+
outputs.append(gr.update(value=f"**๊ฒ์์ด:** {query}\n**ํ์ฌ๊น์ง ๋ฐ๊ฒฌ๋ ๋ด์ค:** {len(unique_results)}๊ฑด", visible=True))
|
499 |
+
|
500 |
+
# ๊ฒฐ๊ณผ ์ปดํฌ๋ํธ ์
๋ฐ์ดํธ
|
501 |
+
for idx, comp in enumerate(global_article_components):
|
502 |
+
if idx < len(unique_results):
|
503 |
+
article = unique_results[idx]
|
504 |
+
image_url = article.get('image_url', '')
|
505 |
+
image_update = gr.update(value=image_url, visible=True) if image_url and not image_url.startswith('data:image') else gr.update(value=None, visible=False)
|
506 |
+
|
507 |
+
korean_summary = translate_to_korean(article['snippet'])
|
508 |
+
|
509 |
+
outputs.extend([
|
510 |
+
gr.update(visible=True),
|
511 |
+
gr.update(value=f"### [{article['title']}]({article['link']})"),
|
512 |
+
image_update,
|
513 |
+
gr.update(value=f"**์์ฝ:** {article['snippet']}\n\n**ํ๊ธ ์์ฝ:** {korean_summary}"),
|
514 |
+
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **๊ตญ๊ฐ:** {article['source_country']} | **์๊ฐ:** {article['time']}")
|
515 |
+
])
|
516 |
+
else:
|
517 |
+
outputs.extend([
|
518 |
+
gr.update(visible=False), gr.update(), gr.update(),
|
519 |
+
gr.update(), gr.update()
|
520 |
+
])
|
521 |
+
|
522 |
+
outputs.append(unique_results)
|
523 |
+
yield outputs
|
524 |
+
|
525 |
+
except Exception as e:
|
526 |
+
print(f"Error searching {country}: {str(e)}")
|
527 |
+
continue
|
528 |
+
|
529 |
+
# ์ต์ข
์ํ ์
๋ฐ์ดํธ
|
530 |
+
final_status = f"๊ฒ์ ์๋ฃ! ์ด {len(unique_results)}๊ฐ์ ๋ด์ค๊ฐ ๋ฐ๊ฒฌ๋์์ต๋๋ค."
|
531 |
+
outputs[0] = gr.update(value=final_status, visible=True)
|
532 |
+
yield outputs
|
533 |
+
|
534 |
+
search_outputs = [
|
535 |
+
status_message,
|
536 |
+
translated_query_display,
|
537 |
+
gr.Markdown(visible=False)
|
538 |
+
]
|
539 |
+
|
540 |
+
for comp in article_components:
|
541 |
+
search_outputs.extend([
|
542 |
+
comp['group'], comp['title'], comp['image'],
|
543 |
+
comp['snippet'], comp['info']
|
544 |
+
])
|
545 |
+
search_outputs.append(articles_state)
|
546 |
+
|
547 |
+
search_button.click(
|
548 |
+
search_and_display,
|
549 |
+
inputs=[query, country, articles_state],
|
550 |
+
outputs=search_outputs,
|
551 |
+
show_progress=True
|
552 |
+
)
|
553 |
+
|
554 |
+
# ์ ์ธ๊ณ ๊ฒ์ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
555 |
+
global_search_outputs = [
|
556 |
+
status_message_global,
|
557 |
+
translated_query_display_global,
|
558 |
+
]
|
559 |
+
|
560 |
+
for comp in global_article_components:
|
561 |
+
global_search_outputs.extend([
|
562 |
+
comp['group'], comp['title'], comp['image'],
|
563 |
+
comp['snippet'], comp['info']
|
564 |
+
])
|
565 |
+
global_search_outputs.append(articles_state_global)
|
566 |
+
|
567 |
+
search_button_global.click(
|
568 |
+
search_global,
|
569 |
+
inputs=[query_global, articles_state_global],
|
570 |
+
outputs=global_search_outputs,
|
571 |
+
show_progress=True
|
572 |
+
)
|
573 |
+
|
574 |
+
iface.launch(auth=("it1","chosun1"))
|