File size: 5,489 Bytes
833ef3a
 
 
 
456ed62
833ef3a
 
 
 
 
 
 
8d65abf
c43a736
 
8d65abf
c43a736
 
8d65abf
833ef3a
 
 
 
 
 
456ed62
3960c92
 
 
 
c43a736
456ed62
3960c92
8d65abf
 
 
3960c92
8d65abf
 
833ef3a
c2d0882
 
c43a736
c2d0882
 
833ef3a
456ed62
3960c92
 
 
 
 
 
 
833ef3a
3960c92
 
 
 
 
 
 
 
 
 
c43a736
 
 
 
c2d0882
833ef3a
3960c92
c43a736
 
e683bf1
 
833ef3a
8d65abf
 
c43a736
 
 
 
 
 
8d65abf
c43a736
833ef3a
 
 
 
 
 
 
 
 
 
c2d0882
8d65abf
3960c92
 
8d65abf
c2d0882
 
 
c43a736
8d65abf
 
3960c92
e683bf1
 
8d65abf
e683bf1
 
c43a736
8d65abf
 
e683bf1
 
8d65abf
 
3960c92
 
8d65abf
 
c2d0882
 
c43a736
c2d0882
833ef3a
c2d0882
833ef3a
 
 
 
 
 
 
 
8d65abf
 
c43a736
833ef3a
c43a736
833ef3a
 
8d65abf
833ef3a
c43a736
833ef3a
8d65abf
833ef3a
 
8d65abf
833ef3a
c43a736
833ef3a
c43a736
833ef3a
 
 
 
 
 
 
 
8d65abf
833ef3a
 
8d65abf
833ef3a
d0f30fa
 
833ef3a
 
c43a736
 
833ef3a
 
 
 
 
 
 
 
 
 
 
c43a736
833ef3a
0fb7ee6
833ef3a
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9245b
3960c92
e683bf1
 
 
 
 
8d65abf
 
 
e683bf1
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
import logging
import random
import warnings
import os
import gradio as gr
import numpy as np
import torch
from diffusers import FluxControlNetModel
from diffusers.pipelines import FluxControlNetPipeline
from gradio_imageslider import ImageSlider
from PIL import Image
from huggingface_hub import snapshot_download
import gc

# Clear memory
gc.collect()
if torch.cuda.is_available():
    torch.cuda.empty_cache()

css = """
#col-container {
    margin: 0 auto;
    max-width: 512px;
}
"""

# Device configuration
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float32

huggingface_token = os.getenv("HF_TOKEN")

# Modified model configuration
model_config = {
    "low_cpu_mem_usage": True,
    "torch_dtype": dtype,
    "use_safetensors": False,  # Disabled safetensors
}

model_path = snapshot_download(
    repo_id="black-forest-labs/FLUX.1-dev",
    repo_type="model",
    ignore_patterns=["*.md", "*..gitattributes", "*.bin"],
    local_dir="FLUX.1-dev", 
    token=huggingface_token,
)

# Load models with modified configuration
try:
    controlnet = FluxControlNetModel.from_pretrained(
        "jasperai/Flux.1-dev-Controlnet-Upscaler",
        **model_config
    )
    controlnet.to(device)

    pipe = FluxControlNetPipeline.from_pretrained(
        model_path,
        controlnet=controlnet,
        **model_config
    )
    pipe.to(device)

except Exception as e:
    print(f"Error loading models: {str(e)}")
    raise

# Enable optimizations
pipe.enable_attention_slicing(1)
pipe.enable_vae_slicing()

MAX_SEED = 1000000
MAX_PIXEL_BUDGET = 64 * 64

def process_input(input_image, upscale_factor):
    input_image = input_image.convert('RGB')
    
    w, h = input_image.size
    max_size = int(np.sqrt(MAX_PIXEL_BUDGET))
    
    new_w = min(w, max_size)
    new_h = min(h, max_size)
    input_image = input_image.resize((new_w, new_h), Image.LANCZOS)
    
    w = new_w - new_w % 8
    h = new_h - new_h % 8
    
    return input_image.resize((w, h)), w, h

def infer(
    seed,
    randomize_seed,
    input_image,
    num_inference_steps,
    upscale_factor,
    controlnet_conditioning_scale,
    progress=gr.Progress(track_tqdm=True),
):
    try:
        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        
        if randomize_seed:
            seed = random.randint(0, MAX_SEED)
            
        input_image, w, h = process_input(input_image, upscale_factor)
        
        with torch.inference_mode():
            generator = torch.Generator(device=device).manual_seed(seed)
            image = pipe(
                prompt="",
                control_image=input_image,
                controlnet_conditioning_scale=controlnet_conditioning_scale,
                num_inference_steps=num_inference_steps,
                guidance_scale=1.5,
                height=h,
                width=w,
                generator=generator,
            ).images[0]
            
        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        
        return [input_image, image, seed]

    except Exception as e:
        gr.Error(f"Error: {str(e)}")
        return None

with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
    with gr.Row():
        run_button = gr.Button(value="Run")

    with gr.Row():
        with gr.Column(scale=4):
            input_im = gr.Image(label="Input Image", type="pil")
        with gr.Column(scale=1):
            num_inference_steps = gr.Slider(
                label="Steps",
                minimum=1,
                maximum=10,
                step=1,
                value=5,
            )
            upscale_factor = gr.Slider(
                label="Scale",
                minimum=1,
                maximum=1,
                step=1,
                value=1,
            )
            controlnet_conditioning_scale = gr.Slider(
                label="Control Scale",
                minimum=0.1,
                maximum=0.3,
                step=0.1,
                value=0.2,
            )
            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=42,
            )
            randomize_seed = gr.Checkbox(label="Random Seed", value=True)

    with gr.Row():
        result = ImageSlider(label="Result", type="pil", interactive=True)

    current_dir = os.path.dirname(os.path.abspath(__file__))
    
    examples = gr.Examples(
        examples=[
            [42, False, os.path.join(current_dir, "z1.webp"), 5, 1, 0.2],
            [42, False, os.path.join(current_dir, "z2.webp"), 5, 1, 0.2],
        ],
        inputs=[
            seed,
            randomize_seed,
            input_im,
            num_inference_steps,
            upscale_factor,
            controlnet_conditioning_scale,
        ],
        fn=infer,
        outputs=result,
        cache_examples=False,
    )

    gr.on(
        [run_button.click],
        fn=infer,
        inputs=[
            seed,
            randomize_seed,
            input_im,
            num_inference_steps,
            upscale_factor,
            controlnet_conditioning_scale,
        ],
        outputs=result,
        show_api=False,
    )

# Launch configuration
demo.queue(max_size=1).launch(
    share=False,
    debug=True,
    show_error=True,
    max_threads=1,
    enable_queue=True,
    cache_examples=False,
    quiet=True,
)