Stay Informed on the Latest AI Breakthroughs with Your Personal News Aggregator and Summarizer!
Stay Informed on the Latest AI Breakthroughs with Your Personal News Aggregator and Summarizer!
Are you struggling to keep up with the rapid advancements in Artificial Intelligence? Dozens of articles are published daily, making it a daunting task to filter through the noise and understand the key developments.
Introducing your new secret weapon: a Python-powered AI news aggregator and summarizer! This program automates the process of collecting the latest news on AI, Machine Learning, and Deep Learning, and then intelligently summarizes the content for you, saving you valuable time and effort.
How it Works:
This intelligent agent leverages the power of several Python libraries and cutting-edge AI models to deliver concise and informative summaries right to your fingertips:
-
News Collection with NewsAPI: The program starts by using the NewsAPI.org to fetch the most recent articles related to Artificial Intelligence, AI, Machine Learning, and Deep Learning. By specifying keywords and a time frame (in this case, the last day), it gathers a relevant pool of news from various sources.
Pythonimport requests import datetime import pandas as pd from newspaper import Article import openai import time def get_ai_news_newsapi(api_key, last_n_days=3): """ Get AI news using NewsAPI.org Args: api_key (str): Your **NewsAPI API Key** last_n_days (int): Number of past days to search Returns: pd.DataFrame: DataFrame containing news articles """ # Calculate dates end_date = datetime.date.today() start_date = end_date - datetime.timedelta(days=last_n_days) url = ('https://newsapi.org/v2/everything?' f'q=artificial+intelligence+OR+AI+OR+machine+learning+OR+deep+learning&' f'from={start_date}&' f'to={end_date}&' 'sortBy=publishedAt&' 'language=en&' f'apiKey={api_key}') response = requests.get(url) data = response.json() if data['status'] == 'ok': articles = data['articles'] df = pd.DataFrame(articles) # Convert publishedAt to datetime df['publishedAt'] = pd.to_datetime(df['publishedAt']) # Sort by date (newest first) df = df.sort_values('publishedAt', ascending=False) # Select relevant columns df = df[['title', 'source', 'publishedAt', 'description', 'url']] return df else: print("Error fetching news:", data.get('message', 'Unknown error')) return pd.DataFrame() # Usage: api_key = 'YOUR_NEWSAPI_KEY_HERE' # Replace with your actual API key ai_news = get_ai_news_newsapi(api_key, 1) print(f"Total {len(ai_news)} articles collected")
Note: To use this part of the program, you will need to sign up for a free API key at
. Remember to replaceNewsAPI.org 'YOUR_NEWSAPI_KEY_HERE'
with your actual key. -
Article Content Extraction with Newspaper3k: Once the relevant articles are identified, the program utilizes the
newspaper3k
library (imported asnewspaper
) to extract the main content from each article URL. This ensures that the summarization is based on the full text rather than just the headline or description.Pythondef extract_article_content(url): """Extract main text content from a news article URL""" try: article = Article(url) article.download() article.parse() return article.text except Exception as e: print(f"Error extracting content from {url}: {str(e)}") return None article_list = [] for index, article in ai_news.iterrows(): print(f"It's collection article content of '{article['title']}, {article['url']}'") content = extract_article_content(article['url']) time.sleep(2) # Be respectful to websites! article_list.append( {'title':article['title'], 'url':article['url'], 'content':content}) if index >= 4: break # Limiting to the first 5 articles for demonstration
A small delay (
time.sleep(2)
) is intentionally added to be respectful to the websites being scraped. For demonstration purposes, the program is set to process a maximum of the first 5 articles. -
Intelligent Summarization with OpenAI's GPT-4: The heart of the summarization process lies in the integration with OpenAI's powerful GPT-4 model. For each extracted article, a prompt is constructed including the article's title and content, instructing the AI to generate a concise summary.
Pythonimport openai def summarize_article(title, content, max_token=300): prompt = f"Summarize this AI news article : {title} - {content}" response = openai.ChatCompletion.create( # Use ChatCompletion for gpt-3.5-turbo model="gpt-4", # Specify the new model messages=[{"role": "user", "content": prompt}], # Format for gpt-3.5-turbo max_tokens=max_token # Adjust as needed ) return response.choices[0].message.content.strip() # Extract the summary text openai.api_key = "YOUR_OPENAI_API_KEY_HERE" # Replace with your actual API key for article in article_list: article['summarized']=summarize_article(article['title'], article['content'], 300)
Important: To utilize the summarization feature, you will need an API key from OpenAI. You can obtain one by signing up at
. Remember to replaceOpenAI Platform "YOUR_OPENAI_API_KEY_HERE"
with your actual API key. Themax_tokens
parameter controls the length of the generated summary. -
Displaying the Results: Finally, the program iterates through the summarized articles and neatly prints the title and its corresponding concise summary to the console.
Pythonfor article in article_list: print("==================================================================") print(f"title : {article['title']}") print(f"summzrized : {article['summarized']}") print("==================================================================")
Benefits of Using This AI News Agent:
- Stay Up-to-Date Effortlessly: No more endless scrolling through news websites. Get the essence of the latest AI news in one place.
- Save Time: Quickly grasp the main points of multiple articles without having to read each one in full.
- Focus on What Matters: Identify key trends and important developments in the AI field efficiently.
- Customizable: You can easily adjust the number of days to search, the keywords used, and the length of the summaries.
Get Started Today!
While this blog post provides an overview of the program's functionality and includes the source code, remember to replace the placeholder API keys with your own from NewsAPI.org and OpenAI to run it successfully.
This AI news aggregator and summarizer is a powerful tool for anyone looking to stay informed in the fast-paced world of Artificial Intelligence. Give it a try and experience a smarter way to consume news!
Comments
Post a Comment