Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,32 +21,9 @@ TIMEOUT_LIMIT = 30 # Maximum time in seconds to process summaries
|
|
21 |
|
22 |
# News sources
|
23 |
NEWS_SOURCES = {
|
24 |
-
"Technology": {
|
25 |
-
|
26 |
-
|
27 |
-
},
|
28 |
-
"Business": {
|
29 |
-
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Business.xml",
|
30 |
-
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=business-finance&post_type=best"
|
31 |
-
},
|
32 |
-
"Science": {
|
33 |
-
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Science.xml"
|
34 |
-
},
|
35 |
-
"World News": {
|
36 |
-
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/World.xml",
|
37 |
-
"BBC": "http://feeds.bbci.co.uk/news/world/rss.xml",
|
38 |
-
"CNN": "http://rss.cnn.com/rss/edition_world.rss",
|
39 |
-
"reutersagency": "https://www.reutersagency.com/feed/?taxonomy=best-regions&post_type=best"
|
40 |
-
},
|
41 |
-
"Sports": {
|
42 |
-
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Sports.xml",
|
43 |
-
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=sports&post_type=best"
|
44 |
-
},
|
45 |
-
"Health": {
|
46 |
-
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Health.xml",
|
47 |
-
"politico": "http://rss.politico.com/healthcare.xml",
|
48 |
-
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=health&post_type=best"
|
49 |
-
}
|
50 |
}
|
51 |
|
52 |
# Initialize cache
|
@@ -84,6 +61,7 @@ def fetch_rss_news(categories):
|
|
84 |
try:
|
85 |
feed = feedparser.parse(url)
|
86 |
for entry in feed.entries:
|
|
|
87 |
published = datetime(*entry.published_parsed[:6], tzinfo=pytz.UTC)
|
88 |
if published > cutoff_time:
|
89 |
articles.append({
|
@@ -114,6 +92,33 @@ def summarize_text(text):
|
|
114 |
logging.error(f"Summarization failed: {e}")
|
115 |
return "Summary unavailable."
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
def generate_user_summary(name):
|
118 |
"""Generate a personalized news summary based on user preferences."""
|
119 |
# Load preferences
|
@@ -135,16 +140,10 @@ def generate_user_summary(name):
|
|
135 |
if not articles:
|
136 |
return "No recent news found in your selected categories."
|
137 |
|
138 |
-
# Summarize articles
|
139 |
-
summaries =
|
140 |
-
for article in articles:
|
141 |
-
summary = summarize_text(article["description"])
|
142 |
-
summaries.append(f"""**{article['title']}**
|
143 |
-
**Category:** {article['category']} | **Source:** {article['source']} | **Published:** {article['published'].strftime('%Y-%m-%d %H:%M')}
|
144 |
-
{summary}
|
145 |
-
[Read more]({article['link']})
|
146 |
-
---""")
|
147 |
|
|
|
148 |
return "\n\n".join(summaries) if summaries else "No summaries available."
|
149 |
|
150 |
# Gradio interface
|
@@ -191,15 +190,3 @@ if __name__ == "__main__":
|
|
191 |
|
192 |
|
193 |
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
21 |
|
22 |
# News sources
|
23 |
NEWS_SOURCES = {
|
24 |
+
"Technology": {"NYTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Technology.xml"},
|
25 |
+
"Business": {"Reuters": "https://www.reutersagency.com/feed/?best-topics=business-finance&post_type=best"},
|
26 |
+
"World": {"BBC": "http://feeds.bbci.co.uk/news/world/rss.xml"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
}
|
28 |
|
29 |
# Initialize cache
|
|
|
61 |
try:
|
62 |
feed = feedparser.parse(url)
|
63 |
for entry in feed.entries:
|
64 |
+
# Parse publication time
|
65 |
published = datetime(*entry.published_parsed[:6], tzinfo=pytz.UTC)
|
66 |
if published > cutoff_time:
|
67 |
articles.append({
|
|
|
92 |
logging.error(f"Summarization failed: {e}")
|
93 |
return "Summary unavailable."
|
94 |
|
95 |
+
def summarize_articles(articles):
|
96 |
+
"""Summarize all fetched articles."""
|
97 |
+
summaries = []
|
98 |
+
for article in articles:
|
99 |
+
try:
|
100 |
+
content = article["description"]
|
101 |
+
title = article["title"]
|
102 |
+
category = article["category"]
|
103 |
+
source = article["source"]
|
104 |
+
link = article["link"]
|
105 |
+
published = article["published"].strftime('%Y-%m-%d %H:%M')
|
106 |
+
|
107 |
+
# Summarize article content
|
108 |
+
summary = summarize_text(content)
|
109 |
+
|
110 |
+
if summary:
|
111 |
+
summaries.append(f"""
|
112 |
+
**{title}**
|
113 |
+
**Category:** {category} | **Source:** {source} | **Published:** {published}
|
114 |
+
{summary}
|
115 |
+
[Read more]({link})
|
116 |
+
---""")
|
117 |
+
except Exception as e:
|
118 |
+
logging.error(f"Error summarizing article: {e}")
|
119 |
+
continue
|
120 |
+
return summaries
|
121 |
+
|
122 |
def generate_user_summary(name):
|
123 |
"""Generate a personalized news summary based on user preferences."""
|
124 |
# Load preferences
|
|
|
140 |
if not articles:
|
141 |
return "No recent news found in your selected categories."
|
142 |
|
143 |
+
# Summarize all articles
|
144 |
+
summaries = summarize_articles(articles)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
+
# Combine and return summaries
|
147 |
return "\n\n".join(summaries) if summaries else "No summaries available."
|
148 |
|
149 |
# Gradio interface
|
|
|
190 |
|
191 |
|
192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|