Spaces:
Sleeping
Sleeping
| """This module contains all tasks specific for summarizer | |
| @Author: Karthick T. Sharma | |
| """ | |
| from .base import Model | |
| from src.utils.text_process import postprocess_summary, split_text | |
| class AbstractiveSummarizer(Model): | |
| """Summarize input context.""" | |
| _instance = None | |
| def __new__(cls): | |
| if cls._instance is None: | |
| cls._instance = super(AbstractiveSummarizer, cls).__new__(cls) | |
| cls._instance._init_model() | |
| return cls._instance | |
| def _init_model(self): | |
| """Initialize corpus summarizer only once.""" | |
| super().__init__(model_name='google-t5/t5-base') | |
| # def __init__(self): | |
| # """Initialize corpus summarizer.""" | |
| # # NOTE: Default | |
| # super().__init__(model_name='google-t5/t5-base') | |
| # super().__init__(model_name='t5-base', path_id='1-50SZ_WIHX4A6mkpsz-t0EAF_VhtHb-9') | |
| # super().__init__(model_name='t5-small', path_id='1ODslrpbSXB0HWAGymYmyJn5nFO8GELpd') | |
| def preprocess_input(self, model_input): | |
| """Process model input. | |
| Args: | |
| model_input (str): bulk text that needs to be processed. | |
| Returns: | |
| list(str): processed text chunks. | |
| """ | |
| return split_text(model_input) | |
| def summarize(self, context): | |
| """Generate abstrative summary of given context. | |
| Args: | |
| context (str): input corpus. | |
| Returns: | |
| str: summarized text. | |
| """ | |
| return postprocess_summary(super().inference( | |
| num_beams=3, no_repeat_ngram_size=2, model_max_length=512, | |
| num_return_sequences=1, summarize=context)) | |