Sumayyea commited on
Commit
df535ff
·
verified ·
1 Parent(s): 5827258

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +105 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import streamlit as st
3
+ from transformers import BartTokenizer, BartForConditionalGeneration
4
+ from sklearn.feature_extraction.text import TfidfVectorizer
5
+ from sklearn.cluster import KMeans
6
+ from nltk.tokenize import sent_tokenize
7
+ import nltk
8
+ nltk.download('punkt')
9
+
10
+ # Load BART model and tokenizer
11
+ @st.cache_resource
12
+ def load_model():
13
+ tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
14
+ model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
15
+ return tokenizer, model
16
+
17
+ tokenizer, model = load_model()
18
+
19
+ # Function for single document summarization
20
+ def summarize(text, max_length=150, min_length=50):
21
+ inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors='pt')
22
+ summary_ids = model.generate(
23
+ inputs['input_ids'],
24
+ num_beams=4,
25
+ max_length=max_length,
26
+ min_length=min_length,
27
+ early_stopping=True
28
+ )
29
+ return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
30
+
31
+ # Function for clustering documents
32
+ def cluster_documents(documents, n_clusters=3):
33
+ vectorizer = TfidfVectorizer(stop_words='english')
34
+ X = vectorizer.fit_transform(documents)
35
+ kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(X)
36
+ return kmeans.labels_
37
+
38
+ # Function for multi-document summarization
39
+ def multi_document_summarize(documents):
40
+ # Cluster documents
41
+ if len(documents) > 3:
42
+ n_clusters = min(3, len(documents)//2)
43
+ clusters = cluster_documents(documents, n_clusters=n_clusters)
44
+ else:
45
+ clusters = np.zeros(len(documents))
46
+
47
+ # Summarize each cluster
48
+ summaries = []
49
+ for cluster_id in np.unique(clusters):
50
+ cluster_docs = [doc for doc, c in zip(documents, clusters) if c == cluster_id]
51
+ combined_text = " ".join(cluster_docs)
52
+
53
+ # Split into chunks if too long for BART
54
+ max_chunk_length = 1000
55
+ if len(combined_text.split()) > max_chunk_length:
56
+ sentences = sent_tokenize(combined_text)
57
+ current_chunk = []
58
+ chunks = []
59
+ for sentence in sentences:
60
+ if len(" ".join(current_chunk + [sentence]).split()) <= max_chunk_length:
61
+ current_chunk.append(sentence)
62
+ else:
63
+ chunks.append(" ".join(current_chunk))
64
+ current_chunk = [sentence]
65
+ if current_chunk:
66
+ chunks.append(" ".join(current_chunk))
67
+ else:
68
+ chunks = [combined_text]
69
+
70
+ # Summarize each chunk and combine
71
+ cluster_summary = " ".join([summarize(chunk) for chunk in chunks])
72
+ summaries.append(cluster_summary)
73
+
74
+ # Create final summary
75
+ final_summary = summarize(" ".join(summaries))
76
+ return final_summary
77
+
78
+ # Streamlit UI
79
+ st.title("Multi-Document Summarization")
80
+ st.write("This system summarizes multiple related documents using clustering and BART.")
81
+
82
+ uploaded_files = st.file_uploader(
83
+ "Upload text files (one document per file)",
84
+ type=['txt'],
85
+ accept_multiple_files=True
86
+ )
87
+
88
+ if uploaded_files:
89
+ documents = []
90
+ for file in uploaded_files:
91
+ documents.append(file.read().decode("utf-8"))
92
+
93
+ if st.button("Generate Summary"):
94
+ with st.spinner("Processing documents..."):
95
+ summary = multi_document_summarize(documents)
96
+
97
+ st.subheader("Final Summary")
98
+ st.write(summary)
99
+
100
+ st.subheader("Input Documents Preview")
101
+ for i, doc in enumerate(documents):
102
+ st.write(f"### Document {i+1} (First 200 chars)")
103
+ st.write(doc[:200] + "...")
104
+ else:
105
+ st.info("Please upload some text files to get started.")
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ scikit-learn
4
+ nltk
5
+ torch
6
+ numpy