-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarization_with_bart.py
24 lines (20 loc) · 1.04 KB
/
summarization_with_bart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from transformers import pipeline
def summarize_email_conditional(email_text, summarizer, min_input_length=50):
"""
Summarizes the email if it's longer than min_input_length.
Adjusts max_length parameter based on the length of the email.
Args:
- email_text (str): The text of the email to summarize.
- min_input_length (int): Minimum length of email to apply summarization.
Returns:
- str: The summary of the email or the original email if below the min_input_length.
"""
# Only summarize if the email is longer than min_input_length
if len(email_text.split()) > min_input_length:
# Dynamically set max_length to be about 75% of the email length, or any ratio that suits your need
max_length = max(12, int(len(email_text.split()) * 0.75))
summary = summarizer(email_text, max_length=max_length, min_length=5, do_sample=False)
return summary[0]['summary_text']
else:
# Return the original email text if it's not long enough to require summarization
return email_text