File size: 2,107 Bytes
6ce28c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from streamlit.components.v1 import html
from transformers import BartForConditionalGeneration, BartTokenizer

# Function to add custom CSS for background styling
def set_background(style):
    component = """
        <style>
            .stApp {
                %s
            }
        </style>
    """ % style
    return html(component, height=0)

def summarize_data(data, max_length, model, tokenizer):
    inputs = tokenizer.encode("summarize: " + data, return_tensors="pt", max_length=1024, truncation=True)
    summary_ids = model.generate(inputs, max_length=max_length, min_length=max_length//4, length_penalty=2.0, num_beams=4, early_stopping=True)

    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary

def main():
    # Set background style
    set_background("""
        background-image: url('https://your-image-url.jpg');
        background-size: cover;
    """)

    # Title and description
    st.title("Data Summarization")
    st.write("Enter your data, set the summary length, and click submit.")

    # Text input box
    data = st.text_area("Enter your Data", height=200)

    # Summary Length slider
    max_length = st.slider("Summary Length", min_value=20, max_value=1000, step=1)

    # Load the pretrained BART model and tokenizer
    model_name = "facebook/bart-large-cnn"
    model = BartForConditionalGeneration.from_pretrained(model_name)
    tokenizer = BartTokenizer.from_pretrained(model_name)

    # Submit button
    if st.button("Submit"):
        if not data:
            st.warning("Please enter some data.")
        else:
            # Summarize data
            result = summarize_data(data, max_length, model, tokenizer)
            
            # Display result in a stylized output box
            st.markdown(
                f"<div style='background-color:#8c88f9; padding: 10px; border-radius: 10px;'>"
                f"<h3>Your Summary</h3>"
                f"<p>{result}</p>"
                f"</div>",
                unsafe_allow_html=True,
            )

if __name__ == "__main__":
    main()