From 28bf30272aba2cd36739702f3dd7f340f14cc538 Mon Sep 17 00:00:00 2001 From: SaharSabbaghh Date: Thu, 29 May 2025 12:54:37 +0300 Subject: [PATCH] Add summarization script and update README --- README.md | 17 +++++++++++ summarizer.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 summarizer.py diff --git a/README.md b/README.md index 76f26d1..a9cb525 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ # Codex-Test + Testing Codex Features + +## Summarizer Script + +`summarizer.py` provides abstractive summarization for long-form news +articles using a transformer-based model. Named entities are extracted +with spaCy and the script appends any missing entities to the summary to +help preserve important relationships. + +### Usage +``` +python summarizer.py path/to/article.txt +``` + +Ensure that the `transformers` and `spacy` packages are installed and +that the spaCy model `en_core_web_sm` is available. Model weights must be +downloaded beforehand in environments without network access. diff --git a/summarizer.py b/summarizer.py new file mode 100644 index 0000000..e1ff768 --- /dev/null +++ b/summarizer.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +"""Abstractive summarization script preserving named entities. + +This script reads a text file containing a news article and outputs an +abstractive summary generated by a transformer-based model from the +Hugging Face Transformers library. It uses spaCy to extract named +entities from the original article and checks that the generated summary +includes these entities. Entities missing from the summary are appended +at the end to encourage preservation of key information and +relationships. + +Example usage: + python summarizer.py article.txt + +Requirements: + transformers + spacy + en_core_web_sm (spaCy model) + +Note: Running this script requires downloading model weights. In a +restricted environment without network access, download the required +models beforehand. +""" + +import argparse +from pathlib import Path +from typing import Set + +import spacy +from transformers import pipeline + + +def load_article(path: Path) -> str: + """Read the article from a file.""" + with path.open(encoding="utf-8") as f: + return f.read() + + +def extract_entities(text: str, nlp) -> Set[str]: + """Return the set of named entities in the text.""" + doc = nlp(text) + return {ent.text for ent in doc.ents} + + +def generate_summary(text: str, model_name: str) -> str: + """Generate a summary using a transformer-based summarization pipeline.""" + summarizer = pipeline("summarization", model=model_name) + result = summarizer(text, max_length=200, min_length=60, truncation=True) + return result[0]["summary_text"] + + +def ensure_entities(summary: str, entities: Set[str]) -> str: + """Append missing named entities to the summary.""" + missing = [ent for ent in entities if ent not in summary] + if missing: + summary += "\n\nMissing entities: " + ", ".join(missing) + return summary + + +def main() -> None: + parser = argparse.ArgumentParser(description="Summarize news articles") + parser.add_argument("article", type=Path, help="Path to the article text file") + parser.add_argument( + "--model", + default="facebook/bart-large-cnn", + help="Hugging Face model for summarization", + ) + args = parser.parse_args() + + text = load_article(args.article) + + nlp = spacy.load("en_core_web_sm") + entities = extract_entities(text, nlp) + + summary = generate_summary(text, args.model) + summary = ensure_entities(summary, entities) + + print(summary) + + +if __name__ == "__main__": + main()