-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (129 loc) · 4.83 KB
/
app.py
File metadata and controls
146 lines (129 loc) · 4.83 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import random
import os
from argparse import ArgumentParser
import gradio as gr
import torchvision.transforms as transforms
from accelerate.utils import set_seed
from omegaconf import OmegaConf
from dotenv import load_dotenv
from PIL import Image
from HYPIR.enhancer.sd2 import SD2Enhancer
from HYPIR.utils.captioner import GPTCaptioner
load_dotenv()
error_image = Image.open(os.path.join("assets", "gradio_error_img.png"))
parser = ArgumentParser()
parser.add_argument("--config", type=str, required=True)
parser.add_argument("--local", action="store_true")
parser.add_argument("--port", type=int, default=7860)
parser.add_argument("--gpt_caption", action="store_true")
parser.add_argument("--max_size", type=str, default=None, help="Comma-seperated image size")
parser.add_argument("--device", type=str, default="cuda")
args = parser.parse_args()
max_size = args.max_size
if max_size is not None:
max_size = tuple(int(x) for x in max_size.split(","))
if len(max_size) != 2:
raise ValueError(f"Invalid max size: {max_size}")
print(f"Max size set to {max_size}, max pixels: {max_size[0] * max_size[1]}")
if args.gpt_caption:
if (
"GPT_API_KEY" not in os.environ
or "GPT_BASE_URL" not in os.environ
or "GPT_MODEL" not in os.environ
):
raise ValueError(
"If you want to use gpt-generated caption, "
"please specify both `GPT_API_KEY`, `GPT_BASE_URL` and `GPT_MODEL` in your .env file. "
"See README.md for more details."
)
captioner = GPTCaptioner(
api_key=os.getenv("GPT_API_KEY"),
base_url=os.getenv("GPT_BASE_URL"),
model=os.getenv("GPT_MODEL"),
)
to_tensor = transforms.ToTensor()
config = OmegaConf.load(args.config)
if config.base_model_type == "sd2":
model = SD2Enhancer(
base_model_path=config.base_model_path,
weight_path=config.weight_path,
lora_modules=config.lora_modules,
lora_rank=config.lora_rank,
model_t=config.model_t,
coeff_t=config.coeff_t,
device=args.device,
)
model.init_models()
else:
raise ValueError(config.base_model_type)
def process(
image,
prompt,
upscale,
patch_size,
stride,
seed,
progress=gr.Progress(track_tqdm=True),
):
if seed == -1:
seed = random.randint(0, 2**32 - 1)
set_seed(seed)
image = image.convert("RGB")
# Check image size
if max_size is not None:
out_w, out_h = tuple(int(x * upscale) for x in image.size)
if out_w * out_h > max_size[0] * max_size[1]:
return error_image, (
"Failed: The requested resolution exceeds the maximum pixel limit. "
f"Your requested resolution is ({out_h}, {out_w}). "
f"The maximum allowed pixel count is {max_size[0]} x {max_size[1]} "
f"= {max_size[0] * max_size[1]} :("
)
if prompt == "auto":
if args.gpt_caption:
prompt = captioner(image)
else:
return error_image, "Failed: This gradio is not launched with gpt-caption support :("
image_tensor = to_tensor(image).unsqueeze(0)
try:
pil_image = model.enhance(
lq=image_tensor,
prompt=prompt,
upscale=upscale,
patch_size=patch_size,
stride=stride,
return_type="pil",
)[0]
except Exception as e:
return error_image, f"Failed: {e} :("
return pil_image, f"Success! :)\nUsed prompt: {prompt}"
MARKDOWN = """
## HYPIR: Harnessing Diffusion-Yielded Score Priors for Image Restoration
[GitHub](https://github.com/XPixelGroup/HYPIR) | [Paper](TODO) | [Project Page](TODO)
If HYPIR is helpful for you, please help star the GitHub Repo. Thanks!
"""
block = gr.Blocks().queue()
with block:
with gr.Row():
gr.Markdown(MARKDOWN)
with gr.Row():
with gr.Column():
image = gr.Image(type="pil")
prompt = gr.Textbox(label=(
"Prompt (Input 'auto' to use gpt-generated caption)"
if args.gpt_caption else "Prompt"
))
upscale = gr.Slider(minimum=1, maximum=8, value=1, label="Upscale Factor", step=1)
patch_size = gr.Slider(minimum=512, maximum=1024, value=512, label="Patch Size", step=128)
stride = gr.Slider(minimum=256, maximum=1024, value=256, label="Patch Stride", step=128)
seed = gr.Number(label="Seed", value=-1)
run = gr.Button(value="Run")
with gr.Column():
result = gr.Image(type="pil", format="png")
status = gr.Textbox(label="status", interactive=False)
run.click(
fn=process,
inputs=[image, prompt, upscale, patch_size, stride, seed],
outputs=[result, status],
)
block.launch(server_name="0.0.0.0" if not args.local else "127.0.0.1", server_port=args.port)