Kingify-2Way / app.py
swcrazyfan's picture
Update app.py
0a31182
import torch
from transformers import (T5ForConditionalGeneration,T5Tokenizer)
import gradio as gr
best_model_path = "swcrazyfan/Kingify-2Way-T5-Large-v1_1"
model = T5ForConditionalGeneration.from_pretrained(best_model_path)
tokenizer = T5Tokenizer.from_pretrained("swcrazyfan/Kingify-2Way-T5-Large-v1_1")
def tokenize_data(text, dekingify):
# Tokenize the review body
if dekingify == "Dekingify":
input_ = "dekingify: " + str(text) + ' </s>'
else:
input_ = "kingify: " + str(text) + ' </s>'
max_len = 512
# tokenize inputs
tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt')
inputs={"input_ids": tokenized_inputs['input_ids'],
"attention_mask": tokenized_inputs['attention_mask']}
return inputs
#def generate_answers(text, max_length, num_beams, dekingify):
def generate_answers(text, dekingify):
inputs = tokenize_data(text, dekingify)
results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,
num_beams=5,
max_length=512,
min_length=1,
early_stopping=True,
num_return_sequences=1)
answer = tokenizer.decode(results[0], skip_special_tokens=True)
return answer
#iface = gr.Interface(title="Kingify 2Way", description="Write anything below. Then, click submit to 'DeKingify' it.", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10), gr.inputs.Slider(label="Maximum Length", minimum=1, maximum=512, default=512, step=1), gr.inputs.Slider(label="Number of Beams", minimum=1, maximum=10, default=5, step=1), gr.inputs.Radio(label="What do you want to do?", choices=["Kingify", "Dekingify"])], outputs=["text"])
iface = gr.Interface(title="Kingify 2Way", description="This is a custom AI model that translates modern English into 17th-century English or 'King James' English (and visa). Write anything below. Then, selevt 'Kingify' or 'DeKingify', and click submit.", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10), gr.inputs.Radio(label="What do you want to do?", choices=["Kingify", "Dekingify"])], outputs=["text"])
iface.launch(inline=False)