Asanaly commited on
Commit
e169923
·
verified ·
1 Parent(s): 4a85f73

Create summarizer.py

Browse files
Files changed (1) hide show
  1. summarizer.py +22 -0
summarizer.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BartTokenizer, BartForConditionalGeneration
2
+
3
+ tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
4
+ model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
5
+
6
+ def generate_summary(text: str) -> str:
7
+ text = text.strip()
8
+
9
+ if len(text) < 50:
10
+ return "Text is too short to summarize."
11
+
12
+ inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
13
+
14
+ summary_ids = model.generate(
15
+ inputs["input_ids"],
16
+ num_beams=4,
17
+ max_length=200,
18
+ min_length=40,
19
+ early_stopping=True
20
+ )
21
+
22
+ return tokenizer.decode(summary_ids[0], skip_special_tokens=True)