File size: 2,909 Bytes
3cd3a7f
 
 
fe0213c
7efaeeb
 
 
 
 
 
 
fe0213c
7efaeeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300c3be
7efaeeb
35f7c80
7efaeeb
 
991a27b
3451c21
c11e306
 
 
 
 
 
f185777
991a27b
c11e306
991a27b
3451c21
c11e306
7efaeeb
991a27b
7efaeeb
0538750
f185777
 
 
 
 
 
0538750
 
f185777
0538750
 
 
991a27b
35f7c80
 
 
f185777
ecd5aa9
f185777
 
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
70
71
72
73
74
75
import os 
os.system("pip install gradio==4.8.0")
os.system("pip install -U gradio")
import spaces
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()

@spaces.GPU(enable_queue=True)
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=strength,
                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.Image(type="pil",label="Source Image"),
    gr.Textbox(lines=2,label="Prompt"),
    gr.Textbox(lines=2,label="Negative Prompt"),
    gr.Slider(minimum=0,maximum=1,value=1.0,label="Strength"),
    gr.Slider(minimum=-100000,maximum=100000,value=1,label="Seed"),
    gr.Slider(minimum=0,maximum=100,value=20,label="Num Inference Steps")
]
outputs=[
    gr.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"
# create a queue of the requests



demo=gr.Interface(fn=super_esr,inputs=inputs,outputs=outputs,title=title,description=description)

demo.queue(max_size=20).launch()
# demo.launch()