simonesp commited on
Commit
72f0845
1 Parent(s): 60b8562

New try last version

Browse files

Not a GPU necessary anymore
Different cover styles possible

Files changed (1) hide show
  1. app.py +92 -46
app.py CHANGED
@@ -1,33 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from transformers import pipeline
 
2
  import gradio as gr
3
  import re
4
- import os
5
  import torch
6
  from torch import autocast
7
- from diffusers import StableDiffusionPipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # Available models for pipeline
10
- # checkpoint = 'wvangils/CTRL-Beatles-Lyrics-finetuned-newlyrics'
11
- # checkpoint = 'wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics'
12
- checkpoint = 'wvangils/GPT-Neo-125m-Beatles-Lyrics-finetuned-newlyrics'
13
- # checkpoint = 'wvangils/GPT2-Beatles-Lyrics-finetuned-newlyrics'
14
- # checkpoint = 'wvangils/DistilGPT2-Beatles-Lyrics-finetuned-newlyrics'
15
- # checkpoint = 'wvangils/BLOOM-350m-Beatles-Lyrics-finetuned-newlyrics'
16
 
17
- # setup all the pipelines we need
18
  title_generator = pipeline('summarization', model='czearing/story-to-title')
19
  lyrics_generator = pipeline("text-generation", model=checkpoint)
20
- image_generator = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"
21
- , revision="fp16"
22
- , torch_dtype=torch.float16
23
- , use_auth_token=os.environ['hf_token'])
24
 
25
- # What device to use?
26
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
27
- image_generator.to(device)
 
 
 
28
 
29
- # Create function for generation
30
- def generate_beatles(input_prompt, temperature, top_p):
 
31
  generated_lyrics = lyrics_generator(input_prompt
32
  , max_length = 100
33
  , num_return_sequences = 1
@@ -42,55 +88,55 @@ def generate_beatles(input_prompt, temperature, top_p):
42
  #, do_sample = True # Default = False
43
  )[0]["generated_text"]
44
 
 
45
  lyrics_sentences = re.sub('\n', '. ', generated_lyrics)
46
- #title_generator = pipeline('summarization', model='czearing/story-to-title')
 
47
  title = title_generator(lyrics_sentences, min_length=1, max_length=10, repetition_penalty=2.5)[0]['summary_text']
48
 
49
- # Set a seed for reproducebility
50
- # seed = torch.Generator("cuda").manual_seed(42)
51
- image_input = "Oil painting for " + title
52
 
53
  # Generate the image, [PIL format](https://pillow.readthedocs.io/en/stable/)
54
- with autocast("cuda"):
55
- image = image_generator(image_input, num_inference_steps=50, guidance_scale=8.5)[0][0]
56
-
57
- return (title, generated_lyrics, image)
58
-
59
-
60
- # Create textboxes for input and output
61
- 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)
62
- gen_lyrics = gr.Textbox(label="Lyrics by The Beatles and chosen language model:", lines=15)
63
- gen_title = gr.Textbox(label="Proposed songtitle", lines=1)
64
- gen_image = gr.Image(label="Proposed song cover", type="pil")
65
 
66
  # Create textboxes for input and output
67
- 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)
68
- gen_lyrics = gr.Textbox(label="Lyrics by The Beatles and chosen language model:", lines=15)
69
- gen_title = gr.Textbox(label="Proposed songtitle", lines=1)
70
- gen_image = gr.Image(label="Proposed song cover", type="pil")
 
71
 
72
  # Layout and text above the App
73
  title='Beatles lyrics generator'
74
- description="<p style='text-align: center'>We've fine-tuned multiple language models on lyrics from The Beatles to generate Beatles-like text. Below are the results we obtained fine-tuning a GPT neo model. After generation a title is generated using <a href='https://huggingface.co/czearing/story-to-title' target='_blank'>this model</a>. On top we use the generated title to suggest an album cover using <a href='https://huggingface.co/CompVis/stable-diffusion-v1-4' target='_blank'>Stable Diffusion 1.4</a>. Give it a try!</p>"
75
  article="""<p style='text-align: left'>These text generation models that output Beatles-like text were created by data scientists working for <a href='https://cmotions.nl/' target="_blank">Cmotions.</a>
76
  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='https://huggingface.co/bigscience/bloom-560m' target='_blank'>BLOOM 560m</a>.
77
  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. Currently we are fine-tuning models and are evaluating the results. Once finished we will publish a blog at this <a href='https://www.theanalyticslab.nl/blogs/' target='_blank'>location </a> with all the steps we took including a Python notebook using Huggingface.
78
  The default output contains 100 tokens and has a repetition penalty of 1.0.
79
  </p>"""
80
-
 
 
 
81
 
82
  # Let users select their own temperature and top-p
83
- 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)
84
- 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)
 
85
  #checkpoint = gr.Radio(checkpoint_choices, value='wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics', interactive=True, label = 'Select fine-tuned model', show_label=True)
86
 
87
  # Use generate Beatles function in demo-app Gradio
88
  gr.Interface(fn=generate_beatles
89
- , inputs=[input_box, temperature, top_p]
90
- , outputs=[gen_title, gen_lyrics, gen_image]
91
  , title=title
 
92
  , description=description
93
  , article=article
94
  , allow_flagging='never'
95
- ).launch()
96
-
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """20221027_Generate_Beatles_with_Gradio_faster_version.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1spCrOvhSvByMN8J-4iGZ9wk3I8U-9elU
8
+
9
+ # Build a demo
10
+
11
+ Put together a Gradio App bases on the Huggingface tutorials so that users can generate Beatles-like poetry based on an input prompt.
12
+ """
13
+
14
+ # Commented out IPython magic to ensure Python compatibility.
15
+ # %%capture
16
+ # !pip install transformers
17
+
18
+ # Commented out IPython magic to ensure Python compatibility.
19
+ # %%capture
20
+ # !pip install gradio
21
+
22
+ # Commented out IPython magic to ensure Python compatibility.
23
+ # %%capture
24
+ # !pip install --upgrade diffusers transformers scipy ftfy "ipywidgets>=7,<8"
25
+
26
+ # Commented out IPython magic to ensure Python compatibility.
27
+ # %%capture
28
+ # !pip install flag
29
+
30
+ # Import libraries
31
  from transformers import pipeline
32
+ from numpy import random
33
  import gradio as gr
34
  import re
 
35
  import torch
36
  from torch import autocast
37
+ #from diffusers import StableDiffusionPipeline
38
+ from PIL import Image
39
+ import flag
40
+ import os
41
+
42
+ image_input_styles = ["Random", "Beatles style", "Pencil sketch", "Oil painting", "Pop art", "Piet Mondriaan"]
43
+
44
+ # Function to get random image type for image input
45
+ def get_image_input(title, given_input_style):
46
+ if given_input_style == 'Random':
47
+ image_input_styles_new = image_input_styles.copy()
48
+ image_input_styles_new.pop(0)
49
+ random_choice = random.randint(len(image_input_styles_new)-1)
50
+ final_style = image_input_styles_new[random_choice]
51
+ else:
52
+ final_style = given_input_style
53
+ image_input = final_style + ' for ' + title
54
+ return image_input, final_style
55
 
56
+ # Available models for generate lyrics pipeline
57
+ checkpoint = 'wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics'
58
+ # checkpoint = 'wvangils/GPT-Neo-125m-Beatles-Lyrics-finetuned-newlyrics'
59
+ # checkpoint = 'wvangils/BLOOM-560m-Beatles-Lyrics-finetuned'
 
 
 
60
 
61
+ # Setup all the pipelines we need
62
  title_generator = pipeline('summarization', model='czearing/story-to-title')
63
  lyrics_generator = pipeline("text-generation", model=checkpoint)
64
+ # Image generator: stable diffusion from an existing HuggingFace space
65
+ stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
 
 
66
 
67
+ # Create 4 images for the given prompt and receive the first one
68
+ # This function uses an existing HuggingFace space where the number of created images cannot be modified
69
+ def get_image(prompt):
70
+ gallery_dir = stable_diffusion(prompt, fn_index=2)
71
+ images = [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]
72
+ return [images[0]]
73
 
74
+ # Create function for lyrics generation
75
+ def generate_beatles(input_prompt, temperature, top_p, given_input_style):
76
+ # Create generator for different models
77
  generated_lyrics = lyrics_generator(input_prompt
78
  , max_length = 100
79
  , num_return_sequences = 1
 
88
  #, do_sample = True # Default = False
89
  )[0]["generated_text"]
90
 
91
+ # Put lyrics in the right form
92
  lyrics_sentences = re.sub('\n', '. ', generated_lyrics)
93
+
94
+ # Create a title based on the generated lyrics
95
  title = title_generator(lyrics_sentences, min_length=1, max_length=10, repetition_penalty=2.5)[0]['summary_text']
96
 
97
+ # Create an image based on the generated title
98
+ image_input, image_style = get_image_input(title, given_input_style)
 
99
 
100
  # Generate the image, [PIL format](https://pillow.readthedocs.io/en/stable/)
101
+ image = get_image(image_input)
102
+ return (title, generated_lyrics, image, image_style)
 
 
 
 
 
 
 
 
 
103
 
104
  # Create textboxes for input and output
105
+ input_box = gr.Textbox(label="Write the start of a song here", placeholder="Write the start of a song here", value="Looking beyond the Broad Horizon", lines=2, max_lines=5)
106
+ gen_lyrics = gr.Textbox(label="Lyrics by The Beatles and chosen language model", lines=15)
107
+ gen_title = gr.Textbox(label="Proposed songtitle", lines=1)
108
+ gen_image = gr.Gallery(label="Proposed song cover").style(grid=1, height="auto")
109
+ gen_image_style = gr.Textbox(label="Image style", lines=1)
110
 
111
  # Layout and text above the App
112
  title='Beatles lyrics generator'
113
+ description="<p style='text-align: center'>We've fine-tuned multiple language models on lyrics from The Beatles to generate Beatles-like text. Below are the results we obtained fine-tuning a GPT Neo model. After generation a title is generated using <a href='https://huggingface.co/czearing/story-to-title' target='_blank'>this model</a>. On top we use the generated title to suggest an album cover using <a href='https://huggingface.co/CompVis/stable-diffusion-v1-4' target='_blank'>Stable Diffusion 1.4</a>. Give it a try!</p>"
114
  article="""<p style='text-align: left'>These text generation models that output Beatles-like text were created by data scientists working for <a href='https://cmotions.nl/' target="_blank">Cmotions.</a>
115
  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='https://huggingface.co/bigscience/bloom-560m' target='_blank'>BLOOM 560m</a>.
116
  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. Currently we are fine-tuning models and are evaluating the results. Once finished we will publish a blog at this <a href='https://www.theanalyticslab.nl/blogs/' target='_blank'>location </a> with all the steps we took including a Python notebook using Huggingface.
117
  The default output contains 100 tokens and has a repetition penalty of 1.0.
118
  </p>"""
119
+ css = """
120
+ button:lang(en-US) {
121
+ }
122
+ """
123
 
124
  # Let users select their own temperature and top-p
125
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Change the temperature \n (higher temperature = more creative in lyrics generation, but posibbly less Beatly)", value=0.5, show_label=True) #high = sensitive for low probability tokens
126
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Change top probability of the next word \n (higher top probability = more words to choose from for the next word, but possibly less Beatly)", value=0.5, show_label=True)
127
+ given_input_style = gr.Dropdown(choices=image_input_styles, value="Random", label="Choose the art style for the lyrics cover", show_label=True)
128
  #checkpoint = gr.Radio(checkpoint_choices, value='wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics', interactive=True, label = 'Select fine-tuned model', show_label=True)
129
 
130
  # Use generate Beatles function in demo-app Gradio
131
  gr.Interface(fn=generate_beatles
132
+ , inputs=[input_box, temperature, top_p, given_input_style]
133
+ , outputs=[gen_title, gen_lyrics, gen_image, gen_image_style]
134
  , title=title
135
+ , css=css
136
  , description=description
137
  , article=article
138
  , allow_flagging='never'
139
+ ).launch(debug=True, share=True)
140
+
141
+ #generate_beatles(input_prompt="When I look out my window", temperature=0.7, top_p=0.5)
142
+