-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_chapter_sum.py
48 lines (39 loc) · 1.69 KB
/
ai_chapter_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import argparse
from transformers import pipeline
# Load the summarization pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Function to split text into manageable chunks
def chunk_text(text, max_chunk_length=500):
sentences = text.split(". ")
current_chunk = ""
chunks = []
for sentence in sentences:
if len(current_chunk) + len(sentence) <= max_chunk_length:
current_chunk += sentence + ". "
else:
chunks.append(current_chunk.strip())
current_chunk = sentence + ". "
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def summarize_large_text(text):
# Split the large text into chunks
chunks = chunk_text(text)
# Generate summaries for each chunk
summaries = []
for chunk in chunks:
summary = summarizer(chunk, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
summaries.append(summary)
# Combine individual summaries and then re-summarize for final concise summary
combined_summary = " ".join(summaries)
final_summary = summarizer(combined_summary, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
return final_summary
if __name__ == "__main__":
# Set up command-line argument parsing
parser = argparse.ArgumentParser(description="Summarize large text input into a concise 5-7 sentence summary.")
parser.add_argument("text", type=str, help="The text to summarize")
args = parser.parse_args()
# Generate and print the summary
summary = summarize_large_text(args.text)
print("Final Summary:")
print(summary)