|
import os |
|
import time |
|
|
|
import requests |
|
import streamlit as st |
|
|
|
API_URL = "https://api-inference.huggingface.co/models/pere/nb-nn-translation" |
|
|
|
|
|
def translate(text, wait=True): |
|
headers = {"Authorization": f"Bearer {os.environ['BEARER']}"} |
|
payload = { |
|
"inputs": text, |
|
"options": { |
|
"wait_for_model": not wait |
|
} |
|
} |
|
response = requests.post(API_URL, headers=headers, json=payload) |
|
json_response = response.json() |
|
if (isinstance(json_response, dict) |
|
and "error" in json_response |
|
and "estimated_time" in json_response): |
|
st.write(json_response) |
|
if wait: |
|
with st.spinner(json_response["error"]): |
|
bar = st.progress(0) |
|
time_to_load = int(json_response["estimated_time"]) + 1 |
|
for progress in range(time_to_load): |
|
bar.progress(progress / time_to_load) |
|
time.sleep(1) |
|
bar.empty() |
|
return translate(text, wait=False) |
|
else: |
|
return "We could not load the model" |
|
elif (isinstance(json_response, list) |
|
and "translation_text" in json_response[0]): |
|
return json_response[0]["translation_text"] |
|
else: |
|
return f"Oops, something went wrong: {str(json_response)}" |
|
|
|
|
|
st.set_page_config( |
|
page_title='Norwegian Bokmål to Nynorsk', |
|
page_icon='translator-icon.png', |
|
) |
|
st.title("Bokmål ⇔ Nynorsk") |
|
st.sidebar.write(""" |
|
Here are some sample texts in Norwegian Bokmål and Norwegian Nynorsk that you can try to translate. They are here presented in pairs (Bokmål, Nynorsk, Bokmål...). This way you can also see a suggested translation of the text. As you can see there are a lot of similarities between the languages. Since there also are some grammatical differences, the translation task can not be solved by dictionary replacements. A finetuned model on top of a pretrained t5-base from a balanced corpus, seem to solve the task with a SACREBLEU-score of 88.17. |
|
""") |
|
|
|
masked_texts = [ |
|
"Min tekst", |
|
"Din tekst" |
|
] |
|
input_text = st.sidebar.selectbox("Select a Text", options=masked_texts) |
|
|
|
text = st.text_area("Enter text:", |
|
input_text, |
|
height=None, |
|
max_chars=None, |
|
key=None, |
|
help="Enter your text here", |
|
) |
|
|
|
if st.button('Translate'): |
|
if str(text).strip() == "": |
|
st.warning('Please **enter text** for translation') |
|
else: |
|
st.info(str(translate(text))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|