| from transformers import pipeline | |
| from functools import lru_cache | |
| class SummarizerService: | |
| _instance = None | |
| def __new__(cls): | |
| if cls._instance is None: | |
| cls._instance = super().__new__(cls) | |
| # Initialize the summarizer only once | |
| cls._instance.summarizer = pipeline("summarization", model="google/flan-t5-small") | |
| return cls._instance | |
| def summarize(self, text, ratio=0.5, min_length=30): | |
| # Calculate dynamic max_length based on input length | |
| input_length = len(text.split()) | |
| max_length = max(int(input_length * ratio), min_length) | |
| return self.summarizer( | |
| text, | |
| max_length=max_length, | |
| min_length=min_length, | |
| do_sample=False | |
| )[0]['summary_text'] | |