Spaces:
Running
Running
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from datetime import datetime, timedelta
|
4 |
-
import pycountry
|
5 |
-
import json
|
6 |
|
7 |
-
#
|
8 |
-
API_KEY = "
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
except requests.RequestException as e:
|
56 |
-
return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>"
|
57 |
-
|
58 |
-
if news_data['status'] != 'ok':
|
59 |
-
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>"
|
60 |
-
|
61 |
-
articles = news_data['articles']
|
62 |
-
|
63 |
-
# removed ๊ธฐ์ฌ ํํฐ๋ง
|
64 |
-
filtered_articles = [article for article in articles if article.get('title') != '[Removed]' and article.get('description') != '[Removed]']
|
65 |
-
|
66 |
-
if not filtered_articles:
|
67 |
-
if country_code != 'all':
|
68 |
-
# ํน์ ๊ตญ๊ฐ์์ ๊ฒฐ๊ณผ๊ฐ ์์ ๊ฒฝ์ฐ, ์ ์ฒด ๊ตญ๊ฐ์์ ๊ฒ์
|
69 |
-
return get_news(keyword, article_count, 'All Countries')
|
70 |
-
else:
|
71 |
-
related_keywords = get_related_keywords(keyword)
|
72 |
-
suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>"
|
73 |
-
f"<p>Suggestions:</p>"
|
74 |
-
f"<ul>"
|
75 |
-
f"<li>Try one of these related keywords: {', '.join(related_keywords)}</li>"
|
76 |
-
f"<li>Increase the number of articles</li>"
|
77 |
-
f"<li>Check for any spelling errors in your keyword</li>"
|
78 |
-
f"</ul>")
|
79 |
-
return suggestions + f"<pre>{debug_info}</pre>"
|
80 |
-
|
81 |
-
html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
|
82 |
-
if country_code == 'all':
|
83 |
-
html_output += "<p><em>Showing results from all countries</em></p>"
|
84 |
else:
|
85 |
-
|
86 |
-
|
87 |
-
html_output += f"<p>Found {len(filtered_articles)} relevant articles (after removing any '[Removed]' articles)</p>"
|
88 |
-
|
89 |
-
for article in filtered_articles[:article_count]: # ์์ฒญํ ๊ธฐ์ฌ ์๋งํผ๋ง ํ์
|
90 |
-
title = article['title']
|
91 |
-
link = article['url']
|
92 |
-
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
|
93 |
-
source = article.get('source', {}).get('name', 'Unknown Source')
|
94 |
-
description = article.get('description', 'No description available')
|
95 |
-
|
96 |
-
html_output += f"""
|
97 |
-
<div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>
|
98 |
-
<h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3>
|
99 |
-
<p style='color: #006621;'>{source}</p>
|
100 |
-
<p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p>
|
101 |
-
<p>{description}</p>
|
102 |
-
</div>
|
103 |
-
"""
|
104 |
|
105 |
-
|
106 |
-
return html_output
|
107 |
|
|
|
108 |
iface = gr.Interface(
|
109 |
-
fn=
|
110 |
inputs=[
|
111 |
-
gr.Textbox(label="Enter keyword"),
|
112 |
-
gr.
|
113 |
-
gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
|
114 |
],
|
115 |
outputs=gr.HTML(),
|
116 |
-
title="
|
117 |
-
description="Search for news articles
|
118 |
-
theme=gr.themes.Soft()
|
119 |
)
|
120 |
|
|
|
121 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from datetime import datetime, timedelta
|
|
|
|
|
4 |
|
5 |
+
# Google Custom Search API ํค์ ๊ฒ์ ์์ง ID
|
6 |
+
API_KEY = "YOUR_API_KEY_HERE"
|
7 |
+
SEARCH_ENGINE_ID = "YOUR_SEARCH_ENGINE_ID_HERE"
|
8 |
+
|
9 |
+
# ์ง์๋๋ ๊ตญ๊ฐ ๋ฆฌ์คํธ
|
10 |
+
COUNTRIES = {
|
11 |
+
'United States': 'countryUS', 'United Kingdom': 'countryGB', 'India': 'countryIN',
|
12 |
+
'Australia': 'countryAU', 'Canada': 'countryCA', 'Germany': 'countryDE',
|
13 |
+
'France': 'countryFR', 'Italy': 'countryIT', 'Spain': 'countryES', 'Brazil': 'countryBR',
|
14 |
+
'Mexico': 'countryMX', 'Argentina': 'countryAR', 'Japan': 'countryJP',
|
15 |
+
'South Korea': 'countryKR', 'Russia': 'countryRU', 'China': 'countryCN',
|
16 |
+
'Netherlands': 'countryNL', 'Sweden': 'countrySE', 'Poland': 'countryPL', 'Turkey': 'countryTR'
|
17 |
+
}
|
18 |
+
|
19 |
+
def search_news(keyword, country):
|
20 |
+
# 24์๊ฐ ์ ๋ ์ง ๊ณ์ฐ
|
21 |
+
one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
|
22 |
+
|
23 |
+
# API ์์ฒญ URL ๋ฐ ๋งค๊ฐ๋ณ์ ์ค์
|
24 |
+
url = "https://www.googleapis.com/customsearch/v1"
|
25 |
+
params = {
|
26 |
+
'key': API_KEY,
|
27 |
+
'cx': SEARCH_ENGINE_ID,
|
28 |
+
'q': keyword,
|
29 |
+
'dateRestrict': 'd1', # ์ต๊ทผ 1์ผ ๋ด ๊ฒฐ๊ณผ๋ง
|
30 |
+
'lr': 'lang_en', # ์์ด ๊ฒฐ๊ณผ๋ง
|
31 |
+
'sort': 'date', # ๋ ์ง์ ์ ๋ ฌ
|
32 |
+
'num': 10, # ์ต๋ 10๊ฐ ๊ฒฐ๊ณผ
|
33 |
+
'siteSearch': 'news.google.com', # Google News๋ก ์ ํ
|
34 |
+
}
|
35 |
+
|
36 |
+
if country != 'All Countries':
|
37 |
+
params['cr'] = COUNTRIES[country]
|
38 |
+
|
39 |
+
# API ์์ฒญ
|
40 |
+
response = requests.get(url, params=params)
|
41 |
+
results = response.json()
|
42 |
+
|
43 |
+
# ๊ฒฐ๊ณผ ํฌ๋งทํ
|
44 |
+
formatted_results = ""
|
45 |
+
if 'items' in results:
|
46 |
+
for item in results['items']:
|
47 |
+
title = item['title']
|
48 |
+
link = item['link']
|
49 |
+
snippet = item['snippet']
|
50 |
+
formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
|
51 |
+
formatted_results += f"<p>{snippet}</p><br>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
else:
|
53 |
+
formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
return formatted_results
|
|
|
56 |
|
57 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
58 |
iface = gr.Interface(
|
59 |
+
fn=search_news,
|
60 |
inputs=[
|
61 |
+
gr.Textbox(label="Enter keyword (in English)"),
|
62 |
+
gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country")
|
|
|
63 |
],
|
64 |
outputs=gr.HTML(),
|
65 |
+
title="Google News Search",
|
66 |
+
description="Search for news articles from the last 24 hours using Google Custom Search API."
|
|
|
67 |
)
|
68 |
|
69 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
70 |
iface.launch()
|