gradio/demo/text_generation/run.ipynb
Ali Abdalla de36820ef5
Fix various issues with demos on website (#6268)
* fix demos

* demos on landing page

* make code interactive on playground

* add changeset

* try new secret

* formatting

* fix fake_gan

* demo notebooks

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2023-11-02 17:35:07 -07:00

1.4 KiB

Gradio Demo: text_generation

This text generation demo takes in input text and returns generated text. It uses the Transformers library to set up the model and has two examples.

    
In [ ]:
!pip install -q gradio git+https://github.com/huggingface/transformers gradio torch
In [ ]:
import gradio as gr
from transformers import pipeline

generator = pipeline('text-generation', model='gpt2')

def generate(text):
    result = generator(text, max_length=30, num_return_sequences=1)
    return result[0]["generated_text"]

examples = [
    ["The Moon's orbit around Earth has"],
    ["The smooth Borealis basin in the Northern Hemisphere covers 40%"],
]

demo = gr.Interface(
    fn=generate,
    inputs=gr.Textbox(lines=5, label="Input Text"),
    outputs=gr.Textbox(label="Generated Text"),
    examples=examples
)

demo.launch()