seawolf2357 commited on
Commit
b403d83
ยท
verified ยท
1 Parent(s): 84aff1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -36
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
- import concurrent.futures
5
 
6
  # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
7
  API_KEY = "AIzaSyDUz3wkGal0ewRtPlzeMit88bV4hS4ZIVY"
@@ -68,7 +68,8 @@ COUNTRIES = {
68
  'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
69
  }
70
 
71
- def search_news(keyword, country, search_all_countries=False):
 
72
  # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
73
  one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
74
 
@@ -85,7 +86,7 @@ def search_news(keyword, country, search_all_countries=False):
85
  'siteSearch': 'news.google.com', # Google News๋กœ ์ œํ•œ
86
  }
87
 
88
- if not search_all_countries and country != 'All Countries':
89
  params['cr'] = COUNTRIES[country]
90
 
91
  # API ์š”์ฒญ
@@ -106,46 +107,16 @@ def search_news(keyword, country, search_all_countries=False):
106
 
107
  return formatted_results
108
 
109
- def search_all_countries(keyword):
110
- def search_country(country_name):
111
- result = search_news(keyword, country_name)
112
- if "No news found" not in result:
113
- return country_name, result
114
- return None
115
-
116
- with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
117
- future_to_country = {executor.submit(search_country, country): country for country in COUNTRIES.keys()}
118
- matching_countries = []
119
- for future in concurrent.futures.as_completed(future_to_country):
120
- result = future.result()
121
- if result:
122
- matching_countries.append(result)
123
-
124
- if matching_countries:
125
- output = "<h2>Countries with matching news:</h2>"
126
- for country, news in matching_countries:
127
- output += f"<h3>{country}</h3>{news}<hr>"
128
- return output
129
- else:
130
- return f"No news found for '{keyword}' in any country within the last 24 hours."
131
-
132
- def news_search(keyword, country, search_all):
133
- if search_all:
134
- return search_all_countries(keyword)
135
- else:
136
- return search_news(keyword, country)
137
-
138
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
139
  iface = gr.Interface(
140
- fn=news_search,
141
  inputs=[
142
  gr.Textbox(label="Enter keyword (in English)"),
143
- gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country"),
144
- gr.Checkbox(label="Search all countries")
145
  ],
146
  outputs=gr.HTML(),
147
  title="Google News Search",
148
- description="Search for news articles from the last 24 hours using Google Custom Search API. You can search in a specific country or across all countries."
149
  )
150
 
151
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ (๊ณต๊ฐœ ๋งํฌ ์ƒ์„ฑ)
 
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
+
5
 
6
  # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
7
  API_KEY = "AIzaSyDUz3wkGal0ewRtPlzeMit88bV4hS4ZIVY"
 
68
  'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
69
  }
70
 
71
+
72
+ def search_news(keyword, country):
73
  # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
74
  one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
75
 
 
86
  'siteSearch': 'news.google.com', # Google News๋กœ ์ œํ•œ
87
  }
88
 
89
+ if country != 'All Countries':
90
  params['cr'] = COUNTRIES[country]
91
 
92
  # API ์š”์ฒญ
 
107
 
108
  return formatted_results
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
111
  iface = gr.Interface(
112
+ fn=search_news,
113
  inputs=[
114
  gr.Textbox(label="Enter keyword (in English)"),
115
+ gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country")
 
116
  ],
117
  outputs=gr.HTML(),
118
  title="Google News Search",
119
+ description="Search for news articles from the last 24 hours using Google Custom Search API."
120
  )
121
 
122
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ (๊ณต๊ฐœ ๋งํฌ ์ƒ์„ฑ)