Views
No views yet
T5ForConditionalGeneration(
(shared): Embedding(32128, 512)
(encoder): T5Stack(
(embed_tokens): Embedding(32128, 512)
(block): ModuleList(...)
(final_layer_norm): LayerNorm((512,), eps=1e-12)
(dropout): Dropout(p=0.1)
)
(decoder): T5Stack(
(embed_tokens): Embedding(32128, 512)
(block): ModuleList(...)
(final_layer_norm): LayerNorm((512,), eps=1e-12)
(dropout): Dropout(p=0.1)
)
(lm_head): Linear(in_features=512, out_features=32128, bias=False)
)pip install -U transformers torch datasets1from transformers import T5ForConditionalGeneration, T5Tokenizer
2import torch
3
4# Model Name
5model_name = "your_fine_tuned_model_id"
6tokenizer = T5Tokenizer.from_pretrained(model_name)
7model = T5ForConditionalGeneration.from_pretrained(model_name)
8
9# Move model to GPU if available
10device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11model.to(device)
12
13# Inference
14news_summary = "Ministry of Education has announced a major reform in the national curriculum to enhance digital literacy among students."
15inputs = tokenizer(news_summary, max_length=128, truncation=True, padding="max_length", return_tensors="pt").to(device)
16outputs = model.generate(
17 input_ids=inputs["input_ids"],
18 attention_mask=inputs["attention_mask"],
19 max_length=20,
20 num_beams=5,
21 early_stopping=True
22)
23headline = tokenizer.decode(outputs[0], skip_special_tokens=True)
24print(f"Generated Headline: {headline}")article_summary:
Type: string
Min length: ~20 tokens
Mean length: ~50-60 tokens (estimated)
Max length: ~128 tokens
headline:
Type: string
Min length: ~5 tokens
Mean length: ~10-15 tokens
Max length: ~20 tokens