Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Cache the summarizer to avoid reloading on every interaction
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_summarizer():
|
| 7 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
| 8 |
+
|
| 9 |
+
summarizer = load_summarizer()
|
| 10 |
+
|
| 11 |
+
# Streamlit UI setup
|
| 12 |
+
st.title("📝 Text Summarization App")
|
| 13 |
+
|
| 14 |
+
# User input box (allows large input text)
|
| 15 |
+
user_input = st.text_area("Enter text to summarize:", "", height=300)
|
| 16 |
+
|
| 17 |
+
# Custom CSS to position the slider in the top-right corner
|
| 18 |
+
st.markdown("""
|
| 19 |
+
<style>
|
| 20 |
+
.slider-container {
|
| 21 |
+
position: fixed;
|
| 22 |
+
top: 10px;
|
| 23 |
+
right: 10px;
|
| 24 |
+
z-index: 1000;
|
| 25 |
+
}
|
| 26 |
+
</style>
|
| 27 |
+
""", unsafe_allow_html=True)
|
| 28 |
+
|
| 29 |
+
# Slider for adjusting summary length in the top-right corner
|
| 30 |
+
with st.container():
|
| 31 |
+
st.markdown('<div class="slider-container">', unsafe_allow_html=True)
|
| 32 |
+
summary_length = st.slider(
|
| 33 |
+
"Adjust Summary Length",
|
| 34 |
+
min_value=1,
|
| 35 |
+
max_value=3,
|
| 36 |
+
value=2,
|
| 37 |
+
step=1,
|
| 38 |
+
format="Summary Length: %d"
|
| 39 |
+
)
|
| 40 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 41 |
+
|
| 42 |
+
# Set min and max summary length based on the selected slider value
|
| 43 |
+
if summary_length == 1: # Short
|
| 44 |
+
min_len = 10
|
| 45 |
+
max_len = 50
|
| 46 |
+
elif summary_length == 2: # Medium
|
| 47 |
+
min_len = 50
|
| 48 |
+
max_len = 150
|
| 49 |
+
else: # Long
|
| 50 |
+
min_len = 150
|
| 51 |
+
max_len = 300
|
| 52 |
+
|
| 53 |
+
# Display the selected range for feedback
|
| 54 |
+
st.write(f"Selected Summary Length: {'Short' if summary_length == 1 else 'Medium' if summary_length == 2 else 'Long'}")
|
| 55 |
+
|
| 56 |
+
# Button to trigger summarization
|
| 57 |
+
if st.button("Summarize"):
|
| 58 |
+
if user_input.strip(): # Ensure there's input before summarizing
|
| 59 |
+
try:
|
| 60 |
+
# Generate summary using the model with custom min and max length
|
| 61 |
+
summarized_text = summarizer(user_input,
|
| 62 |
+
max_length=max_len,
|
| 63 |
+
min_length=min_len,
|
| 64 |
+
length_penalty=2.0,
|
| 65 |
+
num_beams=4,
|
| 66 |
+
early_stopping=True)[0]['summary_text']
|
| 67 |
+
|
| 68 |
+
# Display the generated summary
|
| 69 |
+
st.subheader("Summarized Text:")
|
| 70 |
+
st.write(summarized_text)
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"An error occurred while summarizing: {e}")
|
| 74 |
+
else:
|
| 75 |
+
st.warning("Please enter some text to summarize.")
|