import gradio as gr from transformers import pipeline title = "tory Generator" # gpt-neo-2.7B gpt-j-6B def generate(text,the_model,max_length,temperature,repetition_penalty): generator = pipeline('text-generation', model=the_model) result = generator(text, num_return_sequences=3, max_length=max_length, temperature=temperature, repetition_penalty = repetition_penalty, no_repeat_ngram_size=2,early_stopping=False) return result[0]["generated_text"],result[1]["generated_text"],result[2]["generated_text"] def complete_with_gpt(text,context,the_model,max_length,temperature,repetition_penalty): # Use the last 50 characters of the text as context # text[:-context] + return generate(text[-context:],the_model,max_length,temperature,repetition_penalty) def send(text1,context,text2): return text1 + text2[context:] with gr.Blocks() as demo: textbox = gr.Textbox(placeholder="Type here and press enter...", lines=4) context = gr.Slider(value=50,label="Truncate input text length",minimum=1,maximum=300) the_model = gr.Dropdown(choices=['gpt2','gpt2-medium','gpt2-large','gpt2-xl'],value = 'gpt2',label="Choose model") max_length = gr.Slider(value=50,label="Max Generate Length",minimum=1,maximum=300) temperature = gr.Slider(value=1.0,label="Temperature",minimum=0.0,maximum=1.0,step=0.05) repetition_penalty = gr.Slider(value=1.1,label="Repetition penalty",minimum=0.2,maximum=2,step=0.1) output1 = gr.Textbox(lines=4) output2 = gr.Textbox(lines=4) output3 = gr.Textbox(lines=4) btn = gr.Button("Generate") btn.click(complete_with_gpt,inputs=[textbox,context,the_model,max_length,temperature,repetition_penalty], outputs=[output1,output2,output3]) send1 = gr.Button("Send1 to Origin Textbox").click(send,inputs=[textbox,context,output1],outputs=textbox) send2 = gr.Button("Send2 to Origin Textbox").click(send,inputs=[textbox,context,output2],outputs=textbox) send3 = gr.Button("Send3 to Origin Textbox").click(send,inputs=[textbox,context,output3],outputs=textbox) demo.launch()