import gradio as gr import torch from PIL import Image import numpy as np from spectro import wav_bytes_from_spectrogram_image from diffusers import StableDiffusionPipeline from diffusers import StableDiffusionImg2ImgPipeline from share_btn import community_icon_html, loading_icon_html, share_js device = "cuda" MODEL_ID = "riffusion/riffusion-model-v1" pipe = StableDiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=torch.float16) pipe = pipe.to(device) pipe2 = StableDiffusionImg2ImgPipeline.from_pretrained(MODEL_ID, torch_dtype=torch.float16) pipe2 = pipe2.to(device) spectro_from_wav = gr.Interface.load("spaces/fffiloni/audio-to-spectrogram") def predict(prompt, negative_prompt, audio_input, duration): if audio_input == None : return classic(prompt, negative_prompt, duration) else : return style_transfer(prompt, negative_prompt, audio_input) def classic(prompt, negative_prompt, duration): if duration == 5: width_duration=512 else : width_duration = 512 + ((int(duration)-5) * 128) spec = pipe(prompt, negative_prompt=negative_prompt, height=512, width=width_duration).images[0] print(spec) wav = wav_bytes_from_spectrogram_image(spec) with open("output.wav", "wb") as f: f.write(wav[0].getbuffer()) return spec, 'output.wav', gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) def style_transfer(prompt, negative_prompt, audio_input): spec = spectro_from_wav(audio_input) print(spec) # Open the image im = Image.open(spec) # Open the image im = image_from_spectrogram(im, 1) new_spectro = pipe2(prompt=prompt, image=im, strength=0.5, guidance_scale=7).images wav = wav_bytes_from_spectrogram_image(new_spectro[0]) with open("output.wav", "wb") as f: f.write(wav[0].getbuffer()) return new_spectro[0], 'output.wav', gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) def image_from_spectrogram( spectrogram: np.ndarray, max_volume: float = 50, power_for_image: float = 0.25 ) -> Image.Image: """ Compute a spectrogram image from a spectrogram magnitude array. """ # Apply the power curve data = np.power(spectrogram, power_for_image) # Rescale to 0-255 data = data * 255 / max_volume # Invert data = 255 - data # Convert to a PIL image image = Image.fromarray(data.astype(np.uint8)) # Flip Y image = image.transpose(Image.FLIP_TOP_BOTTOM) # Convert to RGB image = image.convert("RGB") return image title = """
Describe a musical prompt, generate music by getting a spectrogram image & sound.
Do you need faster results ? You can skip the queue by duplicating this space:
About the model: Riffusion is a latent text-to-image diffusion model capable of generating spectrogram images given any text input. These spectrograms can be converted into audio clips.
—
The Riffusion model was created by fine-tuning the Stable-Diffusion-v1-5 checkpoint.
—
The model is intended for research purposes only. Possible research areas and tasks include
generation of artworks, audio, and use in creative processes, applications in educational or creative tools, research on generative models.
You may also like: