CoolFace
Apppublic

DarthVaderAI/Diffusion-Art

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
1likes
app.py47 linesDownload Raw Back to root
1import gradio as gr2import torch3 4from PIL import Image5 6from PIL import Image7from  Diffusion import diffusionandclipimagegenereation18 9device="cpu"10 11source_img = gr.Image(source="upload", type="filepath", label="init_img | 256*256px")12gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")13 14ef resize(value,img):15  #baseheight = value16  img = Image.open(img)17  #hpercent = (baseheight/float(img.size[1]))18  #wsize = int((float(img.size[0])*float(hpercent)))19  #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)20  img = img.resize((value,value), Image.Resampling.LANCZOS)21  return img22 23 24def infer(source_img, prompt, guide, steps, seed, strength): 25    generator = torch.Generator('cpu').manual_seed(seed)26    27    source_image = resize(512, source_img)28    source_image.save('source.png')29    30    images_list = img_pipe([prompt] * 2, init_image=source_image, strength=strength, guidance_scale=guide, num_inference_steps=steps)31    images = []32    safe_image = Image.open(r"unsafe.png")33    34    for i, image in enumerate(images_list["sample"]):35        if(images_list["nsfw_content_detected"][i]):36            images.append(safe_image)37        else:38            images.append(image)    39    return images40 41gr.Interface(fn=infer, inputs=[source_img,42    "text",43    gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),44    gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),45    gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True),46    gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .75)],47    outputs=gallery,title=title,description=description, allow_flagging="manual", flagging_dir="flagged").queue(max_size=100).launch(enable_queue=True)