csebuetnlp/xlsum
Updated • 5.27k • 152
How to use nafisehNik/mt5-persian-summary with Transformers:
# Use a pipeline as a high-level helper
# Warning: Pipeline type "summarization" is no longer supported in transformers v5.
# You must load the model directly (see below) or downgrade to v4.x with:
# 'pip install "transformers<5.0.0'
from transformers import pipeline
pipe = pipeline("summarization", model="nafisehNik/mt5-persian-summary") # Load model directly
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("nafisehNik/mt5-persian-summary")
model = AutoModelForSeq2SeqLM.from_pretrained("nafisehNik/mt5-persian-summary")This model is fine-tuned to generate summaries based on the input provided. It has been fine-tuned on a wide range of Persian news data, including BBC news and pn_summary.
from transformers import AutoModelForSeq2SeqLM, MT5Tokenizer
model = AutoModelForSeq2SeqLM.from_pretrained('nafisehNik/mt5-persian-summary')
tokenizer = MT5Tokenizer.from_pretrained("nafisehNik/mt5-persian-summary")
# method for summary generation, using the global model and tokenizer
def generate_summary(model, abstract, num_beams = 2, repetition_penalty = 1.0,
length_penalty = 2.0, early_stopping = True, max_output_length = 120):
source_encoding=tokenizer(abstract, max_length=1000, padding="max_length", truncation=True, return_attention_mask=True, add_special_tokens=True, return_tensors="pt")
generated_ids=model.generate(
input_ids=source_encoding["input_ids"],
attention_mask=source_encoding["attention_mask"],
num_beams=num_beams,
max_length=max_output_length,
repetition_penalty=repetition_penalty,
length_penalty=length_penalty,
early_stopping=early_stopping,
use_cache=True
)
preds=[tokenizer.decode(gen_id, skip_special_tokens=True, clean_up_tokenization_spaces=True)
for gen_id in generated_ids]
return "".join(preds)
text = "YOUR INPUT TEXT"
result = generate_summary(model=model, abstract=text, num_beams=2, max_output_length=120)
If you find this model useful, make a link to the huggingface model.