wvangils commited on
Commit
bc97edd
1 Parent(s): 7711322

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import libraries
2
+ from transformers import pipeline
3
+ from numpy import random
4
+ import gradio as gr
5
+ import re
6
+ import torch
7
+ from torch import autocast
8
+ import os
9
+
10
+ # Array with song cover art styles
11
+ image_input_styles = ["Random", "Pencil sketch", "Oil painting", "Pop art", "Piet Mondriaan"]
12
+
13
+ # Get image type for image input
14
+ """
15
+ The default setting for the art style dropdown is "Random". The below function determines which style is chosen
16
+ If set to "Random", copy the art style array and remove "Random" to prevent "Random" from being a chosen art style
17
+ """
18
+ def get_image_input(title, given_input_style):
19
+ if given_input_style == 'Random':
20
+ image_input_styles_new = image_input_styles.copy()
21
+ image_input_styles_new.pop(0)
22
+ random_choice = random.randint(len(image_input_styles_new)-1)
23
+ final_style = image_input_styles_new[random_choice]
24
+ else:
25
+ final_style = given_input_style
26
+ image_input = 'Cover for ' + title.lower() + ' in style of ' + final_style
27
+ return image_input, final_style
28
+
29
+ # Available models for generate lyrics pipeline
30
+ # checkpoint = 'wvangils/GPT-Medium-Beatles-Lyrics-finetuned-newlyrics'
31
+ # checkpoint = 'wvangils/GPT-Neo-125m-Beatles-Lyrics-finetuned-newlyrics'
32
+ checkpoint = 'wvangils/BLOOM-560m-Beatles-Lyrics-finetuned'
33
+
34
+ # Setup all the pipelines we need
35
+ title_generator = pipeline('summarization', model='czearing/story-to-title')
36
+ lyrics_generator = pipeline("text-generation", model=checkpoint)
37
+
38
+ # For the image generator we use stable diffusion from an existing HuggingFace space, Gradio accelerated backend
39
+ stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion-1")
40
+
41
+ # Create 4 images for the given prompt and receive the first one
42
+ # This function uses an existing HuggingFace space where the number of created images cannot be modified
43
+ def get_image(prompt):
44
+ gallery_dir = stable_diffusion(prompt, fn_index=2)
45
+ images = [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]
46
+ return [images[0]]
47
+
48
+ # Lyrics generation
49
+ def generate_beatles(input_prompt, temperature, top_p, given_input_style):
50
+ # Create generator for different models
51
+ generated_lyrics = lyrics_generator(input_prompt
52
+ , max_length = 100
53
+ , num_return_sequences = 1
54
+ , return_full_text = True
55
+ , temperature = temperature
56
+ , top_p = top_p # Default 1.0
57
+ , no_repeat_ngram_size = 3 # Default = 0
58
+ , repetition_penalty = 1.0 # Default = 1.0
59
+ )[0]["generated_text"]
60
+
61
+ # Put lyrics in the right form
62
+ lyrics_sentences = re.sub('\n', '. ', generated_lyrics)
63
+
64
+ # Create a title based on the generated lyrics
65
+ title = title_generator(lyrics_sentences, min_length=1, max_length=10, repetition_penalty=2.5)[0]['summary_text']
66
+
67
+ # Create an image based on the generated title
68
+ image_input, image_style = get_image_input(title, given_input_style)
69
+
70
+ # Generate the image
71
+ image = get_image(image_input)
72
+ return (title, generated_lyrics, image, image_style)
73
+
74
+ # Create textboxes for input and output
75
+ input_box = gr.Textbox(label="Write the start of a song here", placeholder="Write the start of a new song here", value="Looking out of my window", lines=2, max_lines=5)
76
+ gen_lyrics = gr.Textbox(label="Song lyrics", lines=15)
77
+ gen_title = gr.Textbox(label="Proposed songtitle", lines=1)
78
+ gen_image = gr.Gallery(label="Proposed song cover").style(grid=1, height="auto")
79
+ gen_image_style = gr.Textbox(label="Image style", lines=1)
80
+
81
+ # Layout and text around the app
82
+ title='Beatles lyrics generator'
83
+ 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>"
84
+ 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>
85
+ 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>.
86
+ 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.
87
+ The default output contains 100 tokens and has a repetition penalty of 1.0.
88
+ </p>"""
89
+ css = """
90
+ .gr-button-primary {
91
+ text-indent: -9999px;
92
+ line-height: 0;
93
+ }
94
+ .gr-button-primary:after {
95
+ content: "Beatlify!";
96
+ text-indent: 0;
97
+ display: block;
98
+ line-height: initial;
99
+ }
100
+ """
101
+
102
+ # Let users select their own temperature and top-p
103
+ 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.7, show_label=True) #high = sensitive for low probability tokens
104
+ 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)
105
+ given_input_style = gr.Dropdown(choices=image_input_styles, value="Random", label="Choose the art style for the lyrics cover", show_label=True)
106
+
107
+ # Use generate Beatles function in demo-app Gradio
108
+ gr.Interface(fn=generate_beatles
109
+ , inputs=[input_box, temperature, top_p, given_input_style]
110
+ , outputs=[gen_title, gen_lyrics, gen_image, gen_image_style]
111
+ , title=title
112
+ , css=css
113
+ , description=description
114
+ , article=article
115
+ , allow_flagging='never'
116
+ ).launch()