JoshKeesee commited on
Commit
47def4a
·
verified ·
1 Parent(s): 682a8f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -1
app.py CHANGED
@@ -1,3 +1,132 @@
1
  import gradio as gr
 
2
 
3
- gr.load("models/Hyeon2/riffusion-musiccaps").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
 
4
+ from PIL import Image
5
+ import numpy as np
6
+ from spectro import wav_bytes_from_spectrogram_image
7
+
8
+ from diffusers import StableDiffusionPipeline
9
+ from diffusers import StableDiffusionImg2ImgPipeline
10
+
11
+ device = "cpu"
12
+ MODEL_ID = "Hyeon2/riffusion-musiccaps"
13
+ pipe = StableDiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=torch.float16)
14
+ pipe = pipe.to(device)
15
+
16
+ def predict(prompt, negative_prompt, audio_input, duration):
17
+ return classic(prompt, negative_prompt, duration)
18
+
19
+ def classic(prompt, negative_prompt, duration):
20
+ if duration == 5:
21
+ width_duration=512
22
+ else:
23
+ width_duration = 512 + ((int(duration) - 5) * 128)
24
+ spec = pipe(prompt, negative_prompt=negative_prompt, height=512, width=width_duration).images[0]
25
+ print(spec)
26
+ wav = wav_bytes_from_spectrogram_image(spec)
27
+ with open("output.wav", "wb") as f:
28
+ f.write(wav[0].getbuffer())
29
+ return spec, 'output.wav'
30
+
31
+ title = """
32
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
33
+ <div
34
+ style="
35
+ display: inline-flex;
36
+ align-items: center;
37
+ gap: 0.8rem;
38
+ font-size: 1.75rem;
39
+ margin-bottom: 10px;
40
+ line-height: 1em;
41
+ "
42
+ >
43
+ <h1 style="font-weight: 600; margin-bottom: 7px;">
44
+ Riffusion-Musiccaps real-time music generation
45
+ </h1>
46
+ </div>
47
+ <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
48
+ Describe a musical prompt, generate music by getting a spectrogram image & sound.
49
+ </p>
50
+ </div>
51
+ """
52
+
53
+ css = '''
54
+ #col-container, #col-container-2 {max-width: 510px; margin-left: auto; margin-right: auto;}
55
+ a {text-decoration-line: underline; font-weight: 600;}
56
+ div#record_btn > .mt-6 {
57
+ margin-top: 0!important;
58
+ }
59
+ div#record_btn > .mt-6 button {
60
+ width: 100%;
61
+ height: 40px;
62
+ }
63
+ .footer {
64
+ margin-bottom: 45px;
65
+ margin-top: 10px;
66
+ text-align: center;
67
+ border-bottom: 1px solid #e5e5e5;
68
+ }
69
+ .footer>p {
70
+ font-size: .8rem;
71
+ display: inline-block;
72
+ padding: 0 10px;
73
+ transform: translateY(10px);
74
+ background: white;
75
+ }
76
+ .dark .footer {
77
+ border-color: #303030;
78
+ }
79
+ .dark .footer>p {
80
+ background: #0b0f19;
81
+ }
82
+ .animate-spin {
83
+ animation: spin 1s linear infinite;
84
+ }
85
+ @keyframes spin {
86
+ from {
87
+ transform: rotate(0deg);
88
+ }
89
+ to {
90
+ transform: rotate(360deg);
91
+ }
92
+ }
93
+ #share-btn-container {
94
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
95
+ }
96
+ #share-btn {
97
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
98
+ }
99
+ #share-btn * {
100
+ all: unset;
101
+ }
102
+ #share-btn-container div:nth-child(-n+2){
103
+ width: auto !important;
104
+ min-height: 0px !important;
105
+ }
106
+ #share-btn-container .wrap {
107
+ display: none !important;
108
+ }
109
+ '''
110
+
111
+ with gr.Blocks(css="style.css") as demo:
112
+
113
+ with gr.Column(elem_id="col-container"):
114
+
115
+ gr.HTML(title)
116
+
117
+ prompt_input = gr.Textbox(placeholder="a cat diva singing in a New York jazz club", label="Musical prompt", elem_id="prompt-in")
118
+ audio_input = gr.Audio(sources=["upload"], type="filepath", visible=False)
119
+ with gr.Row():
120
+ negative_prompt = gr.Textbox(label="Negative prompt")
121
+ duration_input = gr.Slider(label="Duration in seconds", minimum=5, maximum=10, step=1, value=8, elem_id="duration-slider")
122
+
123
+ send_btn = gr.Button(value="Get a new spectrogram!", elem_id="submit-btn")
124
+
125
+ with gr.Column(elem_id="col-container-2"):
126
+
127
+ spectrogram_output = gr.Image(label="spectrogram image result", elem_id="img-out")
128
+ sound_output = gr.Audio(type='filepath', label="spectrogram sound", elem_id="music-out")
129
+
130
+ send_btn.click(predict, inputs=[prompt_input, negative_prompt, audio_input, duration_input], outputs=[spectrogram_output, sound_output])
131
+
132
+ demo.queue(max_size=250).launch(debug=True, ssr_mode=False)