Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import pipeline | |
| # Initialize the summarization pipeline | |
| summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
| # Streamlit UI setup | |
| st.title("๐ Text Summarization App") | |
| # User input box (allows large input text) | |
| user_input = st.text_area("Enter text to summarize:", "", height=300) | |
| # Custom CSS to position the slider in the top-right corner | |
| st.markdown(""" | |
| <style> | |
| .slider-container { | |
| position: fixed; | |
| top: 10px; | |
| right: 10px; | |
| z-index: 1000; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Slider for adjusting summary length in the top-right corner | |
| with st.container(): | |
| st.markdown('<div class="slider-container">', unsafe_allow_html=True) | |
| summary_length = st.slider( | |
| "Adjust Summary Length", | |
| min_value=1, | |
| max_value=3, | |
| value=2, | |
| step=1, | |
| format="Summary Length: %d" | |
| ) | |
| st.markdown('</div>', unsafe_allow_html=True) | |
| # Set min and max summary length based on the selected slider value | |
| if summary_length == 1: # Short | |
| min_len = 10 | |
| max_len = 50 | |
| elif summary_length == 2: # Medium | |
| min_len = 50 | |
| max_len = 150 | |
| else: # Long | |
| min_len = 150 | |
| max_len = 300 | |
| # Display the selected range for feedback | |
| st.write(f"Selected Summary Length: {'Short' if summary_length == 1 else 'Medium' if summary_length == 2 else 'Long'}") | |
| # Function to split text into manageable chunks | |
| def chunk_text(text, max_chunk_size=1024): | |
| tokens = text.split() | |
| chunks = [] | |
| current_chunk = [] | |
| for token in tokens: | |
| current_chunk.append(token) | |
| if len(' '.join(current_chunk)) > max_chunk_size: | |
| chunks.append(' '.join(current_chunk[:-1])) | |
| current_chunk = [token] | |
| chunks.append(' '.join(current_chunk)) # Add the final chunk | |
| return chunks | |
| # Button to trigger summarization | |
| if st.button("Summarize"): | |
| if user_input.strip(): # Ensure there's input before summarizing | |
| try: | |
| # Split the input into chunks | |
| text_chunks = chunk_text(user_input) | |
| # Summarize each chunk separately | |
| summaries = [] | |
| for chunk in text_chunks: | |
| summary = summarizer(chunk, max_length=max_len, min_length=min_len, length_penalty=2.0, num_beams=4, early_stopping=True)[0]['summary_text'] | |
| summaries.append(summary) | |
| # Combine summaries from all chunks | |
| full_summary = " ".join(summaries) | |
| # Display the generated summary | |
| st.subheader("Summarized Text:") | |
| st.write(full_summary) | |
| except Exception as e: | |
| st.error(f"An error occurred while summarizing: {e}") | |
| else: | |
| st.warning("Please enter some text to summarize.") | |