patent-summarizer / util /summarizer.py
amielle's picture
feat: Add initial model for background summarizer
b2fdf59
raw
history blame
No virus
3.26 kB
from transformers import BigBirdPegasusForConditionalGeneration, AutoTokenizer
# by default encoder-attention is `block_sparse` with num_random_blocks=3, block_size=64
# TODO: add pre-trained summarizer models
# Placeholder text for testing input
test_text = """
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
"""
summary_options = ["Abstract", "Background", "Claims"]
def get_word_index(s, idx):
words = re.findall(r'\s*\S+\s*', s)
return sum(map(len, words[:idx])) + len(words[idx]) - len(words[idx].lstrip())
class PatentSummarizer():
def __init__(self, base_model_name="google/bigbird-pegasus-large-bigpatent"):
# Possible to tweak other summaries with different models in the future
self.model = dict()
self.tokenizer = dict()
self.base_model = BigBirdPegasusForConditionalGeneration.from_pretrained(base_model_name)
self.base_tokenizer = AutoTokenizer.from_pretrained(base_model_name)
self.max_word_input = 1000
def pipeline(patent_information, summaries_generated, min_char_abs, min_char_bg, min_char_claims):
# TODO: add checker if valid patent info, return None if invalid
# TODO: add scraper to get document
# TODO: add parser to get the following info from the base document:
abstract, background, claims = None, None, None
summaries = list()
if "Abstract" in summaries_generated:
abstract_summary = summarizer.generate_abs_summary(abstract, min_char_abs)
summaries.append(abstract_summary)
else:
summaries.append(None)
if "Background" in summaries_generated:
background_summary = summarizer.generate_bg_summary(background, min_char_bg)
summaries.append(background_summary)
else:
summaries.append(None)
if "Claims" in summaries_generated:
claims_summary = summarizer.generate_claims_summary(claims, min_char_claims)
summaries.append(claims_summary)
else:
summaries.append(None)
return summaries
def generate_abs_summary(abstract, min_char_abs):
return "Abstract" + test_text
def generate_bg_summary(background, min_char_bg):
stop_idx = get_word_index(background, self.max_word_input)
inputs = self.base_tokenizer(background[0:stop_idx],
return_tensors='pt')
prediction = self.base_model.generate(**inputs)
bg_summary = self.base_tokenizer.batch_decode(prediction)
bg_summary = textproc.clean_text(bg_summary[0])
return bg_summary
def generate_claims_summary(claims, min_char_claims):
return "Claims" + test_text