Spaces:
Running
Running
# URL: https://huggingface.co/spaces/gradio/text_generation | |
# imports | |
import gradio as gr | |
from transformers import GPT2Tokenizer, GPT2Model | |
import torch | |
# loading the model | |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2') | |
model = GPT2Model.from_pretrained('gpt2') | |
# defining the core function | |
def generate(text): | |
generation_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
result = generation_pipeline(text) | |
return result[0]["generated_text"] | |
# defining title and examples | |
title = "Text Generation with GPT-2" | |
examples = [ | |
["The Moon's orbit around Earth has"], | |
["The smooth Borealis basin in the Northern Hemisphere covers 40%"], | |
] | |
# defining the interface | |
demo = gr.Interface( | |
fn=generate, | |
inputs=gr.inputs.Textbox(lines=5, label="Input Text"), | |
outputs=gr.outputs.Textbox(label="Generated Text"), | |
title=title, | |
examples=examples | |
) | |
# launching | |
demo.launch() | |