Skip to content
Snippets Groups Projects
T5_summarizer.py 1.7 KiB
Newer Older
import torch             

from transformers import AutoTokenizer, AutoModelWithLMHead

tokenizer = AutoTokenizer.from_pretrained('t5-base')                     
model = AutoModelWithLMHead.from_pretrained('t5-base', return_dict=True)

inputs = tokenizer.encode("summarize: " + """The Saturday night shooting came as the city's majority Asian community was marking the Lunar New Year, transforming one of the most auspicious days of the calendar into a tragedy. 
“There is a lot of fear and anxiety out there. People are fearful of this kind of situation where our joyous Lunar New Year celebration was turned utterly upside down into tragedy and fear,” Rep. Judy Chu, who represents Monterey Park, said Monday at a candlelight vigil for the victims.
California Gov. Gavin Newsom was meeting with victims of the shooting Monday when he was pulled away to be briefed on another fatal mass shooting in Half Moon Bay - the state's second mass shooting in three days. “Tragedy upon tragedy,” Newsom said in a tweet.
Authorities revealed new details Monday from their search of the home of the Monterey Park shooting suspect, 72-year-old Huu Can Tran, in Hemet, about 80 miles east of Monterey Park.
Detectives executed a search warrant and found “hundreds of rounds” of ammunition, a .308-caliber rifle, various electronic devices and evidence leading officials to believe he was “manufacturing homemade firearm suppressors,” Los Angeles County Sheriff Robert Luna said.""",                  
return_tensors='pt',              
max_length=512,             
truncation=True)

summary_ids = model.generate(inputs, max_length=150, min_length=80, length_penalty=5., num_beams=2)
summary = tokenizer.decode(summary_ids[0])
print(summary)