QuoQaGo / app.py
snoop2head's picture
ignore whitespace inputs
640c8c2
raw
history blame
2.29 kB
# -*- coding: utf-8 -*-
import numpy as np
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
st.set_page_config(
page_title="์ฟผ์นด๊ณ ", layout="wide", initial_sidebar_state="expanded"
)
@st.cache
def load_model(model_name):
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
return model
tokenizer = AutoTokenizer.from_pretrained("QuoQA-NLP/KE-T5-Ko2En-Base")
ko2en_model = load_model("QuoQA-NLP/KE-T5-Ko2En-Base")
en2ko_model = load_model("QuoQA-NLP/KE-T5-En2Ko-Base")
st.title("๐Ÿป ์ฟผ์นด๊ณ  ๋ฒˆ์—ญ๊ธฐ")
st.write("์ขŒ์ธก์— ๋ฒˆ์—ญ ๋ชจ๋“œ๋ฅผ ์„ ํƒํ•˜๊ณ , CTRL+Enter(CMD+Enter)๋ฅผ ๋ˆ„๋ฅด์„ธ์š” ๐Ÿค—")
st.write("Select Translation Mode at the left and press CTRL+Enter(CMD+Enter)๐Ÿค—")
translation_list = ["ํ•œ๊ตญ์–ด์—์„œ ์˜์–ด | Korean to English", "์˜์–ด์—์„œ ํ•œ๊ตญ์–ด | English to Korean"]
translation_mode = st.sidebar.radio("๋ฒˆ์—ญ ๋ชจ๋“œ๋ฅผ ์„ ํƒ(Translation Mode):", translation_list)
default_value = '์‹ ํ•œ์นด๋“œ ๊ด€๊ณ„์ž๋Š” "๊ณผ๊ฑฐ ๋‚ด๋†“์€ ์ƒํ’ˆ์˜ ๊ฒฝ์šฐ ์ถœ์‹œ 2๊ฐœ์›” ๋งŒ์— ์ ๊ธˆ ๊ฐ€์ž…์ด 4๋งŒ์—ฌ ์ขŒ์— ๋‹ฌํ•  ์ •๋„๋กœ ์ธ๊ธฐ๋ฅผ ๋Œ์—ˆ๋‹ค"๋ฉด์„œ "๊ธˆ๋ฆฌ ์ธ์ƒ์— ๋”ฐ๋ผ ์ ๊ธˆ ๊ธˆ๋ฆฌ๋ฅผ ๋” ์˜ฌ๋ ค ๋งŽ์€ ๊ณ ๊ฐ์ด ๋ชฐ๋ฆด ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•˜๊ณ  ์žˆ๋‹ค"๊ณ  ๋งํ–ˆ๋‹ค.'
src_text = st.text_area(
"๋ฒˆ์—ญํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”:",
default_value,
height=300,
max_chars=200,
)
print(src_text)
if src_text == "":
st.warning("Please **enter text** for translation")
else:
# translate into english sentence
if translation_mode == translation_list[0]:
model = ko2en_model
else:
model = en2ko_model
translation_result = model.generate(
**tokenizer(
src_text,
return_tensors="pt",
padding="max_length",
truncation=True,
max_length=64,
),
max_length=64,
num_beams=5,
repetition_penalty=1.3,
no_repeat_ngram_size=3,
num_return_sequences=1,
)
translation_result = tokenizer.decode(
translation_result[0],
clean_up_tokenization_spaces=True,
skip_special_tokens=True,
)
print(f"{src_text} -> {translation_result}")
st.write(translation_result)
print(translation_result)