File size: 653 Bytes
72e4742
7474f2b
72e4742
7474f2b
c588c93
 
 
7474f2b
c588c93
72e4742
7474f2b
72e4742
7474f2b
 
c588c93
 
7474f2b
c588c93
 
 
 
 
 
 
 
 
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
from transformers import pipeline
from functools import lru_cache

# Load CPU-friendly summarization model
summarizer = pipeline(
    "summarization", 
    model="sshleifer/distilbart-cnn-12-6",
    framework="pt"
)

@lru_cache(maxsize=200)
def summarize_text(text, max_length=100):
    """Efficient summarization with caching"""
    if not text or len(text) < 100:
        return text
    
    # Truncate to model capacity
    truncated = text[:1024]
    
    return summarizer(
        truncated,
        max_length=max_length,
        min_length=30,
        do_sample=False,  # Faster without sampling
        truncation=True
    )[0]['summary_text']