Spaces:
Sleeping
Sleeping
PyTorch utilized.
Browse files
app.py
CHANGED
|
@@ -1,29 +1,32 @@
|
|
| 1 |
-
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from transformers import BartTokenizer,
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
input_length = len(tokenizer.encode(text, return_tensors='tf', max_length=1024, truncation=True)[0])
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
max_length = int(input_length * 0.6)
|
| 15 |
min_length = int(input_length * 0.5)
|
| 16 |
length_penalty = 1.5
|
| 17 |
-
elif style ==
|
| 18 |
max_length = int(input_length * 0.45)
|
| 19 |
min_length = int(input_length * 0.35)
|
| 20 |
length_penalty = 1.2
|
| 21 |
-
else:
|
| 22 |
max_length = int(input_length * 0.4)
|
| 23 |
min_length = int(input_length * 0.3)
|
| 24 |
length_penalty = 1.0
|
| 25 |
|
| 26 |
-
inputs = tokenizer.encode(text, return_tensors='tf', max_length=1024, truncation=True)
|
| 27 |
summary_ids = model.generate(
|
| 28 |
inputs,
|
| 29 |
max_length=max_length,
|
|
@@ -31,27 +34,26 @@ def summarize(text, style):
|
|
| 31 |
length_penalty=length_penalty,
|
| 32 |
num_beams=4,
|
| 33 |
no_repeat_ngram_size=3,
|
| 34 |
-
early_stopping=True
|
| 35 |
)
|
| 36 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True
|
| 37 |
|
| 38 |
-
if not summary.endswith((
|
| 39 |
-
summary +=
|
| 40 |
return summary
|
| 41 |
|
| 42 |
-
st.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
if st.button(
|
| 50 |
-
if user_input:
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
st.write(summary)
|
| 54 |
else:
|
| 55 |
-
st.
|
| 56 |
-
|
| 57 |
-
# End of program 2.0
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 3 |
|
| 4 |
+
@st.cache_resource
|
| 5 |
+
def load_model():
|
| 6 |
+
model_name = "facebook/bart-large-cnn"
|
| 7 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
return tokenizer, model
|
| 10 |
|
| 11 |
+
tokenizer, model = load_model()
|
|
|
|
| 12 |
|
| 13 |
+
def summarize(text: str, style: str):
|
| 14 |
+
inputs = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True)
|
| 15 |
+
input_length = len(inputs[0])
|
| 16 |
+
|
| 17 |
+
if style == "Normal":
|
| 18 |
max_length = int(input_length * 0.6)
|
| 19 |
min_length = int(input_length * 0.5)
|
| 20 |
length_penalty = 1.5
|
| 21 |
+
elif style == "Precise":
|
| 22 |
max_length = int(input_length * 0.45)
|
| 23 |
min_length = int(input_length * 0.35)
|
| 24 |
length_penalty = 1.2
|
| 25 |
+
else: # Accurate
|
| 26 |
max_length = int(input_length * 0.4)
|
| 27 |
min_length = int(input_length * 0.3)
|
| 28 |
length_penalty = 1.0
|
| 29 |
|
|
|
|
| 30 |
summary_ids = model.generate(
|
| 31 |
inputs,
|
| 32 |
max_length=max_length,
|
|
|
|
| 34 |
length_penalty=length_penalty,
|
| 35 |
num_beams=4,
|
| 36 |
no_repeat_ngram_size=3,
|
| 37 |
+
early_stopping=True,
|
| 38 |
)
|
| 39 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 40 |
|
| 41 |
+
if not summary.endswith((".", "!", "?")):
|
| 42 |
+
summary += "."
|
| 43 |
return summary
|
| 44 |
|
| 45 |
+
st.set_page_config(page_title="Text Summarizer", page_icon="📝", layout="centered")
|
| 46 |
+
st.title("🧠 Text Summarizer")
|
| 47 |
+
st.write("Summarize long text into concise and clear summaries using **BART**.")
|
| 48 |
+
|
| 49 |
+
user_input = st.text_area("Enter text to summarize:", height=200)
|
| 50 |
+
summary_style = st.selectbox("Choose summarization style:", ("Normal", "Precise", "Accurate"))
|
| 51 |
+
|
| 52 |
+
if st.button("Summarize"):
|
| 53 |
+
if user_input.strip():
|
| 54 |
+
with st.spinner("Summarizing... ⏳"):
|
| 55 |
+
summary = summarize(user_input, summary_style)
|
| 56 |
+
st.subheader("Summary:")
|
| 57 |
st.write(summary)
|
| 58 |
else:
|
| 59 |
+
st.warning("⚠️ Please enter some text to summarize.")
|
|
|
|
|
|