Spaces:
Runtime error
Runtime error
Create summarizer.py
Browse files- modules/summarizer.py +34 -0
modules/summarizer.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/summarizer.py
|
| 2 |
+
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from config.settings import SUMMARIZER_MODEL
|
| 5 |
+
|
| 6 |
+
# Load summarization pipeline
|
| 7 |
+
summarizer_pipeline = pipeline("summarization", model=SUMMARIZER_MODEL)
|
| 8 |
+
|
| 9 |
+
def summarize_texts(texts: list, max_length: int = 60, min_length: int = 20) -> list:
|
| 10 |
+
"""
|
| 11 |
+
Generate summaries for a list of texts.
|
| 12 |
+
Returns a list of dictionaries with original text and summary.
|
| 13 |
+
"""
|
| 14 |
+
results = []
|
| 15 |
+
for t in texts:
|
| 16 |
+
if t.strip():
|
| 17 |
+
try:
|
| 18 |
+
summary = summarizer_pipeline(
|
| 19 |
+
t,
|
| 20 |
+
max_length=max_length,
|
| 21 |
+
min_length=min_length,
|
| 22 |
+
do_sample=False
|
| 23 |
+
)[0]["summary_text"]
|
| 24 |
+
|
| 25 |
+
results.append({
|
| 26 |
+
"text": t,
|
| 27 |
+
"summary": summary
|
| 28 |
+
})
|
| 29 |
+
except Exception as e:
|
| 30 |
+
results.append({
|
| 31 |
+
"text": t,
|
| 32 |
+
"summary": f"ERROR: {str(e)}"
|
| 33 |
+
})
|
| 34 |
+
return results
|