A web application that fetches news articles and analyzes their political leaning using machine learning.
- 🔍 Search for news articles using NewsAPI
- 🤖 AI-powered political leaning classification (Left / Center / Right)
- 📊 Confidence scores for each prediction
- 🎨 Modern, responsive web interface
- 🌙 Dark / Light mode toggle
- Install Dependencies
pip install -r requirements.txt
2. Configure Environment
Get a NewsAPI key from newsapi.org.
Create a .env file and add:
NEWSAPI_KEY=your_actual_api_key_here
3. Model Setup
The political bias classifier model is hosted on Hugging Face Hub and will automatically download on first run.
Model: JESHWINM/news-political-analyzer-model
Option A (Recommended): Auto-download on backend start (no setup needed).
Option B: Manual download:
python setup_model.py
Option C: Use your own model
Set MODEL_NAME in .env
Or place model files in the models/ directory
4. Start Backend API
python start_backend.py
Runs FastAPI at: http://localhost:8001
5. Start Frontend
In another terminal:
reflex run
Runs Reflex frontend at: http://localhost:3000
🖥️ Usage
Open http://localhost:3000
Enter a search term (e.g., politics, economy, healthcare)
Click Analyze News
View results with political leaning badges and confidence scores
📡 API Endpoints
GET / → API status
POST /predict → Predict political leaning of text
POST /news → Fetch + analyze news articles
Docs available at: http://localhost:8001/docs
📦 Model Requirements
Your pickle model should:
Accept text input
Implement predict() and predict_proba()
Return predictions as integers:
0 = Left
1 = Center
2 = Right
🛠️ Troubleshooting
Backend Issues
Check model location
Verify NewsAPI key is valid
Ensure backend is running on port 8001
Frontend Issues
Start backend before frontend
Check browser console for JS errors
Verify Reflex version compatibility
🏗️ Architecture
Frontend: Reflex (Python-based React)
Backend: FastAPI
ML Model: Scikit-learn classifier
News Source: NewsAPI
Styling: Responsive UI with Tailwind-like utilities
☁️ Deployment
Supports:
Railway (railway.json, nixpacks.toml) ✅ Recommended
Render (render.yaml)
Heroku (Procfile)
Vercel (Reflex export compatible)
Railway Deployment
Backend
Go to railway.app → New Project → Deploy from GitHub
Select your repo
Railway auto-detects backend config
Set environment variables:
NEWSAPI_KEY = your API key
MODEL_NAME = JESHWINM/news-political-analyzer-model
Frontend
In same project → New Service → GitHub Repo
Select Static Site
Build command:
reflex export --frontend-only
Output directory: .web/static
Add env var:
API_URL = backend URL (e.g., https://real-news-backend.railway.app)
Alternative: CLI
npm install -g @railway/cli
railway login
railway init
railway up