|
import streamlit as st |
|
from transformers import pipeline |
|
import textwrap |
|
|
|
st.title('Hugging Face BERT Summarizer') |
|
|
|
|
|
models = ["sshleifer/distilbart-cnn-12-6", "facebook/bart-large-cnn", "t5-base", "t5-large", "google/pegasus-newsroom"] |
|
|
|
|
|
model = st.sidebar.selectbox("Choose a model", models) |
|
|
|
uploaded_file = st.file_uploader("Choose a .txt file", type="txt") |
|
|
|
if uploaded_file is not None: |
|
user_input = uploaded_file.read().decode('utf-8') |
|
total_length = len(user_input.split()) |
|
|
|
|
|
min_length_percentage = st.sidebar.slider('Minimum Length %', min_value=10, max_value=100, value=50) |
|
max_length_percentage = min_length_percentage + 10 |
|
st.sidebar.text(f'Maximum Length %: {max_length_percentage}') |
|
|
|
if st.button('Summarize'): |
|
summarizer = pipeline('summarization', model=model) |
|
summarized_text = "" |
|
|
|
|
|
chunks = textwrap.wrap(user_input, 500) |
|
|
|
|
|
for chunk in chunks: |
|
min_length = max(int(total_length * min_length_percentage / 100), 1) |
|
max_length = int(total_length * max_length_percentage / 100) |
|
summarized = summarizer(chunk, max_length=max_length, min_length=min_length, do_sample=False) |
|
summarized_text += summarized[0]['summary_text'] + " " |
|
|
|
st.text_area('Summarized Text', summarized_text, height=200) |
|
|