Spaces:
Paused
Paused
Update app.py
Browse files9 unmodified lines1011121314151 unmodified line17181920212223242526272829303132333435363738394041424344452 unmodified lines484950515253545556575859606162632 unmodified lines6667686970717217 unmodified lines9091929394959660 unmodified lines1571581591601611621639 unmodified linesfrom safetensors.torch import load_filefrom PIL import Image# Constantsbases = {"Cartoon": "frankjoshua/toonyou_beta6",1 unmodified line"3d": "Lykon/DreamShaper","Anime": "Yntec/mistoonAnime2"}# Preload modelsif not torch.cuda.is_available():raise NotImplementedError("No GPU detected!")device = "cuda"dtype = torch.float16motion_loaded = Nonepipes = {}for base_name, base_path in bases.items():pipe = AnimateDiffPipeline.from_pretrained(base_path, torch_dtype=dtype).to(device)pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")pipes[base_name] = pipe# Function@spaces.GPU(duration=60,queue=False)def generate_image(prompt, base="Realistic", motion="Default", step=8, progress=gr.Progress()):global pipesglobal motion_loadedpipe = pipes[base]if motion_loaded != motion:pipe.unload_lora_weights()2 unmodified linespipe.set_adapters(["motion"], [0.7])motion_loaded = motion# Load step model if not already loadedrepo = "ByteDance/AnimateDiff-Lightning"ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"try:pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt, local_files_only=True), device=device), strict=False)except:pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)# Generate imageoutput = pipe(prompt=f"{base} image of {prompt}", guidance_scale=1.2, num_inference_steps=step)name = str(uuid.uuid4()).replace("-", "")2 unmodified linesreturn path# Gradio Interfacewith gr.Blocks(css="style.css") as demo:gr.HTML(17 unmodified lines"3d","Anime",],value="Realistic",interactive=True)select_motion = gr.Dropdown(60 unmodified linesfn=generate_image,inputs=[prompt],outputs=[video],cache_examples="lazy",)demo.queue().launch()9 unmodified lines101112131415161718191 unmodified line21222324252627282930313233343536373839404142434445464748495051525354555657585960612 unmodified lines6465666768692 unmodified lines72737475767717 unmodified lines959697989910010160 unmodified lines1621631641651661671681691709 unmodified linesfrom safetensors.torch import load_filefrom PIL import ImageMORE = """ ## TRY Other Demos### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image"""# Constantsbases = {"Cartoon": "frankjoshua/toonyou_beta6",1 unmodified line"3d": "Lykon/DreamShaper","Anime": "Yntec/mistoonAnime2"}step_loaded = Nonebase_loaded = "Realistic"motion_loaded = None# Ensure model and scheduler are initialized in GPU-enabled functionif not torch.cuda.is_available():raise NotImplementedError("No GPU detected!")device = "cuda"dtype = torch.float16pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")# Safety checkersfrom transformers import CLIPFeatureExtractorfeature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")# Function@spaces.GPU(duration=60,queue=False)def generate_image(prompt, base="Realistic", motion="", step=8, progress=gr.Progress()):global step_loadedglobal base_loadedglobal motion_loadedprint(prompt, base, step)if step_loaded != step:repo = "ByteDance/AnimateDiff-Lightning"ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)step_loaded = stepif base_loaded != base:pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)base_loaded = baseif motion_loaded != motion:pipe.unload_lora_weights()2 unmodified linespipe.set_adapters(["motion"], [0.7])motion_loaded = motionoutput = pipe(prompt=f"{base} image of {prompt}", guidance_scale=1.2, num_inference_steps=step)name = str(uuid.uuid4()).replace("-", "")2 unmodified linesreturn path# Gradio Interfacewith gr.Blocks(css="style.css") as demo:gr.HTML(17 unmodified lines"3d","Anime",],value=base_loaded,interactive=True)select_motion = gr.Dropdown(60 unmodified linesfn=generate_image,inputs=[prompt],outputs=[video],cache_examples=True,)demo.queue().launch()Translate
app.py+31-24