Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,977 Bytes
7c89d3a d7ca9c8 eeb4ef5 d7ca9c8 b22f2c5 7c89d3a b22f2c5 303d638 b22f2c5 a495ef9 7c89d3a 303d638 7c89d3a 303d638 eeb4ef5 7c89d3a b22f2c5 303d638 b22f2c5 303d638 b22f2c5 303d638 d7ca9c8 303d638 77f9404 7c89d3a 303d638 7c89d3a 303d638 3f35512 7c89d3a 2139aa8 7c89d3a 2139aa8 7c89d3a 2139aa8 7c89d3a 303d638 b000100 303d638 b22f2c5 303d638 b22f2c5 77f9404 303d638 77f9404 7c89d3a 83686fb 7c89d3a b22f2c5 83686fb b22f2c5 83686fb b22f2c5 83686fb b22f2c5 7c89d3a 303d638 b22f2c5 83686fb 7c89d3a b22f2c5 83686fb b22f2c5 83686fb b22f2c5 7c89d3a 877f3e5 7c89d3a 77f9404 0269eb6 e629ed6 0269eb6 5d6e388 af44365 5d6e388 68ae68f 0f15bd0 af44365 ec512c8 af44365 0f15bd0 af44365 540e65e 0f15bd0 540e65e ec512c8 af44365 ec512c8 0fd0930 ec512c8 af44365 ec512c8 0fd0930 af44365 |
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
import os
import torch
import gradio as gr
import spaces
from PIL import Image
from diffusers import DiffusionPipeline
from huggingface_hub import snapshot_download
from test_ccsr_tile import load_pipeline
import argparse
from accelerate import Accelerator
# Global variables
class ModelContainer:
def __init__(self):
self.pipeline = None
self.generator = None
self.accelerator = None
self.is_initialized = False
model_container = ModelContainer()
class Args:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@spaces.GPU
def initialize_models():
"""Initialize models only if they haven't been initialized yet"""
if model_container.is_initialized:
return True
try:
# Download model repository (only once)
model_path = snapshot_download(
repo_id="NightRaven109/CCSRModels",
token=os.environ['Read2']
)
# Set up default arguments
args = Args(
pretrained_model_path=os.path.join(model_path, "stable-diffusion-2-1-base"),
controlnet_model_path=os.path.join(model_path, "Controlnet"),
vae_model_path=os.path.join(model_path, "vae"),
mixed_precision="fp16",
tile_vae=False,
sample_method="ddpm",
vae_encoder_tile_size=1024,
vae_decoder_tile_size=224
)
# Initialize accelerator
model_container.accelerator = Accelerator(
mixed_precision=args.mixed_precision,
)
# Load pipeline
model_container.pipeline = load_pipeline(args, model_container.accelerator,
enable_xformers_memory_efficient_attention=False)
# Set models to eval mode
model_container.pipeline.unet.eval()
model_container.pipeline.controlnet.eval()
model_container.pipeline.vae.eval()
model_container.pipeline.text_encoder.eval()
# Move pipeline to CUDA and set to eval mode once
model_container.pipeline = model_container.pipeline.to("cuda")
# Initialize generator
model_container.generator = torch.Generator("cuda")
# Set initialization flag
model_container.is_initialized = True
return True
except Exception as e:
print(f"Error initializing models: {str(e)}")
return False
@torch.no_grad() # Add no_grad decorator for inference
@spaces.GPU
def process_image(
input_image,
prompt="clean, texture, high-resolution, 8k",
negative_prompt="blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed",
guidance_scale=2.5,
conditioning_scale=1.0,
num_inference_steps=6,
seed=None,
upscale_factor=4,
color_fix_method="adain"
):
# Initialize models if not already done
if not model_container.is_initialized:
if not initialize_models():
return None
try:
# Create args object
args = Args(
added_prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
conditioning_scale=conditioning_scale,
num_inference_steps=num_inference_steps,
seed=seed,
upscale=upscale_factor,
process_size=512,
align_method=color_fix_method,
t_max=0.6666,
t_min=0.0,
tile_diffusion=False,
tile_diffusion_size=None,
tile_diffusion_stride=None,
start_steps=999,
start_point='lr',
use_vae_encode_condition=True,
sample_times=1
)
# Set seed if provided
if seed is not None:
model_container.generator.manual_seed(seed)
# Process input image
validation_image = Image.fromarray(input_image)
ori_width, ori_height = validation_image.size
# Resize logic
resize_flag = False
if ori_width < args.process_size//args.upscale or ori_height < args.process_size//args.upscale:
scale = (args.process_size//args.upscale)/min(ori_width, ori_height)
validation_image = validation_image.resize((round(scale*ori_width), round(scale*ori_height)))
resize_flag = True
validation_image = validation_image.resize((validation_image.size[0]*args.upscale, validation_image.size[1]*args.upscale))
validation_image = validation_image.resize((validation_image.size[0]//8*8, validation_image.size[1]//8*8))
width, height = validation_image.size
# Generate image
inference_time, output = model_container.pipeline(
args.t_max,
args.t_min,
args.tile_diffusion,
args.tile_diffusion_size,
args.tile_diffusion_stride,
args.added_prompt,
validation_image,
num_inference_steps=args.num_inference_steps,
generator=model_container.generator,
height=height,
width=width,
guidance_scale=args.guidance_scale,
negative_prompt=args.negative_prompt,
conditioning_scale=args.conditioning_scale,
start_steps=args.start_steps,
start_point=args.start_point,
use_vae_encode_condition=True,
)
image = output.images[0]
# Apply color fixing if specified
if args.align_method != "none":
from myutils.wavelet_color_fix import wavelet_color_fix, adain_color_fix
fix_func = wavelet_color_fix if args.align_method == "wavelet" else adain_color_fix
image = fix_func(image, validation_image)
if resize_flag:
image = image.resize((ori_width*args.upscale, ori_height*args.upscale))
return image
except Exception as e:
print(f"Error processing image: {str(e)}")
import traceback
traceback.print_exc()
return None
# Define default values
DEFAULT_VALUES = {
"prompt": "clean, texture, high-resolution, 8k",
"negative_prompt": "blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed",
"guidance_scale": 3,
"conditioning_scale": 1.0,
"num_steps": 6,
"seed": None,
"upscale_factor": 4,
"color_fix_method": "adain"
}
# Define example data
EXAMPLES = [
[
"examples/1.png", # Input image path
"clean, texture, high-resolution, 8k", # Prompt
"blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed", # Negative prompt
3.0, # Guidance scale
1.0, # Conditioning scale
6, # Num steps
42, # Seed
4, # Upscale factor
"wavelet" # Color fix method
],
[
"examples/22.png",
"clean, texture, high-resolution, 8k",
"blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed",
3.0,
1.0,
6,
123,
4,
"wavelet"
],
[
"examples/4.png",
"clean, texture, high-resolution, 8k",
"blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed",
3.0,
1.0,
6,
123,
4,
"wavelet"
],
[
"examples/9D03D7F206775949.png",
"clean, texture, high-resolution, 8k",
"blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed",
3.0,
1.0,
6,
123,
4,
"wavelet"
],
[
"examples/3.jpeg",
"clean, texture, high-resolution, 8k",
"blurry, dotted, noise, raster lines, unclear, lowres, over-smoothed",
2.5,
1.0,
6,
456,
4,
"wavelet"
]
]
# Create interface components
with gr.Blocks(title="Texture Super-Resolution") as demo:
gr.Markdown("## Texture Super-Resolution")
gr.Markdown("Upload a texture to enhance its resolution.")
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Input Image")
with gr.Accordion("Advanced Options", open=False):
prompt = gr.Textbox(label="Prompt", value=DEFAULT_VALUES["prompt"])
negative_prompt = gr.Textbox(label="Negative Prompt", value=DEFAULT_VALUES["negative_prompt"])
guidance_scale = gr.Slider(minimum=1.0, maximum=20.0, value=DEFAULT_VALUES["guidance_scale"], label="Guidance Scale")
conditioning_scale = gr.Slider(minimum=0.1, maximum=2.0, value=DEFAULT_VALUES["conditioning_scale"], label="Conditioning Scale")
num_steps = gr.Slider(minimum=1, maximum=50, value=DEFAULT_VALUES["num_steps"], step=1, label="Number of Steps")
seed = gr.Number(label="Seed", value=DEFAULT_VALUES["seed"])
upscale_factor = gr.Slider(minimum=1, maximum=8, value=DEFAULT_VALUES["upscale_factor"], step=1, label="Upscale Factor")
color_fix_method = gr.Dropdown(
choices=["none", "wavelet", "adain"],
label="Color Fix Method",
value=DEFAULT_VALUES["color_fix_method"]
)
with gr.Row():
clear_btn = gr.Button("Clear")
submit_btn = gr.Button("Submit", variant="primary")
with gr.Column():
output_image = gr.Image(label="Generated Image")
# Add examples
gr.Examples(
examples=EXAMPLES,
inputs=[
input_image, prompt, negative_prompt, guidance_scale,
conditioning_scale, num_steps, seed, upscale_factor,
color_fix_method
],
outputs=output_image,
fn=process_image,
cache_examples=True # Cache the results for faster loading
)
# Define submit action
submit_btn.click(
fn=process_image,
inputs=[
input_image, prompt, negative_prompt, guidance_scale,
conditioning_scale, num_steps, seed, upscale_factor,
color_fix_method
],
outputs=output_image
)
# Define clear action that resets to default values
def reset_to_defaults():
return [
None, # input_image
DEFAULT_VALUES["prompt"],
DEFAULT_VALUES["negative_prompt"],
DEFAULT_VALUES["guidance_scale"],
DEFAULT_VALUES["conditioning_scale"],
DEFAULT_VALUES["num_steps"],
DEFAULT_VALUES["seed"],
DEFAULT_VALUES["upscale_factor"],
DEFAULT_VALUES["color_fix_method"]
]
clear_btn.click(
fn=reset_to_defaults,
inputs=None,
outputs=[
input_image, prompt, negative_prompt, guidance_scale,
conditioning_scale, num_steps, seed, upscale_factor,
color_fix_method
]
)
if __name__ == "__main__":
demo.launch() |