Update app.py
Browse files
app.py
CHANGED
@@ -17,16 +17,29 @@ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
17 |
# Extractive summarization using TextRank
|
18 |
def extractive_summarization(text, num_sentences=3):
|
19 |
sentences = sent_tokenize(text)
|
|
|
|
|
20 |
if len(sentences) <= num_sentences:
|
21 |
-
return
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Abstractive summarization using BART
|
32 |
def abstractive_summarization(text, length):
|
|
|
17 |
# Extractive summarization using TextRank
|
18 |
def extractive_summarization(text, num_sentences=3):
|
19 |
sentences = sent_tokenize(text)
|
20 |
+
|
21 |
+
# Handle cases where input text is too short
|
22 |
if len(sentences) <= num_sentences:
|
23 |
+
return "Text is too short for extractive summarization."
|
24 |
+
|
25 |
+
try:
|
26 |
+
vectorizer = TfidfVectorizer(stop_words="english")
|
27 |
+
sentence_vectors = vectorizer.fit_transform(sentences)
|
28 |
+
|
29 |
+
# Handle cases where vectorization fails due to low variation in text
|
30 |
+
if sentence_vectors.shape[0] < num_sentences:
|
31 |
+
return "Insufficient unique content for extractive summarization."
|
32 |
+
|
33 |
+
similarity_matrix = cosine_similarity(sentence_vectors)
|
34 |
+
graph = nx.from_numpy_array(similarity_matrix)
|
35 |
+
scores = nx.pagerank(graph)
|
36 |
+
|
37 |
+
ranked_sentences = sorted(((scores[i], s) for i, s in enumerate(sentences)), reverse=True)
|
38 |
+
return " ".join([s for _, s in ranked_sentences[:num_sentences]])
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error in extractive summarization: {str(e)}"
|
42 |
+
|
43 |
|
44 |
# Abstractive summarization using BART
|
45 |
def abstractive_summarization(text, length):
|