openfree commited on
Commit
4ad50c2
ยท
verified ยท
1 Parent(s): 74a32a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -128
app.py CHANGED
@@ -313,21 +313,27 @@ def serphouse_search(query, country):
313
  css = """
314
  footer {visibility: hidden;}
315
 
316
- /* ์ „์—ญ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ปจํ…Œ์ด๋„ˆ ์Šคํƒ€์ผ */
317
- #global_results_container {
318
- margin-top: 20px;
319
- padding-top: 20px;
320
- }
321
-
322
- /* ์ง„ํ–‰ ์ƒํƒœ ์ปจํ…Œ์ด๋„ˆ ์Šคํƒ€์ผ */
323
- #progress_status_container {
324
  position: sticky;
325
  top: 0;
326
- background: white;
327
  padding: 10px;
328
- margin-bottom: 20px;
329
  border-bottom: 2px solid #eee;
330
- z-index: 100;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  }
332
  """
333
 
@@ -343,12 +349,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
343
  query = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
344
  country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ€", value="South Korea")
345
 
346
- # ๊ฒ€์ƒ‰ ์ƒํƒœ ๋ฉ”์‹œ์ง€
347
  status_message = gr.Markdown("", visible=True)
348
-
349
- # ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ปดํฌ๋„ŒํŠธ
350
  translated_query_display = gr.Markdown(visible=False)
351
-
352
  search_button = gr.Button("๊ฒ€์ƒ‰", variant="primary")
353
 
354
  progress = gr.Progress()
@@ -371,28 +373,24 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
371
  'index': i,
372
  })
373
 
 
374
  with gr.Tab("์ „์„ธ๊ณ„"):
375
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 67๊ฐœ๊ตญ์˜ 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๋ฅผ ์ตœ๋Œ€ 1000๊ฐœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
376
-
377
  with gr.Column():
378
- # ์ƒ๋‹จ ๊ฒ€์ƒ‰ ์˜์—ญ
379
  with gr.Row():
380
  query_global = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
381
  search_button_global = gr.Button("์ „์„ธ๊ณ„ ๊ฒ€์ƒ‰", variant="primary")
382
-
383
- # ์ง„ํ–‰ ์ƒํƒœ ํ‘œ์‹œ ์˜์—ญ (๊ณ ์ • ์œ„์น˜)
384
- with gr.Column(elem_id="progress_status_container"):
385
- status_message_global = gr.Markdown("", visible=True)
386
- translated_query_display_global = gr.Markdown(visible=False)
387
- progress_global = gr.Progress()
388
-
389
- # ๊ตฌ๋ถ„์„ 
390
  gr.Markdown("---")
391
-
392
- # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜์—ญ (์Šคํฌ๋กค ๊ฐ€๋Šฅ)
393
- with gr.Column(elem_id="global_results_container"):
394
  articles_state_global = gr.State([])
395
-
396
  global_article_components = []
397
  for i in range(1000):
398
  with gr.Group(visible=False) as article_group:
@@ -400,7 +398,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
400
  image = gr.Image(width=200, height=150)
401
  snippet = gr.Markdown()
402
  info = gr.Markdown()
403
-
404
  global_article_components.append({
405
  'group': article_group,
406
  'title': title,
@@ -410,16 +408,10 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
410
  'index': i,
411
  })
412
 
413
-
414
-
415
-
416
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
417
- # ๊ฒ€์ƒ‰ ์ƒํƒœ ๋ฉ”์‹œ์ง€ ์—…๋ฐ์ดํŠธ
418
  status_msg = "๊ฒ€์ƒ‰์„ ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ฆฌ์„ธ์š”..."
419
 
420
  progress(0, desc="๊ฒ€์ƒ‰์–ด ๋ฒˆ์—ญ ์ค‘...")
421
-
422
- # ๊ฒ€์ƒ‰์–ด ๋ฒˆ์—ญ
423
  translated_query = translate_query(query, country)
424
  translated_display = f"**์›๋ณธ ๊ฒ€์ƒ‰์–ด:** {query}\n**๋ฒˆ์—ญ๋œ ๊ฒ€์ƒ‰์–ด:** {translated_query}" if translated_query != query else f"**๊ฒ€์ƒ‰์–ด:** {query}"
425
 
@@ -428,8 +420,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
428
  progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘...")
429
 
430
  outputs = []
431
- outputs.append(gr.update(value=status_msg, visible=True)) # ์ƒํƒœ ๋ฉ”์‹œ์ง€
432
- outputs.append(gr.update(value=translated_display, visible=True)) # ๋ฒˆ์—ญ ๊ฒฐ๊ณผ
433
 
434
  if error_message:
435
  outputs.append(gr.update(value=error_message, visible=True))
@@ -449,7 +441,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
449
  image_url = article['image_url']
450
  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)
451
 
452
- # ์š”์•ฝ ๋‚ด์šฉ ํ•œ๊ธ€ ๋ฒˆ์—ญ
453
  korean_summary = translate_to_korean(article['snippet'])
454
 
455
  outputs.extend([
@@ -468,100 +459,88 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
468
 
469
  progress(1.0, desc="์™„๋ฃŒ!")
470
  outputs.append(articles_state)
471
-
472
- # ๊ฒ€์ƒ‰ ์™„๋ฃŒ ํ›„ ์ƒํƒœ ๋ฉ”์‹œ์ง€ ์ˆจ๊น€
473
  outputs[0] = gr.update(value="", visible=False)
474
 
475
  return outputs
476
 
477
  def search_global(query, articles_state_global, progress=gr.Progress()):
478
- status_msg = "์ „์„ธ๊ณ„ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค. ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค..."
479
- all_results = []
480
- displayed_count = 0
481
-
482
- # ์ดˆ๊ธฐ ์ถœ๋ ฅ ์„ค์ • - ์ƒ๋‹จ ์ƒํƒœ ํ‘œ์‹œ๋งŒ ์—…๋ฐ์ดํŠธ
483
- outputs = []
484
- outputs.append(gr.update(value=status_msg, visible=True)) # ์ƒํƒœ ๋ฉ”์‹œ์ง€
485
- outputs.append(gr.update(value=f"**๊ฒ€์ƒ‰์–ด:** {query}", visible=True)) # ๊ฒ€์ƒ‰์–ด ํ‘œ์‹œ
486
-
487
- # ๊ฒฐ๊ณผ ์ปดํฌ๋„ŒํŠธ ์ดˆ๊ธฐํ™”
488
- for _ in global_article_components:
489
- outputs.extend([
490
- gr.update(visible=False), gr.update(), gr.update(),
491
- gr.update(), gr.update()
492
- ])
493
- outputs.append([])
494
-
495
- yield outputs
496
-
497
- # ๊ตญ๊ฐ€๋ณ„ ๊ฒ€์ƒ‰ ๋ฐ ์‹ค์‹œ๊ฐ„ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
498
- for idx, (country, location) in enumerate(COUNTRY_LOCATIONS.items()):
499
- progress(idx / len(COUNTRY_LOCATIONS), f"์ง„ํ–‰ ์ƒํ™ฉ: {idx + 1}/{len(COUNTRY_LOCATIONS)} ๊ตญ๊ฐ€ ๊ฒ€์ƒ‰ ์ค‘... (ํ˜„์žฌ: {country})")
500
-
501
- try:
502
- error_message, articles = serphouse_search(query, country)
503
- if not error_message and articles:
504
- for article in articles:
505
- article['source_country'] = country
506
-
507
- all_results.extend(articles)
508
- sorted_results = sorted(all_results, key=lambda x: x.get('time', ''), reverse=True)
509
-
510
- # ์ค‘๋ณต ์ œ๊ฑฐ
511
- seen_urls = set()
512
- unique_results = []
513
- for article in sorted_results:
514
- url = article.get('link', '')
515
- if url not in seen_urls:
516
- seen_urls.add(url)
517
- unique_results.append(article)
518
-
519
- unique_results = unique_results[:1000]
520
-
521
- # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์—…๋ฐ์ดํŠธ
522
- outputs = []
523
- # ์ƒ๋‹จ ์ƒํƒœ ํ‘œ์‹œ ์—…๋ฐ์ดํŠธ
524
- outputs.append(gr.update(
525
- value=f"๊ฒ€์ƒ‰ ์ง„ํ–‰ ์ค‘... ({idx + 1}/{len(COUNTRY_LOCATIONS)} ๊ตญ๊ฐ€ ์™„๋ฃŒ)\nํ˜„์žฌ๊นŒ์ง€ ๋ฐœ๊ฒฌ๋œ ๋‰ด์Šค: {len(unique_results)}๊ฑด",
526
- visible=True
527
- ))
528
- outputs.append(gr.update(value=f"**๊ฒ€์ƒ‰์–ด:** {query}", visible=True))
529
-
530
- # ๊ฒฐ๊ณผ ์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ
531
- for idx, comp in enumerate(global_article_components):
532
- if idx < len(unique_results):
533
- article = unique_results[idx]
534
- image_url = article.get('image_url', '')
535
- 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)
536
-
537
- korean_summary = translate_to_korean(article['snippet'])
538
-
539
- outputs.extend([
540
- gr.update(visible=True),
541
- gr.update(value=f"### [{article['title']}]({article['link']})"),
542
- image_update,
543
- gr.update(value=f"**์š”์•ฝ:** {article['snippet']}\n\n**ํ•œ๊ธ€ ์š”์•ฝ:** {korean_summary}"),
544
- gr.update(value=f"**์ถœ์ฒ˜:** {article['channel']} | **๊ตญ๊ฐ€:** {article['source_country']} | **์‹œ๊ฐ„:** {article['time']}")
545
- ])
546
- else:
547
- outputs.extend([
548
- gr.update(visible=False), gr.update(), gr.update(),
549
- gr.update(), gr.update()
550
- ])
551
-
552
- outputs.append(unique_results)
553
- yield outputs
554
-
555
- except Exception as e:
556
- print(f"Error searching {country}: {str(e)}")
557
- continue
558
-
559
- # ์ตœ์ข… ์ƒํƒœ ์—…๋ฐ์ดํŠธ
560
- final_status = f"๊ฒ€์ƒ‰ ์™„๋ฃŒ! ์ด {len(unique_results)}๊ฐœ์˜ ๋‰ด์Šค๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
561
- outputs[0] = gr.update(value=final_status, visible=True)
562
- yield outputs
563
 
564
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
 
566
  search_outputs = [
567
  status_message,
@@ -583,7 +562,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
583
  show_progress=True
584
  )
585
 
586
- # ์ „์„ธ๊ณ„ ๊ฒ€์ƒ‰ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
587
  global_search_outputs = [
588
  status_message_global,
589
  translated_query_display_global,
 
313
  css = """
314
  footer {visibility: hidden;}
315
 
316
+ #status_area {
 
 
 
 
 
 
 
317
  position: sticky;
318
  top: 0;
319
+ background-color: white;
320
  padding: 10px;
 
321
  border-bottom: 2px solid #eee;
322
+ z-index: 1000;
323
+ margin-bottom: 20px;
324
+ }
325
+
326
+ #results_area {
327
+ margin-top: 20px;
328
+ padding-top: 10px;
329
+ }
330
+
331
+ .progress-container {
332
+ display: none !important;
333
+ }
334
+
335
+ .progress-container:first-child {
336
+ display: block !important;
337
  }
338
  """
339
 
 
349
  query = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
350
  country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ€", value="South Korea")
351
 
 
352
  status_message = gr.Markdown("", visible=True)
 
 
353
  translated_query_display = gr.Markdown(visible=False)
 
354
  search_button = gr.Button("๊ฒ€์ƒ‰", variant="primary")
355
 
356
  progress = gr.Progress()
 
373
  'index': i,
374
  })
375
 
376
+ # ์ „์„ธ๊ณ„ ํƒญ
377
  with gr.Tab("์ „์„ธ๊ณ„"):
378
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 67๊ฐœ๊ตญ์˜ 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๋ฅผ ์ตœ๋Œ€ 1000๊ฐœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
379
+
380
  with gr.Column():
 
381
  with gr.Row():
382
  query_global = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
383
  search_button_global = gr.Button("์ „์„ธ๊ณ„ ๊ฒ€์ƒ‰", variant="primary")
384
+
385
+ with gr.Column(elem_id="status_area", visible=True):
386
+ status_message_global = gr.Markdown("")
387
+ progress_global = gr.Progress(track_tqdm=True)
388
+
 
 
 
389
  gr.Markdown("---")
390
+
391
+ with gr.Column(elem_id="results_area"):
 
392
  articles_state_global = gr.State([])
393
+
394
  global_article_components = []
395
  for i in range(1000):
396
  with gr.Group(visible=False) as article_group:
 
398
  image = gr.Image(width=200, height=150)
399
  snippet = gr.Markdown()
400
  info = gr.Markdown()
401
+
402
  global_article_components.append({
403
  'group': article_group,
404
  'title': title,
 
408
  'index': i,
409
  })
410
 
 
 
 
411
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
 
412
  status_msg = "๊ฒ€์ƒ‰์„ ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ฆฌ์„ธ์š”..."
413
 
414
  progress(0, desc="๊ฒ€์ƒ‰์–ด ๋ฒˆ์—ญ ์ค‘...")
 
 
415
  translated_query = translate_query(query, country)
416
  translated_display = f"**์›๋ณธ ๊ฒ€์ƒ‰์–ด:** {query}\n**๋ฒˆ์—ญ๋œ ๊ฒ€์ƒ‰์–ด:** {translated_query}" if translated_query != query else f"**๊ฒ€์ƒ‰์–ด:** {query}"
417
 
 
420
  progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘...")
421
 
422
  outputs = []
423
+ outputs.append(gr.update(value=status_msg, visible=True))
424
+ outputs.append(gr.update(value=translated_display, visible=True))
425
 
426
  if error_message:
427
  outputs.append(gr.update(value=error_message, visible=True))
 
441
  image_url = article['image_url']
442
  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)
443
 
 
444
  korean_summary = translate_to_korean(article['snippet'])
445
 
446
  outputs.extend([
 
459
 
460
  progress(1.0, desc="์™„๋ฃŒ!")
461
  outputs.append(articles_state)
 
 
462
  outputs[0] = gr.update(value="", visible=False)
463
 
464
  return outputs
465
 
466
  def search_global(query, articles_state_global, progress=gr.Progress()):
467
+ status_msg = "์ „์„ธ๊ณ„ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค. ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค..."
468
+ all_results = []
469
+
470
+ outputs = [
471
+ gr.update(value=status_msg, visible=True),
472
+ gr.update(value="", visible=False),
473
+ ]
474
+
475
+ for _ in global_article_components:
476
+ outputs.extend([
477
+ gr.update(visible=False), gr.update(), gr.update(),
478
+ gr.update(), gr.update()
479
+ ])
480
+ outputs.append([])
481
+
482
+ yield outputs
483
+
484
+ for idx, (country, location) in enumerate(COUNTRY_LOCATIONS.items()):
485
+ progress((idx + 1) / len(COUNTRY_LOCATIONS), f"์ง„ํ–‰ ์ƒํ™ฉ: {idx + 1}/{len(COUNTRY_LOCATIONS)} ๊ตญ๊ฐ€ ๊ฒ€์ƒ‰ ์ค‘... (ํ˜„์žฌ: {country})")
486
+
487
+ try:
488
+ error_message, articles = serphouse_search(query, country)
489
+ if not error_message and articles:
490
+ for article in articles:
491
+ article['source_country'] = country
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
 
493
+ all_results.extend(articles)
494
+ sorted_results = sorted(all_results, key=lambda x: x.get('time', ''), reverse=True)
495
+
496
+ seen_urls = set()
497
+ unique_results = []
498
+ for article in sorted_results:
499
+ url = article.get('link', '')
500
+ if url not in seen_urls:
501
+ seen_urls.add(url)
502
+ unique_results.append(article)
503
+
504
+ unique_results = unique_results[:1000]
505
+
506
+ outputs = []
507
+ outputs.append(gr.update(
508
+ value=f"๊ฒ€์ƒ‰ ์ง„ํ–‰ ์ค‘... ({idx + 1}/{len(COUNTRY_LOCATIONS)} ๊ตญ๊ฐ€ ์™„๋ฃŒ)\nํ˜„์žฌ๊นŒ์ง€ ๋ฐœ๊ฒฌ๋œ ๋‰ด์Šค: {len(unique_results)}๊ฑด",
509
+ visible=True
510
+ ))
511
+ outputs.append(gr.update(value=f"**๊ฒ€์ƒ‰์–ด:** {query}", visible=True))
512
+
513
+ for idx, comp in enumerate(global_article_components):
514
+ if idx < len(unique_results):
515
+ article = unique_results[idx]
516
+ image_url = article.get('image_url', '')
517
+ 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)
518
+
519
+ korean_summary = translate_to_korean(article['snippet'])
520
+
521
+ outputs.extend([
522
+ gr.update(visible=True),
523
+ gr.update(value=f"### [{article['title']}]({article['link']})"),
524
+ image_update,
525
+ gr.update(value=f"**์š”์•ฝ:** {article['snippet']}\n\n**ํ•œ๊ธ€ ์š”์•ฝ:** {korean_summary}"),
526
+ gr.update(value=f"**์ถœ์ฒ˜:** {article['channel']} | **๊ตญ๊ฐ€:** {article['source_country']} | **์‹œ๊ฐ„:** {article['time']}")
527
+ ])
528
+ else:
529
+ outputs.extend([
530
+ gr.update(visible=False), gr.update(), gr.update(),
531
+ gr.update(), gr.update()
532
+ ])
533
+
534
+ outputs.append(unique_results)
535
+ yield outputs
536
+
537
+ except Exception as e:
538
+ print(f"Error searching {country}: {str(e)}")
539
+ continue
540
+
541
+ final_status = f"๊ฒ€์ƒ‰ ์™„๋ฃŒ! ์ด {len(unique_results)}๊ฐœ์˜ ๋‰ด์Šค๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
542
+ outputs[0] = gr.update(value=final_status, visible=True)
543
+ yield outputs
544
 
545
  search_outputs = [
546
  status_message,
 
562
  show_progress=True
563
  )
564
 
 
565
  global_search_outputs = [
566
  status_message_global,
567
  translated_query_display_global,