File size: 2,322 Bytes
4fd8256
 
 
 
 
 
2a9d3fa
 
 
4fd8256
 
 
 
 
 
 
 
 
2beda2c
2a9d3fa
 
 
 
 
 
ec0b50d
db6849b
ec0b50d
 
 
4fd8256
2a9d3fa
4fd8256
 
2beda2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a9d3fa
 
2beda2c
ec0b50d
2a9d3fa
ec0b50d
2a9d3fa
2beda2c
 
2a9d3fa
 
 
 
 
 
 
4fd8256
ec0b50d
 
 
 
 
 
d19be82
4fd8256
 
 
 
ec0b50d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import numpy as np
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM


st.set_page_config(
    page_title="KoQuillBot", layout="wide", initial_sidebar_state="expanded"
)

tokenizer = AutoTokenizer.from_pretrained("QuoQA-NLP/KE-T5-Ko2En-Base")
ko2en_model = AutoModelForSeq2SeqLM.from_pretrained("QuoQA-NLP/KE-T5-Ko2En-Base")
en2ko_model = AutoModelForSeq2SeqLM.from_pretrained("QuoQA-NLP/KE-T5-En2Ko-Base")


st.title("🤖 KoQuillBot")


default_value = "이건 한국어 문장 변환기 QuillBot입니다."
src_text = st.text_area(
    "바꾸고 싶은 문장을 입력하세요:",
    default_value,
    height=50,
    max_chars=200,
)
print(src_text)



if st.button("문장 변환") or src_text == default_value:
    if src_text == "":
        st.warning("Please **enter text** for translation")

    else:
        # translate into english sentence
        english_translation = ko2en_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,
        )
        english_translation = tokenizer.decode(
            english_translation[0],
            clean_up_tokenization_spaces=True,
            skip_special_tokens=True,
        )

        # translate back to korean
        korean_translation = en2ko_model.generate(
            **tokenizer(
                english_translation,
                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,
        )

        korean_translation = tokenizer.decode(
            korean_translation[0],
            clean_up_tokenization_spaces=True,
            skip_special_tokens=True,
        )
        print(f"{src_text} -> {english_translation} -> {korean_translation}")
else:
    pass


st.write(korean_translation)
print(korean_translation)