openfree commited on
Commit
b8dec7f
Β·
verified Β·
1 Parent(s): e41017a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -40
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.Markdown("검색어λ₯Ό μž…λ ₯ν•˜κ³  μ›ν•˜λŠ” κ΅­κ°€(67개ꡭ)λ₯Ό μ„ νƒν•˜λ©΄, 검색어와 μΌμΉ˜ν•˜λŠ” 24μ‹œκ°„ 이내 λ‰΄μŠ€λ₯Ό μ΅œλŒ€ 100개 좜λ ₯ν•©λ‹ˆλ‹€.")
299
- gr.Markdown("κ΅­κ°€ 선택후 검색어에 'ν•œκΈ€'을 μž…λ ₯ν•˜λ©΄ ν˜„μ§€ μ–Έμ–΄λ‘œ λ²ˆμ—­λ˜μ–΄ κ²€μƒ‰ν•©λ‹ˆλ‹€. 예: 'Taiwan' κ΅­κ°€ 선택후 'μ‚Όμ„±' μž…λ ₯μ‹œ 'δΈ‰ζ˜Ÿ'으둜 μžλ™ 검색")
300
-
301
- with gr.Column():
302
- with gr.Row():
303
- query = gr.Textbox(label="검색어")
304
- country = gr.Dropdown(MAJOR_COUNTRIES, label="κ΅­κ°€", value="South Korea")
305
-
306
- # 검색 μƒνƒœ λ©”μ‹œμ§€
307
- status_message = gr.Markdown("", visible=True)
308
-
309
- # λ²ˆμ—­ κ²°κ³Όλ₯Ό λ³΄μ—¬μ£ΌλŠ” μ»΄ν¬λ„ŒνŠΈ
310
- translated_query_display = gr.Markdown(visible=False)
311
-
312
- search_button = gr.Button("검색", variant="primary")
313
-
314
- progress = gr.Progress()
315
- articles_state = gr.State([])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
 
317
- article_components = []
318
- for i in range(100):
319
- with gr.Group(visible=False) as article_group:
320
- title = gr.Markdown()
321
- image = gr.Image(width=200, height=150)
322
- snippet = gr.Markdown()
323
- info = gr.Markdown()
 
 
324
 
325
- article_components.append({
326
- 'group': article_group,
327
- 'title': title,
328
- 'image': image,
329
- 'snippet': snippet,
330
- 'info': info,
331
- 'index': i,
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"))