alphahg's picture
Update app.py
567c6c2
# %%
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from nltk.tokenize import sent_tokenize
import gc
import nltk
nltk.download('punkt')
# from PyKakao import KoGPT
# kogpt_api = KoGPT(service_key = "")
import openai
openai.api_key = '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'
gpt2_tokenizer = AutoTokenizer.from_pretrained('gpt2')
#en2ko = 'alphahg/m2m100_418M-finetuned-en-to-ko-4770260'#'alphahg/mbart-large-50-finetuned-en-to-ko-8603428-finetuned-en-to-ko-9914408'
en2ko = 'alphahg/mbart-large-50-finetuned-en-to-ko-8603428-finetuned-en-to-ko-9914408'
ko2en = 'alphahg/opus-mt-ko-en-finetuned-ko-to-en-2780616'
ensum = 'allenai/led-large-16384-arxiv'
kosum = 'alphahg/pko-t5-small-finetuned-paper-4564652' #'lcw99/t5-base-korean-text-summary'
#en_pipe = pipeline('translation', model=en2ko, tokenizer=en2ko, src_lang = "en", tgt_lang = "ko", device_map="auto")
en2ko_model = AutoModelForSeq2SeqLM.from_pretrained(en2ko)
en_pipe = pipeline('translation', model=en2ko_model, tokenizer=en2ko, src_lang = "en_XX", tgt_lang = "ko_KR")
ko_pipe = pipeline('translation', model=ko2en, tokenizer=ko2en)
style_pipe = pipeline('translation', model=en2ko_model, tokenizer=en2ko, src_lang = "ko_KR", tgt_lang = "ko_KR")
en_sum = pipeline('summarization', model=ensum, tokenizer=ensum)
ko_sum = pipeline('summarization', model=kosum, tokenizer=kosum)
def len_tokens(text, pipe):
return len(pipe.tokenizer(text)['input_ids'])
def split_sent(sentences, pipe, max_len=256):
if not sentences:
return []
paragraphs = []
example = sentences[0]
for i in range(1, len(sentences)):
if len_tokens(example + ' ' + sentences[i], pipe) > max_len:
paragraphs.append(example)
example = sentences[i]
else:
example += ' ' + sentences[i]
paragraphs.append(example)
return paragraphs
# chatbot = Chatbot({
# #"session_token": "eyJhbGciOiJkaX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aZjFyXyjmP82S3VnhEyON390aIL7j9Y0wGnHzOkn54OfyxxGeo2mFAIv9kthL_Fi8d9G_rvvQOBUM2a7kjF5-n8wby0YDujoRl0ETg379HyMVf7F2BHWQ8nAbICRxWZ7EzPLwzrVjPiQVPZklkrVYgEmGxZDrgEG8IeNi7FMgGruaQ1tENczRMXzaApK8k6-FXKhfFIV7dN95tP4k6tnnxRFoMAUWcXwQCzRH8YhID36TAUFdBQ-c52MTogPo1Rki1N49j_e7Mph1OABQX2Fw9-CukT6reQkp3nGrwi0IKnKoyGhHOBHK3kzQwINfjOBbNpOjP-6MX_9kiRTstN2GLte8w0QJQVl84o8ACTjV8N4rhI6xyLKIvqoyZ6jNO3SYs8fEutmZO8-qB0iksIiQHupxQgcmgbAyM.KxAbJWqMvwm0PYtq7MuR6A"
# }, conversation_id=None, parent_id=None) # You can start a custom conversation
# %%
def translate(text, lang, gpt_fix=False):
from_en = False if lang == 'ν•œμ˜' else True
sentences = sent_tokenize(text)
#print(sentences)
if not sentences:
return ''
paragraphs = split_sent(sentences, en_pipe, max_len=180) if from_en else split_sent(sentences, ko_pipe)
#print(paragraphs)
ret = []
for text in paragraphs:
result = en_pipe(text) if from_en else ko_pipe(text)
ret.append(result[0]['translation_text'])
translated = ' '.join(ret)
gc.collect()
if gpt_fix:
if lang == 'ν•œμ˜':
prompt = 'Improve given formal article without adding:'
elif lang == 'μ˜ν•œ':
prompt = "좔가적인 λ‚΄μš©μ—†μ΄ 주어진 글을 κ°œμ„ ν•΄:"
def fix_sent(sent):
number_of_tokens = len(gpt2_tokenizer(sent)['input_ids'])
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt+'\n'+sent,
temperature=0,
max_tokens=number_of_tokens+128,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
return response.choices[0].text.strip()
# def fix_sent(sent):
# generated = kogpt_api.generate(prompt+'\n'+sent, max_tokens=256)
# return generated['generations'][0]['text']
translated = fix_sent(translated)
return translated
#%%
def translate_with_sum(text, lang, gpt_fix=False):
from_en = False if lang == 'ν•œμ˜' else True
if lang == 'μ˜ν•œ':
summary = en_sum(text, max_length=int(len_tokens(text, en_sum)/2)+32)
text = summary[0]['summary_text']
sentences = sent_tokenize(text)
#print(sentences)
if not sentences:
return ''
paragraphs = split_sent(sentences, en_pipe if from_en else ko_pipe)
#print(paragraphs)
ret = []
for text in paragraphs:
result = en_pipe(text) if from_en else ko_pipe(text)
ret.append(result[0]['translation_text'])
summarized = ' '.join(ret)
if lang == 'ν•œμ˜':
summary = en_sum(summarized, max_length=int(len_tokens(summarized, en_sum)/2)+32)
return summary[0]['summary_text']
gc.collect()
return summarized
def summarize(text, lang):
if lang == 'Korean':
summarizer = ko_sum
elif lang == 'English':
summarizer = en_sum
summary = summarizer(text, max_length=int(len_tokens(text, summarizer) * 0.7))[0]['summary_text']
return summary
def translate_styleonly(text):
sentences = sent_tokenize(text)
paragraphs = split_sent(sentences, style_pipe, max_len=180)
#print(paragraphs)
ret = []
for text in paragraphs:
result = style_pipe(text)
ret.append(result[0]['translation_text'])
translated = ' '.join(ret)
gc.collect()
return translated
# %%
interface1 = gr.Interface(fn=translate, inputs=["text", gr.Radio(["μ˜ν•œ", "ν•œμ˜"], value='μ˜ν•œ'), 'checkbox'], outputs="text")
interface2 = gr.Interface(fn=translate_with_sum, inputs=["text", gr.Radio(["μ˜ν•œ", "ν•œμ˜"], value='μ˜ν•œ')], outputs="text")
parallel_interface = gr.Parallel(interface1, interface2)
summarize_interface = gr.Interface(fn=summarize, inputs=["text", gr.Radio(["Korean", "English"], value='Korean')], outputs="text")
style_interface = gr.Interface(fn=translate_styleonly, inputs=["text"], outputs="text")
demo = gr.TabbedInterface([parallel_interface, summarize_interface, style_interface], ['λ²ˆμ—­ 및 μš”μ•½', 'μš”μ•½', 'μŠ€νƒ€μΌ λ²ˆμ—­'], css="footer {visibility: hidden}") # 'μš”μ•½'
demo.launch() # Share the demo
# %%