import gradio as gr import torch from diffusers import AudioLDM2Pipeline import random # make Space compatible with CPU duplicates if torch.cuda.is_available(): device = "cuda" torch_dtype = torch.float16 else: device = "cpu" torch_dtype = torch.float32 # load the diffusers pipeline repo_id = "cvssp/audioldm2" pipe = AudioLDM2Pipeline.from_pretrained(repo_id, torch_dtype=torch_dtype).to(device) # pipe.unet = torch.compile(pipe.unet) # set the generator for reproducibility generator = torch.Generator(device) def text2audio(text, negative_prompt, duration, guidance_scale, random_seed, n_candidates): if text is None: raise gr.Error("Please provide a text input.") waveforms = pipe( text, audio_length_in_s=duration, guidance_scale=guidance_scale, num_inference_steps=200, negative_prompt=negative_prompt, num_waveforms_per_prompt=n_candidates if n_candidates else 1, generator=generator.manual_seed(int(random_seed)), )["audios"] return gr.make_waveform((16000, waveforms[0]), bg_image="bg.png") # def selections_to_audio(descriptive_group, weather_group, nature_group, musical_group, human_group, duration, guidance_scale, random_seed, n_candidates): # combined_terms = [] # groups_to_combine = [descriptive_group, weather_group, nature_group, musical_group, human_group] # for group in groups_to_combine: # combined_terms.extend(group) # if len(combined_terms) == 0: # raise gr.Error("Please select at least one term in any group.") # text = " ".join(combined_terms) # waveforms = pipe( # text, # audio_length_in_s=duration, # guidance_scale=guidance_scale, # num_inference_steps=200, # negative_prompt="Low quality.", # num_waveforms_per_prompt=n_candidates if n_candidates else 1, # generator=generator.manual_seed(int(random_seed)), # )["audios"] # return gr.make_waveform((16000, waveforms[0]), bg_image="bg.png") def selections_to_audio(prompt_display, duration, guidance_scale, random_seed, n_candidates, randomize): if prompt_display is None or len(prompt_display) == 0: raise gr.Error("Please select at least one term in any group.") if randomize: random_seed = random.randint(0, 999999) waveforms = pipe( prompt_display, audio_length_in_s=duration, guidance_scale=guidance_scale, num_inference_steps=200, negative_prompt="Low quality.", num_waveforms_per_prompt=n_candidates if n_candidates else 1, generator=generator.manual_seed(int(random_seed)), )["audios"] return (gr.make_waveform((16000, waveforms[0]), bg_image="bg.png"), prompt_display) def update_prompt_display(descriptive_group, nature_group, water_scene_group, forest_scene_group, jungle_scene_group, beach_scene_group, swamp_scene_group, musical_group, temple_scene_group, gathering_scene_group, prompt_display): combined_terms = [] groups_to_combine = [ descriptive_group, nature_group, water_scene_group, forest_scene_group, jungle_scene_group, beach_scene_group, swamp_scene_group, musical_group, temple_scene_group, gathering_scene_group, ] for group in groups_to_combine: combined_terms.extend(group) if len(combined_terms) == 0: return prompt_display return " ".join(combined_terms) iface = gr.Blocks() # weather_choices = [ # "raindrops", # "snowfall", # "rainy", # "thunder", # "rainstorm", # "wind", # "foggy", # "fog", # "blizzard" # ] nature_choices = [ "prairie", "nature", "mountain", "countryside", "rattling", "midnight", "twilight", "dawn", "dusk", "noon", "afternoon", "morning", ] # human_choices = [ # "village", # "tavern", # "garden", # "park", # "gentle", # "bonfire", # "campfire", # "gathering", # "cooking", # "temple", # "church", # "camping", # "hum", # "creaks", # "marketplace", # "farm", # "barnyard" # ] descriptive_choices = [ "calm", "lively", "magic", "magical", "serene", "meandering", "floating", "peaceful", "gentle", "busy", ] musical_choices = [ "music", "ambient", "chorus", "singing bowls", "bells", "symphony", "orchestra", "piano", "strings", "violin", "brass", "trumpet", "flute", "woodwinds", "drums", "steel drums", "calypso", "reggae", "electronic", "dance", "classical", "jazzy", "jazz", "guitar", "synth", "synthesizer", "organ", "rock", "pop", ] water_scene_choices = [ "water", "stream", "river", "ocean", "waves", "spring", "bubbling", "flowing", "creek", "brook", "babbling brook", "rain", "raindrops", "rainy", "rainstorm", "snow", "snowfall", "blizzard", "thunder", "lightning strike", "wind", "gust", "gale", "foggy", "fog", "cloud", "cloudy", "mist", "misty", "haze", "hazy", ] forest_scene_choices = [ "forest", "chirping", "birds", "crickets", "owls", "crows", "warbler", "sparrow", "goldfinch", "blackbird", "blue jay", "squirrels", "chipmunks", "rabbits", "trees", "rustling", ] jungle_scene_choices = [ "jungle", "rain forest", "parrots", "monkeys", "lion", "elephant", "rumbling", ] beach_scene_choices = [ "beach", "coast", "costal", "seagulls", "ocean", "waves", ] swamp_scene_choices = [ "swamp ambience", "swamp", "marsh", "bog", "wetlands", "frogs", "toad", "alligators", "turtles", "ducks", "geese", "swans", "reeds", ] temple_scene_choices = [ "temple", "church", "church bells", "pipe organ", "chanting", "singing bowls", "incantation", ] gathering_scene_choices = [ "gathering", "village", "tavern", "coffee shop", "garden", "park", "bonfire", "campfire", "cooking", "camping", "hum", "creaks", "marketplace", "farm", "barnyard", "conversation", "chatter", "road", "highway", "automobile", "truck", "engine", "traffic", "city", "urban", "footsteps", "radio", "machinery", "factory", ] def append_to_prompt(original_prompt): token_to_append = random.choice(prompt_append_tokens) return original_prompt + " " + token_to_append def append_selected_values_to_prompt(original_prompt, selected_values): return original_prompt + " " + " ".join(selected_values) with iface: gr.HTML( """

AudioLDM 2: A General Framework for Audio, Music, and Speech Generation

[Paper] [Project page] [🧨 Diffusers]

""" ) gr.HTML("""This is the demo for AudioLDM 2, powered by 🧨 Diffusers. Demo uses the checkpoint AudioLDM 2 base. For faster inference without waiting in queue, you may duplicate the space and upgrade to a GPU in the settings.""") with gr.Group(): # textbox = gr.Textbox( # value="The vibrant beat of Brazilian samba drums.", # max_lines=1, # label="Input text", # info="Your text is important for the audio quality. Please ensure it is descriptive by using more adjectives.", # elem_id="prompt-in", # ) # negative_textbox = gr.Textbox( # value="Low quality.", # max_lines=1, # label="Negative prompt", # info="Enter a negative prompt not to guide the audio generation. Selecting appropriate negative prompts can improve the audio quality significantly.", # elem_id="prompt-in", # ) # checkbox_group = gr.CheckboxGroup( # choices=prompt_append_tokens, # label="Terms to append to the prompt", # info="Selected terms will be added to the end of the text in Input text when the Append button is pressed. The order in which the terms are added is the order in which they were selected." # ) descriptive_group = gr.CheckboxGroup( choices=descriptive_choices, label="Descriptive words" ) nature_group = gr.CheckboxGroup( choices=nature_choices, label="Natural sounds" ) water_scene_group = gr.CheckboxGroup( choices=water_scene_choices, label="Water and weather" ) forest_scene_group = gr.CheckboxGroup( choices=forest_scene_choices, label="Forest" ) jungle_scene_group = gr.CheckboxGroup( choices=jungle_scene_choices, label="Jungle" ) beach_scene_group = gr.CheckboxGroup( choices=beach_scene_choices, label="Beach" ) swamp_scene_group = gr.CheckboxGroup( choices=swamp_scene_choices, label="Swamp" ) musical_group = gr.CheckboxGroup( choices=musical_choices, label="Musical sounds" ) gathering_scene_group = gr.CheckboxGroup( choices=gathering_scene_choices, label="Gatherings" ) temple_scene_group = gr.CheckboxGroup( choices=temple_scene_choices, label="Temple" ) with gr.Accordion("Click to modify detailed configurations", open=False): prompt_display = gr.Textbox( max_lines=1, interactive=True, label="Prompt sent to model for audio generation", info="Only used when no checkboxes are selected." ) randomize = gr.Checkbox( label="Randomize seed used for generation", value=True, ) seed = gr.Number( value=45, label="Seed", info="Change this value (any integer number) will lead to a different generation result.", ) duration = gr.Slider(5, 15, value=5, step=2.5, label="Duration (seconds)") guidance_scale = gr.Slider( 0, 7, value=3.5, step=0.5, label="Guidance scale", info="Larger => better quality and relevancy to text; Smaller => better diversity", ) n_candidates = gr.Slider( 1, 5, value=1, step=1, label="Number waveforms to generate", info="Automatic quality control. This number control the number of candidates (e.g., generate three audios and choose the best to show you). A larger value usually lead to better quality with heavier computation", ) actual_prompt = gr.Textbox( max_lines=1, interactive=False, label="The prompt used for the current audio output." ) outputs = gr.Video(label="Output", elem_id="output-video") checkbox_groups = [ descriptive_group, nature_group, water_scene_group, forest_scene_group, jungle_scene_group, beach_scene_group, swamp_scene_group, musical_group, temple_scene_group, gathering_scene_group, ] clearButton = gr.ClearButton(components=checkbox_groups, value="Clear") btn = gr.Button("Submit") update_inputs=[] update_inputs.extend(checkbox_groups) update_inputs.append(prompt_display) btn.click( update_prompt_display, inputs=update_inputs, outputs=[prompt_display], ).then( selections_to_audio, inputs=[prompt_display, duration, guidance_scale, seed, n_candidates, randomize], outputs=[outputs, actual_prompt], ) # btn.click( # selections_to_audio, # inputs=[prompt_display, duration, guidance_scale, seed, n_candidates, randomize], # outputs=[outputs, actual_prompt], # ) # for group in checkbox_groups: # group.change( # update_prompt_display, # inputs=checkbox_groups, # outputs=[prompt_display], # ) iface.queue(max_size=20).launch()