SDv2 Simpsons
Demo for SD2 Simpsons BLIP Stable Diffusion 2, fine-tuned model.
{"Add the following tokens to your prompts for the model to work properly: prefix" if prefix else ""}
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler import gradio as gr import torch from PIL import Image model_id = 'Norod78/sd2-simpsons-blip' prefix = None scheduler = DPMSolverMultistepScheduler( beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=1000, trained_betas=None, thresholding=False, algorithm_type="dpmsolver++", solver_type="midpoint", lower_order_final=True, ) pipe = StableDiffusionPipeline.from_pretrained( model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, scheduler=scheduler) pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained( model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, scheduler=scheduler) if torch.cuda.is_available(): pipe = pipe.to("cuda") pipe_i2i = pipe_i2i.to("cuda") def error_str(error, title="Error"): return f"""#### {title} {error}""" if error else "" def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""): if torch.cuda.is_available(): generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None else: if seed != 0: generator = torch.Generator() generator.manual_seed(seed) else: generator = None try: if img is not None: return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None else: return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None except Exception as e: return None, error_str(e) def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator): result = pipe( prompt, negative_prompt = neg_prompt, num_inference_steps = int(steps), guidance_scale = guidance, width = width, height = height, generator = generator) return replace_nsfw_images(result) def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator): ratio = min(height / img.height, width / img.width) img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.Resampling.LANCZOS) result = pipe_i2i( prompt, negative_prompt = neg_prompt, image = img, num_inference_steps = int(steps), strength = strength, guidance_scale = guidance, generator = generator) return replace_nsfw_images(result) def replace_nsfw_images(results): for i in range(len(results.images)): if 'nsfw_content_detected' in results and results.nsfw_content_detected[i]: results.images[i] = Image.open("nsfw.png") return results.images[0] css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem} """ with gr.Blocks(css=css) as demo: gr.HTML( f"""
Demo for SD2 Simpsons BLIP Stable Diffusion 2, fine-tuned model.
{"Add the following tokens to your prompts for the model to work properly: prefix" if prefix else ""}
This space was created using SD Space Creator.