Spaces:
Runtime error
Runtime error
Commit
·
7efaeeb
1
Parent(s):
58b4760
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from diffusers import ControlNetModel, DiffusionPipeline
|
| 5 |
+
from diffusers.utils import load_image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import warnings
|
| 8 |
+
warnings.filterwarnings("ignore")
|
| 9 |
+
def resize_for_condition_image(input_image: Image, resolution: int):
|
| 10 |
+
input_image = input_image.convert("RGB")
|
| 11 |
+
W, H = input_image.size
|
| 12 |
+
k = float(resolution) / min(H, W)
|
| 13 |
+
H *= k
|
| 14 |
+
W *= k
|
| 15 |
+
H = int(round(H / 64.0)) * 64
|
| 16 |
+
W = int(round(W / 64.0)) * 64
|
| 17 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
| 18 |
+
return img
|
| 19 |
+
|
| 20 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
controlnet = ControlNetModel.from_pretrained('lllyasviel/control_v11f1e_sd15_tile',
|
| 22 |
+
torch_dtype=torch.float16)
|
| 23 |
+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
|
| 24 |
+
custom_pipeline="stable_diffusion_controlnet_img2img",
|
| 25 |
+
controlnet=controlnet,
|
| 26 |
+
torch_dtype=torch.float16).to(device)
|
| 27 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 28 |
+
|
| 29 |
+
source_image = load_image('https://huggingface.co/lllyasviel/control_v11f1e_sd15_tile/resolve/main/images/original.png')
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def super_esr(source_image,prompt,negative_prompt,strength,seed,num_inference_steps):
|
| 33 |
+
condition_image = resize_for_condition_image(source_image, 1024)
|
| 34 |
+
image = pipe(prompt=prompt,#"best quality",
|
| 35 |
+
negative_prompt="blur, lowres, bad anatomy, bad hands, cropped, worst quality",
|
| 36 |
+
image=condition_image,
|
| 37 |
+
controlnet_conditioning_image=condition_image,
|
| 38 |
+
width=condition_image.size[0],
|
| 39 |
+
height=condition_image.size[1],
|
| 40 |
+
strength=1.0,
|
| 41 |
+
generator=seed,
|
| 42 |
+
num_inference_steps=num_inference_steps,
|
| 43 |
+
).image
|
| 44 |
+
print(source_image,prompt,negative_prompt,strength,seed,num_inference_steps)
|
| 45 |
+
return source_image
|
| 46 |
+
|
| 47 |
+
#define laund take input nsame as super_esr function
|
| 48 |
+
def launch():
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.inputs.Image(type="pil",label="Source Image"),
|
| 51 |
+
gr.inputs.Textbox(lines=2,label="Prompt"),
|
| 52 |
+
gr.inputs.Textbox(lines=2,label="Negative Prompt"),
|
| 53 |
+
gr.inputs.Slider(minimum=0,maximum=1,label="Strength"),
|
| 54 |
+
gr.inputs.Slider(minimum=0,maximum=100,label="Seed"),
|
| 55 |
+
gr.inputs.Slider(minimum=0,maximum=100,label="Num Inference Steps")
|
| 56 |
+
]
|
| 57 |
+
outputs=[
|
| 58 |
+
gr.outputs.Image(type="pil",label="Output Image")
|
| 59 |
+
]
|
| 60 |
+
title="Super ESR"
|
| 61 |
+
description="Super ESR is a super resolution model that uses diffusion to generate high resolution images from low resolution images"
|
| 62 |
+
examples=[
|
| 63 |
+
["https://i.imgur.com/9IqyX1F.png","best quality","blur, lowres, bad anatomy, bad hands, cropped, worst quality",1.0,0,100],
|
| 64 |
+
["https://i.imgur.com/9IqyX1F.png","best quality","blur, lowres, bad anatomy, bad hands, cropped, worst quality",1.0,0,100],
|
| 65 |
+
]
|
| 66 |
+
gr.Interface(fn=super_esr,inputs=inputs,outputs=outputs,title=title,description=description,examples=examples).launch(share=True)
|
| 67 |
+
|
| 68 |
+
launch()
|