|
|
|
import streamlit as st |
|
from transformers import T5ForConditionalGeneration, T5TokenizerFast, T5Config |
|
|
|
@st.cache(allow_output_mutation=True, suppress_st_warning=True) |
|
def load_model(): |
|
model_name = "north/demo-deuncaser-base" |
|
config = T5Config.from_pretrained(model_name) |
|
|
|
|
|
|
|
model = T5ForConditionalGeneration.from_pretrained(model_name,config=config) |
|
tokenizer = T5TokenizerFast.from_pretrained(model_name) |
|
return (model, tokenizer) |
|
|
|
def deuncase(model, tokenizer, text): |
|
encoded_txt = tokenizer(text, return_tensors="pt") |
|
generated_tokens = model.generate( |
|
**encoded_txt |
|
) |
|
return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) |
|
|
|
def spaces(): |
|
st.ta.value= "About" |
|
return None |
|
|
|
st.title("DeUnCaser") |
|
|
|
expander = st.sidebar.expander("About") |
|
expander.write("This web app adds spaces, punctation and capitalisation back into the text.") |
|
|
|
optionx = st.sidebar.selectbox( |
|
"Examples:", |
|
("tirsdag var travel for ukrainas president volodymyr zelenskyj på morgenen tok han imot polens statsminister mateusz morawiecki","tirsdagvartravelforukrainaspresidentvolodymyrzelenskyjpåkveldentokhanimotpolensstatsministermateuszmorawiecki","deterikkelettåholderedepåstoreogsmåbokstavermanmåforeksempelhuskestorforbokstavnårmanskriveromkrimhalvøyamenkunbrukelitenforbokstavnårmanhenvisertilenkrimroman","detteerenlitendemosomerlagetavperegilkummervoldhanerenforskersomtidligerejobbetvednasjonalbiblioteketimoirana")) |
|
|
|
|
|
st.sidebar.write("This web app adds spaces, punctation and capitalisation back into the text.") |
|
st.sidebar.write("You can use the examples below, but too really test the effect of the model: Write or copy text from the Internet, and then use the buttons to remove spaces, puctation, cases etc. Try to restore the text.") |
|
|
|
|
|
|
|
col1, col2, col3 = st.columns([1,1,1]) |
|
with col1: |
|
st.button('Remove Casing') |
|
with col2: |
|
st.button('Remove Punctation') |
|
with col3: |
|
st.button('Remove Spaces') |
|
|
|
text = st.text_area(f"",max_chars=1000, value= "") |
|
|
|
run = st.button('Run DeUnCaser') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if run: |
|
model, tokenizer = load_model() |
|
translated_text = deuncase(model, tokenizer, text) |
|
st.write(translated_text[0] if translated_text else "Unknown Error Translating Text") |
|
|
|
|