AI-based News Summarizer
Abstract
Section titled “Abstract”AI-based News Summarizer is a Python project that uses NLP to generate concise summaries of news articles. The application features text extraction, summarization algorithms, and a CLI interface, demonstrating best practices in text processing and summarization.
Prerequisites
Section titled “Prerequisites”- Python 3.8 or above
- A code editor or IDE
- Basic understanding of NLP and text summarization
- Required libraries:
nltk,sumy,requests
Before you Start
Section titled “Before you Start”Install Python and the required libraries:
pip install nltk sumy requestsGetting Started
Section titled “Getting Started”Create a Project
Section titled “Create a Project”- Create a folder named
ai-based-news-summarizer. - Open the folder in your code editor or IDE.
- Create a file named
ai_based_news_summarizer.py. - Copy the code below into your file.
Write the Code
Section titled “Write the Code”AI-based News Summarizer
pch.viewSource"""
AI-based News Summarizer
Features:
- News summarization using NLP
- Keyword extraction
- Web interface (Flask)
- Modular design
- Error handling
"""
from flask import Flask, request, render_template_string
import sys
import re
from collections import Counter
try:
from gensim.summarization import summarize
except ImportError:
summarize = None
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
summary = ''
keywords = []
if request.method == 'POST':
text = request.form['text']
if summarize:
summary = summarize(text)
else:
summary = '\n'.join(text.split('.')[:3])
words = re.findall(r'\w+', text.lower())
freq = Counter(words)
keywords = [w for w, c in freq.most_common(10)]
return render_template_string('''<form method="post"><textarea name="text" rows="10" cols="80"></textarea><br><input type="submit" value="Summarize"></form><h2>Summary</h2><pre>{{summary}}</pre><h2>Keywords</h2><pre>{{keywords}}</pre>''', summary=summary, keywords=', '.join(keywords))
def smoke_test():
"""Exercise every GET route once, without starting a server.
`app.test_client()` dispatches a real request through the real application
object -- no socket, no port, no waiting. A web project that cannot be
driven this way cannot be tested either, so this is worth having whether or
not anything is capturing the output.
"""
print("smoke test: dispatching one request per route\n")
with app.test_client() as client:
rules = sorted(app.url_map.iter_rules(), key=lambda rule: str(rule))
checked = 0
for rule in rules:
if "GET" not in rule.methods or rule.arguments:
continue
response = client.get(str(rule))
body = response.get_data(as_text=True)
body = " ".join(body.split())[:60]
print(f" GET {str(rule):26} {response.status_code} {body}")
checked += 1
print(f"\n{checked} route(s) answered. Pass --serve to start the real "
f"server instead.")
if __name__ == "__main__":
# Serving is opt-in, because a run that never returns cannot be
# tested or captured. With no arguments the file answers every
# route once and exits; `--serve` starts the real server.
if "--serve" in sys.argv:
try:
app.run(debug=True)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
else:
smoke_test() Example Usage
Section titled “Example Usage”python ai_based_news_summarizer.pyWhat it produces
Section titled “What it produces”Running the file exactly as it ships takes 2.1 s and prints:
smoke test: dispatching one request per route
GET / 200 <form method="post"><textarea name="text" rows="10" cols="80
1 route(s) answered. Pass --serve to start the real server instead.How it fits together
Section titled “How it fits together”Read from the top: this is what runs when you execute the file, and which function calls which. It is generated from the code, so it cannot drift from it.
flowchart TD
RUN(["python ai_based_news_summarizer.py"])
index("index")
smoke_test("smoke_test")
RUN --> smoke_test
Explanation
Section titled “Explanation”Key Features
Section titled “Key Features”- Text Extraction: Fetches and processes news articles.
- Summarization Algorithms: Uses NLP for extractive summarization.
- Error Handling: Validates inputs and manages exceptions.
- CLI Interface: Interactive command-line usage.
Code Breakdown
Section titled “Code Breakdown”- What it imports (lines 11–14)
from flask import Flask, request, render_template_string
import sys
import re
from collections import Counterindex— the function (lines 23–35)
def index():
summary = ''
keywords = []
if request.method == 'POST':
text = request.form['text']
if summarize:
summary = summarize(text)
else:
summary = '\n'.join(text.split('.')[:3])
words = re.findall(r'\w+', text.lower())
freq = Counter(words)
keywords = [w for w, c in freq.most_common(10)]
return render_template_string('''<form method="post"><textarea name="text" rows="10" cols="80"></textarea><br><input type="submit" value="Summarize"></form><h2>Summary</h2><pre>{{summary}}</pre><h2>Keywords</h2><pre>{{keywords}}</pre>''', summary=summary, keywords=', '.join(keywords))The file defines 1 top-level symbol in all; the whole thing is above under Write the Code.
Features
Section titled “Features”- NLP-Based Summarization: High-accuracy news summaries
- Modular Design: Separate functions for fetching and summarizing
- Error Handling: Manages invalid inputs and exceptions
- Production-Ready: Scalable and maintainable code
Next Steps
Section titled “Next Steps”Enhance the project by:
- Supporting batch summarization
- Creating a GUI with Tkinter or a web app with Flask
- Adding support for more summarization algorithms
- Unit testing for reliability
Educational Value
Section titled “Educational Value”This project teaches:
- NLP Fundamentals: Text extraction and summarization
- Software Design: Modular, maintainable code
- Error Handling: Writing robust Python code
Real-World Applications
Section titled “Real-World Applications”- News Aggregators
- Content Management
- Educational Tools
Conclusion
Section titled “Conclusion”AI-based News Summarizer demonstrates how to build a scalable and accurate news summarization tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in media, education, and more. For more advanced projects, visit Python Central Hub.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading