Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,15 @@ import pycountry
|
|
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 |
|
@@ -16,7 +25,7 @@ for code in COUNTRY_CODES:
|
|
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"
|
@@ -35,21 +44,29 @@ def get_news(keyword, article_count, country):
|
|
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()
|
41 |
news_data = response.json()
|
|
|
|
|
|
|
|
|
|
|
42 |
except requests.RequestException as e:
|
43 |
-
return f"<p style='color: red;'>Error fetching news: {str(e)}</p>"
|
44 |
|
45 |
if news_data['status'] != 'ok':
|
46 |
-
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p>"
|
47 |
|
48 |
articles = news_data['articles']
|
49 |
|
50 |
if not articles:
|
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:
|
@@ -66,6 +83,7 @@ def get_news(keyword, article_count, country):
|
|
66 |
</div>
|
67 |
"""
|
68 |
|
|
|
69 |
return html_output
|
70 |
|
71 |
iface = gr.Interface(
|
@@ -76,8 +94,8 @@ iface = gr.Interface(
|
|
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 |
)
|
83 |
|
|
|
6 |
# NewsAPI key (이것을 실제 API 키로 대체해야 합니다)
|
7 |
API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
|
8 |
|
9 |
+
import gradio as gr
|
10 |
+
import requests
|
11 |
+
from datetime import datetime, timedelta
|
12 |
+
import pycountry
|
13 |
+
import json
|
14 |
+
|
15 |
+
# NewsAPI key (이것을 실제 API 키로 대체해야 합니다)
|
16 |
+
API_KEY = "YOUR_NEWSAPI_KEY_HERE"
|
17 |
+
|
18 |
# 국가 코드 리스트
|
19 |
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()
|
20 |
|
|
|
25 |
country = pycountry.countries.get(alpha_2=code.upper())
|
26 |
COUNTRIES[code] = country.name
|
27 |
except AttributeError:
|
28 |
+
COUNTRIES[code] = code.upper()
|
29 |
|
30 |
def get_news(keyword, article_count, country):
|
31 |
base_url = "https://newsapi.org/v2/top-headlines" if country != 'all' else "https://newsapi.org/v2/everything"
|
|
|
44 |
if country != 'all':
|
45 |
params['country'] = country
|
46 |
|
47 |
+
debug_info = f"API Request URL: {base_url}\n"
|
48 |
+
debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
|
49 |
+
|
50 |
try:
|
51 |
response = requests.get(base_url, params=params, timeout=10)
|
52 |
response.raise_for_status()
|
53 |
news_data = response.json()
|
54 |
+
|
55 |
+
debug_info += f"API Response Status: {response.status_code}\n"
|
56 |
+
debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
|
57 |
+
debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"
|
58 |
+
|
59 |
except requests.RequestException as e:
|
60 |
+
return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>"
|
61 |
|
62 |
if news_data['status'] != 'ok':
|
63 |
+
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>"
|
64 |
|
65 |
articles = news_data['articles']
|
66 |
|
67 |
if not articles:
|
68 |
return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' within the last 48 hours.<br>"
|
69 |
+
f"Try a different keyword or check back later.</p><pre>{debug_info}</pre>")
|
70 |
|
71 |
html_output = f"<h2>News results for '{keyword}' in {COUNTRIES[country]}</h2>"
|
72 |
for article in articles:
|
|
|
83 |
</div>
|
84 |
"""
|
85 |
|
86 |
+
html_output += f"<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>"
|
87 |
return html_output
|
88 |
|
89 |
iface = gr.Interface(
|
|
|
94 |
gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
|
95 |
],
|
96 |
outputs=gr.HTML(),
|
97 |
+
title="Debug: Enhanced Visual News Search",
|
98 |
+
description="Search for news articles from the last 48 hours using NewsAPI. Debug information is included.",
|
99 |
theme=gr.themes.Soft()
|
100 |
)
|
101 |
|