import base64 from io import StringIO from math import ceil import streamlit as st from utils import get_resources, simplify st.set_page_config(page_title="Text Simplification in Dutch", page_icon="🏃") BATCH_SIZE = 8 if "text_to_simplify" not in st.session_state: st.session_state["text_to_simplify"] = None st.title("🏃 Text Simplification in Dutch") fupload_check = st.checkbox("File upload?") st.markdown( "Make sure that the file or text in the text box contains **one sentence per line**. Empty lines will" " be removed." ) if fupload_check: uploaded_file = st.file_uploader("Text file", label_visibility="collapsed") if uploaded_file is not None: stringio = StringIO(uploaded_file.getvalue().decode("utf-8")) st.session_state["text_to_simplify"] = stringio.read().strip() else: st.session_state["text_to_simplify"] = None else: st.session_state["text_to_simplify"] = st.text_area( label="Sentences to translate", label_visibility="collapsed", height=200, value="Met het naderen van de zonovergoten middaghemel op deze betoverende dag, waarbij de atmosferische omstandigheden een onbelemmerde convergentie van cumulusbewolking en uitgestrekte stratosferische azuurblauwe wijdheid faciliteren, lijken de geaggregeerde weersverschijnselen van vandaag, die variëren van sporadische plensbuien tot kalme zuchtjes wind en zeldzame opvlammingen van bliksem, de delicate balans tussen meteorologische complexiteit en eenvoud te weerspiegelen, waardoor de gepassioneerde observator met een gevoel van ontzag en verwondering wordt vervuld.", ).strip() def _get_increment_size(num_sents) -> int: if num_sents == 1: return 100 else: return ceil(100 / num_sents) btn_col, results_col = st.columns(2) btn_ct = btn_col.empty() pbar_ct = st.empty() error_ct = st.empty() simpl_ct = st.container() if st.session_state["text_to_simplify"]: if btn_ct.button("Simplify text"): error_ct.empty() lines = [ strip_line for line in st.session_state["text_to_simplify"].splitlines() if (strip_line := line.strip()) ] num_sentences = len(lines) pbar = pbar_ct.progress(0, text=f"Simplifying sentences in batches of {BATCH_SIZE}...") increment = _get_increment_size(num_sentences) percent_done = 0 model, tokenizer = get_resources() simpl_ct.caption("Simplified text") output_ct = simpl_ct.empty() all_simplifications = [] html = "