[ADD] forget to add tools/summarizer_service.py now adding.
Browse files- tools/summarizer_service.py +24 -0
tools/summarizer_service.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from functools import lru_cache
|
| 3 |
+
|
| 4 |
+
class SummarizerService:
|
| 5 |
+
_instance = None
|
| 6 |
+
|
| 7 |
+
def __new__(cls):
|
| 8 |
+
if cls._instance is None:
|
| 9 |
+
cls._instance = super().__new__(cls)
|
| 10 |
+
# Initialize the summarizer only once
|
| 11 |
+
cls._instance.summarizer = pipeline("summarization", model="google/flan-t5-small")
|
| 12 |
+
return cls._instance
|
| 13 |
+
|
| 14 |
+
def summarize(self, text, ratio=0.5, min_length=30):
|
| 15 |
+
# Calculate dynamic max_length based on input length
|
| 16 |
+
input_length = len(text.split())
|
| 17 |
+
max_length = max(int(input_length * ratio), min_length)
|
| 18 |
+
|
| 19 |
+
return self.summarizer(
|
| 20 |
+
text,
|
| 21 |
+
max_length=max_length,
|
| 22 |
+
min_length=min_length,
|
| 23 |
+
do_sample=False
|
| 24 |
+
)[0]['summary_text']
|