Spaces:
Runtime error
Runtime error
from transformers import pipeline | |
import gradio as gr | |
import re | |
import torch | |
from torch import autocast | |
from diffusers import StableDiffusionPipeline | |
# Available models for pipeline | |
# checkpoint = 'wvangils/CTRL-Beatles-Lyrics-finetuned-newlyrics' | |
# checkpoint = 'wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics' | |
# checkpoint = 'wvangils/GPT-Neo-125m-Beatles-Lyrics-finetuned-newlyrics' | |
# checkpoint = 'wvangils/GPT2-Beatles-Lyrics-finetuned-newlyrics' | |
# checkpoint = 'wvangils/DistilGPT2-Beatles-Lyrics-finetuned-newlyrics' | |
# checkpoint = 'wvangils/BLOOM-350m-Beatles-Lyrics-finetuned-newlyrics' | |
checkpoint_choices = ['wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics', 'wvangils/GPT-Neo-125m-Beatles-Lyrics-finetuned-newlyrics', 'wvangils/BLOOM-560m-Beatles-Lyrics-finetuned'] | |
# Create function for generation | |
def generate_beatles(checkpoint, input_prompt, temperature, top_p): | |
# Create generator for different models | |
generator = pipeline("text-generation", model=checkpoint) | |
generated_lyrics = generator(input_prompt | |
, max_length = 50 | |
, num_return_sequences = 1 | |
, return_full_text = True | |
, verbose = 0 | |
#, num_beams = 1 | |
#, early_stopping = True # Werkt niet goed lijkt | |
, temperature = temperature | |
#, top_k = 50 # Default 50 | |
, top_p = top_p # Default 1.0 | |
, no_repeat_ngram_size = 3 # Default = 0 | |
, repetition_penalty = 1.0 # Default = 1.0 | |
#, do_sample = True # Default = False | |
)[0]["generated_text"] | |
lyrics_sentences = re.sub('\n', '. ', generated_lyrics) | |
title_generator = pipeline('summarization', model='czearing/story-to-title') | |
title = title_generator(lyrics_sentences, min_length=1, max_length=10, repetition_penalty=2.5)[0]['summary_text'] | |
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" | |
, revision="fp16" | |
, torch_dtype=torch.float16 | |
#, use_auth_token=True | |
, use_auth_token="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") | |
# And move the pipeline to GPU | |
pipe = pipe.to("cuda") | |
# Set a seed for reproducebility | |
seed = torch.Generator("cuda").manual_seed(42) | |
image_input = "Oil painting for " + title | |
# Generate the image, [PIL format](https://pillow.readthedocs.io/en/stable/) | |
with autocast("cuda"): | |
image = pipe(image_input, num_inference_steps=50, generator=seed, guidance_scale=8.5)["sample"][0] | |
return (title, generated_lyrics, image) | |
# Create textboxes for input and output | |
input_box = gr.Textbox(label="Input prompt:", placeholder="Write the start of a song here", value="In my dreams I am", lines=2, max_lines=5) | |
gen_lyrics = gr.Textbox(label="Lyrics by The Beatles and chosen language model:", lines=15) | |
gen_title = gr.Textbox(label="Proposed songtitle", lines=1) | |
gen_image = gr.Image(label="Proposed song cover", type="pil") | |
# Layout and text above the App | |
title='Beatles lyrics generator' | |
description="<p style='text-align: center'>Multiple language models were fine-tuned on lyrics from The Beatles to generate Beatles-like text. Give it a try!</p>" | |
article="""<p style='text-align: left'>A couple of data scientists working for <a href='https://cmotions.nl/' target="_blank">Cmotions</a> came together to construct a text generation model that will output Beatles-like text. | |
We tried several text generation models that we were able to load in Colab: a general <a href='https://huggingface.co/gpt2-medium' target='_blank'>GPT2-medium</a> model, the Eleuther AI small-sized GPT model <a href='https://huggingface.co/EleutherAI/gpt-neo-125M' target='_blank'>GPT-Neo</a> and the new kid on the block build by the <a href='https://bigscience.notion.site/BLOOM-BigScience-176B-Model-ad073ca07cdf479398d5f95d88e218c4' target='_blank'>Bigscience</a> initiative <a href='bigscience/bloom-350m' target='_blank'>BLOOM 350m</a>. | |
Further we've put together a <a href='https://huggingface.co/datasets/cmotions/Beatles_lyrics' target='_blank'> Huggingface dataset</a> containing all known lyrics created by The Beatles. <a href='https://www.theanalyticslab.nl/blogs/' target='_blank'>Read this blog </a> to see how this model was build in a Python notebook using Huggingface. | |
The default output contains 100 tokens and has a repetition penalty of 1.0. | |
</p>""" | |
# Let users select their own temperature and top-p | |
temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Temperature (high = sensitive for low probability tokens)", value=0.7, show_label=True) | |
top_p = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Top-p (sample next possible words from given probability p)", value=0.5, show_label=True) | |
checkpoint = gr.Radio(checkpoint_choices, value='wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics', interactive=True, label = 'Select fine-tuned model', show_label=True) | |
# Use generate Beatles function in demo-app Gradio | |
gr.Interface(fn=generate_beatles | |
, inputs=[checkpoint, input_box, temperature, top_p] | |
, outputs=[gen_title, gen_lyrics, gen_image] | |
#, examples=examples # output is not very fancy as you have to specify all inputs for every example | |
, title=title | |
, description=description | |
, article=article | |
, allow_flagging='never' | |
).launch() |