Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,27 @@ import os
|
|
5 |
from datetime import datetime, timedelta
|
6 |
from huggingface_hub import InferenceClient
|
7 |
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
API_KEY = os.getenv("SERPHOUSE_API_KEY")
|
11 |
# hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
@@ -295,49 +315,76 @@ footer {visibility: hidden;}
|
|
295 |
"""
|
296 |
|
297 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μλΉμ€") as iface:
|
298 |
-
gr.
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
|
|
|
|
324 |
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
334 |
def search_and_display(query, country, articles_state, progress=gr.Progress()):
|
335 |
-
#
|
336 |
status_msg = "κ²μμ μ§νμ€μ
λλ€. μ μλ§ κΈ°λ€λ¦¬μΈμ..."
|
337 |
|
338 |
progress(0, desc="κ²μμ΄ λ²μ μ€...")
|
339 |
-
|
340 |
-
# κ²μμ΄ λ²μ
|
341 |
translated_query = translate_query(query, country)
|
342 |
translated_display = f"**μλ³Έ κ²μμ΄:** {query}\n**λ²μλ κ²μμ΄:** {translated_query}" if translated_query != query else f"**κ²μμ΄:** {query}"
|
343 |
|
@@ -346,8 +393,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μλΉμ€") as
|
|
346 |
progress(0.5, desc="κ²°κ³Ό μ²λ¦¬ μ€...")
|
347 |
|
348 |
outputs = []
|
349 |
-
outputs.append(gr.update(value=status_msg, visible=True))
|
350 |
-
outputs.append(gr.update(value=translated_display, visible=True))
|
351 |
|
352 |
if error_message:
|
353 |
outputs.append(gr.update(value=error_message, visible=True))
|
@@ -367,7 +414,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μλΉμ€") as
|
|
367 |
image_url = article['image_url']
|
368 |
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)
|
369 |
|
370 |
-
# μμ½ λ΄μ© νκΈ λ²μ
|
371 |
korean_summary = translate_to_korean(article['snippet'])
|
372 |
|
373 |
outputs.extend([
|
@@ -386,12 +432,74 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μλΉμ€") as
|
|
386 |
|
387 |
progress(1.0, desc="μλ£!")
|
388 |
outputs.append(articles_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
|
390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
391 |
outputs[0] = gr.update(value="", visible=False)
|
392 |
|
393 |
return outputs
|
394 |
|
|
|
395 |
search_outputs = [
|
396 |
status_message,
|
397 |
translated_query_display,
|
@@ -412,4 +520,24 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μλΉμ€") as
|
|
412 |
show_progress=True
|
413 |
)
|
414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
iface.launch(auth=("it1","chosun1"))
|
|
|
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"))
|
|
|
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 |
+
status_message = gr.Markdown("", visible=True)
|
330 |
+
translated_query_display = gr.Markdown(visible=False)
|
331 |
+
search_button = gr.Button("κ²μ", variant="primary")
|
332 |
+
|
333 |
+
progress = gr.Progress()
|
334 |
+
articles_state = gr.State([])
|
335 |
+
|
336 |
+
article_components = []
|
337 |
+
for i in range(100):
|
338 |
+
with gr.Group(visible=False) as article_group:
|
339 |
+
title = gr.Markdown()
|
340 |
+
image = gr.Image(width=200, height=150)
|
341 |
+
snippet = gr.Markdown()
|
342 |
+
info = gr.Markdown()
|
343 |
+
|
344 |
+
article_components.append({
|
345 |
+
'group': article_group,
|
346 |
+
'title': title,
|
347 |
+
'image': image,
|
348 |
+
'snippet': snippet,
|
349 |
+
'info': info,
|
350 |
+
'index': i,
|
351 |
+
})
|
352 |
|
353 |
+
# μ μΈκ³ ν
|
354 |
+
with gr.Tab("μ μΈκ³"):
|
355 |
+
gr.Markdown("κ²μμ΄λ₯Ό μ
λ ₯νλ©΄ 67κ°κ΅μ 24μκ° μ΄λ΄ λ΄μ€λ₯Ό μ΅λ 1000κ° μΆλ ₯ν©λλ€.")
|
356 |
+
|
357 |
+
with gr.Column():
|
358 |
+
query_global = gr.Textbox(label="κ²μμ΄")
|
359 |
+
status_message_global = gr.Markdown("", visible=True)
|
360 |
+
translated_query_display_global = gr.Markdown(visible=False)
|
361 |
+
search_button_global = gr.Button("μ μΈκ³ κ²μ", variant="primary")
|
362 |
|
363 |
+
progress_global = gr.Progress()
|
364 |
+
articles_state_global = gr.State([])
|
365 |
+
|
366 |
+
global_article_components = []
|
367 |
+
for i in range(1000):
|
368 |
+
with gr.Group(visible=False) as article_group:
|
369 |
+
title = gr.Markdown()
|
370 |
+
image = gr.Image(width=200, height=150)
|
371 |
+
snippet = gr.Markdown()
|
372 |
+
info = gr.Markdown()
|
373 |
+
|
374 |
+
global_article_components.append({
|
375 |
+
'group': article_group,
|
376 |
+
'title': title,
|
377 |
+
'image': image,
|
378 |
+
'snippet': snippet,
|
379 |
+
'info': info,
|
380 |
+
'index': i,
|
381 |
+
})
|
382 |
|
383 |
def search_and_display(query, country, articles_state, progress=gr.Progress()):
|
384 |
+
# κΈ°μ‘΄ search_and_display ν¨μ μ½λ μ μ§
|
385 |
status_msg = "κ²μμ μ§νμ€μ
λλ€. μ μλ§ κΈ°λ€λ¦¬μΈμ..."
|
386 |
|
387 |
progress(0, desc="κ²μμ΄ λ²μ μ€...")
|
|
|
|
|
388 |
translated_query = translate_query(query, country)
|
389 |
translated_display = f"**μλ³Έ κ²μμ΄:** {query}\n**λ²μλ κ²μμ΄:** {translated_query}" if translated_query != query else f"**κ²μμ΄:** {query}"
|
390 |
|
|
|
393 |
progress(0.5, desc="κ²°κ³Ό μ²λ¦¬ μ€...")
|
394 |
|
395 |
outputs = []
|
396 |
+
outputs.append(gr.update(value=status_msg, visible=True))
|
397 |
+
outputs.append(gr.update(value=translated_display, visible=True))
|
398 |
|
399 |
if error_message:
|
400 |
outputs.append(gr.update(value=error_message, visible=True))
|
|
|
414 |
image_url = article['image_url']
|
415 |
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)
|
416 |
|
|
|
417 |
korean_summary = translate_to_korean(article['snippet'])
|
418 |
|
419 |
outputs.extend([
|
|
|
432 |
|
433 |
progress(1.0, desc="μλ£!")
|
434 |
outputs.append(articles_state)
|
435 |
+
outputs[0] = gr.update(value="", visible=False)
|
436 |
+
|
437 |
+
return outputs
|
438 |
+
|
439 |
+
def search_global(query, articles_state_global, progress=gr.Progress()):
|
440 |
+
status_msg = "μ μΈκ³ κ²μμ μ§νμ€μ
λλ€. μ μλ§ κΈ°λ€λ¦¬μΈμ..."
|
441 |
+
all_results = []
|
442 |
|
443 |
+
progress(0, desc="μ μΈκ³ κ²μ μμ...")
|
444 |
+
|
445 |
+
for idx, (country, location) in enumerate(COUNTRY_LOCATIONS.items()):
|
446 |
+
progress(idx / len(COUNTRY_LOCATIONS), f"{country} κ²μ μ€...")
|
447 |
+
try:
|
448 |
+
error_message, articles = serphouse_search(query, country)
|
449 |
+
if not error_message and articles:
|
450 |
+
for article in articles:
|
451 |
+
article['source_country'] = country
|
452 |
+
all_results.extend(articles)
|
453 |
+
except Exception as e:
|
454 |
+
print(f"Error searching {country}: {str(e)}")
|
455 |
+
continue
|
456 |
+
|
457 |
+
# κ²°κ³Ό μ λ ¬ λ° μ€λ³΅ μ κ±°
|
458 |
+
seen_urls = set()
|
459 |
+
unique_results = []
|
460 |
+
for article in sorted(all_results, key=lambda x: x.get('time', ''), reverse=True):
|
461 |
+
url = article.get('link', '')
|
462 |
+
if url not in seen_urls:
|
463 |
+
seen_urls.add(url)
|
464 |
+
unique_results.append(article)
|
465 |
+
|
466 |
+
# μ΅λ 1000κ°λ‘ μ ν
|
467 |
+
final_results = unique_results[:1000]
|
468 |
+
|
469 |
+
outputs = []
|
470 |
+
outputs.append(gr.update(value=status_msg, visible=True))
|
471 |
+
outputs.append(gr.update(value=f"**κ²μμ΄:** {query}", visible=True))
|
472 |
+
|
473 |
+
progress(0.5, desc="κ²°κ³Ό μ²λ¦¬ μ€...")
|
474 |
+
|
475 |
+
for idx, comp in enumerate(global_article_components):
|
476 |
+
if idx < len(final_results):
|
477 |
+
article = final_results[idx]
|
478 |
+
image_url = article.get('image_url', '')
|
479 |
+
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)
|
480 |
+
|
481 |
+
korean_summary = translate_to_korean(article['snippet'])
|
482 |
+
|
483 |
+
outputs.extend([
|
484 |
+
gr.update(visible=True),
|
485 |
+
gr.update(value=f"### [{article['title']}]({article['link']})"),
|
486 |
+
image_update,
|
487 |
+
gr.update(value=f"**μμ½:** {article['snippet']}\n\n**νκΈ μμ½:** {korean_summary}"),
|
488 |
+
gr.update(value=f"**μΆμ²:** {article['channel']} | **κ΅κ°:** {article['source_country']} | **μκ°:** {article['time']}")
|
489 |
+
])
|
490 |
+
else:
|
491 |
+
outputs.extend([
|
492 |
+
gr.update(visible=False), gr.update(), gr.update(),
|
493 |
+
gr.update(), gr.update()
|
494 |
+
])
|
495 |
+
|
496 |
+
progress(1.0, desc="μλ£!")
|
497 |
+
outputs.append(final_results)
|
498 |
outputs[0] = gr.update(value="", visible=False)
|
499 |
|
500 |
return outputs
|
501 |
|
502 |
+
# κ΅κ°λ³ κ²μ μ΄λ²€νΈ μ°κ²°
|
503 |
search_outputs = [
|
504 |
status_message,
|
505 |
translated_query_display,
|
|
|
520 |
show_progress=True
|
521 |
)
|
522 |
|
523 |
+
# μ μΈκ³ κ²μ μ΄λ²€νΈ μ°κ²°
|
524 |
+
global_search_outputs = [
|
525 |
+
status_message_global,
|
526 |
+
translated_query_display_global,
|
527 |
+
]
|
528 |
+
|
529 |
+
for comp in global_article_components:
|
530 |
+
global_search_outputs.extend([
|
531 |
+
comp['group'], comp['title'], comp['image'],
|
532 |
+
comp['snippet'], comp['info']
|
533 |
+
])
|
534 |
+
global_search_outputs.append(articles_state_global)
|
535 |
+
|
536 |
+
search_button_global.click(
|
537 |
+
search_global,
|
538 |
+
inputs=[query_global, articles_state_global],
|
539 |
+
outputs=global_search_outputs,
|
540 |
+
show_progress=True
|
541 |
+
)
|
542 |
+
|
543 |
iface.launch(auth=("it1","chosun1"))
|