File size: 2,793 Bytes
edeb228
 
 
4c68814
 
edeb228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c68814
 
 
 
 
 
 
 
 
 
 
 
 
 
edeb228
 
 
 
4c68814
 
 
 
 
 
 
 
 
 
 
edeb228
 
 
4c68814
edeb228
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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.")