Spaces:
Sleeping
Sleeping
import torch | |
from transformers import (T5ForConditionalGeneration,T5Tokenizer) | |
import gradio as gr | |
best_model_path = "swcrazyfan/Dekingify-T5-Large" | |
model = T5ForConditionalGeneration.from_pretrained(best_model_path) | |
tokenizer = T5Tokenizer.from_pretrained("swcrazyfan/Dekingify-T5-Large") | |
def tokenize_data(text): | |
# Tokenize the review body | |
# input_ = "paraphrase: "+ str(text) + ' >' | |
input_ = "deking: " + 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): | |
inputs = tokenize_data(text) | |
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(fn=generate_answers, inputs=["Write your text here..."], outputs=["Jamesified text"]) | |
#iface.launch(inline=False, share=True) | |
iface = gr.Interface(title="Dekingify", description="Write anything from around the 17th-century below. Then, click submit to 'Dekingify' it (make the text more modern).", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10)], outputs=["text"]) | |
#iface = gr.Interface(title="King Jamesify” fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original",lines=30)],outputs=[gr.outputs.Textbox(label="King Jamesified", lines=30)]) | |
iface.launch(inline=False) |