Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
import keras_nlp
|
5 |
+
import PyPDF2
|
6 |
+
import docx2txt
|
7 |
+
|
8 |
+
# Load your Keras model
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model_and_preprocessor():
|
11 |
+
bart_billsum = keras_nlp.models.BartSeq2SeqLM.from_preset('Grey01/bart_billsum')
|
12 |
+
|
13 |
+
# Load the default BART preprocessor (assuming you saved its configuration)
|
14 |
+
preprocessor = keras_nlp.models.BartSeq2SeqLMPreprocessor.from_preset('bart_base_en', encoder_sequence_length=512,
|
15 |
+
decoder_sequence_length=128,)
|
16 |
+
|
17 |
+
return model, preprocessor
|
18 |
+
|
19 |
+
model, preprocessor = load_model_and_preprocessor()
|
20 |
+
|
21 |
+
st.title("SummarizeIt")
|
22 |
+
|
23 |
+
# File uploader
|
24 |
+
uploaded_file = st.file_uploader("Choose a file", type=["pdf", "txt", "docx"])
|
25 |
+
|
26 |
+
# Text extraction
|
27 |
+
text = ""
|
28 |
+
if uploaded_file is not None:
|
29 |
+
if uploaded_file.type == "application/pdf":
|
30 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
31 |
+
for page in pdf_reader.pages:
|
32 |
+
text += page.extract_text()
|
33 |
+
elif uploaded_file.type == "text/plain":
|
34 |
+
text = uploaded_file.read().decode("utf-8")
|
35 |
+
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
36 |
+
text = docx2txt.process(uploaded_file)
|
37 |
+
# Text input for direct text entry
|
38 |
+
user_input = st.text_area("Or paste your text here:")
|
39 |
+
text = user_input if user_input else text # Prioritize user input over file
|
40 |
+
|
41 |
+
def generate_text(model, input_texts, max_length=200, print_time_taken=False):
|
42 |
+
# Convert input_texts to a list if it's a Dataset
|
43 |
+
if isinstance(input_texts, datasets.Dataset):
|
44 |
+
input_texts = input_texts.to_list()
|
45 |
+
chunks = [input_texts[i:i+512] for i in range(0, len(input_texts), 512)]
|
46 |
+
#initialize an empty list to store summaries
|
47 |
+
summaries = []
|
48 |
+
# generate summaries for each chunk
|
49 |
+
for chunk in chunks:
|
50 |
+
# Assuming your model's generate method can handle a batch of inputs
|
51 |
+
summary = model.generate(input_texts, max_length=max_length)
|
52 |
+
summaries.append(summary)
|
53 |
+
return summary
|
54 |
+
|
55 |
+
generated_summaries = generate_text(
|
56 |
+
model,
|
57 |
+
text, # Pass the list of documents directly
|
58 |
+
)
|
59 |
+
st.subheader("Generated Summary:")
|
60 |
+
st.write(summary)
|