Spaces:
Runtime error
Runtime error
Duplicate from fffiloni/Music-To-Image
Browse filesCo-authored-by: Sylvain Filoni <fffiloni@users.noreply.huggingface.co>
- .gitattributes +37 -0
- README.md +13 -0
- app.py +407 -0
- examples/blank.md +0 -0
- examples/electronic.mp3 +0 -0
- examples/folk.wav +3 -0
- examples/orchestra.wav +3 -0
- requirements.txt +9 -0
- share_btn.py +73 -0
.gitattributes
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
examples/folk.wav filter=lfs diff=lfs merge=lfs -text
|
37 |
+
examples/orchestra.wav filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Music To Image
|
3 |
+
emoji: 🎶🌅
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: gray
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.39.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: fffiloni/Music-To-Image
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,407 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tempfile
|
3 |
+
import os
|
4 |
+
hf_token = os.environ.get('HF_TOKEN')
|
5 |
+
|
6 |
+
lpmc_client = gr.load("seungheondoh/LP-Music-Caps-demo", src="spaces")
|
7 |
+
|
8 |
+
from gradio_client import Client
|
9 |
+
|
10 |
+
client = Client("https://fffiloni-test-llama-api-debug.hf.space/", hf_token=hf_token)
|
11 |
+
lyrics_client = Client("https://fffiloni-music-to-lyrics.hf.space/")
|
12 |
+
visualizer_client = Client("https://fffiloni-animated-audio-visualizer-1024.hf.space/", hf_token=hf_token)
|
13 |
+
|
14 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
15 |
+
|
16 |
+
from compel import Compel, ReturnedEmbeddingsType
|
17 |
+
from diffusers import DiffusionPipeline
|
18 |
+
import torch
|
19 |
+
|
20 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",
|
21 |
+
torch_dtype=torch.float16,
|
22 |
+
use_safetensors=True,
|
23 |
+
variant="fp16")
|
24 |
+
pipe.to("cuda")
|
25 |
+
|
26 |
+
compel = Compel(
|
27 |
+
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
28 |
+
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
29 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
30 |
+
requires_pooled=[False, True]
|
31 |
+
)
|
32 |
+
|
33 |
+
#pipe.enable_model_cpu_offload()
|
34 |
+
|
35 |
+
# if using torch < 2.0
|
36 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
37 |
+
|
38 |
+
from pydub import AudioSegment
|
39 |
+
|
40 |
+
import yt_dlp as youtube_dl
|
41 |
+
from moviepy.editor import VideoFileClip
|
42 |
+
|
43 |
+
YT_LENGTH_LIMIT_S = 480 # limit to 1 hour YouTube files
|
44 |
+
|
45 |
+
def download_yt_audio(yt_url, filename):
|
46 |
+
info_loader = youtube_dl.YoutubeDL()
|
47 |
+
|
48 |
+
try:
|
49 |
+
info = info_loader.extract_info(yt_url, download=False)
|
50 |
+
except youtube_dl.utils.DownloadError as err:
|
51 |
+
raise gr.Error(str(err))
|
52 |
+
|
53 |
+
file_length = info["duration_string"]
|
54 |
+
file_h_m_s = file_length.split(":")
|
55 |
+
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
56 |
+
|
57 |
+
if len(file_h_m_s) == 1:
|
58 |
+
file_h_m_s.insert(0, 0)
|
59 |
+
if len(file_h_m_s) == 2:
|
60 |
+
file_h_m_s.insert(0, 0)
|
61 |
+
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
62 |
+
|
63 |
+
if file_length_s > YT_LENGTH_LIMIT_S:
|
64 |
+
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
65 |
+
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
66 |
+
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
67 |
+
|
68 |
+
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
69 |
+
|
70 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
71 |
+
try:
|
72 |
+
ydl.download([yt_url])
|
73 |
+
except youtube_dl.utils.ExtractorError as err:
|
74 |
+
raise gr.Error(str(err))
|
75 |
+
|
76 |
+
|
77 |
+
def convert_to_mp3(input_path, output_path):
|
78 |
+
try:
|
79 |
+
video_clip = VideoFileClip(input_path)
|
80 |
+
audio_clip = video_clip.audio
|
81 |
+
print("Converting to MP3...")
|
82 |
+
audio_clip.write_audiofile(output_path)
|
83 |
+
except Exception as e:
|
84 |
+
print("Error:", e)
|
85 |
+
|
86 |
+
def load_youtube_audio(yt_link):
|
87 |
+
|
88 |
+
gr.Info("Loading your YouTube link ... ")
|
89 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
90 |
+
filepath = os.path.join(tmpdirname, "video.mp4")
|
91 |
+
download_yt_audio(yt_link, filepath)
|
92 |
+
|
93 |
+
mp3_output_path = "video_sound.mp3"
|
94 |
+
convert_to_mp3(filepath, mp3_output_path)
|
95 |
+
print("Conversion complete. MP3 saved at:", mp3_output_path)
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
return mp3_output_path
|
100 |
+
|
101 |
+
def cut_audio(input_path, output_path, max_duration):
|
102 |
+
audio = AudioSegment.from_file(input_path)
|
103 |
+
|
104 |
+
if len(audio) > max_duration:
|
105 |
+
audio = audio[:max_duration]
|
106 |
+
|
107 |
+
audio.export(output_path, format="mp3")
|
108 |
+
|
109 |
+
return output_path
|
110 |
+
|
111 |
+
def get_text_after_colon(input_text):
|
112 |
+
# Find the first occurrence of ":"
|
113 |
+
colon_index = input_text.find(":")
|
114 |
+
|
115 |
+
# Check if ":" exists in the input_text
|
116 |
+
if colon_index != -1:
|
117 |
+
# Extract the text after the colon
|
118 |
+
result_text = input_text[colon_index + 1:].strip()
|
119 |
+
return result_text
|
120 |
+
else:
|
121 |
+
# Return the original text if ":" is not found
|
122 |
+
return input_text
|
123 |
+
|
124 |
+
|
125 |
+
def solo_xd(prompt):
|
126 |
+
images = pipe(prompt=prompt).images[0]
|
127 |
+
return images
|
128 |
+
|
129 |
+
def get_visualizer_video(audio_in, image_in, song_title):
|
130 |
+
|
131 |
+
title = f"""{song_title.upper()}\nMusic-to-Image demo by @fffiloni | HuggingFace
|
132 |
+
"""
|
133 |
+
|
134 |
+
visualizer_video = visualizer_client.predict(
|
135 |
+
title, # str in 'title' Textbox component
|
136 |
+
audio_in, # str (filepath or URL to file) in 'audio_in' Audio component
|
137 |
+
image_in, # str (filepath or URL to image) in 'image_in' Image component
|
138 |
+
"my_music_to_image_awesome_video.mp4",
|
139 |
+
api_name="/predict"
|
140 |
+
)
|
141 |
+
|
142 |
+
return visualizer_video[0]
|
143 |
+
|
144 |
+
def infer(audio_file, has_lyrics):
|
145 |
+
print("NEW INFERENCE ...")
|
146 |
+
gr.Info('Truncating your audio to the first 30 seconds')
|
147 |
+
truncated_audio = cut_audio(audio_file, "trunc_audio.mp3", 30000)
|
148 |
+
processed_audio = truncated_audio
|
149 |
+
|
150 |
+
print("Calling LP Music Caps...")
|
151 |
+
gr.Info('Calling LP Music Caps...')
|
152 |
+
cap_result = lpmc_client(
|
153 |
+
truncated_audio, # str (filepath or URL to file) in 'audio_path' Audio component
|
154 |
+
api_name="predict"
|
155 |
+
)
|
156 |
+
print(f"MUSIC DESC: {cap_result}")
|
157 |
+
|
158 |
+
if has_lyrics == "Yes" :
|
159 |
+
print("""———
|
160 |
+
Getting Lyrics ...
|
161 |
+
Note: We only take the first minute of the song
|
162 |
+
""")
|
163 |
+
truncated_lyrics = cut_audio(audio_file, "trunc_lyrics.mp3", 60000)
|
164 |
+
gr.Info("Getting Lyrics ...")
|
165 |
+
lyrics_result = lyrics_client.predict(
|
166 |
+
truncated_lyrics, # str (filepath or URL to file) in 'Song input' Audio component
|
167 |
+
fn_index=0
|
168 |
+
)
|
169 |
+
print(f"LYRICS: {lyrics_result}")
|
170 |
+
|
171 |
+
llama_q = f"""
|
172 |
+
I'll give you a music description + the lyrics of the song.
|
173 |
+
Give me an image description that would fit well with the music description, reflecting the lyrics too.
|
174 |
+
Be creative, do not do list, just an image description as required. Try to think about human characters first.
|
175 |
+
Your image description must fit well for a stable diffusion prompt.
|
176 |
+
|
177 |
+
Here's the music description :
|
178 |
+
|
179 |
+
« {cap_result} »
|
180 |
+
|
181 |
+
And here are the lyrics :
|
182 |
+
|
183 |
+
« {lyrics_result} »
|
184 |
+
|
185 |
+
"""
|
186 |
+
elif has_lyrics == "No" :
|
187 |
+
|
188 |
+
llama_q = f"""
|
189 |
+
I'll give you a music description.
|
190 |
+
Give me an image description that would fit well with the music description.
|
191 |
+
Be creative, do not do list, just an image description as required. Try to think about human characters first.
|
192 |
+
Your image description must fit well for a stable diffusion prompt.
|
193 |
+
|
194 |
+
Here's the music description :
|
195 |
+
|
196 |
+
« {cap_result} »
|
197 |
+
"""
|
198 |
+
print("""———
|
199 |
+
Calling Llama2 ...
|
200 |
+
""")
|
201 |
+
gr.Info("Calling Llama2 ...")
|
202 |
+
result = client.predict(
|
203 |
+
llama_q, # str in 'Message' Textbox component
|
204 |
+
"M2I",
|
205 |
+
api_name="/predict"
|
206 |
+
)
|
207 |
+
|
208 |
+
result = get_text_after_colon(result)
|
209 |
+
|
210 |
+
print(f"Llama2 result: {result}")
|
211 |
+
|
212 |
+
#gr.Info("Prompt Optimization ...")
|
213 |
+
#get_shorter_prompt = f"""
|
214 |
+
#From this image description, please provide a short but efficient summary for a good Stable Diffusion prompt:
|
215 |
+
#'{result}'
|
216 |
+
#"""
|
217 |
+
|
218 |
+
#shorten = client.predict(
|
219 |
+
# get_shorter_prompt, # str in 'Message' Textbox component
|
220 |
+
# api_name="/predict"
|
221 |
+
#)
|
222 |
+
|
223 |
+
#print(f'SHORTEN PROMPT: {shorten}')
|
224 |
+
|
225 |
+
# ———
|
226 |
+
print("""———
|
227 |
+
Calling SD-XL ...
|
228 |
+
""")
|
229 |
+
gr.Info('Calling SD-XL ...')
|
230 |
+
prompt = result
|
231 |
+
conditioning, pooled = compel(prompt)
|
232 |
+
images = pipe(prompt_embeds=conditioning, pooled_prompt_embeds=pooled).images[0]
|
233 |
+
|
234 |
+
print("Finished")
|
235 |
+
|
236 |
+
#return cap_result, result, images
|
237 |
+
return processed_audio, images, result, gr.update(visible=True), gr.Group.update(visible=True)
|
238 |
+
|
239 |
+
css = """
|
240 |
+
#col-container {max-width: 780px; margin-left: auto; margin-right: auto;}
|
241 |
+
a {text-decoration-line: underline; font-weight: 600;}
|
242 |
+
.animate-spin {
|
243 |
+
animation: spin 1s linear infinite;
|
244 |
+
}
|
245 |
+
@keyframes spin {
|
246 |
+
from {
|
247 |
+
transform: rotate(0deg);
|
248 |
+
}
|
249 |
+
to {
|
250 |
+
transform: rotate(360deg);
|
251 |
+
}
|
252 |
+
}
|
253 |
+
#share-btn-container {
|
254 |
+
display: flex;
|
255 |
+
padding-left: 0.5rem !important;
|
256 |
+
padding-right: 0.5rem !important;
|
257 |
+
background-color: #000000;
|
258 |
+
justify-content: center;
|
259 |
+
align-items: center;
|
260 |
+
border-radius: 9999px !important;
|
261 |
+
max-width: 13rem;
|
262 |
+
}
|
263 |
+
div#share-btn-container > div {
|
264 |
+
flex-direction: row;
|
265 |
+
background: black;
|
266 |
+
align-items: center;
|
267 |
+
}
|
268 |
+
#share-btn-container:hover {
|
269 |
+
background-color: #060606;
|
270 |
+
}
|
271 |
+
#share-btn {
|
272 |
+
all: initial;
|
273 |
+
color: #ffffff;
|
274 |
+
font-weight: 600;
|
275 |
+
cursor:pointer;
|
276 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
277 |
+
margin-left: 0.5rem !important;
|
278 |
+
padding-top: 0.5rem !important;
|
279 |
+
padding-bottom: 0.5rem !important;
|
280 |
+
right:0;
|
281 |
+
}
|
282 |
+
#share-btn * {
|
283 |
+
all: unset;
|
284 |
+
}
|
285 |
+
#share-btn-container div:nth-child(-n+2){
|
286 |
+
width: auto !important;
|
287 |
+
min-height: 0px !important;
|
288 |
+
}
|
289 |
+
#share-btn-container .wrap {
|
290 |
+
display: none !important;
|
291 |
+
}
|
292 |
+
#share-btn-container.hidden {
|
293 |
+
display: none!important;
|
294 |
+
}
|
295 |
+
.footer {
|
296 |
+
margin-bottom: 45px;
|
297 |
+
margin-top: 10px;
|
298 |
+
text-align: center;
|
299 |
+
border-bottom: 1px solid #e5e5e5;
|
300 |
+
}
|
301 |
+
.footer>p {
|
302 |
+
font-size: .8rem;
|
303 |
+
display: inline-block;
|
304 |
+
padding: 0 10px;
|
305 |
+
transform: translateY(10px);
|
306 |
+
background: white;
|
307 |
+
}
|
308 |
+
.dark .footer {
|
309 |
+
border-color: #303030;
|
310 |
+
}
|
311 |
+
.dark .footer>p {
|
312 |
+
background: #0b0f19;
|
313 |
+
}
|
314 |
+
"""
|
315 |
+
with gr.Blocks(css=css) as demo:
|
316 |
+
with gr.Column(elem_id="col-container"):
|
317 |
+
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
318 |
+
<div
|
319 |
+
style="
|
320 |
+
display: inline-flex;
|
321 |
+
align-items: center;
|
322 |
+
gap: 0.8rem;
|
323 |
+
font-size: 1.75rem;
|
324 |
+
"
|
325 |
+
>
|
326 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
327 |
+
Music To Image
|
328 |
+
</h1>
|
329 |
+
</div>
|
330 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
331 |
+
Sends an audio into <a href="https://huggingface.co/spaces/seungheondoh/LP-Music-Caps-demo" target="_blank">LP-Music-Caps</a>
|
332 |
+
to generate a audio caption which is then translated to an illustrative image description with Llama2, and finally run through
|
333 |
+
Stable Diffusion XL to generate an image from the audio ! <br /><br />
|
334 |
+
Note: Only the first 30 seconds of your audio will be used for inference.
|
335 |
+
</p>
|
336 |
+
</div>""")
|
337 |
+
|
338 |
+
audio_input = gr.Audio(label="Music input", type="filepath", source="upload")
|
339 |
+
with gr.Row():
|
340 |
+
youtube_link = gr.Textbox(show_label=False, placeholder="you can also paste YT link and load it")
|
341 |
+
yt_load_btn = gr.Button("Load YT song")
|
342 |
+
|
343 |
+
with gr.Row():
|
344 |
+
has_lyrics = gr.Radio(label="Does your audio has lyrics ?", choices=["Yes", "No"], value="No", info="If yes, the image should reflect the lyrics, but be aware that because we add a step (getting lyrics), inference will take more time.")
|
345 |
+
song_title = gr.Textbox(label="Song Title", placeholder="Title: ", interactive=True, info="If you want to share your result, please provide the title of your audio sample :)", elem_id="song-title")
|
346 |
+
|
347 |
+
infer_btn = gr.Button("Generate Image from Music")
|
348 |
+
#lpmc_cap = gr.Textbox(label="Lp Music Caps caption")
|
349 |
+
|
350 |
+
with gr.Group():
|
351 |
+
|
352 |
+
with gr.Row():
|
353 |
+
|
354 |
+
llama_trans_cap = gr.Textbox(label="Llama Image Suggestion", placeholder="Llama2 image prompt suggestion will be displayed here ;)", visible=True, lines=12, max_lines=18, elem_id="llama-prompt")
|
355 |
+
|
356 |
+
with gr.Tab("Image Result"):
|
357 |
+
img_result = gr.Image(label="Image Result", elem_id="image-out", interactive=False, type="filepath")
|
358 |
+
|
359 |
+
with gr.Tab("Video visualizer"):
|
360 |
+
|
361 |
+
with gr.Column():
|
362 |
+
processed_audio = gr.Audio(type="filepath", visible=False)
|
363 |
+
visualizer_video = gr.Video(label="Video visualizer output")
|
364 |
+
get_visualizer_vid = gr.Button("Export as video !")
|
365 |
+
|
366 |
+
with gr.Row():
|
367 |
+
|
368 |
+
tryagain_btn = gr.Button("Try another image ?", visible=False)
|
369 |
+
|
370 |
+
with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
371 |
+
community_icon = gr.HTML(community_icon_html)
|
372 |
+
loading_icon = gr.HTML(loading_icon_html)
|
373 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
374 |
+
|
375 |
+
gr.Examples(examples=[["./examples/electronic.mp3", "No"],["./examples/folk.wav", "No"], ["./examples/orchestra.wav", "No"]],
|
376 |
+
fn=infer,
|
377 |
+
inputs=[audio_input, has_lyrics],
|
378 |
+
outputs=[processed_audio, img_result, llama_trans_cap, tryagain_btn, share_group],
|
379 |
+
cache_examples=True
|
380 |
+
)
|
381 |
+
|
382 |
+
gr.HTML("""
|
383 |
+
<div class="footer">
|
384 |
+
<p>
|
385 |
+
Music to Image Demo by 🤗 <a href="https://twitter.com/fffiloni" target="_blank">Sylvain Filoni</a>
|
386 |
+
</p>
|
387 |
+
</div>
|
388 |
+
<div id="may-like-container" style="display: flex;justify-content: center;flex-direction: column;align-items: center;">
|
389 |
+
<p style="font-size: 0.8em;margin-bottom: 4px;">You may also like: </p>
|
390 |
+
<div id="may-like" style="display:flex; align-items:center; justify-content: center;height:20px;">
|
391 |
+
<svg height="20" width="182" style="margin-left:4px">
|
392 |
+
<a href="https://huggingface.co/spaces/fffiloni/Music-To-Zeroscope" target="_blank">
|
393 |
+
<image href="https://img.shields.io/badge/🤗 Spaces-Music To Zeroscope-blue" src="https://img.shields.io/badge/🤗 Spaces-Music To Zeroscope-blue.png" height="20"/>
|
394 |
+
</a>
|
395 |
+
</svg>
|
396 |
+
</div>
|
397 |
+
</div>
|
398 |
+
""")
|
399 |
+
|
400 |
+
#infer_btn.click(fn=infer, inputs=[audio_input], outputs=[lpmc_cap, llama_trans_cap, img_result])
|
401 |
+
yt_load_btn.click(fn=load_youtube_audio, inputs=[youtube_link], outputs=[audio_input], queue=False)
|
402 |
+
infer_btn.click(fn=infer, inputs=[audio_input, has_lyrics], outputs=[processed_audio, img_result, llama_trans_cap, tryagain_btn, share_group])
|
403 |
+
share_button.click(None, [], [], _js=share_js)
|
404 |
+
tryagain_btn.click(fn=solo_xd, inputs=[llama_trans_cap], outputs=[img_result])
|
405 |
+
get_visualizer_vid.click(fn=get_visualizer_video, inputs=[processed_audio, img_result, song_title], outputs=[visualizer_video], queue=False)
|
406 |
+
|
407 |
+
demo.queue(max_size=20).launch()
|
examples/blank.md
ADDED
File without changes
|
examples/electronic.mp3
ADDED
Binary file (480 kB). View file
|
|
examples/folk.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ed9c5e4e03b706c464993ab770388fd3a52b240e4c054ec279322dd48c0cb7a8
|
3 |
+
size 1918830
|
examples/orchestra.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:97135f273616d39b11db37a1ec61e0bd729b82c085f7dcdc472b26a37affca8a
|
3 |
+
size 1323632
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pydub
|
2 |
+
invisible_watermark
|
3 |
+
transformers
|
4 |
+
diffusers
|
5 |
+
compel
|
6 |
+
accelerate
|
7 |
+
safetensors
|
8 |
+
moviepy
|
9 |
+
yt-dlp
|
share_btn.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
|
2 |
+
<path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
|
3 |
+
<path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
|
4 |
+
</svg>"""
|
5 |
+
|
6 |
+
loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
|
7 |
+
style="color: #ffffff;
|
8 |
+
"
|
9 |
+
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
10 |
+
|
11 |
+
share_js = """async () => {
|
12 |
+
async function uploadFile(file){
|
13 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
14 |
+
const response = await fetch(UPLOAD_URL, {
|
15 |
+
method: 'POST',
|
16 |
+
headers: {
|
17 |
+
'Content-Type': file.type,
|
18 |
+
'X-Requested-With': 'XMLHttpRequest',
|
19 |
+
},
|
20 |
+
body: file, /// <- File inherits from Blob
|
21 |
+
});
|
22 |
+
const url = await response.text();
|
23 |
+
return url;
|
24 |
+
}
|
25 |
+
|
26 |
+
async function getOutputImgFile(imgEl){
|
27 |
+
const res = await fetch(imgEl.src);
|
28 |
+
const blob = await res.blob();
|
29 |
+
const imgId = Date.now() % 200;
|
30 |
+
const isPng = imgEl.src.startsWith(`data:image/png`);
|
31 |
+
if(isPng){
|
32 |
+
const fileName = `sd-perception-${{imgId}}.png`;
|
33 |
+
return new File([blob], fileName, { type: 'image/png' });
|
34 |
+
}else{
|
35 |
+
const fileName = `sd-perception-${{imgId}}.jpg`;
|
36 |
+
return new File([blob], fileName, { type: 'image/jpeg' });
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
|
41 |
+
const outputImgEl = gradioEl.querySelector('#image-out img');
|
42 |
+
const llamaPrompt = gradioEl.querySelector('#llama-prompt textarea').value;
|
43 |
+
const songTitle = gradioEl.querySelector('#song-title textarea').value;
|
44 |
+
|
45 |
+
|
46 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
47 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
48 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
49 |
+
|
50 |
+
shareBtnEl.style.pointerEvents = 'none';
|
51 |
+
shareIconEl.style.display = 'none';
|
52 |
+
loadingIconEl.style.removeProperty('display');
|
53 |
+
|
54 |
+
|
55 |
+
const outputFile = await getOutputImgFile(outputImgEl);
|
56 |
+
const urlOutputImg = await uploadFile(outputFile);
|
57 |
+
|
58 |
+
const descriptionMd = `
|
59 |
+
#### Llama2 Prompt :
|
60 |
+
${llamaPrompt}
|
61 |
+
#### Image output:
|
62 |
+
<img src="${urlOutputImg}" />
|
63 |
+
`;
|
64 |
+
const params = new URLSearchParams({
|
65 |
+
title: songTitle,
|
66 |
+
description: descriptionMd,
|
67 |
+
});
|
68 |
+
const paramsStr = params.toString();
|
69 |
+
window.open(`https://huggingface.co/spaces/fffiloni/Music-To-Image/discussions/new?${paramsStr}`, '_blank');
|
70 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
71 |
+
shareIconEl.style.removeProperty('display');
|
72 |
+
loadingIconEl.style.display = 'none';
|
73 |
+
}"""
|