File size: 3,031 Bytes
7efaeeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c67855
3451c21
c11e306
 
 
 
 
 
 
4c67855
c11e306
4c67855
3451c21
c11e306
7efaeeb
4c67855
7efaeeb
0538750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c67855
21d4e0f
 
3451c21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

import torch
from PIL import Image
from diffusers import ControlNetModel, DiffusionPipeline
from diffusers.utils import load_image
import gradio as gr 
import warnings
warnings.filterwarnings("ignore")
def resize_for_condition_image(input_image: Image, resolution: int):
    input_image = input_image.convert("RGB")
    W, H = input_image.size
    k = float(resolution) / min(H, W)
    H *= k
    W *= k
    H = int(round(H / 64.0)) * 64
    W = int(round(W / 64.0)) * 64
    img = input_image.resize((W, H), resample=Image.LANCZOS)
    return img

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
controlnet = ControlNetModel.from_pretrained('lllyasviel/control_v11f1e_sd15_tile', 
                                             torch_dtype=torch.float16)
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
                                         custom_pipeline="stable_diffusion_controlnet_img2img",
                                         controlnet=controlnet,
                                         torch_dtype=torch.float16).to(device)
pipe.enable_xformers_memory_efficient_attention()

def super_esr(source_image,prompt,negative_prompt,strength,seed,num_inference_steps):
    condition_image = resize_for_condition_image(source_image, 1024)
    generator =  torch.Generator(device="cuda").manual_seed(seed)
    
    image = pipe(prompt=prompt,#"best quality", 
                negative_prompt="blur, lowres, bad anatomy, bad hands, cropped, worst quality", 
                image=condition_image, 
                controlnet_conditioning_image=condition_image, 
                width=condition_image.size[0],
                height=condition_image.size[1],
                strength=1.0,
                generator=generator,
                num_inference_steps=num_inference_steps,
                ).images[0]
    # print(source_image,prompt,negative_prompt,strength,seed,num_inference_steps)
    return image

# define and take input the same as the super_esr function

inputs=[
    gr.inputs.Image(type="pil",label="Source Image"),
    gr.inputs.Textbox(lines=2,label="Prompt"),
    gr.inputs.Textbox(lines=2,label="Negative Prompt"),
    gr.inputs.Slider(minimum=0,maximum=1,label="Strength"),
    gr.inputs.Slider(minimum=0,maximum=100,label="Seed"),
    gr.inputs.Slider(minimum=0,maximum=100,label="Num Inference Steps")
]
outputs=[
    gr.outputs.Image(type="pil",label="Output Image")
]
title="Super ESR"
description="Super ESR is a super resolution model that uses diffusion to generate high resolution images from low resolution images"
examples=[
    ["https://i.imgur.com/9IqyX1F.png","best quality","blur, lowres, bad anatomy, bad hands, cropped, worst quality",1.0,0,100],
    ["https://i.imgur.com/9IqyX1F.png","best quality","blur, lowres, bad anatomy, bad hands, cropped, worst quality",1.0,0,100],
]
# create a queue of the requests
x=gr.Interface(fn=super_esr,inputs=inputs,outputs=outputs,title=title,description=description,examples=examples)
x.launch()