File size: 800 Bytes
a1fbb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from smolagents import tool
from transformers import pipeline

# load once globally (uses a small summarization model from Hugging Face)
summarizer_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")

@tool
def summarizer(text: str, max_length: int = 100, min_length: int = 30) -> str:
    """Summarize a long piece of text into a shorter version.
    Args:
        text: The text that needs to be summarized.
        max_length: Maximum length of the summary.
        min_length: Minimum length of the summary.
    """
    try:
        summary = summarizer_pipeline(
            text, max_length=max_length, min_length=min_length, do_sample=False
        )
        return summary[0]["summary_text"]
    except Exception as e:
        return f"Error during summarization: {str(e)}"