Spaces:
Runtime error
Runtime error
File size: 922 Bytes
e3c9dd9 f12af95 71eba68 e3c9dd9 71eba68 8cce078 89b5cec 8cce078 f12af95 8cce078 e3c9dd9 8cce078 2ac3c8d e3c9dd9 505265f e3c9dd9 6593789 b15e5fa e3c9dd9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import gradio as gr
from transformers import GPT2LMHeadModel, AutoTokenizer, pipeline
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# load pretrained + finetuned GPT2
model = GPT2LMHeadModel.from_pretrained("./model/gpt2-355M")
model = model.to(device)
# create tokenizer
tokenizer = AutoTokenizer.from_pretrained(
"gpt2",
pad_token='<|endoftext|>'
)
trump = pipeline("text-generation", model=model, tokenizer=tokenizer, config={"max_length":140})
def generate(text):
result = trump(text, num_return_sequences=1)
return result[0]["generated_text"].replace('"', '') # remove quotation marks
examples = [
["Why does the lying news media"],
["The democrats have"],
["Today I'll be"],
]
demo = gr.Interface(
fn=generate,
inputs=gr.Textbox(lines=5, label="Prompt"),
outputs=gr.Textbox(label="Generated Trump Tweet"),
examples=examples
)
demo.launch() |