Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline, T5ForConditionalGeneration, T5Tokenizer
|
4 |
+
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
|
5 |
+
|
6 |
+
# generate lyrics
|
7 |
+
lyrics_generator = pipeline("text-generation", "ECE1786-AG/lyrics-generator")
|
8 |
+
|
9 |
+
# summarize lyrics
|
10 |
+
model = T5ForConditionalGeneration.from_pretrained("Michau/t5-base-en-generate-headline")
|
11 |
+
tokenizer = T5Tokenizer.from_pretrained("Michau/t5-base-en-generate-headline")
|
12 |
+
|
13 |
+
# generate single cover
|
14 |
+
scheduler = EulerDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-2", subfolder="scheduler")
|
15 |
+
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2", scheduler=scheduler, revision="fp16", torch_dtype=torch.float16)
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
pipe = pipe.to(device)
|
18 |
+
|
19 |
+
def generate_lyrics(genre, prompt):
|
20 |
+
complete_prompt = "<BOS> <{0}>\n{1}".format(genre, prompt)
|
21 |
+
lyrics = lyrics_generator(complete_prompt, max_length=1024)
|
22 |
+
lyrics = lyrics[0]['generated_text']
|
23 |
+
lyrics = lyrics.split('\n', 1)[1] # remove first line from the generated lyrics
|
24 |
+
|
25 |
+
return lyrics
|
26 |
+
|
27 |
+
def summarize_lyrics(lyrics):
|
28 |
+
text = "headline: " + lyrics
|
29 |
+
encoding = tokenizer.encode_plus(text, return_tensors = "pt")
|
30 |
+
input_ids = encoding["input_ids"]
|
31 |
+
attention_masks = encoding["attention_mask"]
|
32 |
+
beam_outputs = model.generate(
|
33 |
+
input_ids = input_ids,
|
34 |
+
attention_mask = attention_masks,
|
35 |
+
max_length = 100,
|
36 |
+
num_beams = 5,
|
37 |
+
early_stopping = True,
|
38 |
+
)
|
39 |
+
result = tokenizer.decode(beam_outputs[0])
|
40 |
+
result = result.replace('<pad>', '')
|
41 |
+
result = result.replace('</s>', '')
|
42 |
+
|
43 |
+
return result
|
44 |
+
|
45 |
+
def generate_cover(prompt, style, effect):
|
46 |
+
prompt = summarize_lyrics(prompt) # call function summarize_lyrics to summarize lyrics
|
47 |
+
prompt = prompt + ", " + effect + ", album cover, artistic, " + style
|
48 |
+
print(prompt)
|
49 |
+
image = pipe(prompt).images[0]
|
50 |
+
return image
|
51 |
+
|
52 |
+
demo = gr.Blocks()
|
53 |
+
with demo:
|
54 |
+
gr.HTML(
|
55 |
+
"""
|
56 |
+
<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
57 |
+
<div style="display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;">
|
58 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;">ArtIstic GENREator</h1>
|
59 |
+
</div>
|
60 |
+
<p style="margin-bottom: 10px; font-size: 94%">Generate Inspirational Lyrics and Single Cover</p>
|
61 |
+
</div>
|
62 |
+
"""
|
63 |
+
)
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
|
67 |
+
# Left column (lyrics generation)
|
68 |
+
with gr.Column():
|
69 |
+
with gr.Accordion("Step 1. Generate Lyrics"):
|
70 |
+
gr.Markdown("Enter the starting text and select genre to generate lyrics")
|
71 |
+
with gr.Row():
|
72 |
+
input_start_text = gr.Textbox(placeholder='I am', label="Starting Text")
|
73 |
+
input_lyrics_type = gr.Radio(choices=['pop', 'rap', 'country', 'rock', 'r&b'], value='pop', label="Lyrics Genre")
|
74 |
+
button_gen_lyrics = gr.Button("Generate Lyrics", variant="primary")
|
75 |
+
output_generated_lyrics = gr.Textbox(label="Generated Lyrics", lines=8)
|
76 |
+
|
77 |
+
# Right column (single cover generation)
|
78 |
+
with gr.Column():
|
79 |
+
with gr.Accordion("Step 2. Generate Single Cover"):
|
80 |
+
gr.Markdown("Cover will be generated based on style, effect and generated lyrics")
|
81 |
+
with gr.Row():
|
82 |
+
input_cover_style = gr.Dropdown(choices=['painted', 'abstract', 'minimalist', 'illustrated', 'photographic', 'vintage'], value='painted', label="Track Cover Style")
|
83 |
+
input_cover_effect = gr.Radio(choices=['black and white', 'highly detailed', 'blurred'], value='highly detailed', label="Track Cover Effect")
|
84 |
+
button_gen_cover = gr.Button("Generate Cover", variant="primary")
|
85 |
+
output_generated_cover = gr.Image(label="Generated Cover")
|
86 |
+
|
87 |
+
# Bind functions to buttons
|
88 |
+
button_gen_lyrics.click(fn=generate_lyrics, inputs=[input_lyrics_type , input_start_text], outputs=output_generated_lyrics)
|
89 |
+
button_gen_cover.click(fn=generate_cover, inputs=[output_generated_lyrics, input_cover_style, input_cover_effect], outputs=output_generated_cover)
|
90 |
+
|
91 |
+
demo.launch(debug=True)
|