import spaces from diffusers import ( StableDiffusionPipeline, DPMSolverMultistepScheduler, DiffusionPipeline, ) import gradio as gr import torch from PIL import Image import time import psutil import random from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker start_time = time.time() current_steps = 25 SAFETY_CHECKER = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker", torch_dtype=torch.float16) UPSCALER = DiffusionPipeline.from_pretrained("stabilityai/sd-x2-latent-upscaler", torch_dtype=torch.float16) UPSCALER.to("cuda") UPSCALER.enable_xformers_memory_efficient_attention() class Model: def __init__(self, name, path=""): self.name = name self.path = path if path != "": self.pipe_t2i = StableDiffusionPipeline.from_pretrained( path, torch_dtype=torch.float16, safety_checker=SAFETY_CHECKER ) self.pipe_t2i.scheduler = DPMSolverMultistepScheduler.from_config( self.pipe_t2i.scheduler.config ) else: self.pipe_t2i = None models = [ #Model("Stable Diffusion v1-4", "CompVis/stable-diffusion-v1-4"), # Model("Stable Diffusion v1-5", "runwayml/stable-diffusion-v1-5"), Model("anything-v4.0", "xyn-ai/anything-v4.0"), ] MODELS = {m.name: m for m in models} device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶" def error_str(error, title="Error"): return ( f"""#### {title} {error}""" if error else "" ) @spaces.GPU def inference( prompt, neg_prompt, guidance, steps, seed, model_name, ): print(psutil.virtual_memory()) # print memory usage if seed == 0: seed = random.randint(0, 2147483647) generator = torch.Generator("cuda").manual_seed(seed) try: low_res_image, up_res_image = txt_to_img( model_name, prompt, neg_prompt, guidance, steps, generator, ) return low_res_image, up_res_image, f"Done. Seed: {seed}", except Exception as e: return None, None, error_str(e) def txt_to_img( model_name, prompt, neg_prompt, guidance, steps, generator, ): pipe = MODELS[model_name].pipe_t2i if torch.cuda.is_available(): pipe = pipe.to("cuda") pipe.enable_xformers_memory_efficient_attention() low_res_latents = pipe( prompt, negative_prompt=neg_prompt, num_inference_steps=int(steps), guidance_scale=guidance, generator=generator, output_type="latent", ).images with torch.no_grad(): low_res_image = pipe.decode_latents(low_res_latents) low_res_image = pipe.numpy_to_pil(low_res_image) up_res_image = UPSCALER( prompt=prompt, negative_prompt=neg_prompt, image=low_res_latents, num_inference_steps=20, guidance_scale=0, generator=generator, ).images pipe.to("cpu") torch.cuda.empty_cache() return low_res_image[0], up_res_image[0] def replace_nsfw_images(results): for i in range(len(results.images)): if results.nsfw_content_detected[i]: results.images[i] = Image.open("nsfw.png") return results.images with gr.Blocks(css="style.css") as demo: gr.HTML( f"""
Demo for the Anything v4 model hooked with the ultra-fast Latent Upscaler
Space by 🤗 Hugging Face, models by Stability AI, andite, linaqruf and others ❤️
This space uses the DPM-Solver++ sampler by Cheng Lu, et al..
This is a Demo Space For:
Stability AI's Latent Upscaler