Spaces:
Running
Running
File size: 11,738 Bytes
41c3554 912f3cb 9f9e0f1 db7a151 8379514 0e97c1f 9b9dfb3 a21b264 efd9487 2cc6057 a21b264 b2a401d a21b264 0e97c1f 372c5e7 efd9487 372c5e7 efd9487 fe71d78 372c5e7 efd9487 372c5e7 92e609e efd9487 372c5e7 4e3626e efd9487 41e7bb5 372c5e7 c4b815a fa5e64a b2a401d efd9487 b2a401d c2ec36e b2a401d 397603a b2a401d c2ec36e 397603a 9b9dfb3 9dbc598 a21b264 9dbc598 a21b264 9dbc598 efd9487 a21b264 9dbc598 efd9487 c4b815a 9b9dfb3 4e3626e cc8ee0f 9b9dfb3 0908597 92e609e 0908597 9dbc598 0908597 efd9487 0908597 2c245c6 c2ec36e c4b815a 0908597 9dbc598 0908597 7c1aeac db3de66 7c1aeac 91e6d60 5f7526f |
1 2 3 4 5 6 7 8 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
import gradio as gr
from transformers import pipeline
import feedparser
from datetime import datetime, timedelta
import pytz
from bs4 import BeautifulSoup
import hashlib
import threading
import logging
import requests
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Global settings
OPENROUTER_API_KEY = "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"
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
SUMMARIZER_MODELS = {
"Default (facebook/bart-large-cnn)": "local_bart",
"Free Model (distilbart-cnn-6-6)": "local_distilbart",
"OpenRouter (Claude-3)": "anthropic/claude-3-haiku"
}
CACHE_SIZE = 500
RSS_FETCH_INTERVAL = timedelta(hours=8)
ARTICLE_LIMIT = 5
# Updated categories and news sources
CATEGORIES = ["Technology", "Business", "Science", "World News", "Sports", "Health"]
NEWS_SOURCES = {
"Technology": {
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Technology.xml",
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=tech&post_type=best",
"alarabiya arabic": "https://www.alarabiya.net/feed/rss2/ar/technology.xml",
},
"Business": {
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Business.xml",
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=business-finance&post_type=best",
"alwatanvoice arabic": "https://feeds.alwatanvoice.com/ar/business.xml",
},
"Science": {
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Science.xml"
},
"World News": {
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/World.xml",
"BBC": "http://feeds.bbci.co.uk/news/world/rss.xml",
"CNN": "http://rss.cnn.com/rss/edition_world.rss",
"reutersagency": "https://www.reutersagency.com/feed/?taxonomy=best-regions&post_type=best",
"france24 arabic": "https://www.france24.com/ar/rss",
"aljazera arabic": "https://www.aljazeera.net/aljazeerarss/a7c186be-1baa-4bd4-9d80-a84db769f779/73d0e1b4-532f-45ef-b135-bfdff8b8cab9",
},
"Sports": {
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Sports.xml",
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=sports&post_type=best",
"france24 arabic": "https://www.france24.com/ar/%D8%B1%D9%8A%D8%A7%D8%B6%D8%A9/rss",
},
"Health": {
"TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Health.xml",
"politico": "http://rss.politico.com/healthcare.xml",
"reutersagency": "https://www.reutersagency.com/feed/?best-topics=health&post_type=best"
},
}
class NewsCache:
def __init__(self, size):
self.cache = {}
self.size = size
self.lock = threading.Lock()
def get(self, key):
with self.lock:
return self.cache.get(key)
def set(self, key, value):
with self.lock:
if len(self.cache) >= self.size:
oldest_key = next(iter(self.cache))
del self.cache[oldest_key]
self.cache[key] = value
cache = NewsCache(CACHE_SIZE)
def detect_language(text):
"""Detect if the text is primarily Arabic"""
if not text:
return False
arabic_chars = len([c for c in text if '\u0600' <= c <= '\u06FF'])
return (arabic_chars / len(text)) > 0.5
def summarize_text(text, model_name):
try:
content_hash = hashlib.md5(text.encode()).hexdigest()
cached_summary = cache.get(content_hash)
if cached_summary:
logger.info("Using cached summary")
return cached_summary
is_arabic = detect_language(text)
if is_arabic or model_name == "OpenRouter (Claude-3)":
logger.info("Using OpenRouter for summarization")
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"HTTP-Referer": "https://localhost:7860", # Replace with your actual domain
"X-Title": "News Summarizer App",
"Content-Type": "application/json"
}
prompt = f"Please provide a concise summary of the following news article in the same language as the original text. Keep the summary brief and focused on key points:\n\n{text}"
data = {
"model": "anthropic/claude-3-haiku",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 150
}
response = requests.post(OPENROUTER_API_URL, headers=headers, json=data)
response.raise_for_status()
summary = response.json()["choices"][0]["message"]["content"]
else:
logger.info("Using local model for summarization")
model_path = "facebook/bart-large-cnn" if model_name == "Default (facebook/bart-large-cnn)" else "sshleifer/distilbart-cnn-6-6"
summarizer = pipeline("summarization", model=model_path, device=-1)
result = summarizer(text, max_length=120, min_length=40, truncation=True)
summary = result[0]['summary_text']
cache.set(content_hash, summary)
return summary
except Exception as e:
logger.error(f"Error in summarization: {str(e)}")
return f"Summary unavailable. Error: {str(e)}"
def fetch_rss_news(tech_sources, business_sources, science_sources, world_sources, sports_sources, health_sources):
articles = []
cutoff_time = datetime.now(pytz.UTC) - RSS_FETCH_INTERVAL
category_sources = {
"Technology": tech_sources if tech_sources else [],
"Business": business_sources if business_sources else [],
"Science": science_sources if science_sources else [],
"World News": world_sources if world_sources else [],
"Sports": sports_sources if sports_sources else [],
"Health": health_sources if health_sources else []
}
logger.info(f"Selected sources: {category_sources}")
for category, sources in category_sources.items():
if not sources:
continue
logger.info(f"Processing category: {category} with sources: {sources}")
for source in sources:
if source in NEWS_SOURCES[category]:
url = NEWS_SOURCES[category][source]
try:
logger.info(f"Fetching from URL: {url}")
feed = feedparser.parse(url)
if hasattr(feed, 'status') and feed.status != 200:
logger.warning(f"Failed to fetch feed from {url}. Status: {feed.status}")
continue
for entry in feed.entries:
try:
published = datetime(*entry.published_parsed[:6], tzinfo=pytz.UTC)
if published > cutoff_time:
articles.append({
"title": entry.title,
"description": BeautifulSoup(entry.description, "html.parser").get_text(),
"link": entry.link,
"category": category,
"source": source,
"published": published
})
except (AttributeError, TypeError) as e:
logger.error(f"Error processing entry: {str(e)}")
continue
except Exception as e:
logger.error(f"Error fetching feed from {url}: {str(e)}")
continue
logger.info(f"Total articles fetched: {len(articles)}")
articles = sorted(articles, key=lambda x: x["published"], reverse=True)[:ARTICLE_LIMIT]
return articles
def summarize_articles(articles, model_name):
summaries = []
for article in articles:
content = article["description"]
summary = summarize_text(content, model_name)
summaries.append(f"""
<div style='margin-bottom: 20px; white-space: pre-wrap;'>
π° {article['title']}
π Summary: {summary}
- π Category: {article['category']}
- π‘ Source: {article['source']}
- π Read More: <a href="{article['link']}" target="_blank" style="text-decoration: none;">click here</a>
</div>
""")
return "\n".join(summaries)
def get_summary(tech_sources, business_sources, science_sources, world_sources,
sports_sources, health_sources, selected_model):
try:
if not any([tech_sources, business_sources, science_sources,
world_sources, sports_sources, health_sources]):
return "Please select at least one news source."
articles = fetch_rss_news(tech_sources, business_sources, science_sources,
world_sources, sports_sources, health_sources)
if not articles:
return "No recent news found from the selected sources."
return summarize_articles(articles, selected_model)
except Exception as e:
logger.error(f"Error in get_summary: {str(e)}")
return f"An error occurred while processing your request: {str(e)}"
# Gradio Interface
demo = gr.Blocks()
with demo:
gr.Markdown("# π° AI News Summarizer")
with gr.Row():
with gr.Column():
tech_sources = gr.CheckboxGroup(
choices=list(NEWS_SOURCES["Technology"].keys()),
label="Technology Sources",
value=[]
)
business_sources = gr.CheckboxGroup(
choices=list(NEWS_SOURCES["Business"].keys()),
label="Business Sources",
value=[]
)
science_sources = gr.CheckboxGroup(
choices=list(NEWS_SOURCES["Science"].keys()),
label="Science Sources",
value=[]
)
with gr.Column():
world_sources = gr.CheckboxGroup(
choices=list(NEWS_SOURCES["World News"].keys()),
label="World News Sources",
value=[]
)
sports_sources = gr.CheckboxGroup(
choices=list(NEWS_SOURCES["Sports"].keys()),
label="Sports Sources",
value=[]
)
health_sources = gr.CheckboxGroup(
choices=list(NEWS_SOURCES["Health"].keys()),
label="Health Sources",
value=[]
)
with gr.Column():
model_selector = gr.Radio(
choices=list(SUMMARIZER_MODELS.keys()),
label="Choose Summarization Model",
value="OpenRouter (Claude-3)" # Changed default to OpenRouter
)
summarize_button = gr.Button("Get News Summary")
summary_output = gr.HTML(label="News Summary") # Changed from gr.Textbox to gr.HTML
summarize_button.click(
get_summary,
inputs=[
tech_sources,
business_sources,
science_sources,
world_sources,
sports_sources,
health_sources,
model_selector
],
outputs=summary_output
)
if __name__ == "__main__":
demo.launch()
|