Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# ββ Configuration ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
GNEWS_API_KEY = os.environ.get("GNEWS_API_KEY")
|
| 7 |
+
if not GNEWS_API_KEY:
|
| 8 |
+
raise ValueError(
|
| 9 |
+
"GNEWS_API_KEY not found. "
|
| 10 |
+
"Add it as a Secret in your Space settings (Settings β Secrets)."
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
BASE_URL = "https://gnews.io/api/v4/top-headlines"
|
| 14 |
+
|
| 15 |
+
# Country codes GNews supports β ten representative choices
|
| 16 |
+
COUNTRIES = {
|
| 17 |
+
"United States": "us",
|
| 18 |
+
"United Kingdom": "gb",
|
| 19 |
+
"Canada": "ca",
|
| 20 |
+
"Australia": "au",
|
| 21 |
+
"India": "in",
|
| 22 |
+
"Germany": "de",
|
| 23 |
+
"France": "fr",
|
| 24 |
+
"Japan": "jp",
|
| 25 |
+
"Brazil": "br",
|
| 26 |
+
"South Africa": "za",
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def fetch_headlines(country_name):
|
| 31 |
+
"""Fetch top headlines for a chosen country from GNews."""
|
| 32 |
+
if not country_name:
|
| 33 |
+
return "Please select a country."
|
| 34 |
+
|
| 35 |
+
country_code = COUNTRIES[country_name]
|
| 36 |
+
|
| 37 |
+
params = {
|
| 38 |
+
"token": GNEWS_API_KEY,
|
| 39 |
+
"country": country_code,
|
| 40 |
+
"max": 10,
|
| 41 |
+
"lang": "en",
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
resp = requests.get(BASE_URL, params=params, timeout=15)
|
| 46 |
+
resp.raise_for_status()
|
| 47 |
+
data = resp.json()
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error fetching news: {e}"
|
| 50 |
+
|
| 51 |
+
articles = data.get("articles", [])
|
| 52 |
+
if not articles:
|
| 53 |
+
return f"No headlines found for {country_name}."
|
| 54 |
+
|
| 55 |
+
lines = []
|
| 56 |
+
for i, article in enumerate(articles, 1):
|
| 57 |
+
title = article.get("title", "No title")
|
| 58 |
+
source = article.get("source", {}).get("name", "Unknown source")
|
| 59 |
+
url = article.get("url", "")
|
| 60 |
+
published = article.get("publishedAt", "")[:10] # date portion
|
| 61 |
+
|
| 62 |
+
lines.append(
|
| 63 |
+
f"### {i}. {title}\n"
|
| 64 |
+
f"**Source:** {source} Β· **Date:** {published}\n"
|
| 65 |
+
f"[Read full article]({url})\n"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return "\n".join(lines)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# ββ Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
+
with gr.Blocks(title="World Headlines") as demo:
|
| 73 |
+
gr.Markdown("# World Headlines")
|
| 74 |
+
gr.Markdown(
|
| 75 |
+
"Pick a country to see its latest English-language headlines, "
|
| 76 |
+
"powered by the [GNews API](https://gnews.io)."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
country = gr.Dropdown(
|
| 80 |
+
choices=list(COUNTRIES.keys()),
|
| 81 |
+
label="Country",
|
| 82 |
+
value="United States",
|
| 83 |
+
)
|
| 84 |
+
btn = gr.Button("Get Headlines", variant="primary")
|
| 85 |
+
output = gr.Markdown(label="Headlines")
|
| 86 |
+
|
| 87 |
+
btn.click(fn=fetch_headlines, inputs=country, outputs=output)
|
| 88 |
+
|
| 89 |
+
demo.launch()
|