-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshorter.py
More file actions
37 lines (29 loc) · 1.25 KB
/
shorter.py
File metadata and controls
37 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from transformers import pipeline
def generate_summary(text: str, device: str="cpu") -> str:
""" summarize decoded text using flan-t5-small """
global summarizer # to not inicialize every time function is called
def summary():
# chunking if file is large
if len(text) < 200:
return text
max_chunk = 512
chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
summaries = [
summarizer(chunk, max_length=100, min_length=25, do_sample=False)[0]['summary_text']
for chunk in chunks
]
combined_text = " ".join(summaries)
input_len = len(combined_text.split())
# set max_length to be ~50% of input length but capped
adaptive_max_length = min(int(input_len * 0.5), 300)
adaptive_min_length = min(50, adaptive_max_length)
return summarizer(
combined_text, max_length=adaptive_max_length, min_length=adaptive_min_length, do_sample=False
)[0]["summary_text"]
try:
if summarizer:
final_summary = summary()
except NameError:
summarizer = pipeline("summarization", model="google/flan-t5-small", device=device)
final_summary = summary()
return final_summary