CoolFace
Apppublic

bigslime/StableDiffusion-Img2Img

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py45 linesDownload Raw Back to root
1import gradio as gr2import torch3import streamlit as st4from PIL import Image5import numpy as np6from io import BytesIO7from diffusers import StableDiffusionImg2ImgPipeline8 9device="cpu"10 11pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=st.secrets['USER_TOKEN'])12pipe.to(device)13 14def resize(w_val,l_val,img):15  img = Image.open(img)16  img = img.resize((w_val,l_val), Image.Resampling.LANCZOS)17  #img = img.resize((value,value), Image.Resampling.LANCZOS)18  return img19 20 21def infer(source_img, prompt, guide, steps, seed, Strength): 22    generator = torch.Generator('cpu').manual_seed(seed)     23    source_image = resize(768, 512, source_img)24    source_image.save('source.png')25    image_list = pipe([prompt], init_image=source_image, strength=Strength, guidance_scale=guide, num_inference_steps=steps)26    images = []27    safe_image = Image.open(r"unsafe.png")28    for i, image in enumerate(image_list["sample"]):29        if(image_list["nsfw_content_detected"][i]):30            images.append(safe_image)31        else:32            images.append(image)    33    return image34 35gr.Interface(fn=infer, inputs=[gr.Image(source="upload", type="filepath", label="Raw Image"), gr.Textbox(label = 'Prompt Input Text'),36    gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),37    gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),38    gr.Slider(39        label = "Seed",40        minimum = 0,41        maximum = 2147483647,42        step = 1,43        randomize = True), gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .5)44    ], outputs='image').queue(max_size=10).launch(enable_queue=True)45