import torch from transformers import (T5ForConditionalGeneration,T5Tokenizer) import gradio as gr best_model_path = "swcrazyfan/KingJamesify-T5-large" model = T5ForConditionalGeneration.from_pretrained(best_model_path) tokenizer = T5Tokenizer.from_pretrained("swcrazyfan/KingJamesify-T5-large") def tokenize_data(text): # Tokenize the review body input_ = "kingify: " + str(text) + ' ' 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, temperature, num_beams, max_length): 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=1, early_stopping=True, num_return_sequences=1, temperature=temperature) answer = tokenizer.decode(results[0], skip_special_tokens=True) return answer iface = gr.Interface(title="Kingify", description="Write anything below. Then, click submit to 'Kingify' it.", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10), gr.inputs.Slider(label="Temperature", default=1.0, min_value=0.0, max_value=2.0, step=0.1), gr.inputs.Slider(label="Number of Beams", default=5, min_value=1, max_value=10, step=1), gr.inputs.Textbox(label="Max Length", default=512, lines=1)], outputs=["text"])