Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
1d7a9d5
ยท
verified ยท
1 Parent(s): b659602

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -1,13 +1,25 @@
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
- import json
5
 
6
  # NewsAPI key (์ด๊ฒƒ์„ ์‹ค์ œ API ํ‚ค๋กœ ๋Œ€์ฒดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค)
7
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
8
 
9
- def get_news(keyword):
10
- base_url = "https://newsapi.org/v2/everything"
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
13
 
@@ -16,9 +28,13 @@ def get_news(keyword):
16
  'q': keyword,
17
  'from': two_days_ago,
18
  'language': 'en',
19
- 'sortBy': 'publishedAt'
 
20
  }
21
-
 
 
 
22
  try:
23
  response = requests.get(base_url, params=params, timeout=10)
24
  response.raise_for_status()
@@ -35,8 +51,8 @@ def get_news(keyword):
35
  return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' within the last 48 hours.<br>"
36
  f"Try a different keyword or check back later.</p>")
37
 
38
- html_output = f"<h2>News results for '{keyword}'</h2>"
39
- for article in articles[:10]: # ์ตœ๋Œ€ 10๊ฐœ์˜ ๊ธฐ์‚ฌ๋งŒ ํ‘œ์‹œ
40
  title = article['title']
41
  link = article['url']
42
  pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
@@ -55,10 +71,12 @@ def get_news(keyword):
55
  iface = gr.Interface(
56
  fn=get_news,
57
  inputs=[
58
- gr.Textbox(label="Enter keyword")
 
 
59
  ],
60
  outputs=gr.HTML(),
61
- title="Visual News Search",
62
  description="Search for news articles from the last 48 hours using NewsAPI.",
63
  theme=gr.themes.Soft()
64
  )
 
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
+ import pycountry
5
 
6
  # NewsAPI key (์ด๊ฒƒ์„ ์‹ค์ œ API ํ‚ค๋กœ ๋Œ€์ฒดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค)
7
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
8
 
9
+ # ๊ตญ๊ฐ€ ์ฝ”๋“œ ๋ฆฌ์ŠคํŠธ
10
+ COUNTRY_CODES = "ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za".split()
11
+
12
+ # ๊ตญ๊ฐ€ ์ฝ”๋“œ์™€ ์ด๋ฆ„ ๋งคํ•‘
13
+ COUNTRIES = {'all': 'All Countries'}
14
+ for code in COUNTRY_CODES:
15
+ try:
16
+ country = pycountry.countries.get(alpha_2=code.upper())
17
+ COUNTRIES[code] = country.name
18
+ except AttributeError:
19
+ COUNTRIES[code] = code.upper() # ๊ตญ๊ฐ€ ์ด๋ฆ„์„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ์ฝ”๋“œ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ์‚ฌ์šฉ
20
+
21
+ def get_news(keyword, article_count, country):
22
+ base_url = "https://newsapi.org/v2/top-headlines" if country != 'all' else "https://newsapi.org/v2/everything"
23
 
24
  two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
25
 
 
28
  'q': keyword,
29
  'from': two_days_ago,
30
  'language': 'en',
31
+ 'sortBy': 'publishedAt',
32
+ 'pageSize': article_count
33
  }
34
+
35
+ if country != 'all':
36
+ params['country'] = country
37
+
38
  try:
39
  response = requests.get(base_url, params=params, timeout=10)
40
  response.raise_for_status()
 
51
  return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' within the last 48 hours.<br>"
52
  f"Try a different keyword or check back later.</p>")
53
 
54
+ html_output = f"<h2>News results for '{keyword}' in {COUNTRIES[country]}</h2>"
55
+ for article in articles:
56
  title = article['title']
57
  link = article['url']
58
  pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
 
71
  iface = gr.Interface(
72
  fn=get_news,
73
  inputs=[
74
+ gr.Textbox(label="Enter keyword"),
75
+ gr.Slider(minimum=10, maximum=100, step=10, value=10, label="Number of Articles"),
76
+ gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
77
  ],
78
  outputs=gr.HTML(),
79
+ title="Enhanced Visual News Search",
80
  description="Search for news articles from the last 48 hours using NewsAPI.",
81
  theme=gr.themes.Soft()
82
  )