Spaces:
Sleeping
Sleeping
Fix: indentation and error handling in streamlit app
Browse files- streamlit_app.py +20 -10
streamlit_app.py
CHANGED
@@ -1,26 +1,36 @@
|
|
1 |
# streamlit_app.py
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
|
5 |
st.set_page_config(page_title="Text Summarizer", layout="centered")
|
6 |
-
|
7 |
st.title("π Text Summarization App")
|
8 |
|
9 |
input_text = st.text_area("Enter text to summarize", height=200)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
if st.button("Summarize"):
|
12 |
if input_text.strip() == "":
|
13 |
st.warning("Please enter some text.")
|
14 |
else:
|
15 |
try:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
st.subheader("Summary")
|
22 |
-
st.success(
|
23 |
-
else:
|
24 |
-
st.error("Failed to summarize. Try again.")
|
25 |
except Exception as e:
|
26 |
st.error(f"Error: {e}")
|
|
|
1 |
# streamlit_app.py
|
2 |
import streamlit as st
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
|
5 |
st.set_page_config(page_title="Text Summarizer", layout="centered")
|
|
|
6 |
st.title("π Text Summarization App")
|
7 |
|
8 |
input_text = st.text_area("Enter text to summarize", height=200)
|
9 |
|
10 |
+
@st.cache_resource
|
11 |
+
def load_model():
|
12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/pegasus-cnn_dailymail")
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained("google/pegasus-cnn_dailymail")
|
14 |
+
return model, tokenizer
|
15 |
+
|
16 |
if st.button("Summarize"):
|
17 |
if input_text.strip() == "":
|
18 |
st.warning("Please enter some text.")
|
19 |
else:
|
20 |
try:
|
21 |
+
with st.spinner("Summarizing..."):
|
22 |
+
model, tokenizer = load_model()
|
23 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True)
|
24 |
+
summary_ids = model.generate(
|
25 |
+
inputs["input_ids"],
|
26 |
+
max_length=100,
|
27 |
+
min_length=30,
|
28 |
+
length_penalty=2.0,
|
29 |
+
num_beams=4,
|
30 |
+
early_stopping=True
|
31 |
+
)
|
32 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
33 |
st.subheader("Summary")
|
34 |
+
st.success(summary)
|
|
|
|
|
35 |
except Exception as e:
|
36 |
st.error(f"Error: {e}")
|