File size: 6,172 Bytes
ac6d958 4d4c9b0 ac6d958 4d4c9b0 0a7cea6 4d4c9b0 0a7cea6 ac6d958 131e75e bf37b0d 0a7cea6 bf37b0d 0a7cea6 bf37b0d 0a7cea6 ac6d958 0a7cea6 131e75e ac6d958 bf37b0d 131e75e bf37b0d 131e75e bf37b0d 131e75e bf37b0d 131e75e bf37b0d c968835 bf37b0d 0a7cea6 c968835 bf37b0d 5a31d97 085de41 5a31d97 bf37b0d 085de41 bf37b0d c968835 5a31d97 085de41 5a31d97 bf37b0d c7f4c0d bf37b0d 0a7cea6 c968835 |
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
en2ko = "KoJLabs/nllb-finetuned-en2ko"
ko2en = "KoJLabs/nllb-finetuned-ko2en"
style = "KoJLabs/bart-speech-style-converter"
en2ko_model = AutoModelForSeq2SeqLM.from_pretrained(en2ko)
ko2en_model = AutoModelForSeq2SeqLM.from_pretrained(ko2en)
style_model = AutoModelForSeq2SeqLM.from_pretrained(style)
en2ko_tokenizer = AutoTokenizer.from_pretrained(en2ko)
ko2en_tokenizer = AutoTokenizer.from_pretrained(ko2en)
style_tokenizer = AutoTokenizer.from_pretrained(style)
def _post_process(self, text):
textList = []
emojiList = []
twit = Twitter()
posText = twit.pos(text)
posArray = np.array(posText)
for i in range(len(posArray)):
if posArray[i][1] == 'KoreanParticle':
emojiList.append(posArray[i][0])
for i in range(len(emojiList)):
splitText = text.split(emojiList[i], maxsplit=1)
if splitText[0] == '':
textList.append('')
else:
textList.append(splitText[0])
try:
if len(splitText[1:]) > 1:
text = ''.join(splitText[1:]).strip()
else:
text = splitText[1:][0].strip()
except:
break
try:
if text in emojiList[i+1]:
pass
except:
textList.append(splitText[-1])
emojiList.append('')
break
## μ΄λͺ¨μ§ μλ κ²½μ°
if len(emojiList) < 1:
emojiList.append('')
textList.append(text)
return emojiList, textList
def translation(source, target, text):
formats = {"English":"eng_Latn", "Korean":"kor_Hang"}
src = formats[source]
tgt = formats[target]
if src == "eng_Latn":
translator = pipeline(
'translation',
model=en2ko_model,
tokenizer=en2ko_tokenizer,
src_lang=src,
tgt_lang=tgt,
)
if src == "kor_Hang":
translator = pipeline(
'translation',
model=ko2en_model,
tokenizer=ko2en_tokenizer,
src_lang=src,
tgt_lang=tgt
)
output = translator(text)
translated_text = output[0]['translation_text']
if (text == '') or (text == '!') or (text == '?') or (text == '.') or (text == ','):
return text
else:
return translated_text
def augmentation(text):
emojiList, textList = _post_process(text)
ko2en_translator = pipeline(
'translation',
model=ko2en_model,
tokenizer=ko2en_tokenizer,
src_lang="kor_Hang",
tgt_lang="eng_Latn"
)
output = ko2en_translator(textList)
outputs = []
for out in output:
outputs.append(out['translation_text'])
ko2en_text = outputs
en2ko_translator = pipeline(
'translation',
model=en2ko_model,
tokenizer=en2ko_tokenizer,
src_lang="eng_Latn",
tgt_lang="kor_Hang",
)
output = en2ko_translator(ko2en_text)
en2ko_text = []
for txt in en2ko_text:
en2ko_text.append(txt['translation_text'])
outList = []
for emo, txt in zip(emojiList, en2ko_text):
output = txt + emo
outList.append(output)
output = ''.join(outList).strip()
return output
def conversion(source, text):
formats = {
"formal":"λ¬Έμ΄μ²΄",
"informal":"ꡬμ΄μ²΄",
"android":"μλλ‘μ΄λ",
"azae":"μμ¬",
"chat":"μ±ν
",
"choding":"μ΄λ±νμ",
"emoticon":"μ΄λͺ¨ν°μ½",
"enfp":"enfp",
"gentle":"μ μ¬",
"halbae":"ν μλ²μ§",
"halmae":"ν λ¨Έλ",
"joongding":"μ€νμ",
"king":"μ",
"naruto":"λ루ν ",
"seonbi":"μ λΉ",
"sosim":"μμ¬ν",
"translator":"λ²μκΈ°",
}
style = formats[source]
input_text = f"{style} νμμΌλ‘ λ³ν:" + text
converter = pipeline(
'text2text-generation',
model=style_model,
tokenizer=style_tokenizer,
)
output = converter(input_text)
generated_text = output[0]['generated_text']
return generated_text
lang = ['English','Korean']
style = ['formal', 'informal', 'android', 'azae', 'chat', 'choding', 'emoticon', 'enfp', \
'gentle', 'halbae', 'halmae', 'joongding', 'king', 'naruto', 'seonbi', 'sosim', 'translator']
translation_app = gr.Interface(
fn=translation,
inputs=[gr.inputs.Dropdown(choices=lang, label='Source Language'), gr.inputs.Dropdown(choices=lang, label='Target Language'), gr.inputs.Textbox(lines=5, label='Text to Translate')],
outputs=[gr.outputs.Textbox(label='Translated Text')],
title="Translation",
enable_queue=True,
)
augmentation_app = gr.Interface(
fn=augmentation,
inputs=[gr.inputs.Textbox(lines=5, label='Korean Text to Augmentation')],
outputs=[gr.outputs.Textbox(label='Augmented Text')],
title="Korean Data Augmentation (w.backtranslation)",
enable_queue=True,
)
conversion_app = gr.Interface(
fn=conversion,
inputs=[gr.inputs.Dropdown(choices=style, label='Speech Style'), gr.inputs.Textbox(lines=5, label='Text to style conversion')],
outputs=[gr.outputs.Textbox(label='Converted Text')],
title="Speech Style Conversion",
enable_queue=True,
)
demo = gr.TabbedInterface([translation_app, augmentation_app, conversion_app], \
["Translation", "Augmentation", "Speech Style conversion"],\
title = 'π₯If you want to download as pip package, \
please visit our github. (https://github.com/KoJLabs/KoTAN) π₯'
)
demo.launch() |