DeKingify / app.py
swcrazyfan's picture
Update app.py
9f1b92c
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_ = "dekingify: " + 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, min_length, num_beams):
def generate_answers(text, max_length, num_beams):
inputs = tokenize_data(text)
results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,
num_beams=num_beams,
max_length=max_length,
# min_length=min_length,
early_stopping=True,
num_return_sequences=1)
answer = tokenizer.decode(results[0], skip_special_tokens=True)
return answer
#iface = gr.Interface(title="DeKingify", 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="Minimum Length", minimum=1, maximum=512, default=1, step=1), gr.inputs.Slider(label="Number of Beams", minimum=1, maximum=50, default=5, step=1)], outputs=["text"])
iface = gr.Interface(title="DeKingify", 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=50, default=5, step=1)], outputs=["text"])
iface.launch(inline=False)