Spaces:
openfree
/
Running on CPU Upgrade

openfree commited on
Commit
b79e8b5
ยท
verified ยท
1 Parent(s): d84de86

Create app-backup2.py

Browse files
Files changed (1) hide show
  1. app-backup2.py +574 -0
app-backup2.py ADDED
@@ -0,0 +1,574 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"))