Spaces:
Runtime error
Runtime error
Create summarizer.py
Browse files- tools/summarizer.py +21 -0
tools/summarizer.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# load once globally (uses a small summarization model from Hugging Face)
|
| 5 |
+
summarizer_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 6 |
+
|
| 7 |
+
@tool
|
| 8 |
+
def summarizer(text: str, max_length: int = 100, min_length: int = 30) -> str:
|
| 9 |
+
"""Summarize a long piece of text into a shorter version.
|
| 10 |
+
Args:
|
| 11 |
+
text: The text that needs to be summarized.
|
| 12 |
+
max_length: Maximum length of the summary.
|
| 13 |
+
min_length: Minimum length of the summary.
|
| 14 |
+
"""
|
| 15 |
+
try:
|
| 16 |
+
summary = summarizer_pipeline(
|
| 17 |
+
text, max_length=max_length, min_length=min_length, do_sample=False
|
| 18 |
+
)
|
| 19 |
+
return summary[0]["summary_text"]
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error during summarization: {str(e)}"
|