First_agent_template / tools /summarizer.py
Mehedi2's picture
Create summarizer.py
a1fbb4d verified
raw
history blame
800 Bytes
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)}"