Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
82 changes: 82 additions & 0 deletions summarizer.py
Original file line number Diff line number Diff line change
@@ -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()