Spaces:
Runtime error
Runtime error
Updated app with custom button
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
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/
|
8 |
|
9 |
# Build a demo
|
10 |
|
@@ -30,13 +30,17 @@ import gradio as gr
|
|
30 |
import re
|
31 |
import torch
|
32 |
from torch import autocast
|
33 |
-
#from diffusers import StableDiffusionPipeline
|
34 |
from PIL import Image
|
35 |
import os
|
36 |
|
37 |
-
|
|
|
38 |
|
39 |
-
#
|
|
|
|
|
|
|
|
|
40 |
def get_image_input(title, given_input_style):
|
41 |
if given_input_style == 'Random':
|
42 |
image_input_styles_new = image_input_styles.copy()
|
@@ -45,7 +49,7 @@ def get_image_input(title, given_input_style):
|
|
45 |
final_style = image_input_styles_new[random_choice]
|
46 |
else:
|
47 |
final_style = given_input_style
|
48 |
-
image_input =
|
49 |
return image_input, final_style
|
50 |
|
51 |
# Available models for generate lyrics pipeline
|
@@ -56,17 +60,17 @@ checkpoint = 'wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics'
|
|
56 |
# Setup all the pipelines we need
|
57 |
title_generator = pipeline('summarization', model='czearing/story-to-title')
|
58 |
lyrics_generator = pipeline("text-generation", model=checkpoint)
|
59 |
-
#
|
60 |
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
|
61 |
|
62 |
# Create 4 images for the given prompt and receive the first one
|
63 |
-
|
64 |
def get_image(prompt):
|
65 |
gallery_dir = stable_diffusion(prompt, fn_index=2)
|
66 |
images = [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]
|
67 |
return [images[0]]
|
68 |
|
69 |
-
#
|
70 |
def generate_beatles(input_prompt, temperature, top_p, given_input_style):
|
71 |
# Create generator for different models
|
72 |
generated_lyrics = lyrics_generator(input_prompt
|
@@ -92,18 +96,18 @@ def generate_beatles(input_prompt, temperature, top_p, given_input_style):
|
|
92 |
# Create an image based on the generated title
|
93 |
image_input, image_style = get_image_input(title, given_input_style)
|
94 |
|
95 |
-
# Generate the image
|
96 |
image = get_image(image_input)
|
97 |
return (title, generated_lyrics, image, image_style)
|
98 |
|
99 |
# Create textboxes for input and output
|
100 |
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)
|
101 |
-
gen_lyrics = gr.Textbox(label="Lyrics
|
102 |
gen_title = gr.Textbox(label="Proposed songtitle", lines=1)
|
103 |
gen_image = gr.Gallery(label="Proposed song cover").style(grid=1, height="auto")
|
104 |
gen_image_style = gr.Textbox(label="Image style", lines=1)
|
105 |
|
106 |
-
# Layout and text
|
107 |
title='Beatles lyrics generator'
|
108 |
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>"
|
109 |
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>
|
@@ -112,12 +116,20 @@ article="""<p style='text-align: left'>These text generation models that output
|
|
112 |
The default output contains 100 tokens and has a repetition penalty of 1.0.
|
113 |
</p>"""
|
114 |
css = """
|
115 |
-
button
|
|
|
|
|
116 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
"""
|
118 |
|
119 |
# Let users select their own temperature and top-p
|
120 |
-
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
|
121 |
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)
|
122 |
given_input_style = gr.Dropdown(choices=image_input_styles, value="Random", label="Choose the art style for the lyrics cover", show_label=True)
|
123 |
#checkpoint = gr.Radio(checkpoint_choices, value='wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics', interactive=True, label = 'Select fine-tuned model', show_label=True)
|
@@ -133,5 +145,5 @@ gr.Interface(fn=generate_beatles
|
|
133 |
, allow_flagging='never'
|
134 |
).launch()
|
135 |
|
136 |
-
#
|
137 |
-
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
+
"""20221027_Generate_Beatles_with_Gradio_faster_version (2).ipynb
|
3 |
|
4 |
Automatically generated by Colaboratory.
|
5 |
|
6 |
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1B2KVLt04uBMgr2ejW19FYaEgk9lnNS0E
|
8 |
|
9 |
# Build a demo
|
10 |
|
|
|
30 |
import re
|
31 |
import torch
|
32 |
from torch import autocast
|
|
|
33 |
from PIL import Image
|
34 |
import os
|
35 |
|
36 |
+
# Array with song cover art styles
|
37 |
+
image_input_styles = ["Random", "The Beatles", "Pencil sketch", "Oil painting", "Pop art", "Piet Mondriaan"]
|
38 |
|
39 |
+
# Get image type for image input
|
40 |
+
"""
|
41 |
+
The default setting for the art style dropdown is "Random". The below function determines which style is chosen
|
42 |
+
If set to "Random", copy the art style array and remove "Random" to prevent "Random" from being a chosen art style
|
43 |
+
"""
|
44 |
def get_image_input(title, given_input_style):
|
45 |
if given_input_style == 'Random':
|
46 |
image_input_styles_new = image_input_styles.copy()
|
|
|
49 |
final_style = image_input_styles_new[random_choice]
|
50 |
else:
|
51 |
final_style = given_input_style
|
52 |
+
image_input = 'Cover for ' + title + ' in style of ' + final_style
|
53 |
return image_input, final_style
|
54 |
|
55 |
# Available models for generate lyrics pipeline
|
|
|
60 |
# Setup all the pipelines we need
|
61 |
title_generator = pipeline('summarization', model='czearing/story-to-title')
|
62 |
lyrics_generator = pipeline("text-generation", model=checkpoint)
|
63 |
+
# For the image generator we use stable diffusion from an existing HuggingFace space
|
64 |
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
|
65 |
|
66 |
# Create 4 images for the given prompt and receive the first one
|
67 |
+
# This function uses an existing HuggingFace space where the number of created images cannot be modified
|
68 |
def get_image(prompt):
|
69 |
gallery_dir = stable_diffusion(prompt, fn_index=2)
|
70 |
images = [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]
|
71 |
return [images[0]]
|
72 |
|
73 |
+
# Lyrics generation
|
74 |
def generate_beatles(input_prompt, temperature, top_p, given_input_style):
|
75 |
# Create generator for different models
|
76 |
generated_lyrics = lyrics_generator(input_prompt
|
|
|
96 |
# Create an image based on the generated title
|
97 |
image_input, image_style = get_image_input(title, given_input_style)
|
98 |
|
99 |
+
# Generate the image
|
100 |
image = get_image(image_input)
|
101 |
return (title, generated_lyrics, image, image_style)
|
102 |
|
103 |
# Create textboxes for input and output
|
104 |
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)
|
105 |
+
gen_lyrics = gr.Textbox(label="Lyrics", lines=15)
|
106 |
gen_title = gr.Textbox(label="Proposed songtitle", lines=1)
|
107 |
gen_image = gr.Gallery(label="Proposed song cover").style(grid=1, height="auto")
|
108 |
gen_image_style = gr.Textbox(label="Image style", lines=1)
|
109 |
|
110 |
+
# Layout and text around the app
|
111 |
title='Beatles lyrics generator'
|
112 |
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>"
|
113 |
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>
|
|
|
116 |
The default output contains 100 tokens and has a repetition penalty of 1.0.
|
117 |
</p>"""
|
118 |
css = """
|
119 |
+
.gr-button-primary {
|
120 |
+
text-indent: -9999px;
|
121 |
+
line-height: 0;
|
122 |
}
|
123 |
+
.gr-button-primary:after {
|
124 |
+
content: "Beatlify!";
|
125 |
+
text-indent: 0;
|
126 |
+
display: block;
|
127 |
+
line-height: initial;
|
128 |
+
}
|
129 |
"""
|
130 |
|
131 |
# Let users select their own temperature and top-p
|
132 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Change the temperature \r\n (higher temperature = more creative in lyrics generation, but posibbly less Beatly)", value=0.5, show_label=True) #high = sensitive for low probability tokens
|
133 |
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)
|
134 |
given_input_style = gr.Dropdown(choices=image_input_styles, value="Random", label="Choose the art style for the lyrics cover", show_label=True)
|
135 |
#checkpoint = gr.Radio(checkpoint_choices, value='wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics', interactive=True, label = 'Select fine-tuned model', show_label=True)
|
|
|
145 |
, allow_flagging='never'
|
146 |
).launch()
|
147 |
|
148 |
+
# Uncomment the line below for testing
|
149 |
+
#generate_beatles(input_prompt="When I look out my window", temperature=0.7, top_p=0.5)
|