Building an AI-Powered News Sentiment Analyzer with Streamlit
The WanNkan: AI-Powered News Sentiment Analyzer is a paradigm shift in how we consume daily information. Instead of scrolling through endless headlines, this tool fetches recent news articles from Google News RSS, analyzes their emotional tone, and provides visual summaries.
In this guide, we will walk through the process of building this app using Python, Streamlit, and Natural Language Processing (NLP) tools.
Key Features
- Sentiment Analysis: Automatically determines if an article is positive, negative, or neutral.
- Automated Summaries: Generates concise snippets of each news entry.
- Word Cloud Visualization: Identifies the most frequent and important terms across all fetched articles.
Technical Implementation
1. Fetching News Articles
We use the feedparser library to interact with the Google News RSS feed. This allows us to extract titles and descriptions based on user-defined topics.
import feedparser def fetch_google_news_rss(topic): url = f'https://news.google.com/rss/search?q={topic}&hl=en-US&gl=US&ceid=US:en' feed = feedparser.parse(url) return [{'title': entry.title, 'description': entry.get('description', ''), 'link': entry.link} for entry in feed.entries]
2. Sentiment Analysis with TextBlob
To understand the "mood" of the news, we leverage TextBlob. It provides a polarity score ranging from -1 (negative) to 1 (positive).
from textblob import TextBlob def analyze_sentiment(text): analysis = TextBlob(text) if analysis.sentiment.polarity > 0: return 'Positive' elif analysis.sentiment.polarity < 0: return 'Negative' return 'Neutral'
3. Visualizing with Word Clouds
A word cloud offers an immediate visual representation of the news landscape. We combine the text from all articles and generate a cloud using matplotlib.
from wordcloud import WordCloud import matplotlib.pyplot as plt def generate_word_cloud(text): wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text) plt.figure(figsize=(10, 5)) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') st.pyplot(plt)
Building the Streamlit UI
Streamlit allows us to transform our Python logic into an interactive web interface. We create a simple text input for topics and a button to trigger the analysis.
import streamlit as st def main(): st.title("WanNkan: AI News Analyzer") topic = st.text_input("Enter a topic (e.g., technology, sports):", "technology") if st.button("Analyze News"): articles = fetch_google_news_rss(topic) # Generate summaries and word cloud logic here...
About the Project
This app was designed to help users stay informed efficiently. By combining sentiment analysis with summaries, users can quickly gauge the public discourse on any given topic.
- Deployment: The app is hosted on Streamlit Cloud for easy access.
Conclusion
This project demonstrates the power of combining NLP and modern web frameworks to create high-leverage data tools. You can extend this application by integrating more advanced LLMs for deeper summarization or adding date filters to track sentiment trends over time.
Resources
- Live Demo: WanNkan on Streamlit Cloud
- Source Code: GitHub Repository