Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import gradio as grimport torchfrom torch import autocastfrom diffusers import StableDiffusionPipeline#model_id = "lunarfish/furrydiffusion"pipe = StableDiffusionPipeline.from_pretrained("lunarfish/furrydiffusion", torch_type=torch.float16, revision="main")num_samples = 2def infer(prompt):images = pipe([prompt] * num_samples, guidance_scale=7.5)["sample"]return imagesblock = gr.Blocks()examples = [['fox'],['rabbit'],['wolf'],]with block as demo:with gr.Group():with gr.Box():with gr.Row().style(mobile_collapse=False, equal_height=True):text = gr.Textbox(label="Enter your prompt", show_label=False, max_lines=1).style(border=(True, False, True, True),rounded=(True, False, False, True),container=False,)btn = gr.Button("Run").style(margin=False,rounded=(False, True, True, False),)gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="generated_id").style(grid=[2], height="auto")ex = gr.Examples(examples=examples, fn=infer, inputs=[text], outputs=gallery, cache_examples=True)ex.dataset.headers = [""]text.submit(infer, inputs=[text], outputs=gallery)btn.click(infer, inputs=[text], outputs=gallery)demo.queue(max_size=25).launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266from diffusers import AutoencoderKL, UNet2DConditionModel, StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepSchedulerimport gradio as grimport torchfrom PIL import Imageimport utilsimport datetimeimport timeimport psutilstart_time = time.time()is_colab = utils.is_google_colab()class Model:def __init__(self, name, path="", prefix=""):self.name = nameself.path = pathself.prefix = prefixself.pipe_t2i = Noneself.pipe_i2i = Nonemodels = [Model("FurryDiffusion", "lunarfish/furrydiffusion", "Furry Diffusion Style"),]scheduler = DPMSolverMultistepScheduler(beta_start=0.00085,beta_end=0.012,beta_schedule="scaled_linear",num_train_timesteps=1000,trained_betas=None,predict_epsilon=True,thresholding=False,algorithm_type="dpmsolver++",solver_type="midpoint",lower_order_final=True,)custom_model = Noneif is_colab:models.insert(0, Model("Custom model"))custom_model = models[0]last_mode = "txt2img"current_model = models[1] if is_colab else models[0]current_model_path = current_model.pathif is_colab:pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16, scheduler=scheduler, safety_checker=lambda images, clip_input: (images, False))else: # download all modelsprint(f"{datetime.datetime.now()} Downloading vae...")vae = AutoencoderKL.from_pretrained(current_model.path, subfolder="vae", torch_dtype=torch.float16)for model in models:try:print(f"{datetime.datetime.now()} Downloading {model.name} model...")unet = UNet2DConditionModel.from_pretrained(model.path, subfolder="unet", torch_dtype=torch.float16)model.pipe_t2i = StableDiffusionPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16, scheduler=scheduler)model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16, scheduler=scheduler)except Exception as e:print(f"{datetime.datetime.now()} Failed to load model " + model.name + ": " + str(e))models.remove(model)pipe = models[0].pipe_t2iif torch.cuda.is_available():pipe = pipe.to("cuda")device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"def error_str(error, title="Error"):return f"""#### {title}{error}""" if error else ""def custom_model_changed(path):models[0].path = pathglobal current_modelcurrent_model = models[0]def on_model_change(model_name):prefix = "Enter prompt. \"" + next((m.prefix for m in models if m.name == model_name), None) + "\" is prefixed automatically" if model_name != models[0].name else "Don't forget to use the custom model prefix in the prompt!"return gr.update(visible = model_name == models[0].name), gr.update(placeholder=prefix)def inference(model_name, prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):print(psutil.virtual_memory()) # print memory usageglobal current_modelfor model in models:if model.name == model_name:current_model = modelmodel_path = current_model.pathgenerator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else Nonetry:if img is not None:return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), Noneelse:return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator), Noneexcept Exception as e:return None, error_str(e)def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator):print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")global last_modeglobal pipeglobal current_model_pathif model_path != current_model_path or last_mode != "txt2img":current_model_path = model_pathif is_colab or current_model == custom_model:pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16, scheduler=scheduler, safety_checker=lambda images, clip_input: (images, False))else:pipe = pipe.to("cpu")pipe = current_model.pipe_t2iif torch.cuda.is_available():pipe = pipe.to("cuda")last_mode = "txt2img"prompt = current_model.prefix + promptresult = pipe(prompt,negative_prompt = neg_prompt,# num_images_per_prompt=n_images,num_inference_steps = int(steps),guidance_scale = guidance,width = width,height = height,generator = generator)return replace_nsfw_images(result)def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):print(f"{datetime.datetime.now()} img_to_img, model: {model_path}")global last_modeglobal pipeglobal current_model_pathif model_path != current_model_path or last_mode != "img2img":current_model_path = model_pathif is_colab or current_model == custom_model:pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16, scheduler=scheduler, safety_checker=lambda images, clip_input: (images, False))else:pipe = pipe.to("cpu")pipe = current_model.pipe_i2iif torch.cuda.is_available():pipe = pipe.to("cuda")last_mode = "img2img"prompt = current_model.prefix + promptratio = min(height / img.height, width / img.width)img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)result = pipe(prompt,negative_prompt = neg_prompt,# num_images_per_prompt=n_images,init_image = img,num_inference_steps = int(steps),strength = strength,guidance_scale = guidance,width = width,height = height,generator = generator)return replace_nsfw_images(result)def replace_nsfw_images(results):if is_colab:return results.images[0]for i in range(len(results.images)):if results.nsfw_content_detected[i]:results.images[i] = Image.open("nsfw.png")return results.images[0]css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-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"""<div class="finetuned-diffusion-div"><div><h1>Furry Diffusion</h1></div><p>This demo is slow on cpu, to use it upgrade to gpu by going to settings after duplicating this space: <a style="display:inline-block" href="https://huggingface.co/spaces/lunarfish/furrydiffusion?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a> </p></p></div>""")with gr.Row():with gr.Column(scale=55):with gr.Group():model_name = gr.Dropdown(label="Model", choices=[m.name for m in models], value=current_model.name)with gr.Box(visible=False) as custom_model_group:custom_model_path = gr.Textbox(label="Custom model path", placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion", interactive=True)gr.HTML("<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>")with gr.Row():prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="Enter prompt. Style applied automatically").style(container=False)generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))image_out = gr.Image(height=512)# gallery = gr.Gallery(# label="Generated images", show_label=False, elem_id="gallery"# ).style(grid=[1], height="auto")error_output = gr.Markdown()with gr.Column(scale=45):with gr.Tab("Options"):with gr.Group():neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")# n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)with gr.Row():guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)with gr.Row():width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)with gr.Tab("Image to image"):with gr.Group():image = gr.Image(label="Image", height=256, tool="editor", type="pil")strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)if is_colab:model_name.change(on_model_change, inputs=model_name, outputs=[custom_model_group, prompt], queue=False)custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)inputs = [model_name, prompt, guidance, steps, width, height, seed, image, strength, neg_prompt]outputs = [image_out, error_output]prompt.submit(inference, inputs=inputs, outputs=outputs)generate.click(inference, inputs=inputs, outputs=outputs)ex = gr.Examples([[models[0].name, "iron man", 7.5, 50],], inputs=[model_name, prompt, guidance, steps, seed], outputs=outputs, fn=inference, cache_examples=False)gr.HTML("""<div style="border-top: 1px solid #303030;"><br><p>Model by Linaqruf</p></div>""")print(f"Space built in {time.time() - start_time:.2f} seconds")if not is_colab:demo.queue(concurrency_count=1)demo.launch(debug=is_colab, share=is_colab)
app.py+262-52