openfree commited on
Commit
59b7556
ยท
verified ยท
1 Parent(s): db98ece

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -81
app.py CHANGED
@@ -311,7 +311,7 @@ def serphouse_search(query, country):
311
  return format_results_from_raw(response_data)
312
 
313
 
314
- # Hacker News API ๊ด€๋ จ ํ•จ์ˆ˜ ์ถ”๊ฐ€
315
  def get_hn_item(item_id):
316
  """๊ฐœ๋ณ„ ์•„์ดํ…œ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ"""
317
  try:
@@ -323,11 +323,9 @@ def get_hn_item(item_id):
323
  def get_recent_stories():
324
  """์ตœ์‹  ์Šคํ† ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ"""
325
  try:
326
- # newstories ์—”๋“œํฌ์ธํŠธ๋กœ ์ตœ์‹  ์Šคํ† ๋ฆฌ ID ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
327
  response = requests.get("https://hacker-news.firebaseio.com/v0/newstories.json")
328
  story_ids = response.json()
329
 
330
- # 24์‹œ๊ฐ„ ์ด๋‚ด ์Šคํ† ๋ฆฌ๋งŒ ํ•„ํ„ฐ๋ง
331
  recent_stories = []
332
  current_time = datetime.now().timestamp()
333
  day_ago = current_time - (24 * 60 * 60)
@@ -337,8 +335,7 @@ def get_recent_stories():
337
  if story and 'time' in story and story['time'] > day_ago:
338
  recent_stories.append(story)
339
 
340
- # ์ถฉ๋ถ„ํ•œ ์Šคํ† ๋ฆฌ๋ฅผ ๋ชจ์•˜์œผ๋ฉด ์ค‘๋‹จ
341
- if len(recent_stories) >= 100: # ์ตœ๋Œ€ 100๊ฐœ๋กœ ์ œํ•œ
342
  break
343
 
344
  return recent_stories
@@ -354,6 +351,43 @@ def format_hn_time(timestamp):
354
  except:
355
  return "Unknown time"
356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
 
358
  css = """
359
  footer {visibility: hidden;}
@@ -457,12 +491,12 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
457
  'index': i,
458
  })
459
 
 
460
  with gr.Tab("์ „์„ธ๊ณ„"):
461
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 67๊ฐœ๊ตญ(ํ•œ๊ตญ ์ œ์™ธ) ์ „์ฒด์— ๋Œ€ํ•ด ๊ตญ๊ฐ€๋ณ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๊ฐ€ ์ตœ๋Œ€ 1000๊ฐœ ์ˆœ์ฐจ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.")
462
  gr.Markdown("๊ตญ๊ฐ€ ์„ ํƒํ›„ ๊ฒ€์ƒ‰์–ด์— 'ํ•œ๊ธ€'์„ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์ง€ ์–ธ์–ด๋กœ ๋ฒˆ์—ญ๋˜์–ด ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ: 'Taiwan' ๊ตญ๊ฐ€ ์„ ํƒํ›„ '์‚ผ์„ฑ' ์ž…๋ ฅ์‹œ 'ไธ‰ๆ˜Ÿ'์œผ๋กœ ์ž๋™ ๊ฒ€์ƒ‰")
463
 
464
  with gr.Column():
465
- # ์ƒ๋‹จ ์˜์—ญ
466
  with gr.Column(elem_id="status_area"):
467
  with gr.Row():
468
  query_global = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
@@ -471,7 +505,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
471
  status_message_global = gr.Markdown("")
472
  translated_query_display_global = gr.Markdown("")
473
 
474
- # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜์—ญ
475
  with gr.Column(elem_id="results_area"):
476
  articles_state_global = gr.State([])
477
 
@@ -492,6 +525,37 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
492
  'index': i,
493
  })
494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
496
  status_msg = "๊ฒ€์ƒ‰์„ ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ฆฌ์„ธ์š”..."
497
 
@@ -627,6 +691,12 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
627
  outputs[0] = gr.update(value=final_status, visible=True)
628
  yield outputs
629
 
 
 
 
 
 
 
630
  search_outputs = [
631
  status_message,
632
  translated_query_display,
@@ -647,6 +717,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
647
  show_progress=True
648
  )
649
 
 
650
  global_search_outputs = [
651
  status_message_global,
652
  translated_query_display_global,
@@ -665,80 +736,18 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
665
  outputs=global_search_outputs
666
  )
667
 
668
- # ํƒญ ๊ตฌํ˜„ ๋ถ€๋ถ„์— ๋‹ค์Œ ์ฝ”๋“œ ์ถ”๊ฐ€
669
- with gr.Tab("AI ๋ฆฌํฌํ„ฐ"):
670
- gr.Markdown("์ง€๋‚œ 24์‹œ๊ฐ„ ๋™์•ˆ์˜ Hacker News ํฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.")
671
-
672
- with gr.Column():
673
- refresh_button = gr.Button("์ƒˆ๋กœ๊ณ ์นจ", variant="primary")
674
- status_message_hn = gr.Markdown("")
675
-
676
- # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜์—ญ
677
- with gr.Column(elem_id="hn_results_area"):
678
- hn_articles_state = gr.State([])
679
-
680
- hn_article_components = []
681
- for i in range(100): # ์ตœ๋Œ€ 100๊ฐœ ํ‘œ์‹œ
682
- with gr.Group(visible=False) as article_group:
683
- title = gr.Markdown()
684
- info = gr.Markdown()
685
-
686
- hn_article_components.append({
687
- 'group': article_group,
688
- 'title': title,
689
- 'info': info,
690
- 'index': i,
691
- })
692
 
693
- def refresh_hn_stories():
694
- """Hacker News ์Šคํ† ๋ฆฌ ์ƒˆ๋กœ๊ณ ์นจ"""
695
- status_msg = "Hacker News ํฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘..."
696
-
697
- outputs = [gr.update(value=status_msg, visible=True)]
698
-
699
- # ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ ์ดˆ๊ธฐํ™”
700
- for _ in hn_article_components:
701
- outputs.extend([
702
- gr.update(visible=False),
703
- gr.update(),
704
- gr.update()
705
- ])
706
-
707
- # ์ตœ์‹  ์Šคํ† ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ
708
- stories = get_recent_stories()
709
-
710
- # ๊ฒฐ๊ณผ ์—…๋ฐ์ดํŠธ
711
- outputs = [gr.update(value=f"์ด {len(stories)}๊ฐœ์˜ ํฌ์ŠคํŠธ๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค.", visible=True)]
712
-
713
- for idx, comp in enumerate(hn_article_components):
714
- if idx < len(stories):
715
- story = stories[idx]
716
- outputs.extend([
717
- gr.update(visible=True),
718
- gr.update(value=f"### [{story.get('title', 'Untitled')}]({story.get('url', '#')})"),
719
- gr.update(value=f"**์ž‘์„ฑ์ž:** {story.get('by', 'unknown')} | **์‹œ๊ฐ„:** {format_hn_time(story.get('time', 0))} | **์ ์ˆ˜:** {story.get('score', 0)} | **๋Œ“๊ธ€:** {len(story.get('kids', []))}๊ฐœ")
720
- ])
721
- else:
722
- outputs.extend([
723
- gr.update(visible=False),
724
- gr.update(),
725
- gr.update()
726
- ])
727
-
728
- return outputs
729
-
730
- # ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
731
- hn_outputs = [status_message_hn]
732
- for comp in hn_article_components:
733
- hn_outputs.extend([
734
- comp['group'],
735
- comp['title'],
736
- comp['info']
737
- ])
738
-
739
- refresh_button.click(
740
- refresh_hn_stories,
741
- outputs=hn_outputs
742
- )
743
 
744
  iface.launch(auth=("it1","chosun1"))
 
311
  return format_results_from_raw(response_data)
312
 
313
 
314
+ # Hacker News API ๊ด€๋ จ ํ•จ์ˆ˜๋“ค ๋จผ์ € ์ถ”๊ฐ€
315
  def get_hn_item(item_id):
316
  """๊ฐœ๋ณ„ ์•„์ดํ…œ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ"""
317
  try:
 
323
  def get_recent_stories():
324
  """์ตœ์‹  ์Šคํ† ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ"""
325
  try:
 
326
  response = requests.get("https://hacker-news.firebaseio.com/v0/newstories.json")
327
  story_ids = response.json()
328
 
 
329
  recent_stories = []
330
  current_time = datetime.now().timestamp()
331
  day_ago = current_time - (24 * 60 * 60)
 
335
  if story and 'time' in story and story['time'] > day_ago:
336
  recent_stories.append(story)
337
 
338
+ if len(recent_stories) >= 100:
 
339
  break
340
 
341
  return recent_stories
 
351
  except:
352
  return "Unknown time"
353
 
354
+ def refresh_hn_stories():
355
+ """Hacker News ์Šคํ† ๋ฆฌ ์ƒˆ๋กœ๊ณ ์นจ"""
356
+ status_msg = "Hacker News ํฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘..."
357
+
358
+ outputs = [gr.update(value=status_msg, visible=True)]
359
+
360
+ # ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ ์ดˆ๊ธฐํ™”
361
+ for _ in hn_article_components:
362
+ outputs.extend([
363
+ gr.update(visible=False),
364
+ gr.update(),
365
+ gr.update()
366
+ ])
367
+
368
+ # ์ตœ์‹  ์Šคํ† ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ
369
+ stories = get_recent_stories()
370
+
371
+ # ๊ฒฐ๊ณผ ์—…๋ฐ์ดํŠธ
372
+ outputs = [gr.update(value=f"์ด {len(stories)}๊ฐœ์˜ ํฌ์ŠคํŠธ๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค.", visible=True)]
373
+
374
+ for idx, comp in enumerate(hn_article_components):
375
+ if idx < len(stories):
376
+ story = stories[idx]
377
+ outputs.extend([
378
+ gr.update(visible=True),
379
+ gr.update(value=f"### [{story.get('title', 'Untitled')}]({story.get('url', '#')})"),
380
+ gr.update(value=f"**์ž‘์„ฑ์ž:** {story.get('by', 'unknown')} | **์‹œ๊ฐ„:** {format_hn_time(story.get('time', 0))} | **์ ์ˆ˜:** {story.get('score', 0)} | **๋Œ“๊ธ€:** {len(story.get('kids', []))}๊ฐœ")
381
+ ])
382
+ else:
383
+ outputs.extend([
384
+ gr.update(visible=False),
385
+ gr.update(),
386
+ gr.update()
387
+ ])
388
+
389
+ return outputs
390
+
391
 
392
  css = """
393
  footer {visibility: hidden;}
 
491
  'index': i,
492
  })
493
 
494
+ # ์ „์„ธ๊ณ„ ํƒญ
495
  with gr.Tab("์ „์„ธ๊ณ„"):
496
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 67๊ฐœ๊ตญ(ํ•œ๊ตญ ์ œ์™ธ) ์ „์ฒด์— ๋Œ€ํ•ด ๊ตญ๊ฐ€๋ณ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๊ฐ€ ์ตœ๋Œ€ 1000๊ฐœ ์ˆœ์ฐจ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.")
497
  gr.Markdown("๊ตญ๊ฐ€ ์„ ํƒํ›„ ๊ฒ€์ƒ‰์–ด์— 'ํ•œ๊ธ€'์„ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์ง€ ์–ธ์–ด๋กœ ๋ฒˆ์—ญ๋˜์–ด ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ: 'Taiwan' ๊ตญ๊ฐ€ ์„ ํƒํ›„ '์‚ผ์„ฑ' ์ž…๋ ฅ์‹œ 'ไธ‰ๆ˜Ÿ'์œผ๋กœ ์ž๋™ ๊ฒ€์ƒ‰")
498
 
499
  with gr.Column():
 
500
  with gr.Column(elem_id="status_area"):
501
  with gr.Row():
502
  query_global = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
 
505
  status_message_global = gr.Markdown("")
506
  translated_query_display_global = gr.Markdown("")
507
 
 
508
  with gr.Column(elem_id="results_area"):
509
  articles_state_global = gr.State([])
510
 
 
525
  'index': i,
526
  })
527
 
528
+ # AI ๋ฆฌํฌํ„ฐ ํƒญ
529
+ with gr.Tab("AI ๋ฆฌํฌํ„ฐ"):
530
+ gr.Markdown("์ง€๋‚œ 24์‹œ๊ฐ„ ๋™์•ˆ์˜ Hacker News ํฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.")
531
+
532
+ with gr.Column():
533
+ refresh_button = gr.Button("์ƒˆ๋กœ๊ณ ์นจ", variant="primary")
534
+ status_message_hn = gr.Markdown("")
535
+
536
+ with gr.Column(elem_id="hn_results_area"):
537
+ hn_articles_state = gr.State([])
538
+
539
+ hn_article_components = []
540
+ for i in range(100):
541
+ with gr.Group(visible=False) as article_group:
542
+ title = gr.Markdown()
543
+ info = gr.Markdown()
544
+
545
+ hn_article_components.append({
546
+ 'group': article_group,
547
+ 'title': title,
548
+ 'info': info,
549
+ 'index': i,
550
+ })
551
+
552
+
553
+
554
+
555
+
556
+
557
+
558
+ # ๊ธฐ์กด ํ•จ์ˆ˜๋“ค
559
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
560
  status_msg = "๊ฒ€์ƒ‰์„ ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ฆฌ์„ธ์š”..."
561
 
 
691
  outputs[0] = gr.update(value=final_status, visible=True)
692
  yield outputs
693
 
694
+
695
+
696
+
697
+
698
+
699
+ # ๊ตญ๊ฐ€๋ณ„ ํƒญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
700
  search_outputs = [
701
  status_message,
702
  translated_query_display,
 
717
  show_progress=True
718
  )
719
 
720
+ # ์ „์„ธ๊ณ„ ํƒญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
721
  global_search_outputs = [
722
  status_message_global,
723
  translated_query_display_global,
 
736
  outputs=global_search_outputs
737
  )
738
 
739
+ # AI ๋ฆฌํฌํ„ฐ ํƒญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
740
+ hn_outputs = [status_message_hn]
741
+ for comp in hn_article_components:
742
+ hn_outputs.extend([
743
+ comp['group'],
744
+ comp['title'],
745
+ comp['info']
746
+ ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
747
 
748
+ refresh_button.click(
749
+ refresh_hn_stories,
750
+ outputs=hn_outputs
751
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
752
 
753
  iface.launch(auth=("it1","chosun1"))