seawolf2357 commited on
Commit
9d69618
ยท
verified ยท
1 Parent(s): ed4ef14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -7,12 +7,20 @@ import json
7
  # NewsAPI key
8
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
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
  COUNTRIES = {'all': 'All Countries'}
13
  COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})
14
 
15
- def get_news(keyword, article_count, country, extend_search=False):
 
 
 
 
 
 
 
16
  country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
17
 
18
  if country_code == 'all':
@@ -60,19 +68,24 @@ def get_news(keyword, article_count, country, extend_search=False):
60
  if not articles:
61
  if not extend_search:
62
  return get_news(keyword, article_count, country, extend_search=True)
 
 
63
  else:
64
- return (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.<br>"
65
- f"Suggestions:</p>"
66
- f"<ul>"
67
- f"<li>Try a different keyword</li>"
68
- f"<li>Expand your search to all countries</li>"
69
- f"<li>Increase the number of articles</li>"
70
- f"</ul>"
71
- f"<pre>{debug_info}</pre>")
 
72
 
73
  html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
74
  if extend_search:
75
  html_output += "<p><em>Note: Search range was extended due to limited recent results.</em></p>"
 
 
76
 
77
  for article in articles:
78
  title = article['title']
@@ -101,8 +114,8 @@ iface = gr.Interface(
101
  gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
102
  ],
103
  outputs=gr.HTML(),
104
- title="Enhanced Visual News Search",
105
- description="Search for news articles using NewsAPI. If recent results are limited, the search range will be automatically extended.",
106
  theme=gr.themes.Soft()
107
  )
108
 
 
7
  # NewsAPI key
8
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
9
 
10
+
11
  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()
12
 
13
  COUNTRIES = {'all': 'All Countries'}
14
  COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})
15
 
16
+ def get_related_keywords(keyword):
17
+ # ๊ฐ„๋‹จํ•œ ๊ด€๋ จ ํ‚ค์›Œ๋“œ ์ƒ์„ฑ ๋กœ์ง (์‹ค์ œ๋กœ๋Š” ๋” ๋ณต์žกํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‚˜ API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค)
18
+ return [f"{keyword} news", f"{keyword} technology", f"{keyword} company", f"latest {keyword}"]
19
+
20
+ def get_news(keyword, article_count, country, extend_search=False, search_all_countries=False):
21
+ if search_all_countries:
22
+ country = 'All Countries'
23
+
24
  country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
25
 
26
  if country_code == 'all':
 
68
  if not articles:
69
  if not extend_search:
70
  return get_news(keyword, article_count, country, extend_search=True)
71
+ elif not search_all_countries and country != 'All Countries':
72
+ return get_news(keyword, article_count, 'All Countries', extend_search=True, search_all_countries=True)
73
  else:
74
+ related_keywords = get_related_keywords(keyword)
75
+ suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>"
76
+ f"<p>Suggestions:</p>"
77
+ f"<ul>"
78
+ f"<li>Try one of these related keywords: {', '.join(related_keywords)}</li>"
79
+ f"<li>Increase the number of articles</li>"
80
+ f"<li>Check for any spelling errors in your keyword</li>"
81
+ f"</ul>")
82
+ return suggestions + f"<pre>{debug_info}</pre>"
83
 
84
  html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
85
  if extend_search:
86
  html_output += "<p><em>Note: Search range was extended due to limited recent results.</em></p>"
87
+ if search_all_countries:
88
+ html_output += "<p><em>Note: Search was expanded to all countries due to limited results in the specified country.</em></p>"
89
 
90
  for article in articles:
91
  title = article['title']
 
114
  gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
115
  ],
116
  outputs=gr.HTML(),
117
+ title="Advanced Visual News Search",
118
+ description="Search for news articles using NewsAPI. The search will automatically extend if results are limited.",
119
  theme=gr.themes.Soft()
120
  )
121