Spaces:
Runtime error
Runtime error
Stable Audio Open + progbars + mp3 + batched forward + cleanup
Browse filesFiles changed (19)
- .gitattributes+1
- Examples/Beethoven.wav → Examples/Beethoven.mp3Renamed+2-2
- Examples/Cat_dog.wav → Examples/Beethoven_arcade.mp3Renamed+2-2
- Examples/Beethoven_arcade.wav → Examples/Beethoven_piano.mp3Renamed+2-2
- Examples/Beethoven_piano.wav → Examples/Beethoven_rock.mp3Renamed+2-2
- Examples/Cat.wav → Examples/Cat.mp3Renamed+2-2
- Examples/Cat_dog.mp3Added+3
- Examples/ModalJazz.mp3Added+3
- Examples/ModalJazz.wavDeleted-3
- Examples/ModalJazz_banjo.mp3Added+3
- Examples/ModalJazz_banjo.wavDeleted-3
- Examples/Shadows.mp3Added+3
- Examples/Shadows_arcade.mp3Added+3
- README.md+4-1
- app.py+235-158
- inversion_utils.py+139-381
- models.py+469-253
- requirements.txt+3-2
- utils.py+50-16
33 unmodified lines34353633 unmodified lines*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.wav filter=lfs diff=lfs merge=lfs -text33 unmodified lines3435363733 unmodified lines*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.wav filter=lfs diff=lfs merge=lfs -text*.mp3 filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:30a6a087a9e0eb87422aa3b48ad966eabb1dfe105d73a25d356b71d3aee31493size 4828972123version https://git-lfs.github.com/spec/v1oid sha256:3dcc79fe071d118df3caaeeb85d7944f93a5df40bbdb72a26b67bd57da2af7c5size 1097142
123version https://git-lfs.github.com/spec/v1oid sha256:90a97dc229eeccef307dd40db97fb09cc439ce0b45a320fd84b2ea6b03d0deb2size 327822123version https://git-lfs.github.com/spec/v1oid sha256:542bd61d9cc1723ccfd9bfc06b0818e77fc763013827ff1f9289e2ac6a912904size 563040
123version https://git-lfs.github.com/spec/v1oid sha256:ccd929b93c15706f2102a27973d490a84ce0eb97faba6a92ece0c6d81ed2c26esize 1794746123version https://git-lfs.github.com/spec/v1oid sha256:000d82c39d8c41b10188d328e29cb1baa948232bacd693f22e297cc54f4bb707size 563040
123version https://git-lfs.github.com/spec/v1oid sha256:5787c31b0b3c78dec33d651d437364785713042e7cfce2290cf4baf01f65ac6fsize 1794746123version https://git-lfs.github.com/spec/v1oid sha256:c51d75c9094a50c7892449a013b32ffde266a5abd6dad9f00bf3aeec0ee935eesize 1097142
123version https://git-lfs.github.com/spec/v1oid sha256:27b43763a8d9ac90dc78285ed9817b16524f24b4f4d1aa399616f1a04d4a9fd9size 1920508123version https://git-lfs.github.com/spec/v1oid sha256:cff7010e5fb12a57508c7a0941663f1a12bfc8b3b3d01d0973359cd42ae5eb1esize 402542
123version https://git-lfs.github.com/spec/v1oid sha256:72ff727243606215c934552e946f7d97b5e2e39c4d6263f7f36659e3f39f3008size 207403123version https://git-lfs.github.com/spec/v1oid sha256:34cf145b84b6b4669050ca42932fb74ac0f28aabbe6c665f12a877c9809fa9c6size 4153468123version https://git-lfs.github.com/spec/v1oid sha256:846a77046d21ebc3996841404eede9d56797c82b3414025e1ccafe586eaf2959size 9153322123version https://git-lfs.github.com/spec/v1oid sha256:11680068427556981aa6304e6c11bd05debc820ca581c248954c1ffe3cd94569size 2128320123version https://git-lfs.github.com/spec/v1oid sha256:122e0078c0bf2fc96425071706fe0e8674c93cc1d2787fd02c0e2c0f12de5cc5size 6802106123version https://git-lfs.github.com/spec/v1oid sha256:2e0cab2ebda4507641d6a1b5d9b2d888a7526581b7de48540ebf86ce00579908size 1342693123version https://git-lfs.github.com/spec/v1oid sha256:68c84805ea17d0697cd79bc85394754d70fb02f740db4bee4c6ccbb5269a5d84size 13426938 unmodified lines91011121314158 unmodified linespinned: falselicense: cc-by-sa-4.0short_description: Edit audios with text prompts---The 30-second limit was introduced to ensure that queue wait times remain reasonable, especially when there are a lot of users.For that reason pull-requests that change this limit will not be merged. Please clone or duplicate the space to work locally without limits.8 unmodified lines91011121314151617188 unmodified linespinned: falselicense: cc-by-sa-4.0short_description: Edit audios with text promptshf_oauth: truehf_oauth_scopes:- read-repos---The 30-second limit was introduced to ensure that queue wait times remain reasonable, especially when there are a lot of users.For that reason pull-requests that change this limit will not be merged. Please clone or duplicate the space to work locally without limits.
5 unmodified lines678910111213141516171819202122232425262728293031323 unmodified lines363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231245 unmodified lines1301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302311 unmodified line23323423523623723823924024124224324424524624724824925025125225325412 unmodified lines2672682692702712722732742752762772782792802812822832842852862872882893 unmodified lines2932942952962972982993003013023033043053063073083091 unmodified line3113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673685 unmodified linesimport gradio as grimport randomimport torchfrom torch import inference_mode# from tempfile import NamedTemporaryFilefrom typing import Optionalimport numpy as npfrom models import load_modelimport utilsimport spacesfrom inversion_utils import inversion_forward_process, inversion_reverse_process# current_loaded_model = "cvssp/audioldm2-music"# # current_loaded_model = "cvssp/audioldm2-music"# ldm_stable = load_model(current_loaded_model, device, 200) # deafult modelLDM2 = "cvssp/audioldm2"MUSIC = "cvssp/audioldm2-music"LDM2_LARGE = "cvssp/audioldm2-large"device = torch.device("cuda" if torch.cuda.is_available() else "cpu")ldm2 = load_model(model_id=LDM2, device=device)ldm2_large = load_model(model_id=LDM2_LARGE, device=device)ldm2_music = load_model(model_id=MUSIC, device=device)def randomize_seed_fn(seed, randomize_seed):3 unmodified linesreturn seeddef invert(ldm_stable, x0, prompt_src, num_diffusion_steps, cfg_scale_src): # , ldm_stable):# ldm_stable.model.scheduler.set_timesteps(num_diffusion_steps, device=device)with inference_mode():w0 = ldm_stable.vae_encode(x0)# find Zs and wts - forward process_, zs, wts = inversion_forward_process(ldm_stable, w0, etas=1,prompts=[prompt_src],cfg_scales=[cfg_scale_src],prog_bar=True,num_inference_steps=num_diffusion_steps,numerical_fix=True)return zs, wtsdef sample(ldm_stable, zs, wts, steps, prompt_tar, tstart, cfg_scale_tar): # , ldm_stable):# reverse process (via Zs and wT)tstart = torch.tensor(tstart, dtype=torch.int)skip = steps - tstartw0, _ = inversion_reverse_process(ldm_stable, xT=wts, skips=steps - skip,etas=1., prompts=[prompt_tar],neg_prompts=[""], cfg_scales=[cfg_scale_tar],prog_bar=True,zs=zs[:int(steps - skip)])# vae decode imagewith inference_mode():x0_dec = ldm_stable.vae_decode(w0)if x0_dec.dim() < 4:x0_dec = x0_dec[None, :, :, :]with torch.no_grad():audio = ldm_stable.decode_to_mel(x0_dec)return (16000, audio.squeeze().cpu().numpy())def get_duration(input_audio, model_id: str, do_inversion: bool,wts: Optional[torch.Tensor], zs: Optional[torch.Tensor],saved_inv_model: str, source_prompt="", target_prompt="",steps=200, cfg_scale_src=3.5, cfg_scale_tar=12, t_start=45, randomize_seed=True):if model_id == LDM2:factor = 0.8elif model_id == LDM2_LARGE:factor = 1.5else: # MUSICfactor = 1mult = 0if do_inversion or randomize_seed:mult = stepsif input_audio is None:raise gr.Error('Input audio missing!')duration = min(utils.get_duration(input_audio), 30)time_per_iter_of_full = factor * ((t_start /100 * steps)*2 + mult) * 0.25print('expected time:', time_per_iter_of_full / 30 * duration)return max(15, time_per_iter_of_full / 30 * duration)@spaces.GPU(duration=get_duration)def edit(# cache_dir,input_audio,model_id: str,do_inversion: bool,# wtszs_file: str,wts: Optional[torch.Tensor], zs: Optional[torch.Tensor],saved_inv_model: str,source_prompt="",target_prompt="",steps=200,cfg_scale_src=3.5,cfg_scale_tar=12,t_start=45,randomize_seed=True):print(model_id)if model_id == LDM2:ldm_stable = ldm2elif model_id == LDM2_LARGE:ldm_stable = ldm2_largeelse: # MUSICldm_stable = ldm2_music5 unmodified linesif input_audio is None:raise gr.Error('Input audio missing!')x0 = utils.load_audio(input_audio, ldm_stable.get_fn_STFT(), device=device)# if not (do_inversion or randomize_seed):# if not os.path.exists(wtszs_file):# do_inversion = True# Too much time has passedif wts is None or zs is None:do_inversion = Trueif do_inversion or randomize_seed: # always re-run inversionzs_tensor, wts_tensor = invert(ldm_stable=ldm_stable, x0=x0, prompt_src=source_prompt,num_diffusion_steps=steps,cfg_scale_src=cfg_scale_src)# f = NamedTemporaryFile("wb", dir=cache_dir, suffix=".pth", delete=False)# torch.save({'wts': wts_tensor, 'zs': zs_tensor}, f.name)# wtszs_file = f.name# wtszs_file = gr.State(value=f.name)# wts = gr.State(value=wts_tensor)wts = wts_tensorzs = zs_tensor# zs = gr.State(value=zs_tensor)# demo.move_resource_to_block_cache(f.name)saved_inv_model = model_iddo_inversion = Falseelse:# wtszs = torch.load(wtszs_file, map_location=device)# # wtszs = torch.load(wtszs_file.f, map_location=device)# wts_tensor = wtszs['wts']# zs_tensor = wtszs['zs']wts_tensor = wts.to(device)zs_tensor = zs.to(device)# make sure t_start is in the right limit# t_start = change_tstart_range(t_start, steps)output = sample(ldm_stable, zs_tensor, wts_tensor, steps, prompt_tar=target_prompt,tstart=int(t_start / 100 * steps), cfg_scale_tar=cfg_scale_tar)return output, wts.cpu(), zs.cpu(), saved_inv_model, do_inversion# return output, wtszs_file, saved_inv_model, do_inversiondef get_example():case = [['Examples/Beethoven.wav','','A recording of an arcade game soundtrack.',45,'cvssp/audioldm2-music','27s','Examples/Beethoven_arcade.wav',],['Examples/Beethoven.wav','A high quality recording of wind instruments and strings playing.','A high quality recording of a piano playing.',45,'cvssp/audioldm2-music','27s','Examples/Beethoven_piano.wav',],['Examples/ModalJazz.wav','Trumpets playing alongside a piano, bass and drums in an upbeat old-timey cool jazz song.','A banjo playing alongside a piano, bass and drums in an upbeat old-timey cool country song.',45,'cvssp/audioldm2-music','106s','Examples/ModalJazz_banjo.wav',],['Examples/Cat.wav','','A dog barking.',75,'cvssp/audioldm2-large','10s','Examples/Cat_dog.wav',]]return caseintro = """<h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;"> ZETA Editing 🎧 </h1><h2 style="font-weight: 1400; text-align: center; margin-bottom: 7px;"> Zero-Shot Text-Based Audio Editing Using DDPM Inversion 🎛️ </h2><h3 style="margin-bottom: 10px; text-align: center;"><a href="https://arxiv.org/abs/2402.10009">[Paper]</a> | <a href="https://hilamanor.github.io/AudioEditing/">[Project page]</a> | <a href="https://github.com/HilaManor/AudioEditingCode">[Code]</a></h3><p style="font-size: 0.9rem; margin: 0rem; line-height: 1.2em; margin-top:1em">For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<a href="https://huggingface.co/spaces/hilamanor/audioEditing?duplicate=true"><img style="margin-top: 0em; margin-bottom: 0em; display:inline" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" ></a></p>"""help = """<div style="font-size:medium"><b>Instructions:</b><br>1 unmodified line<li>You must provide an input audio and a target prompt to edit the audio. </li><li>T<sub>start</sub> is used to control the tradeoff between fidelity to the original signal and text-adhearance.Lower value -> favor fidelity. Higher value -> apply a stronger edit.</li><li>Make sure that you use an AudioLDM2 version that is suitable for your input audio.For example, use the music version for music and the large version for general audio.</li><li>You can additionally provide a source prompt to guide even further the editing process.</li><li>Longer input will take more time.</li><li><strong>Unlimited length</strong>: This space automatically trims input audio to a maximum length of 30 seconds.For unlimited length, duplicated the space, and remove the trimming by changing the code.Specifically, in the <code style="display:inline; background-color: lightgrey; ">load_audio</code> function in the <code style="display:inline; background-color: lightgrey; ">utils.py</code> file,change <code style="display:inline; background-color: lightgrey; ">duration = min(audioldm.utils.get_duration(audio_path), 30)</code> to<code style="display:inline; background-color: lightgrey; ">duration = audioldm.utils.get_duration(audio_path)</code>.</ul></div>"""with gr.Blocks(css='style.css') as demo: #, delete_cache=(3600, 3600)) as demo:def reset_do_inversion(do_inversion_user, do_inversion):# do_inversion = gr.State(value=True)do_inversion = True12 unmodified linesreturn do_inversion_user, do_inversiongr.HTML(intro)wts = gr.State()zs = gr.State()wtszs = gr.State()# cache_dir = gr.State(demo.GRADIO_CACHE)saved_inv_model = gr.State()# current_loaded_model = gr.State(value="cvssp/audioldm2-music")# ldm_stable = load_model("cvssp/audioldm2-music", device, 200)# ldm_stable = gr.State(value=ldm_stable)do_inversion = gr.State(value=True) # To save some runtime when editing the same thing over and overdo_inversion_user = gr.State(value=False)with gr.Group():gr.Markdown("💡 **note**: input longer than **30 sec** is automatically trimmed (for unlimited input, see the Help section below)")with gr.Row():input_audio = gr.Audio(sources=["upload", "microphone"], type="filepath", editable=True, label="Input Audio",interactive=True, scale=1)output_audio = gr.Audio(label="Edited Audio", interactive=False, scale=1)with gr.Row():tar_prompt = gr.Textbox(label="Prompt", info="Describe your desired edited output",3 unmodified lineswith gr.Row():t_start = gr.Slider(minimum=15, maximum=85, value=45, step=1, label="T-start (%)", interactive=True, scale=3,info="Lower T-start -> closer to original audio. Higher T-start -> stronger edit.")# model_id = gr.Radio(label="AudioLDM2 Version",model_id = gr.Dropdown(label="AudioLDM2 Version",choices=["cvssp/audioldm2","cvssp/audioldm2-large","cvssp/audioldm2-music"],info="Choose a checkpoint suitable for your intended audio and edit",value="cvssp/audioldm2-music", interactive=True, type="value", scale=2)with gr.Row():with gr.Column():submit = gr.Button("Edit")with gr.Accordion("More Options", open=False):with gr.Row():1 unmodified lineinfo="Optional: Describe the original audio input",placeholder="A recording of a happy upbeat classical music piece",)with gr.Row():cfg_scale_src = gr.Number(value=3, minimum=0.5, maximum=25, precision=None,label="Source Guidance Scale", interactive=True, scale=1)cfg_scale_tar = gr.Number(value=12, minimum=0.5, maximum=25, precision=None,label="Target Guidance Scale", interactive=True, scale=1)steps = gr.Number(value=50, step=1, minimum=20, maximum=300,info="Higher values (e.g. 200) yield higher-quality generation.",label="Num Diffusion Steps", interactive=True, scale=1)with gr.Row():seed = gr.Number(value=0, precision=0, label="Seed", interactive=True)randomize_seed = gr.Checkbox(label='Randomize seed', value=False)length = gr.Number(label="Length", interactive=False, visible=False)with gr.Accordion("Help💡", open=False):gr.HTML(help)submit.click(fn=randomize_seed_fn,inputs=[seed, randomize_seed],outputs=[seed], queue=False).then(fn=clear_do_inversion_user, inputs=[do_inversion_user], outputs=[do_inversion_user]).then(fn=edit,inputs=[#cache_dir,input_audio,model_id,do_inversion,# current_loaded_model, ldm_stable,wts, zs,# wtszs,saved_inv_model,src_prompt,tar_prompt,steps,cfg_scale_src,cfg_scale_tar,t_start,randomize_seed],outputs=[output_audio, wts, zs, # wtszs,saved_inv_model, do_inversion] # , current_loaded_model, ldm_stable],).then(post_match_do_inversion, inputs=[do_inversion_user, do_inversion], outputs=[do_inversion_user, do_inversion]).then(lambda x: (demo.temp_file_sets.append(set([str(gr.utils.abspath(x))])) if type(x) is str else None),inputs=wtszs)# demo.move_resource_to_block_cache(wtszs.value)# If sources changed we have to rerun inversioninput_audio.change(fn=reset_do_inversion, inputs=[do_inversion_user, do_inversion], outputs=[do_inversion_user, do_inversion])src_prompt.change(fn=reset_do_inversion, inputs=[do_inversion_user, do_inversion], outputs=[do_inversion_user, do_inversion])model_id.change(fn=reset_do_inversion, inputs=[do_inversion_user, do_inversion], outputs=[do_inversion_user, do_inversion])cfg_scale_src.change(fn=reset_do_inversion, inputs=[do_inversion_user, do_inversion], outputs=[do_inversion_user, do_inversion])steps.change(fn=reset_do_inversion, inputs=[do_inversion_user, do_inversion], outputs=[do_inversion_user, do_inversion])gr.Examples(label="Examples",5 unmodified lines6789101112131415161718192021222324252627282930313 unmodified lines35363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691705 unmodified lines1761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003011 unmodified line30330430530630730830931031131231331431531631731831932032132232332432532632732832912 unmodified lines3423433443453463473483493503513523533543553563573583593603613623633 unmodified lines3673683693703713723733743753763773783793803813821 unmodified line3843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444455 unmodified linesimport gradio as grimport randomimport torchimport osfrom torch import inference_modefrom typing import Optional, Listimport numpy as npfrom models import load_modelimport utilsimport spacesimport huggingface_hubfrom inversion_utils import inversion_forward_process, inversion_reverse_processLDM2 = "cvssp/audioldm2"MUSIC = "cvssp/audioldm2-music"LDM2_LARGE = "cvssp/audioldm2-large"STABLEAUD = "stabilityai/stable-audio-open-1.0"device = torch.device("cuda" if torch.cuda.is_available() else "cpu")ldm2 = load_model(model_id=LDM2, device=device)ldm2_large = load_model(model_id=LDM2_LARGE, device=device)ldm2_music = load_model(model_id=MUSIC, device=device)ldm_stableaud = load_model(model_id=STABLEAUD, device=device, token=os.getenv('PRIV_TOKEN'))def randomize_seed_fn(seed, randomize_seed):3 unmodified linesreturn seeddef invert(ldm_stable, x0, prompt_src, num_diffusion_steps, cfg_scale_src, duration, save_compute):# ldm_stable.model.scheduler.set_timesteps(num_diffusion_steps, device=device)with inference_mode():w0 = ldm_stable.vae_encode(x0)# find Zs and wts - forward process_, zs, wts, extra_info = inversion_forward_process(ldm_stable, w0, etas=1,prompts=[prompt_src],cfg_scales=[cfg_scale_src],num_inference_steps=num_diffusion_steps,numerical_fix=True,duration=duration,save_compute=save_compute)return zs, wts, extra_infodef sample(ldm_stable, zs, wts, extra_info, prompt_tar, tstart, cfg_scale_tar, duration, save_compute):# reverse process (via Zs and wT)tstart = torch.tensor(tstart, dtype=torch.int)w0, _ = inversion_reverse_process(ldm_stable, xT=wts, tstart=tstart,etas=1., prompts=[prompt_tar],neg_prompts=[""], cfg_scales=[cfg_scale_tar],zs=zs[:int(tstart)],duration=duration,extra_info=extra_info,save_compute=save_compute)# vae decode imagewith inference_mode():x0_dec = ldm_stable.vae_decode(w0)if 'stable-audio' not in ldm_stable.model_id:if x0_dec.dim() < 4:x0_dec = x0_dec[None, :, :, :]with torch.no_grad():audio = ldm_stable.decode_to_mel(x0_dec)else:audio = x0_dec.squeeze(0).Treturn (ldm_stable.get_sr(), audio.squeeze().cpu().numpy())def get_duration(input_audio,model_id: str,do_inversion: bool,wts: Optional[torch.Tensor], zs: Optional[torch.Tensor], extra_info: Optional[List],saved_inv_model: str,source_prompt: str = "",target_prompt: str = "",steps: int = 200,cfg_scale_src: float = 3.5,cfg_scale_tar: float = 12,t_start: int = 45,randomize_seed: bool = True,save_compute: bool = True,oauth_token: Optional[gr.OAuthToken] = None):if model_id == LDM2:factor = 1elif model_id == LDM2_LARGE:factor = 2.5elif model_id == STABLEAUD:factor = 3.2else: # MUSICfactor = 1forwards = 0if do_inversion or randomize_seed:forwards = steps if source_prompt == "" else steps * 2 # x2 when there is a prompt textforwards += int(t_start / 100 * steps) * 2duration = min(utils.get_duration(input_audio), utils.MAX_DURATION)time_for_maxlength = factor * forwards * 0.15 # 0.25 is the time per forward passprint('expected time:', time_for_maxlength / utils.MAX_DURATION * duration)spare_time = 5return max(10, time_for_maxlength / utils.MAX_DURATION * duration + spare_time)def verify_model_params(model_id: str, input_audio, src_prompt: str, tar_prompt: str, cfg_scale_src: float,oauth_token: gr.OAuthToken | None):if input_audio is None:raise gr.Error('Input audio missing!')if tar_prompt == "":raise gr.Error("Please provide a target prompt to edit the audio.")if src_prompt != "":if model_id == STABLEAUD and cfg_scale_src != 1:gr.Info("Consider using Source Guidance Scale=1 for Stable Audio Open 1.0.")elif model_id != STABLEAUD and cfg_scale_src != 3:gr.Info(f"Consider using Source Guidance Scale=3 for {model_id}.")if model_id == STABLEAUD:if oauth_token is None:raise gr.Error("You must be logged in to use Stable Audio Open 1.0. Please log in and try again.")try:huggingface_hub.get_hf_file_metadata(huggingface_hub.hf_hub_url(STABLEAUD, 'transformer/config.json'),token=oauth_token.token)print('Has Access')# except huggingface_hub.utils._errors.GatedRepoError:except huggingface_hub.errors.GatedRepoError:raise gr.Error("You need to accept the license agreement to use Stable Audio Open 1.0. ""Visit the <a href='https://huggingface.co/stabilityai/stable-audio-open-1.0'>""model page</a> to get access.")@spaces.GPU(duration=get_duration)def edit(input_audio,model_id: str,do_inversion: bool,wts: Optional[torch.Tensor], zs: Optional[torch.Tensor], extra_info: Optional[List],saved_inv_model: str,source_prompt: str = "",target_prompt: str = "",steps: int = 200,cfg_scale_src: float = 3.5,cfg_scale_tar: float = 12,t_start: int = 45,randomize_seed: bool = True,save_compute: bool = True,oauth_token: Optional[gr.OAuthToken] = None):print(model_id)if model_id == LDM2:ldm_stable = ldm2elif model_id == LDM2_LARGE:ldm_stable = ldm2_largeelif model_id == STABLEAUD:ldm_stable = ldm_stableaudelse: # MUSICldm_stable = ldm2_music5 unmodified linesif input_audio is None:raise gr.Error('Input audio missing!')x0, _, duration = utils.load_audio(input_audio, ldm_stable.get_fn_STFT(), device=device,stft=('stable-audio' not in ldm_stable.model_id), model_sr=ldm_stable.get_sr())if wts is None or zs is None:do_inversion = Trueif do_inversion or randomize_seed: # always re-run inversionzs_tensor, wts_tensor, extra_info_list = invert(ldm_stable=ldm_stable, x0=x0, prompt_src=source_prompt,num_diffusion_steps=steps,cfg_scale_src=cfg_scale_src,duration=duration,save_compute=save_compute)wts = wts_tensorzs = zs_tensorextra_info = extra_info_listsaved_inv_model = model_iddo_inversion = Falseelse:wts_tensor = wts.to(device)zs_tensor = zs.to(device)extra_info_list = [e.to(device) for e in extra_info if e is not None]output = sample(ldm_stable, zs_tensor, wts_tensor, extra_info_list, prompt_tar=target_prompt,tstart=int(t_start / 100 * steps), cfg_scale_tar=cfg_scale_tar, duration=duration,save_compute=save_compute)return output, wts.cpu(), zs.cpu(), [e.cpu() for e in extra_info if e is not None], saved_inv_model, do_inversion# return output, wtszs_file, saved_inv_model, do_inversiondef get_example():case = [['Examples/Beethoven.mp3','','A recording of an arcade game soundtrack.',45,'cvssp/audioldm2-music','27s','Examples/Beethoven_arcade.mp3',],['Examples/Beethoven.mp3','A high quality recording of wind instruments and strings playing.','A high quality recording of a piano playing.',45,'cvssp/audioldm2-music','27s','Examples/Beethoven_piano.mp3',],['Examples/Beethoven.mp3','','Heavy Rock.',40,'stabilityai/stable-audio-open-1.0','27s','Examples/Beethoven_rock.mp3',],['Examples/ModalJazz.mp3','Trumpets playing alongside a piano, bass and drums in an upbeat old-timey cool jazz song.','A banjo playing alongside a piano, bass and drums in an upbeat old-timey cool country song.',45,'cvssp/audioldm2-music','106s','Examples/ModalJazz_banjo.mp3',],['Examples/Shadows.mp3','','8-bit arcade game soundtrack.',40,'stabilityai/stable-audio-open-1.0','34s','Examples/Shadows_arcade.mp3',],['Examples/Cat.mp3','','A dog barking.',75,'cvssp/audioldm2-large','10s','Examples/Cat_dog.mp3',]]return caseintro = """<h1 style="font-weight: 1000; text-align: center; margin: 0px;"> ZETA Editing 🎧 </h1><h2 style="font-weight: 1000; text-align: center; margin: 0px;">Zero-Shot Text-Based Audio Editing Using DDPM Inversion 🎛️ </h2><h3 style="margin-top: 0px; margin-bottom: 10px; text-align: center;"><a href="https://arxiv.org/abs/2402.10009">[Paper]</a> | <a href="https://hilamanor.github.io/AudioEditing/">[Project page]</a> | <a href="https://github.com/HilaManor/AudioEditingCode">[Code]</a></h3><p style="font-size: 1rem; line-height: 1.2em;">For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<a href="https://huggingface.co/spaces/hilamanor/audioEditing?duplicate=true"><img style="margin-top: 0em; margin-bottom: 0em; display:inline" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" ></a></p><p style="margin: 0px;"><b>NEW - 15.10.24:</b> You can now edit using <b>Stable Audio Open 1.0</b>.You must be <b>logged in</b> after accepting the<b><a href="https://huggingface.co/stabilityai/stable-audio-open-1.0">license agreement</a></b> to use it.</br></p><ul style="padding-left:40px; line-height:normal;"><li style="margin: 0px;">Prompts behave differently - e.g.,try "8-bit arcade" directly instead of "a recording of...". Check out the new examples below!</li><li style="margin: 0px;">Try to play around <code>T-start=40%</code>.</li><li style="margin: 0px;">Under "More Options": Use <code>Source Guidance Scale=1</code>,and you can try fewer timesteps (even 20!).</li><li style="margin: 0px;">Stable Audio Open is a general-audio model.For better music editing, duplicate the space and change to a<a href="https://huggingface.co/models?other=base_model:finetune:stabilityai/stable-audio-open-1.0">fine-tuned model for music</a>.</li></ul><p><b>NEW - 15.10.24:</b> Parallel editing is enabled by default.To disable, uncheck <code>Efficient editing</code> under "More Options".Saves a bit of time.</p>"""help = """<div style="font-size:medium"><b>Instructions:</b><br>1 unmodified line<li>You must provide an input audio and a target prompt to edit the audio. </li><li>T<sub>start</sub> is used to control the tradeoff between fidelity to the original signal and text-adhearance.Lower value -> favor fidelity. Higher value -> apply a stronger edit.</li><li>Make sure that you use a model version that is suitable for your input audio.For example, use AudioLDM2-music for music while AudioLDM2-large for general audio.</li><li>You can additionally provide a source prompt to guide even further the editing process.</li><li>Longer input will take more time.</li><li><strong>Unlimited length</strong>: This space automatically trims input audio to a maximum length of 30 seconds.For unlimited length, duplicated the space, and change the<code style="display:inline; background-color: lightgrey;">MAX_DURATION</code> parameterinside <code style="display:inline; background-color: lightgrey;">utils.py</code>to <code style="display:inline; background-color: lightgrey;">None</code>.</li></ul></div>"""css = '.gradio-container {max-width: 1000px !important; padding-top: 1.5rem !important;}' \'.audio-upload .wrap {min-height: 0px;}'# with gr.Blocks(css='style.css') as demo:with gr.Blocks(css=css) as demo:def reset_do_inversion(do_inversion_user, do_inversion):# do_inversion = gr.State(value=True)do_inversion = True12 unmodified linesreturn do_inversion_user, do_inversiongr.HTML(intro)wts = gr.State()zs = gr.State()extra_info = gr.State()saved_inv_model = gr.State()do_inversion = gr.State(value=True) # To save some runtime when editing the same thing over and overdo_inversion_user = gr.State(value=False)with gr.Group():gr.Markdown("💡 **note**: input longer than **30 sec** is automatically trimmed ""(for unlimited input, see the Help section below)")with gr.Row(equal_height=True):input_audio = gr.Audio(sources=["upload", "microphone"], type="filepath",editable=True, label="Input Audio", interactive=True, scale=1, format='wav',elem_classes=['audio-upload'])output_audio = gr.Audio(label="Edited Audio", interactive=False, scale=1, format='wav')with gr.Row():tar_prompt = gr.Textbox(label="Prompt", info="Describe your desired edited output",3 unmodified lineswith gr.Row():t_start = gr.Slider(minimum=15, maximum=85, value=45, step=1, label="T-start (%)", interactive=True, scale=3,info="Lower T-start -> closer to original audio. Higher T-start -> stronger edit.")model_id = gr.Dropdown(label="Model Version",choices=[LDM2,LDM2_LARGE,MUSIC,STABLEAUD],info="Choose a checkpoint suitable for your audio and edit",value="cvssp/audioldm2-music", interactive=True, type="value", scale=2)with gr.Row():submit = gr.Button("Edit", variant="primary", scale=3)gr.LoginButton(value="Login to HF (For Stable Audio)", scale=1)with gr.Accordion("More Options", open=False):with gr.Row():1 unmodified lineinfo="Optional: Describe the original audio input",placeholder="A recording of a happy upbeat classical music piece",)with gr.Row(equal_height=True):cfg_scale_src = gr.Number(value=3, minimum=0.5, maximum=25, precision=None,label="Source Guidance Scale", interactive=True, scale=1)cfg_scale_tar = gr.Number(value=12, minimum=0.5, maximum=25, precision=None,label="Target Guidance Scale", interactive=True, scale=1)steps = gr.Number(value=50, step=1, minimum=10, maximum=300,info="Higher values (e.g. 200) yield higher-quality generation.",label="Num Diffusion Steps", interactive=True, scale=2)with gr.Row(equal_height=True):seed = gr.Number(value=0, precision=0, label="Seed", interactive=True)randomize_seed = gr.Checkbox(label='Randomize seed', value=False)save_compute = gr.Checkbox(label='Efficient editing', value=True)length = gr.Number(label="Length", interactive=False, visible=False)with gr.Accordion("Help💡", open=False):gr.HTML(help)submit.click(fn=verify_model_params,inputs=[model_id, input_audio, src_prompt, tar_prompt, cfg_scale_src],outputs=[]).success(fn=randomize_seed_fn, inputs=[seed, randomize_seed], outputs=[seed], queue=False).then(fn=clear_do_inversion_user, inputs=[do_inversion_user], outputs=[do_inversion_user]).then(fn=edit,inputs=[input_audio,model_id,do_inversion,wts, zs, extra_info,saved_inv_model,src_prompt,tar_prompt,steps,cfg_scale_src,cfg_scale_tar,t_start,randomize_seed,save_compute,],outputs=[output_audio, wts, zs, extra_info, saved_inv_model, do_inversion]).success(fn=post_match_do_inversion,inputs=[do_inversion_user, do_inversion],outputs=[do_inversion_user, do_inversion])# If sources changed we have to rerun inversiongr.on(triggers=[input_audio.change, src_prompt.change, model_id.change, cfg_scale_src.change,steps.change, save_compute.change],fn=reset_do_inversion,inputs=[do_inversion_user, do_inversion],outputs=[do_inversion_user, do_inversion])gr.Examples(label="Examples",
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403412 unmodified lines344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450import torchfrom tqdm import tqdm# from torchvision import transforms as Tfrom typing import List, Optional, Dict, Unionfrom models import PipelineWrapperdef mu_tilde(model, xt, x0, timestep):"mu_tilde(x_t, x_0) DDPM paper eq. 7"prev_timestep = timestep - model.scheduler.config.num_train_timesteps // model.scheduler.num_inference_stepsalpha_prod_t_prev = model.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 \else model.scheduler.final_alpha_cumprodalpha_t = model.scheduler.alphas[timestep]beta_t = 1 - alpha_talpha_bar = model.scheduler.alphas_cumprod[timestep]return ((alpha_prod_t_prev ** 0.5 * beta_t) / (1-alpha_bar)) * x0 + \((alpha_t**0.5 * (1-alpha_prod_t_prev)) / (1 - alpha_bar)) * xtdef sample_xts_from_x0(model, x0, num_inference_steps=50, x_prev_mode=False):"""Samples from P(x_1:T|x_0)"""# torch.manual_seed(43256465436)alpha_bar = model.model.scheduler.alphas_cumprodsqrt_one_minus_alpha_bar = (1-alpha_bar) ** 0.5alphas = model.model.scheduler.alphas# betas = 1 - alphasvariance_noise_shape = (num_inference_steps + 1,model.model.unet.config.in_channels,# model.unet.sample_size,# model.unet.sample_size)x0.shape[-2],x0.shape[-1])timesteps = model.model.scheduler.timesteps.to(model.device)t_to_idx = {int(v): k for k, v in enumerate(timesteps)}xts = torch.zeros(variance_noise_shape).to(x0.device)xts[0] = x0x_prev = x0for t in reversed(timesteps):# idx = t_to_idx[int(t)]idx = num_inference_steps-t_to_idx[int(t)]if x_prev_mode:xts[idx] = x_prev * (alphas[t] ** 0.5) + torch.randn_like(x0) * ((1-alphas[t]) ** 0.5)x_prev = xts[idx].clone()else:xts[idx] = x0 * (alpha_bar[t] ** 0.5) + torch.randn_like(x0) * sqrt_one_minus_alpha_bar[t]# xts = torch.cat([xts, x0 ],dim = 0)return xtsdef forward_step(model, model_output, timestep, sample):next_timestep = min(model.scheduler.config.num_train_timesteps - 2,timestep + model.scheduler.config.num_train_timesteps // model.scheduler.num_inference_steps)# 2. compute alphas, betasalpha_prod_t = model.scheduler.alphas_cumprod[timestep]# alpha_prod_t_next = self.scheduler.alphas_cumprod[next_timestep] if next_ltimestep >= 0 \# else self.scheduler.final_alpha_cumprodbeta_prod_t = 1 - alpha_prod_t# 3. compute predicted original sample from predicted noise also called# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdfpred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)# 5. TODO: simple noising implementatiomnext_sample = model.scheduler.add_noise(pred_original_sample, model_output, torch.LongTensor([next_timestep]))return next_sampledef inversion_forward_process(model: PipelineWrapper,x0: torch.Tensor,etas: Optional[float] = None,prog_bar: bool = False,prompts: List[str] = [""],cfg_scales: List[float] = [3.5],num_inference_steps: int = 50,eps: Optional[float] = None,cutoff_points: Optional[List[float]] = None,numerical_fix: bool = False,extract_h_space: bool = False,extract_skipconns: bool = False,x_prev_mode: bool = False):if len(prompts) > 1 and extract_h_space:raise NotImplementedError("How do you split cfg_scales for hspace? TODO")if len(prompts) > 1 or prompts[0] != "":text_embeddings_hidden_states, text_embeddings_class_labels, \text_embeddings_boolean_prompt_mask = model.encode_text(prompts)# text_embeddings = encode_text(model, prompt)# # classifier free guidancebatch_size = len(prompts)cfg_scales_tensor = torch.ones((batch_size, *x0.shape[1:]), device=model.device, dtype=x0.dtype)# if len(prompts) > 1:# if cutoff_points is None:# cutoff_points = [i * 1 / batch_size for i in range(1, batch_size)]# if len(cfg_scales) == 1:# cfg_scales *= batch_size# elif len(cfg_scales) < batch_size:# raise ValueError("Not enough target CFG scales")# cutoff_points = [int(x * cfg_scales_tensor.shape[2]) for x in cutoff_points]# cutoff_points = [0, *cutoff_points, cfg_scales_tensor.shape[2]]# for i, (start, end) in enumerate(zip(cutoff_points[:-1], cutoff_points[1:])):# cfg_scales_tensor[i, :, end:] = 0# cfg_scales_tensor[i, :, :start] = 0# cfg_scales_tensor[i] *= cfg_scales[i]# if prompts[i] == "":# cfg_scales_tensor[i] = 0# cfg_scales_tensor = T.functional.gaussian_blur(cfg_scales_tensor, kernel_size=15, sigma=1)# else:cfg_scales_tensor *= cfg_scales[0]uncond_embedding_hidden_states, uncond_embedding_class_lables, uncond_boolean_prompt_mask = model.encode_text([""])# uncond_embedding = encode_text(model, "")timesteps = model.model.scheduler.timesteps.to(model.device)variance_noise_shape = (num_inference_steps,model.model.unet.config.in_channels,# model.unet.sample_size,# model.unet.sample_size)x0.shape[-2],x0.shape[-1])if etas is None or (type(etas) in [int, float] and etas == 0):eta_is_zero = Truezs = Noneelse:eta_is_zero = Falseif type(etas) in [int, float]:etas = [etas]*model.model.scheduler.num_inference_stepsxts = sample_xts_from_x0(model, x0, num_inference_steps=num_inference_steps, x_prev_mode=x_prev_mode)alpha_bar = model.model.scheduler.alphas_cumprodzs = torch.zeros(size=variance_noise_shape, device=model.device)hspaces = []skipconns = []t_to_idx = {int(v): k for k, v in enumerate(timesteps)}xt = x0# op = tqdm(reversed(timesteps)) if prog_bar else reversed(timesteps)op = tqdm(timesteps) if prog_bar else timestepsfor t in op:# idx = t_to_idx[int(t)]idx = num_inference_steps - t_to_idx[int(t)] - 1# 1. predict noise residualif not eta_is_zero:xt = xts[idx+1][None]with torch.no_grad():out, out_hspace, out_skipconns = model.unet_forward(xt, timestep=t,encoder_hidden_states=uncond_embedding_hidden_states,class_labels=uncond_embedding_class_lables,encoder_attention_mask=uncond_boolean_prompt_mask)# out = model.unet.forward(xt, timestep= t, encoder_hidden_states=uncond_embedding)if len(prompts) > 1 or prompts[0] != "":cond_out, cond_out_hspace, cond_out_skipconns = model.unet_forward(xt.expand(len(prompts), -1, -1, -1), timestep=t,encoder_hidden_states=text_embeddings_hidden_states,class_labels=text_embeddings_class_labels,encoder_attention_mask=text_embeddings_boolean_prompt_mask)# cond_out = model.unet.forward(xt, timestep=t, encoder_hidden_states = text_embeddings)if len(prompts) > 1 or prompts[0] != "":# # classifier free guidancenoise_pred = out.sample + \(cfg_scales_tensor * (cond_out.sample - out.sample.expand(batch_size, -1, -1, -1))).sum(axis=0).unsqueeze(0)if extract_h_space or extract_skipconns:noise_h_space = out_hspace + cfg_scales[0] * (cond_out_hspace - out_hspace)if extract_skipconns:noise_skipconns = {k: [out_skipconns[k][j] + cfg_scales[0] *(cond_out_skipconns[k][j] - out_skipconns[k][j])for j in range(len(out_skipconns[k]))]for k in out_skipconns}else:noise_pred = out.sampleif extract_h_space or extract_skipconns:noise_h_space = out_hspaceif extract_skipconns:noise_skipconns = out_skipconnsif extract_h_space or extract_skipconns:hspaces.append(noise_h_space)if extract_skipconns:skipconns.append(noise_skipconns)if eta_is_zero:# 2. compute more noisy image and set x_t -> x_t+1xt = forward_step(model.model, noise_pred, t, xt)else:# xtm1 = xts[idx+1][None]xtm1 = xts[idx][None]# pred of x0if model.model.scheduler.config.prediction_type == 'epsilon':pred_original_sample = (xt - (1 - alpha_bar[t]) ** 0.5 * noise_pred) / alpha_bar[t] ** 0.5elif model.model.scheduler.config.prediction_type == 'v_prediction':pred_original_sample = (alpha_bar[t] ** 0.5) * xt - ((1 - alpha_bar[t]) ** 0.5) * noise_pred# direction to xtprev_timestep = t - model.model.scheduler.config.num_train_timesteps // \model.model.scheduler.num_inference_stepsalpha_prod_t_prev = model.get_alpha_prod_t_prev(prev_timestep)variance = model.get_variance(t, prev_timestep)if model.model.scheduler.config.prediction_type == 'epsilon':radom_noise_pred = noise_predelif model.model.scheduler.config.prediction_type == 'v_prediction':radom_noise_pred = (alpha_bar[t] ** 0.5) * noise_pred + ((1 - alpha_bar[t]) ** 0.5) * xtpred_sample_direction = (1 - alpha_prod_t_prev - etas[idx] * variance) ** (0.5) * radom_noise_predmu_xt = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_directionz = (xtm1 - mu_xt) / (etas[idx] * variance ** 0.5)zs[idx] = z# correction to avoid error accumulationif numerical_fix:xtm1 = mu_xt + (etas[idx] * variance ** 0.5)*zxts[idx] = xtm1if zs is not None:# zs[-1] = torch.zeros_like(zs[-1])zs[0] = torch.zeros_like(zs[0])# zs_cycle[0] = torch.zeros_like(zs[0])if extract_h_space:hspaces = torch.concat(hspaces, axis=0)return xt, zs, xts, hspacesif extract_skipconns:hspaces = torch.concat(hspaces, axis=0)return xt, zs, xts, hspaces, skipconnsreturn xt, zs, xtsdef reverse_step(model, model_output, timestep, sample, eta=0, variance_noise=None):# 1. get previous step value (=t-1)prev_timestep = timestep - model.model.scheduler.config.num_train_timesteps // \model.model.scheduler.num_inference_steps# 2. compute alphas, betasalpha_prod_t = model.model.scheduler.alphas_cumprod[timestep]alpha_prod_t_prev = model.get_alpha_prod_t_prev(prev_timestep)beta_prod_t = 1 - alpha_prod_t# 3. compute predicted original sample from predicted noise also called# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdfif model.model.scheduler.config.prediction_type == 'epsilon':pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)elif model.model.scheduler.config.prediction_type == 'v_prediction':pred_original_sample = (alpha_prod_t ** 0.5) * sample - (beta_prod_t ** 0.5) * model_output# 5. compute variance: "sigma_t(η)" -> see formula (16)# σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1)# variance = self.scheduler._get_variance(timestep, prev_timestep)variance = model.get_variance(timestep, prev_timestep)# std_dev_t = eta * variance ** (0.5)# Take care of asymetric reverse process (asyrp)if model.model.scheduler.config.prediction_type == 'epsilon':model_output_direction = model_outputelif model.model.scheduler.config.prediction_type == 'v_prediction':model_output_direction = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample# 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf# pred_sample_direction = (1 - alpha_prod_t_prev - std_dev_t**2) ** (0.5) * model_output_directionpred_sample_direction = (1 - alpha_prod_t_prev - eta * variance) ** (0.5) * model_output_direction# 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdfprev_sample = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_direction# 8. Add noice if eta > 0if eta > 0:if variance_noise is None:variance_noise = torch.randn(model_output.shape, device=model.device)sigma_z = eta * variance ** (0.5) * variance_noiseprev_sample = prev_sample + sigma_zreturn prev_sampledef inversion_reverse_process(model: PipelineWrapper,xT: torch.Tensor,skips: torch.Tensor,fix_alpha: float = 0.1,etas: float = 0,prompts: List[str] = [""],neg_prompts: List[str] = [""],cfg_scales: Optional[List[float]] = None,prog_bar: bool = False,zs: Optional[List[torch.Tensor]] = None,# controller=None,cutoff_points: Optional[List[float]] = None,hspace_add: Optional[torch.Tensor] = None,hspace_replace: Optional[torch.Tensor] = None,skipconns_replace: Optional[Dict[int, torch.Tensor]] = None,zero_out_resconns: Optional[Union[int, List]] = None,asyrp: bool = False,extract_h_space: bool = False,extract_skipconns: bool = False):batch_size = len(prompts)text_embeddings_hidden_states, text_embeddings_class_labels, \text_embeddings_boolean_prompt_mask = model.encode_text(prompts)uncond_embedding_hidden_states, uncond_embedding_class_lables, \uncond_boolean_prompt_mask = model.encode_text(neg_prompts)# text_embeddings = encode_text(model, prompts)# uncond_embedding = encode_text(model, [""] * batch_size)masks = torch.ones((batch_size, *xT.shape[1:]), device=model.device, dtype=xT.dtype)cfg_scales_tensor = torch.ones((batch_size, *xT.shape[1:]), device=model.device, dtype=xT.dtype)# if batch_size > 1:# if cutoff_points is None:# cutoff_points = [i * 1 / batch_size for i in range(1, batch_size)]# if len(cfg_scales) == 1:# cfg_scales *= batch_size# elif len(cfg_scales) < batch_size:# raise ValueError("Not enough target CFG scales")# cutoff_points = [int(x * cfg_scales_tensor.shape[2]) for x in cutoff_points]# cutoff_points = [0, *cutoff_points, cfg_scales_tensor.shape[2]]# for i, (start, end) in enumerate(zip(cutoff_points[:-1], cutoff_points[1:])):# cfg_scales_tensor[i, :, end:] = 0# cfg_scales_tensor[i, :, :start] = 0# masks[i, :, end:] = 0# masks[i, :, :start] = 0# cfg_scales_tensor[i] *= cfg_scales[i]# cfg_scales_tensor = T.functional.gaussian_blur(cfg_scales_tensor, kernel_size=15, sigma=1)# masks = T.functional.gaussian_blur(masks, kernel_size=15, sigma=1)# else:cfg_scales_tensor *= cfg_scales[0]if etas is None:etas = 02 unmodified linesassert len(etas) == model.model.scheduler.num_inference_stepstimesteps = model.model.scheduler.timesteps.to(model.device)# xt = xT.expand(1, -1, -1, -1)xt = xT[skips.max()].unsqueeze(0)op = tqdm(timesteps[-zs.shape[0]:]) if prog_bar else timesteps[-zs.shape[0]:]t_to_idx = {int(v): k for k, v in enumerate(timesteps[-zs.shape[0]:])}hspaces = []skipconns = []for it, t in enumerate(op):# idx = t_to_idx[int(t)]idx = model.model.scheduler.num_inference_steps - t_to_idx[int(t)] - \(model.model.scheduler.num_inference_steps - zs.shape[0] + 1)# # Unconditional embeddingwith torch.no_grad():uncond_out, out_hspace, out_skipconns = model.unet_forward(xt, timestep=t,encoder_hidden_states=uncond_embedding_hidden_states,class_labels=uncond_embedding_class_lables,encoder_attention_mask=uncond_boolean_prompt_mask,mid_block_additional_residual=(None if hspace_add is None else(1 / (cfg_scales[0] + 1)) *(hspace_add[-zs.shape[0]:][it] if hspace_add.shape[0] > 1else hspace_add)),replace_h_space=(None if hspace_replace is None else(hspace_replace[-zs.shape[0]:][it].unsqueeze(0) if hspace_replace.shape[0] > 1else hspace_replace)),zero_out_resconns=zero_out_resconns,replace_skip_conns=(None if skipconns_replace is None else(skipconns_replace[-zs.shape[0]:][it] if len(skipconns_replace) > 1else skipconns_replace))) # encoder_hidden_states = uncond_embedding)# # Conditional embeddingif prompts:with torch.no_grad():cond_out, cond_out_hspace, cond_out_skipconns = model.unet_forward(xt.expand(batch_size, -1, -1, -1),timestep=t,encoder_hidden_states=text_embeddings_hidden_states,class_labels=text_embeddings_class_labels,encoder_attention_mask=text_embeddings_boolean_prompt_mask,mid_block_additional_residual=(None if hspace_add is None else(cfg_scales[0] / (cfg_scales[0] + 1)) *(hspace_add[-zs.shape[0]:][it] if hspace_add.shape[0] > 1else hspace_add)),replace_h_space=(None if hspace_replace is None else(hspace_replace[-zs.shape[0]:][it].unsqueeze(0) if hspace_replace.shape[0] > 1else hspace_replace)),zero_out_resconns=zero_out_resconns,replace_skip_conns=(None if skipconns_replace is None else(skipconns_replace[-zs.shape[0]:][it] if len(skipconns_replace) > 1else skipconns_replace))) # encoder_hidden_states = text_embeddings)z = zs[idx] if zs is not None else None# print(f'idx: {idx}')# print(f't: {t}')z = z.unsqueeze(0)# z = z.expand(batch_size, -1, -1, -1)if prompts:# # classifier free guidance# noise_pred = uncond_out.sample + cfg_scales_tensor * (cond_out.sample - uncond_out.sample)noise_pred = uncond_out.sample + \(cfg_scales_tensor * (cond_out.sample - uncond_out.sample.expand(batch_size, -1, -1, -1))).sum(axis=0).unsqueeze(0)if extract_h_space or extract_skipconns:noise_h_space = out_hspace + cfg_scales[0] * (cond_out_hspace - out_hspace)if extract_skipconns:noise_skipconns = {k: [out_skipconns[k][j] + cfg_scales[0] *(cond_out_skipconns[k][j] - out_skipconns[k][j])for j in range(len(out_skipconns[k]))]for k in out_skipconns}else:noise_pred = uncond_out.sampleif extract_h_space or extract_skipconns:noise_h_space = out_hspaceif extract_skipconns:noise_skipconns = out_skipconnsif extract_h_space or extract_skipconns:hspaces.append(noise_h_space)if extract_skipconns:skipconns.append(noise_skipconns)# 2. compute less noisy image and set x_t -> x_t-1xt = reverse_step(model, noise_pred, t, xt, eta=etas[idx], variance_noise=z)# if controller is not None:# xt = controller.step_callback(xt)# "fix" xtapply_fix = ((skips.max() - skips) > it)if apply_fix.any():apply_fix = (apply_fix * fix_alpha).unsqueeze(1).unsqueeze(2).unsqueeze(3).to(xT.device)xt = (masks * (xt.expand(batch_size, -1, -1, -1) * (1 - apply_fix) +apply_fix * xT[skips.max() - it - 1].expand(batch_size, -1, -1, -1))).sum(axis=0).unsqueeze(0)if extract_h_space:return xt, zs, torch.concat(hspaces, axis=0)if extract_skipconns:return xt, zs, torch.concat(hspaces, axis=0), skipconnsreturn xt, zs1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341352 unmodified lines138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208import torchfrom tqdm import tqdmfrom typing import List, Optional, Tuplefrom models import PipelineWrapperimport gradio as grdef inversion_forward_process(model: PipelineWrapper,x0: torch.Tensor,etas: Optional[float] = None,prompts: List[str] = [""],cfg_scales: List[float] = [3.5],num_inference_steps: int = 50,numerical_fix: bool = False,duration: Optional[float] = None,first_order: bool = False,save_compute: bool = True,progress=gr.Progress()) -> Tuple:if len(prompts) > 1 or prompts[0] != "":text_embeddings_hidden_states, text_embeddings_class_labels, \text_embeddings_boolean_prompt_mask = model.encode_text(prompts)# In the forward negative prompts are not supported currently (TODO)uncond_embeddings_hidden_states, uncond_embeddings_class_lables, uncond_boolean_prompt_mask = model.encode_text([""], negative=True, save_compute=save_compute, cond_length=text_embeddings_class_labels.shape[1]if text_embeddings_class_labels is not None else None)else:uncond_embeddings_hidden_states, uncond_embeddings_class_lables, uncond_boolean_prompt_mask = model.encode_text([""], negative=True, save_compute=False)timesteps = model.model.scheduler.timesteps.to(model.device)variance_noise_shape = model.get_noise_shape(x0, num_inference_steps)if type(etas) in [int, float]:etas = [etas]*model.model.scheduler.num_inference_stepsxts = model.sample_xts_from_x0(x0, num_inference_steps=num_inference_steps)zs = torch.zeros(size=variance_noise_shape, device=model.device)extra_info = [None] * len(zs)if timesteps[0].dtype == torch.int64:t_to_idx = {int(v): k for k, v in enumerate(timesteps)}elif timesteps[0].dtype == torch.float32:t_to_idx = {float(v): k for k, v in enumerate(timesteps)}xt = x0op = tqdm(timesteps, desc="Inverting")model.setup_extra_inputs(xt, init_timestep=timesteps[0], audio_end_in_s=duration,save_compute=save_compute and prompts[0] != "")app_op = progress.tqdm(timesteps, desc="Inverting")for t, _ in zip(op, app_op):idx = num_inference_steps - t_to_idx[int(t) if timesteps[0].dtype == torch.int64 else float(t)] - 1# 1. predict noise residualxt = xts[idx+1][None]xt_inp = model.model.scheduler.scale_model_input(xt, t)with torch.no_grad():if save_compute and prompts[0] != "":comb_out, _, _ = model.unet_forward(xt_inp.expand(2, -1, -1, -1) if hasattr(model.model, 'unet') else xt_inp.expand(2, -1, -1),timestep=t,encoder_hidden_states=torch.cat([uncond_embeddings_hidden_states, text_embeddings_hidden_states], dim=0)if uncond_embeddings_hidden_states is not None else None,class_labels=torch.cat([uncond_embeddings_class_lables, text_embeddings_class_labels], dim=0)if uncond_embeddings_class_lables is not None else None,encoder_attention_mask=torch.cat([uncond_boolean_prompt_mask, text_embeddings_boolean_prompt_mask], dim=0)if uncond_boolean_prompt_mask is not None else None,)out, cond_out = comb_out.sample.chunk(2, dim=0)else:out = model.unet_forward(xt_inp, timestep=t,encoder_hidden_states=uncond_embeddings_hidden_states,class_labels=uncond_embeddings_class_lables,encoder_attention_mask=uncond_boolean_prompt_mask)[0].sampleif len(prompts) > 1 or prompts[0] != "":cond_out = model.unet_forward(xt_inp,timestep=t,encoder_hidden_states=text_embeddings_hidden_states,class_labels=text_embeddings_class_labels,encoder_attention_mask=text_embeddings_boolean_prompt_mask)[0].sampleif len(prompts) > 1 or prompts[0] != "":# # classifier free guidancenoise_pred = out + (cfg_scales[0] * (cond_out - out)).sum(axis=0).unsqueeze(0)else:noise_pred = out# xtm1 = xts[idx+1][None]xtm1 = xts[idx][None]z, xtm1, extra = model.get_zs_from_xts(xt, xtm1, noise_pred, t,eta=etas[idx], numerical_fix=numerical_fix,first_order=first_order)zs[idx] = z# print(f"Fix Xt-1 distance - NORM:{torch.norm(xts[idx] - xtm1):.4g}, MSE:{((xts[idx] - xtm1)**2).mean():.4g}")xts[idx] = xtm1extra_info[idx] = extraif zs is not None:# zs[-1] = torch.zeros_like(zs[-1])zs[0] = torch.zeros_like(zs[0])# zs_cycle[0] = torch.zeros_like(zs[0])del app_op.iterables[0]return xt, zs, xts, extra_infodef inversion_reverse_process(model: PipelineWrapper,xT: torch.Tensor,tstart: torch.Tensor,etas: float = 0,prompts: List[str] = [""],neg_prompts: List[str] = [""],cfg_scales: Optional[List[float]] = None,zs: Optional[List[torch.Tensor]] = None,duration: Optional[float] = None,first_order: bool = False,extra_info: Optional[List] = None,save_compute: bool = True,progress=gr.Progress()) -> Tuple[torch.Tensor, torch.Tensor]:text_embeddings_hidden_states, text_embeddings_class_labels, \text_embeddings_boolean_prompt_mask = model.encode_text(prompts)uncond_embeddings_hidden_states, uncond_embeddings_class_lables, \uncond_boolean_prompt_mask = model.encode_text(neg_prompts,negative=True,save_compute=save_compute,cond_length=text_embeddings_class_labels.shape[1]if text_embeddings_class_labels is not None else None)xt = xT[tstart.max()].unsqueeze(0)if etas is None:etas = 02 unmodified linesassert len(etas) == model.model.scheduler.num_inference_stepstimesteps = model.model.scheduler.timesteps.to(model.device)op = tqdm(timesteps[-zs.shape[0]:], desc="Editing")if timesteps[0].dtype == torch.int64:t_to_idx = {int(v): k for k, v in enumerate(timesteps[-zs.shape[0]:])}elif timesteps[0].dtype == torch.float32:t_to_idx = {float(v): k for k, v in enumerate(timesteps[-zs.shape[0]:])}model.setup_extra_inputs(xt, extra_info=extra_info, init_timestep=timesteps[-zs.shape[0]],audio_end_in_s=duration, save_compute=save_compute)app_op = progress.tqdm(timesteps[-zs.shape[0]:], desc="Editing")for it, (t, _) in enumerate(zip(op, app_op)):idx = model.model.scheduler.num_inference_steps - t_to_idx[int(t) if timesteps[0].dtype == torch.int64 else float(t)] - \(model.model.scheduler.num_inference_steps - zs.shape[0] + 1)xt_inp = model.model.scheduler.scale_model_input(xt, t)# # Unconditional embeddingwith torch.no_grad():# print(f'xt_inp.shape: {xt_inp.shape}')# print(f't.shape: {t.shape}')# print(f'uncond_embeddings_hidden_states.shape: {uncond_embeddings_hidden_states.shape}')# print(f'uncond_embeddings_class_lables.shape: {uncond_embeddings_class_lables.shape}')# print(f'uncond_boolean_prompt_mask.shape: {uncond_boolean_prompt_mask.shape}')# print(f'text_embeddings_hidden_states.shape: {text_embeddings_hidden_states.shape}')# print(f'text_embeddings_class_labels.shape: {text_embeddings_class_labels.shape}')# print(f'text_embeddings_boolean_prompt_mask.shape: {text_embeddings_boolean_prompt_mask.shape}')if save_compute:comb_out, _, _ = model.unet_forward(xt_inp.expand(2, -1, -1, -1) if hasattr(model.model, 'unet') else xt_inp.expand(2, -1, -1),timestep=t,encoder_hidden_states=torch.cat([uncond_embeddings_hidden_states, text_embeddings_hidden_states], dim=0)if uncond_embeddings_hidden_states is not None else None,class_labels=torch.cat([uncond_embeddings_class_lables, text_embeddings_class_labels], dim=0)if uncond_embeddings_class_lables is not None else None,encoder_attention_mask=torch.cat([uncond_boolean_prompt_mask, text_embeddings_boolean_prompt_mask], dim=0)if uncond_boolean_prompt_mask is not None else None,)uncond_out, cond_out = comb_out.sample.chunk(2, dim=0)else:uncond_out = model.unet_forward(xt_inp, timestep=t,encoder_hidden_states=uncond_embeddings_hidden_states,class_labels=uncond_embeddings_class_lables,encoder_attention_mask=uncond_boolean_prompt_mask,)[0].sample# Conditional embeddingcond_out = model.unet_forward(xt_inp,timestep=t,encoder_hidden_states=text_embeddings_hidden_states,class_labels=text_embeddings_class_labels,encoder_attention_mask=text_embeddings_boolean_prompt_mask,)[0].samplez = zs[idx] if zs is not None else Nonez = z.unsqueeze(0)# classifier free guidancenoise_pred = uncond_out + (cfg_scales[0] * (cond_out - uncond_out)).sum(axis=0).unsqueeze(0)# 2. compute less noisy image and set x_t -> x_t-1xt = model.reverse_step_with_custom_noise(noise_pred, t, xt, variance_noise=z,eta=etas[idx], first_order=first_order)del app_op.iterables[0]return xt, zs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454610 unmodified lines575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993005 unmodified lines3063073083093103113123133143153163173183193203213221 unmodified line3243253263273283293302 unmodified lines33333433533633733833934063 unmodified lines4044054064074084094101 unmodified line41241341441541641741866 unmodified lines485486487488489490491492135 unmodified lines628629630631632633634635636637638639import torchfrom diffusers import DDIMSchedulerfrom diffusers import AudioLDM2Pipelinefrom transformers import RobertaTokenizer, RobertaTokenizerFastfrom diffusers.models.unets.unet_2d_condition import UNet2DConditionOutputfrom typing import Any, Dict, List, Optional, Tuple, Unionclass PipelineWrapper(torch.nn.Module):def __init__(self, model_id, device, double_precision=False, *args, **kwargs) -> None:super().__init__(*args, **kwargs)self.model_id = model_idself.device = deviceself.double_precision = double_precisiondef get_sigma(self, timestep) -> float:sqrt_recipm1_alphas_cumprod = torch.sqrt(1.0 / self.model.scheduler.alphas_cumprod - 1)return sqrt_recipm1_alphas_cumprod[timestep]def load_scheduler(self):passdef get_fn_STFT(self):passdef vae_encode(self, x: torch.Tensor):passdef vae_decode(self, x: torch.Tensor):passdef decode_to_mel(self, x: torch.Tensor):passdef encode_text(self, prompts: List[str]) -> Tuple:passdef get_variance(self, timestep, prev_timestep):passdef get_alpha_prod_t_prev(self, prev_timestep):passdef unet_forward(self,sample: torch.FloatTensor,timestep: Union[torch.Tensor, float, int],10 unmodified linesreplace_skip_conns: Optional[Dict[int, torch.Tensor]] = None,return_dict: bool = True,zero_out_resconns: Optional[Union[int, List]] = None) -> Tuple:# By default samples have to be AT least a multiple of the overall upsampling factor.# The overall upsampling factor is equal to 2 ** (# num of upsampling layers).# However, the upsampling interpolation output size can be forced to fit any upsampling size# on the fly if necessary.default_overall_up_factor = 2**self.model.unet.num_upsamplers# upsample size should be forwarded when sample is not a multiple of `default_overall_up_factor`forward_upsample_size = Falseupsample_size = Noneif any(s % default_overall_up_factor != 0 for s in sample.shape[-2:]):# logger.info("Forward upsample size to force interpolation output size.")forward_upsample_size = True# ensure attention_mask is a bias, and give it a singleton query_tokens dimension# expects mask of shape:# [batch, key_tokens]# adds singleton query_tokens dimension:# [batch, 1, key_tokens]# this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:# [batch, heads, query_tokens, key_tokens] (e.g. torch sdp attn)# [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)if attention_mask is not None:# assume that mask is expressed as:# (1 = keep, 0 = discard)# convert mask into a bias that can be added to attention scores:# (keep = +0, discard = -10000.0)attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0attention_mask = attention_mask.unsqueeze(1)# convert encoder_attention_mask to a bias the same way we do for attention_maskif encoder_attention_mask is not None:encoder_attention_mask = (1 - encoder_attention_mask.to(sample.dtype)) * -10000.0encoder_attention_mask = encoder_attention_mask.unsqueeze(1)# 0. center input if necessaryif self.model.unet.config.center_input_sample:sample = 2 * sample - 1.0# 1. timetimesteps = timestepif not torch.is_tensor(timesteps):# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can# This would be a good case for the `match` statement (Python 3.10+)is_mps = sample.device.type == "mps"if isinstance(timestep, float):dtype = torch.float32 if is_mps else torch.float64else:dtype = torch.int32 if is_mps else torch.int64timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)elif len(timesteps.shape) == 0:timesteps = timesteps[None].to(sample.device)# broadcast to batch dimension in a way that's compatible with ONNX/Core MLtimesteps = timesteps.expand(sample.shape[0])t_emb = self.model.unet.time_proj(timesteps)# `Timesteps` does not contain any weights and will always return f32 tensors# but time_embedding might actually be running in fp16. so we need to cast here.# there might be better ways to encapsulate this.t_emb = t_emb.to(dtype=sample.dtype)emb = self.model.unet.time_embedding(t_emb, timestep_cond)if self.model.unet.class_embedding is not None:if class_labels is None:raise ValueError("class_labels should be provided when num_class_embeds > 0")if self.model.unet.config.class_embed_type == "timestep":class_labels = self.model.unet.time_proj(class_labels)# `Timesteps` does not contain any weights and will always return f32 tensors# there might be better ways to encapsulate this.class_labels = class_labels.to(dtype=sample.dtype)class_emb = self.model.unet.class_embedding(class_labels).to(dtype=sample.dtype)if self.model.unet.config.class_embeddings_concat:emb = torch.cat([emb, class_emb], dim=-1)else:emb = emb + class_embif self.model.unet.config.addition_embed_type == "text":aug_emb = self.model.unet.add_embedding(encoder_hidden_states)emb = emb + aug_embelif self.model.unet.config.addition_embed_type == "text_image":# Kadinsky 2.1 - styleif "image_embeds" not in added_cond_kwargs:raise ValueError(f"{self.model.unet.__class__} has the config param `addition_embed_type` set to 'text_image' "f"which requires the keyword argument `image_embeds` to be passed in `added_cond_kwargs`")image_embs = added_cond_kwargs.get("image_embeds")text_embs = added_cond_kwargs.get("text_embeds", encoder_hidden_states)aug_emb = self.model.unet.add_embedding(text_embs, image_embs)emb = emb + aug_embif self.model.unet.time_embed_act is not None:emb = self.model.unet.time_embed_act(emb)if self.model.unet.encoder_hid_proj is not None and self.model.unet.config.encoder_hid_dim_type == "text_proj":encoder_hidden_states = self.model.unet.encoder_hid_proj(encoder_hidden_states)elif self.model.unet.encoder_hid_proj is not None and \self.model.unet.config.encoder_hid_dim_type == "text_image_proj":# Kadinsky 2.1 - styleif "image_embeds" not in added_cond_kwargs:raise ValueError(f"{self.model.unet.__class__} has the config param `encoder_hid_dim_type` set to 'text_image_proj' "f"which requires the keyword argument `image_embeds` to be passed in `added_conditions`")image_embeds = added_cond_kwargs.get("image_embeds")encoder_hidden_states = self.model.unet.encoder_hid_proj(encoder_hidden_states, image_embeds)# 2. pre-processsample = self.model.unet.conv_in(sample)# 3. downdown_block_res_samples = (sample,)for downsample_block in self.model.unet.down_blocks:if hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention:sample, res_samples = downsample_block(hidden_states=sample,temb=emb,encoder_hidden_states=encoder_hidden_states,attention_mask=attention_mask,cross_attention_kwargs=cross_attention_kwargs,encoder_attention_mask=encoder_attention_mask,)else:sample, res_samples = downsample_block(hidden_states=sample, temb=emb)down_block_res_samples += res_samplesif down_block_additional_residuals is not None:new_down_block_res_samples = ()for down_block_res_sample, down_block_additional_residual in zip(down_block_res_samples, down_block_additional_residuals):down_block_res_sample = down_block_res_sample + down_block_additional_residualnew_down_block_res_samples = new_down_block_res_samples + (down_block_res_sample,)down_block_res_samples = new_down_block_res_samples# 4. midif self.model.unet.mid_block is not None:sample = self.model.unet.mid_block(sample,emb,encoder_hidden_states=encoder_hidden_states,attention_mask=attention_mask,cross_attention_kwargs=cross_attention_kwargs,encoder_attention_mask=encoder_attention_mask,)# print(sample.shape)if replace_h_space is None:h_space = sample.clone()else:h_space = replace_h_spacesample = replace_h_space.clone()if mid_block_additional_residual is not None:sample = sample + mid_block_additional_residualextracted_res_conns = {}# 5. upfor i, upsample_block in enumerate(self.model.unet.up_blocks):is_final_block = i == len(self.model.unet.up_blocks) - 1res_samples = down_block_res_samples[-len(upsample_block.resnets):]down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]if replace_skip_conns is not None and replace_skip_conns.get(i):res_samples = replace_skip_conns.get(i)if zero_out_resconns is not None:if (type(zero_out_resconns) is int and i >= (zero_out_resconns - 1)) or \type(zero_out_resconns) is list and i in zero_out_resconns:res_samples = [torch.zeros_like(x) for x in res_samples]# down_block_res_samples = [torch.zeros_like(x) for x in down_block_res_samples]extracted_res_conns[i] = res_samples# if we have not reached the final block and need to forward the# upsample size, we do it hereif not is_final_block and forward_upsample_size:upsample_size = down_block_res_samples[-1].shape[2:]if hasattr(upsample_block, "has_cross_attention") and upsample_block.has_cross_attention:sample = upsample_block(hidden_states=sample,temb=emb,res_hidden_states_tuple=res_samples,encoder_hidden_states=encoder_hidden_states,cross_attention_kwargs=cross_attention_kwargs,upsample_size=upsample_size,attention_mask=attention_mask,encoder_attention_mask=encoder_attention_mask,)else:sample = upsample_block(hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples, upsample_size=upsample_size)# 6. post-processif self.model.unet.conv_norm_out:sample = self.model.unet.conv_norm_out(sample)sample = self.model.unet.conv_act(sample)sample = self.model.unet.conv_out(sample)if not return_dict:return (sample,)return UNet2DConditionOutput(sample=sample), h_space, extracted_res_connsclass AudioLDM2Wrapper(PipelineWrapper):def __init__(self, *args, **kwargs) -> None:super().__init__(*args, **kwargs)if self.double_precision:self.model = AudioLDM2Pipeline.from_pretrained(self.model_id, torch_dtype=torch.float64).to(self.device)else:try:self.model = AudioLDM2Pipeline.from_pretrained(self.model_id, local_files_only=True).to(self.device)except FileNotFoundError:self.model = AudioLDM2Pipeline.from_pretrained(self.model_id, local_files_only=False).to(self.device)def load_scheduler(self):# self.model.scheduler = DDIMScheduler.from_config(self.model_id, subfolder="scheduler")self.model.scheduler = DDIMScheduler.from_pretrained(self.model_id, subfolder="scheduler")def get_fn_STFT(self):from audioldm.audio import TacotronSTFTreturn TacotronSTFT(filter_length=1024,5 unmodified linesmel_fmax=8000,)def vae_encode(self, x):# self.model.vae.disable_tiling()if x.shape[2] % 4:x = torch.nn.functional.pad(x, (0, 0, 4 - (x.shape[2] % 4), 0))return (self.model.vae.encode(x).latent_dist.mode() * self.model.vae.config.scaling_factor).float()# return (self.encode_no_tiling(x).latent_dist.mode() * self.model.vae.config.scaling_factor).float()def vae_decode(self, x):return self.model.vae.decode(1 / self.model.vae.config.scaling_factor * x).sampledef decode_to_mel(self, x):if self.double_precision:tmp = self.model.mel_spectrogram_to_waveform(x[:, 0].detach().double()).detach()tmp = self.model.mel_spectrogram_to_waveform(x[:, 0].detach().float()).detach()1 unmodified linetmp = tmp.unsqueeze(0)return tmpdef encode_text(self, prompts: List[str]):tokenizers = [self.model.tokenizer, self.model.tokenizer_2]text_encoders = [self.model.text_encoder, self.model.text_encoder_2]prompt_embeds_list = []2 unmodified linesfor tokenizer, text_encoder in zip(tokenizers, text_encoders):text_inputs = tokenizer(prompts,padding="max_length" if isinstance(tokenizer, (RobertaTokenizer, RobertaTokenizerFast)) else True,max_length=tokenizer.model_max_length,truncation=True,return_tensors="pt",)63 unmodified linesreturn generated_prompt_embeds, prompt_embeds, attention_maskdef get_variance(self, timestep, prev_timestep):alpha_prod_t = self.model.scheduler.alphas_cumprod[timestep]alpha_prod_t_prev = self.get_alpha_prod_t_prev(prev_timestep)beta_prod_t = 1 - alpha_prod_t1 unmodified linevariance = (beta_prod_t_prev / beta_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)return variancedef get_alpha_prod_t_prev(self, prev_timestep):return self.model.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 \else self.model.scheduler.final_alpha_cumprod66 unmodified lines# 1. timetimesteps = timestepif not torch.is_tensor(timesteps):# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can# This would be a good case for the `match` statement (Python 3.10+)is_mps = sample.device.type == "mps"if isinstance(timestep, float):dtype = torch.float32 if is_mps else torch.float64135 unmodified linesreturn UNet2DConditionOutput(sample=sample), h_space, extracted_res_connsdef forward(self, *args, **kwargs):return selfdef load_model(model_id, device, double_precision=False):ldm_stable = AudioLDM2Wrapper(model_id=model_id, device=device, double_precision=double_precision)ldm_stable.load_scheduler()torch.cuda.empty_cache()return ldm_stable12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916010 unmodified lines1711721731741751761771781791801811821831841851861871881891901911921931941951961975 unmodified lines2032042052062072082092102112122132142152162172182191 unmodified line2212222232242252262272282292 unmodified lines23223323423523623723823924024124263 unmodified lines3063073083093103113121 unmodified line31431531631731831932066 unmodified lines387388389390391392135 unmodified lines528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855import torchfrom diffusers import DDIMScheduler, CosineDPMSolverMultistepSchedulerfrom diffusers.schedulers.scheduling_dpmsolver_sde import BrownianTreeNoiseSamplerfrom diffusers import AudioLDM2Pipeline, StableAudioPipelinefrom transformers import RobertaTokenizer, RobertaTokenizerFast, VitsTokenizerfrom diffusers.models.unets.unet_2d_condition import UNet2DConditionOutputfrom diffusers.models.embeddings import get_1d_rotary_pos_embedfrom typing import Any, Dict, List, Optional, Tuple, Unionimport gradio as grclass PipelineWrapper(torch.nn.Module):def __init__(self, model_id: str,device: torch.device,double_precision: bool = False,token: Optional[str] = None, *args, **kwargs) -> None:super().__init__(*args, **kwargs)self.model_id = model_idself.device = deviceself.double_precision = double_precisionself.token = tokendef get_sigma(self, timestep: int) -> float:sqrt_recipm1_alphas_cumprod = torch.sqrt(1.0 / self.model.scheduler.alphas_cumprod - 1)return sqrt_recipm1_alphas_cumprod[timestep]def load_scheduler(self) -> None:passdef get_fn_STFT(self) -> torch.nn.Module:passdef get_sr(self) -> int:return 16000def vae_encode(self, x: torch.Tensor) -> torch.Tensor:passdef vae_decode(self, x: torch.Tensor) -> torch.Tensor:passdef decode_to_mel(self, x: torch.Tensor) -> torch.Tensor:passdef setup_extra_inputs(self, *args, **kwargs) -> None:passdef encode_text(self, prompts: List[str], **kwargs) -> Tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]:passdef get_variance(self, timestep: torch.Tensor, prev_timestep: torch.Tensor) -> torch.Tensor:passdef get_alpha_prod_t_prev(self, prev_timestep: torch.Tensor) -> torch.Tensor:passdef get_noise_shape(self, x0: torch.Tensor, num_steps: int) -> Tuple[int, ...]:variance_noise_shape = (num_steps,self.model.unet.config.in_channels,x0.shape[-2],x0.shape[-1])return variance_noise_shapedef sample_xts_from_x0(self, x0: torch.Tensor, num_inference_steps: int = 50) -> torch.Tensor:"""Samples from P(x_1:T|x_0)"""alpha_bar = self.model.scheduler.alphas_cumprodsqrt_one_minus_alpha_bar = (1-alpha_bar) ** 0.5variance_noise_shape = self.get_noise_shape(x0, num_inference_steps + 1)timesteps = self.model.scheduler.timesteps.to(self.device)t_to_idx = {int(v): k for k, v in enumerate(timesteps)}xts = torch.zeros(variance_noise_shape).to(x0.device)xts[0] = x0for t in reversed(timesteps):idx = num_inference_steps - t_to_idx[int(t)]xts[idx] = x0 * (alpha_bar[t] ** 0.5) + torch.randn_like(x0) * sqrt_one_minus_alpha_bar[t]return xtsdef get_zs_from_xts(self, xt: torch.Tensor, xtm1: torch.Tensor, noise_pred: torch.Tensor,t: torch.Tensor, eta: float = 0, numerical_fix: bool = True, **kwargs) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]:# pred of x0alpha_bar = self.model.scheduler.alphas_cumprodif self.model.scheduler.config.prediction_type == 'epsilon':pred_original_sample = (xt - (1 - alpha_bar[t]) ** 0.5 * noise_pred) / alpha_bar[t] ** 0.5elif self.model.scheduler.config.prediction_type == 'v_prediction':pred_original_sample = (alpha_bar[t] ** 0.5) * xt - ((1 - alpha_bar[t]) ** 0.5) * noise_pred# direction to xtprev_timestep = t - self.model.scheduler.config.num_train_timesteps // \self.model.scheduler.num_inference_stepsalpha_prod_t_prev = self.get_alpha_prod_t_prev(prev_timestep)variance = self.get_variance(t, prev_timestep)if self.model.scheduler.config.prediction_type == 'epsilon':radom_noise_pred = noise_predelif self.model.scheduler.config.prediction_type == 'v_prediction':radom_noise_pred = (alpha_bar[t] ** 0.5) * noise_pred + ((1 - alpha_bar[t]) ** 0.5) * xtpred_sample_direction = (1 - alpha_prod_t_prev - eta * variance) ** (0.5) * radom_noise_predmu_xt = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_directionz = (xtm1 - mu_xt) / (eta * variance ** 0.5)# correction to avoid error accumulationif numerical_fix:xtm1 = mu_xt + (eta * variance ** 0.5)*zreturn z, xtm1, Nonedef reverse_step_with_custom_noise(self, model_output: torch.Tensor, timestep: torch.Tensor, sample: torch.Tensor,variance_noise: Optional[torch.Tensor] = None, eta: float = 0, **kwargs) -> torch.Tensor:# 1. get previous step value (=t-1)prev_timestep = timestep - self.model.scheduler.config.num_train_timesteps // \self.model.scheduler.num_inference_steps# 2. compute alphas, betasalpha_prod_t = self.model.scheduler.alphas_cumprod[timestep]alpha_prod_t_prev = self.get_alpha_prod_t_prev(prev_timestep)beta_prod_t = 1 - alpha_prod_t# 3. compute predicted original sample from predicted noise also called# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdfif self.model.scheduler.config.prediction_type == 'epsilon':pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)elif self.model.scheduler.config.prediction_type == 'v_prediction':pred_original_sample = (alpha_prod_t ** 0.5) * sample - (beta_prod_t ** 0.5) * model_output# 5. compute variance: "sigma_t(η)" -> see formula (16)# σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1)# variance = self.scheduler._get_variance(timestep, prev_timestep)variance = self.get_variance(timestep, prev_timestep)# std_dev_t = eta * variance ** (0.5)# Take care of asymetric reverse process (asyrp)if self.model.scheduler.config.prediction_type == 'epsilon':model_output_direction = model_outputelif self.model.scheduler.config.prediction_type == 'v_prediction':model_output_direction = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample# 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf# pred_sample_direction = (1 - alpha_prod_t_prev - std_dev_t**2) ** (0.5) * model_output_directionpred_sample_direction = (1 - alpha_prod_t_prev - eta * variance) ** (0.5) * model_output_direction# 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdfprev_sample = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_direction# 8. Add noice if eta > 0if eta > 0:if variance_noise is None:variance_noise = torch.randn(model_output.shape, device=self.device)sigma_z = eta * variance ** (0.5) * variance_noiseprev_sample = prev_sample + sigma_zreturn prev_sampledef unet_forward(self,sample: torch.FloatTensor,timestep: Union[torch.Tensor, float, int],10 unmodified linesreplace_skip_conns: Optional[Dict[int, torch.Tensor]] = None,return_dict: bool = True,zero_out_resconns: Optional[Union[int, List]] = None) -> Tuple:passclass AudioLDM2Wrapper(PipelineWrapper):def __init__(self, *args, **kwargs) -> None:super().__init__(*args, **kwargs)if self.double_precision:self.model = AudioLDM2Pipeline.from_pretrained(self.model_id, torch_dtype=torch.float64, token=self.token).to(self.device)else:try:self.model = AudioLDM2Pipeline.from_pretrained(self.model_id, local_files_only=True, token=self.token).to(self.device)except FileNotFoundError:self.model = AudioLDM2Pipeline.from_pretrained(self.model_id, local_files_only=False, token=self.token).to(self.device)def load_scheduler(self) -> None:self.model.scheduler = DDIMScheduler.from_pretrained(self.model_id, subfolder="scheduler")def get_fn_STFT(self) -> torch.nn.Module:from audioldm.audio import TacotronSTFTreturn TacotronSTFT(filter_length=1024,5 unmodified linesmel_fmax=8000,)def vae_encode(self, x: torch.Tensor) -> torch.Tensor:# self.model.vae.disable_tiling()if x.shape[2] % 4:x = torch.nn.functional.pad(x, (0, 0, 4 - (x.shape[2] % 4), 0))return (self.model.vae.encode(x).latent_dist.mode() * self.model.vae.config.scaling_factor).float()# return (self.encode_no_tiling(x).latent_dist.mode() * self.model.vae.config.scaling_factor).float()def vae_decode(self, x: torch.Tensor) -> torch.Tensor:return self.model.vae.decode(1 / self.model.vae.config.scaling_factor * x).sampledef decode_to_mel(self, x: torch.Tensor) -> torch.Tensor:if self.double_precision:tmp = self.model.mel_spectrogram_to_waveform(x[:, 0].detach().double()).detach()tmp = self.model.mel_spectrogram_to_waveform(x[:, 0].detach().float()).detach()1 unmodified linetmp = tmp.unsqueeze(0)return tmpdef encode_text(self, prompts: List[str], negative: bool = False,save_compute: bool = False, cond_length: int = 0, **kwargs) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:tokenizers = [self.model.tokenizer, self.model.tokenizer_2]text_encoders = [self.model.text_encoder, self.model.text_encoder_2]prompt_embeds_list = []2 unmodified linesfor tokenizer, text_encoder in zip(tokenizers, text_encoders):text_inputs = tokenizer(prompts,padding="max_length" if (save_compute and negative) or isinstance(tokenizer, (RobertaTokenizer, RobertaTokenizerFast))else True,max_length=tokenizer.model_max_lengthif (not save_compute) or ((not negative) or isinstance(tokenizer, (RobertaTokenizer, RobertaTokenizerFast, VitsTokenizer)))else cond_length,truncation=True,return_tensors="pt",)63 unmodified linesreturn generated_prompt_embeds, prompt_embeds, attention_maskdef get_variance(self, timestep: torch.Tensor, prev_timestep: torch.Tensor) -> torch.Tensor:alpha_prod_t = self.model.scheduler.alphas_cumprod[timestep]alpha_prod_t_prev = self.get_alpha_prod_t_prev(prev_timestep)beta_prod_t = 1 - alpha_prod_t1 unmodified linevariance = (beta_prod_t_prev / beta_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)return variancedef get_alpha_prod_t_prev(self, prev_timestep: torch.Tensor) -> torch.Tensor:return self.model.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 \else self.model.scheduler.final_alpha_cumprod66 unmodified lines# 1. timetimesteps = timestepif not torch.is_tensor(timesteps):is_mps = sample.device.type == "mps"if isinstance(timestep, float):dtype = torch.float32 if is_mps else torch.float64135 unmodified linesreturn UNet2DConditionOutput(sample=sample), h_space, extracted_res_connsclass StableAudWrapper(PipelineWrapper):def __init__(self, *args, **kwargs) -> None:super().__init__(*args, **kwargs)try:self.model = StableAudioPipeline.from_pretrained(self.model_id, token=self.token, local_files_only=True).to(self.device)except FileNotFoundError:self.model = StableAudioPipeline.from_pretrained(self.model_id, token=self.token, local_files_only=False).to(self.device)self.model.transformer.eval()self.model.vae.eval()if self.double_precision:self.model = self.model.to(torch.float64)def load_scheduler(self) -> None:self.model.scheduler = CosineDPMSolverMultistepScheduler.from_pretrained(self.model_id, subfolder="scheduler", token=self.token)def encode_text(self, prompts: List[str], negative: bool = False, **kwargs) -> Tuple[torch.Tensor, None, torch.Tensor]:text_inputs = self.model.tokenizer(prompts,padding="max_length",max_length=self.model.tokenizer.model_max_length,truncation=True,return_tensors="pt",)text_input_ids = text_inputs.input_ids.to(self.device)attention_mask = text_inputs.attention_mask.to(self.device)self.model.text_encoder.eval()with torch.no_grad():prompt_embeds = self.model.text_encoder(text_input_ids, attention_mask=attention_mask)[0]if negative and attention_mask is not None: # set the masked tokens to the null embedprompt_embeds = torch.where(attention_mask.to(torch.bool).unsqueeze(2), prompt_embeds, 0.0)prompt_embeds = self.model.projection_model(text_hidden_states=prompt_embeds).text_hidden_statesif attention_mask is None:raise gr.Error("Shouldn't reach here. Please raise an issue if you do.")"""prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])if attention_mask is not None and negative_attention_mask is None:negative_attention_mask = torch.ones_like(attention_mask)elif attention_mask is None and negative_attention_mask is not None:attention_mask = torch.ones_like(negative_attention_mask)"""if prompts == [""]: # emptyreturn torch.zeros_like(prompt_embeds, device=prompt_embeds.device), None, Noneprompt_embeds = prompt_embeds * attention_mask.unsqueeze(-1).to(prompt_embeds.dtype)prompt_embeds = prompt_embeds * attention_mask.unsqueeze(-1).to(prompt_embeds.dtype)return prompt_embeds, None, attention_maskdef get_fn_STFT(self) -> torch.nn.Module:from audioldm.audio import TacotronSTFTreturn TacotronSTFT(filter_length=1024,hop_length=160,win_length=1024,n_mel_channels=64,sampling_rate=44100,mel_fmin=0,mel_fmax=22050,)def vae_encode(self, x: torch.Tensor) -> torch.Tensor:x = x.unsqueeze(0)audio_vae_length = int(self.model.transformer.config.sample_size * self.model.vae.hop_length)audio_shape = (1, self.model.vae.config.audio_channels, audio_vae_length)# check num_channelsif x.shape[1] == 1 and self.model.vae.config.audio_channels == 2:x = x.repeat(1, 2, 1)audio_length = x.shape[-1]audio = x.new_zeros(audio_shape)audio[:, :, : min(audio_length, audio_vae_length)] = x[:, :, :audio_vae_length]encoded_audio = self.model.vae.encode(audio.to(self.device)).latent_distencoded_audio = encoded_audio.sample()return encoded_audiodef vae_decode(self, x: torch.Tensor) -> torch.Tensor:torch.cuda.empty_cache()# return self.model.vae.decode(1 / self.model.vae.config.scaling_factor * x).sampleaud = self.model.vae.decode(x).samplereturn aud[:, :, self.waveform_start:self.waveform_end]def setup_extra_inputs(self, x: torch.Tensor, init_timestep: torch.Tensor,extra_info: Optional[Any] = None,audio_start_in_s: float = 0, audio_end_in_s: Optional[float] = None,save_compute: bool = False) -> None:max_audio_length_in_s = self.model.transformer.config.sample_size * self.model.vae.hop_length / \self.model.vae.config.sampling_rateif audio_end_in_s is None:audio_end_in_s = max_audio_length_in_sif audio_end_in_s - audio_start_in_s > max_audio_length_in_s:raise ValueError(f"The total audio length requested ({audio_end_in_s-audio_start_in_s}s) is longer "f"than the model maximum possible length ({max_audio_length_in_s}). "f"Make sure that 'audio_end_in_s-audio_start_in_s<={max_audio_length_in_s}'.")self.waveform_start = int(audio_start_in_s * self.model.vae.config.sampling_rate)self.waveform_end = int(audio_end_in_s * self.model.vae.config.sampling_rate)self.seconds_start_hidden_states, self.seconds_end_hidden_states = self.model.encode_duration(audio_start_in_s, audio_end_in_s, self.device, False, 1)if save_compute:self.seconds_start_hidden_states = torch.cat([self.seconds_start_hidden_states, self.seconds_start_hidden_states], dim=0)self.seconds_end_hidden_states = torch.cat([self.seconds_end_hidden_states, self.seconds_end_hidden_states], dim=0)self.audio_duration_embeds = torch.cat([self.seconds_start_hidden_states,self.seconds_end_hidden_states], dim=2)# 7. Prepare rotary positional embeddingself.rotary_embedding = get_1d_rotary_pos_embed(self.model.rotary_embed_dim,x.shape[2] + self.audio_duration_embeds.shape[1],use_real=True,repeat_interleave_real=False,)self.model.scheduler._init_step_index(init_timestep)# fix lower_order_nums for the reverse step - Option 1: only start from first order# self.model.scheduler.lower_order_nums = 0# self.model.scheduler.model_outputs = [None] * self.model.scheduler.config.solver_order# fix lower_order_nums for the reverse step - Option 2: start from the correct order with historyt_to_idx = {float(v): k for k, v in enumerate(self.model.scheduler.timesteps)}idx = len(self.model.scheduler.timesteps) - t_to_idx[float(init_timestep)] - 1self.model.scheduler.model_outputs = [None, extra_info[idx] if extra_info is not None else None]self.model.scheduler.lower_order_nums = min(self.model.scheduler.step_index,self.model.scheduler.config.solver_order)# if rand check:# x *= self.model.scheduler.init_noise_sigma# return xdef sample_xts_from_x0(self, x0: torch.Tensor, num_inference_steps: int = 50) -> torch.Tensor:"""Samples from P(x_1:T|x_0)"""sigmas = self.model.scheduler.sigmasshapes = self.get_noise_shape(x0, num_inference_steps + 1)xts = torch.zeros(shapes).to(x0.device)xts[0] = x0timesteps = self.model.scheduler.timesteps.to(self.device)t_to_idx = {float(v): k for k, v in enumerate(timesteps)}for t in reversed(timesteps):# idx = t_to_idx[int(t)]idx = num_inference_steps - t_to_idx[float(t)]n = torch.randn_like(x0)xts[idx] = x0 + n * sigmas[t_to_idx[float(t)]]return xtsdef get_zs_from_xts(self, xt: torch.Tensor, xtm1: torch.Tensor, data_pred: torch.Tensor,t: torch.Tensor, numerical_fix: bool = True, first_order: bool = False, **kwargs) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]:# pred of x0sigmas = self.model.scheduler.sigmastimesteps = self.model.scheduler.timestepssolver_order = self.model.scheduler.config.solver_orderif self.model.scheduler.step_index is None:self.model.scheduler._init_step_index(t)curr_step_index = self.model.scheduler.step_index# Improve numerical stability for small number of stepslower_order_final = (curr_step_index == len(timesteps) - 1) and (self.model.scheduler.config.euler_at_finalor (self.model.scheduler.config.lower_order_final and len(timesteps) < 15)or self.model.scheduler.config.final_sigmas_type == "zero")lower_order_second = ((curr_step_index == len(timesteps) - 2) andself.model.scheduler.config.lower_order_final and len(timesteps) < 15)data_pred = self.model.scheduler.convert_model_output(data_pred, sample=xt)for i in range(solver_order - 1):self.model.scheduler.model_outputs[i] = self.model.scheduler.model_outputs[i + 1]self.model.scheduler.model_outputs[-1] = data_pred# instead of brownian noise, here we calculate the noise ourselvesif (curr_step_index == len(timesteps) - 1) and self.model.scheduler.config.final_sigmas_type == "zero":z = torch.zeros_like(xt)elif first_order or solver_order == 1 or self.model.scheduler.lower_order_nums < 1 or lower_order_final:sigma_t, sigma_s = sigmas[curr_step_index + 1], sigmas[curr_step_index]h = torch.log(sigma_s) - torch.log(sigma_t)z = (xtm1 - (sigma_t / sigma_s * torch.exp(-h)) * xt - (1 - torch.exp(-2.0 * h)) * data_pred) \/ (sigma_t * torch.sqrt(1.0 - torch.exp(-2 * h)))elif solver_order == 2 or self.model.scheduler.lower_order_nums < 2 or lower_order_second:sigma_t = sigmas[curr_step_index + 1]sigma_s0 = sigmas[curr_step_index]sigma_s1 = sigmas[curr_step_index - 1]m0, m1 = self.model.scheduler.model_outputs[-1], self.model.scheduler.model_outputs[-2]h, h_0 = torch.log(sigma_s0) - torch.log(sigma_t), torch.log(sigma_s1) - torch.log(sigma_s0)r0 = h_0 / hD0, D1 = m0, (1.0 / r0) * (m0 - m1)# sde-dpmsolver++z = (xtm1 - (sigma_t / sigma_s0 * torch.exp(-h)) * xt- (1 - torch.exp(-2.0 * h)) * D0- 0.5 * (1 - torch.exp(-2.0 * h)) * D1) \/ (sigma_t * torch.sqrt(1.0 - torch.exp(-2 * h)))# correction to avoid error accumulationif numerical_fix:if first_order or solver_order == 1 or self.model.scheduler.lower_order_nums < 1 or lower_order_final:xtm1 = self.model.scheduler.dpm_solver_first_order_update(data_pred, sample=xt, noise=z)elif solver_order == 2 or self.model.scheduler.lower_order_nums < 2 or lower_order_second:xtm1 = self.model.scheduler.multistep_dpm_solver_second_order_update(self.model.scheduler.model_outputs, sample=xt, noise=z)# If not perfect recon - maybe TODO fix self.model.scheduler.model_outputs as well?if self.model.scheduler.lower_order_nums < solver_order:self.model.scheduler.lower_order_nums += 1# upon completion increase step index by oneself.model.scheduler._step_index += 1return z, xtm1, self.model.scheduler.model_outputs[-2]def get_sr(self) -> int:return self.model.vae.config.sampling_ratedef get_noise_shape(self, x0: torch.Tensor, num_steps: int) -> Tuple[int, int, int]:variance_noise_shape = (num_steps,self.model.transformer.config.in_channels,int(self.model.transformer.config.sample_size))return variance_noise_shapedef reverse_step_with_custom_noise(self, model_output: torch.Tensor, timestep: torch.Tensor, sample: torch.Tensor,variance_noise: Optional[torch.Tensor] = None,first_order: bool = False, **kwargs) -> torch.Tensor:if self.model.scheduler.step_index is None:self.model.scheduler._init_step_index(timestep)# Improve numerical stability for small number of stepslower_order_final = (self.model.scheduler.step_index == len(self.model.scheduler.timesteps) - 1) and (self.model.scheduler.config.euler_at_finalor (self.model.scheduler.config.lower_order_final and len(self.model.scheduler.timesteps) < 15)or self.model.scheduler.config.final_sigmas_type == "zero")lower_order_second = ((self.model.scheduler.step_index == len(self.model.scheduler.timesteps) - 2) andself.model.scheduler.config.lower_order_final and len(self.model.scheduler.timesteps) < 15)model_output = self.model.scheduler.convert_model_output(model_output, sample=sample)for i in range(self.model.scheduler.config.solver_order - 1):self.model.scheduler.model_outputs[i] = self.model.scheduler.model_outputs[i + 1]self.model.scheduler.model_outputs[-1] = model_outputif variance_noise is None:if self.model.scheduler.noise_sampler is None:self.model.scheduler.noise_sampler = BrownianTreeNoiseSampler(model_output, sigma_min=self.model.scheduler.config.sigma_min,sigma_max=self.model.scheduler.config.sigma_max, seed=None)variance_noise = self.model.scheduler.noise_sampler(self.model.scheduler.sigmas[self.model.scheduler.step_index],self.model.scheduler.sigmas[self.model.scheduler.step_index + 1]).to(model_output.device)if first_order or self.model.scheduler.config.solver_order == 1 or \self.model.scheduler.lower_order_nums < 1 or lower_order_final:prev_sample = self.model.scheduler.dpm_solver_first_order_update(model_output, sample=sample, noise=variance_noise)elif self.model.scheduler.config.solver_order == 2 or \self.model.scheduler.lower_order_nums < 2 or lower_order_second:prev_sample = self.model.scheduler.multistep_dpm_solver_second_order_update(self.model.scheduler.model_outputs, sample=sample, noise=variance_noise)if self.model.scheduler.lower_order_nums < self.model.scheduler.config.solver_order:self.model.scheduler.lower_order_nums += 1# upon completion increase step index by oneself.model.scheduler._step_index += 1return prev_sampledef unet_forward(self,sample: torch.FloatTensor,timestep: Union[torch.Tensor, float, int],encoder_hidden_states: torch.Tensor,encoder_attention_mask: Optional[torch.Tensor] = None,return_dict: bool = True,**kwargs) -> Tuple:# Create text_audio_duration_embeds and audio_duration_embedsembeds = torch.cat([encoder_hidden_states, self.seconds_start_hidden_states, self.seconds_end_hidden_states],dim=1)if encoder_attention_mask is None:# handle the batched caseif embeds.shape[0] > 1:embeds[0] = torch.zeros_like(embeds[0], device=embeds.device)else:embeds = torch.zeros_like(embeds, device=embeds.device)noise_pred = self.model.transformer(sample,timestep.unsqueeze(0),encoder_hidden_states=embeds,global_hidden_states=self.audio_duration_embeds,rotary_embedding=self.rotary_embedding)if not return_dict:return (noise_pred.sample,)return noise_pred, None, Nonedef load_model(model_id: str, device: torch.device,double_precision: bool = False, token: Optional[str] = None) -> PipelineWrapper:if 'audioldm2' in model_id:ldm_stable = AudioLDM2Wrapper(model_id=model_id, device=device, double_precision=double_precision, token=token)elif 'stable-audio' in model_id:ldm_stable = StableAudWrapper(model_id=model_id, device=device, double_precision=double_precision, token=token)ldm_stable.load_scheduler()torch.cuda.empty_cache()return ldm_stable
12345678torchnumpy<2torchaudiodiffusersacceleratetransformerstqdmsoundfile123456789torch>2.2.0numpy<2.0.0torchaudiodiffusersacceleratetorchsdetransformerstqdmsoundfile
1 unmodified line234567891 unmodified line11121314151617181920212223242526272829303132333435361 unmodified lineimport torchfrom typing import Optional, List, Tuple, NamedTuple, Unionfrom models import PipelineWrapperfrom audioldm.utils import get_durationclass PromptEmbeddings(NamedTuple):embedding_hidden_states: torch.Tensor1 unmodified lineboolean_prompt_mask: torch.Tensordef load_audio(audio_path: Union[str, np.array], fn_STFT, left: int = 0, right: int = 0, device: Optional[torch.device] = None) -> torch.tensor:if type(audio_path) is str:import audioldmimport audioldm.audioduration = min(get_duration(audio_path), 30)mel, _, _ = audioldm.audio.wav_to_fbank(audio_path, target_length=int(duration * 102.4), fn_STFT=fn_STFT)mel = mel.unsqueeze(0)else:mel = audio_pathc, h, w = mel.shapeleft = min(left, w-1)right = min(right, w - left - 1)mel = mel[:, :, left:w-right]mel = mel.unsqueeze(0).to(device)return meldef get_height_of_spectrogram(length: int, ldm_stable: PipelineWrapper) -> int:1 unmodified line234567891011121 unmodified line1415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869701 unmodified lineimport torchfrom typing import Optional, List, Tuple, NamedTuple, Unionfrom models import PipelineWrapperimport torchaudiofrom audioldm.utils import get_durationMAX_DURATION = 30class PromptEmbeddings(NamedTuple):embedding_hidden_states: torch.Tensor1 unmodified lineboolean_prompt_mask: torch.Tensordef load_audio(audio_path: Union[str, np.array], fn_STFT, left: int = 0, right: int = 0,device: Optional[torch.device] = None,return_wav: bool = False, stft: bool = False, model_sr: Optional[int] = None) -> torch.Tensor:if stft: # AudioLDM/tango loading to spectrogramif type(audio_path) is str:import audioldmimport audioldm.audioduration = get_duration(audio_path)if MAX_DURATION is not None:duration = min(duration, MAX_DURATION)mel, _, wav = audioldm.audio.wav_to_fbank(audio_path, target_length=int(duration * 102.4), fn_STFT=fn_STFT)mel = mel.unsqueeze(0)else:mel = audio_pathc, h, w = mel.shapeleft = min(left, w-1)right = min(right, w - left - 1)mel = mel[:, :, left:w-right]mel = mel.unsqueeze(0).to(device)if return_wav:return mel, 16000, duration, wavreturn mel, model_sr, durationelse:waveform, sr = torchaudio.load(audio_path)if sr != model_sr:waveform = torchaudio.functional.resample(waveform, orig_freq=sr, new_freq=model_sr)# waveform = waveform.numpy()[0, ...]def normalize_wav(waveform):waveform = waveform - torch.mean(waveform)waveform = waveform / (torch.max(torch.abs(waveform)) + 1e-8)return waveform * 0.5waveform = normalize_wav(waveform)# waveform = waveform[None, ...]# waveform = pad_wav(waveform, segment_length)# waveform = waveform[0, ...]waveform = torch.FloatTensor(waveform)if MAX_DURATION is not None:duration = min(waveform.shape[-1] / model_sr, MAX_DURATION)waveform = waveform[:, :int(duration * model_sr)]# cut waveformduration = waveform.shape[-1] / model_srreturn waveform, model_sr, durationdef get_height_of_spectrogram(length: int, ldm_stable: PipelineWrapper) -> int:
.gitattributes+1
Examples/Beethoven.wav → Examples/Beethoven.mp3Renamed+2-2
Examples/Cat_dog.wav → Examples/Beethoven_arcade.mp3Renamed+2-2
Examples/Beethoven_arcade.wav → Examples/Beethoven_piano.mp3Renamed+2-2
Examples/Beethoven_piano.wav → Examples/Beethoven_rock.mp3Renamed+2-2
Examples/Cat.wav → Examples/Cat.mp3Renamed+2-2
Examples/Cat_dog.mp3Added+3
Examples/ModalJazz.mp3Added+3
Examples/ModalJazz.wavDeleted-3
Examples/ModalJazz_banjo.mp3Added+3
Examples/ModalJazz_banjo.wavDeleted-3
Examples/Shadows.mp3Added+3
Examples/Shadows_arcade.mp3Added+3
README.md+4-1
app.py+235-158
inversion_utils.py+139-381
models.py+469-253
requirements.txt+3-2
utils.py+50-16