Upload 8 files
Browse files- LICENSE +9 -0
- README.md +58 -3
- config.json +11 -0
- inference.py +9 -0
- requirements.txt +4 -0
- special_tokens_map.json +5 -0
- tokenizer_config.json +5 -0
- train_summarizer.py +59 -0
LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License 2.0
|
| 2 |
+
|
| 3 |
+
Copyright 2025 hmnshudhmn24
|
| 4 |
+
|
| 5 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
you may not use this file except in compliance with the License.
|
| 7 |
+
You may obtain a copy of the License at
|
| 8 |
+
|
| 9 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
license: apache-2.0
|
| 4 |
+
datasets: cnn_dailymail
|
| 5 |
+
pipeline_tag: summarization
|
| 6 |
+
library_name: transformers
|
| 7 |
+
tags:
|
| 8 |
+
- t5
|
| 9 |
+
- summarization
|
| 10 |
+
- nlp
|
| 11 |
+
- text-generation
|
| 12 |
+
base_model: t5-small
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# 🧠 T5 News Summarizer
|
| 16 |
+
|
| 17 |
+
A fine-tuned **T5-small** model trained on the **CNN/DailyMail dataset** for **news summarization**.
|
| 18 |
+
This model converts long news articles into concise, readable summaries.
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## 📊 Model Details
|
| 23 |
+
|
| 24 |
+
- **Base model:** t5-small
|
| 25 |
+
- **Dataset:** cnn_dailymail v3.0.0
|
| 26 |
+
- **Task:** Summarization
|
| 27 |
+
- **Language:** English
|
| 28 |
+
- **Framework:** PyTorch
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## 🚀 Usage
|
| 33 |
+
|
| 34 |
+
```python
|
| 35 |
+
from transformers import pipeline
|
| 36 |
+
summarizer = pipeline("summarization", model="hmnshudhmn24/t5-news-summarizer")
|
| 37 |
+
|
| 38 |
+
text = "The economy has seen a major shift due to advances in artificial intelligence..."
|
| 39 |
+
print(summarizer(text))
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## 🧩 Example
|
| 45 |
+
|
| 46 |
+
| Input | Output |
|
| 47 |
+
|-------|---------|
|
| 48 |
+
| "AI is transforming industries across the world..." | "AI is changing how industries operate globally." |
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## ⚖️ License
|
| 53 |
+
Licensed under the [Apache 2.0 License](./LICENSE).
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## 🏷️ Tags
|
| 58 |
+
`t5` `summarization` `nlp` `transformers` `cnn_dailymail`
|
config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"T5ForConditionalGeneration"
|
| 4 |
+
],
|
| 5 |
+
"model_type": "t5",
|
| 6 |
+
"d_model": 512,
|
| 7 |
+
"num_heads": 8,
|
| 8 |
+
"num_layers": 6,
|
| 9 |
+
"vocab_size": 32128,
|
| 10 |
+
"decoder_start_token_id": 0
|
| 11 |
+
}
|
inference.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
summarizer = pipeline("summarization", model="hmnshudhmn24/t5-news-summarizer")
|
| 4 |
+
|
| 5 |
+
article = """The rapid development of artificial intelligence has raised questions about its impact on jobs and society.
|
| 6 |
+
Experts believe AI will enhance productivity but may disrupt traditional industries.
|
| 7 |
+
"""
|
| 8 |
+
summary = summarizer(article, max_length=60, min_length=10, do_sample=False)
|
| 9 |
+
print(summary)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.44.0
|
| 2 |
+
datasets>=2.21.0
|
| 3 |
+
torch>=2.2.0
|
| 4 |
+
evaluate>=0.4.2
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"pad_token": "<pad>",
|
| 3 |
+
"eos_token": "</s>",
|
| 4 |
+
"unk_token": "<unk>"
|
| 5 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_max_length": 512,
|
| 3 |
+
"truncation_side": "right",
|
| 4 |
+
"padding_side": "right"
|
| 5 |
+
}
|
train_summarizer.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
from transformers import T5ForConditionalGeneration, T5TokenizerFast, Trainer, TrainingArguments
|
| 3 |
+
import evaluate
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load dataset
|
| 7 |
+
dataset = load_dataset("cnn_dailymail", "3.0.0")
|
| 8 |
+
|
| 9 |
+
# Load tokenizer and model
|
| 10 |
+
tokenizer = T5TokenizerFast.from_pretrained("t5-small")
|
| 11 |
+
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
| 12 |
+
|
| 13 |
+
# Preprocess function
|
| 14 |
+
def preprocess_function(examples):
|
| 15 |
+
inputs = ["summarize: " + doc for doc in examples["article"]]
|
| 16 |
+
model_inputs = tokenizer(inputs, max_length=512, truncation=True)
|
| 17 |
+
labels = tokenizer(text_target=examples["highlights"], max_length=128, truncation=True)
|
| 18 |
+
model_inputs["labels"] = labels["input_ids"]
|
| 19 |
+
return model_inputs
|
| 20 |
+
|
| 21 |
+
tokenized_datasets = dataset.map(preprocess_function, batched=True, remove_columns=["article", "highlights", "id"])
|
| 22 |
+
|
| 23 |
+
# Metrics
|
| 24 |
+
rouge = evaluate.load("rouge")
|
| 25 |
+
|
| 26 |
+
def compute_metrics(eval_pred):
|
| 27 |
+
predictions, labels = eval_pred
|
| 28 |
+
decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True)
|
| 29 |
+
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
|
| 30 |
+
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
|
| 31 |
+
result = rouge.compute(predictions=decoded_preds, references=decoded_labels)
|
| 32 |
+
return {k: v * 100 for k, v in result.items()}
|
| 33 |
+
|
| 34 |
+
# Training arguments
|
| 35 |
+
training_args = TrainingArguments(
|
| 36 |
+
output_dir="./results",
|
| 37 |
+
evaluation_strategy="epoch",
|
| 38 |
+
learning_rate=3e-4,
|
| 39 |
+
per_device_train_batch_size=2,
|
| 40 |
+
per_device_eval_batch_size=2,
|
| 41 |
+
num_train_epochs=1,
|
| 42 |
+
save_strategy="epoch",
|
| 43 |
+
predict_with_generate=True,
|
| 44 |
+
push_to_hub=False
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Trainer
|
| 48 |
+
trainer = Trainer(
|
| 49 |
+
model=model,
|
| 50 |
+
args=training_args,
|
| 51 |
+
train_dataset=tokenized_datasets["train"].select(range(2000)),
|
| 52 |
+
eval_dataset=tokenized_datasets["validation"].select(range(500)),
|
| 53 |
+
tokenizer=tokenizer,
|
| 54 |
+
compute_metrics=compute_metrics
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
trainer.train()
|
| 58 |
+
trainer.save_model("./t5-news-summarizer")
|
| 59 |
+
tokenizer.save_pretrained("./t5-news-summarizer")
|