7jimmy's picture
Create app.py
6ce28c5
raw
history blame
2.11 kB
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()