File size: 6,608 Bytes
ba75c9a
d8f4b67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd09669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8f4b67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212c74b
d8f4b67
 
5991220
212c74b
618778d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8f4b67
 
 
618778d
 
 
d8f4b67
 
 
 
 
 
 
 
 
618778d
 
 
d8f4b67
 
618778d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd09669
d8f4b67
fd9da01
c5a8862
 
 
4a6dd82
c5a8862
 
 
4a6dd82
 
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
import gradio as gr
import logging
import random
import warnings
import os
import numpy as np
import spaces
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
from huggingface_hub import login
import subprocess

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

if torch.cuda.is_available():
    power_device = "GPU"
    device = "cuda"
else:
    power_device = "CPU"
    device = "cpu"

huggingface_token = os.getenv("token1704")

def login_to_huggingface():
    if huggingface_token:
        login(token=huggingface_token)
        model_path = snapshot_download(
            repo_id="black-forest-labs/FLUX.1-dev", 
            repo_type="model", 
            ignore_patterns=["*.md", "*..gitattributes"],
            local_dir="FLUX.1-dev",
            token=huggingface_token, # type a new token-id.
            )
        controlnet = FluxControlNetModel.from_pretrained(
                    "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
        ).to(device)
        pipe = FluxControlNetPipeline.from_pretrained(
            model_path, controlnet=controlnet, torch_dtype=torch.bfloat16
        )
        pipe.to(device)

MAX_SEED = 1000000
MAX_PIXEL_BUDGET = 1024 * 1024

def process_input(input_image, upscale_factor, **kwargs):
    w, h = input_image.size
    w_original, h_original = w, h
    aspect_ratio = w / h

    was_resized = False

    if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
        warnings.warn(
            f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels."
        )
        gr.Info(
            f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing input to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels budget."
        )
        input_image = input_image.resize(
            (
                int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
                int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
            )
        )
        was_resized = True

    # resize to multiple of 8
    w, h = input_image.size
    w = w - w % 8
    h = h - h % 8

    return input_image.resize((w, h)), w_original, h_original, was_resized


@spaces.GPU#(duration=42)
def infer(
    seed,
    randomize_seed,
    input_image,
    num_inference_steps,
    upscale_factor,
    controlnet_conditioning_scale,
    progress=gr.Progress(track_tqdm=True),
):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    true_input_image = input_image
    input_image, w_original, h_original, was_resized = process_input(
        input_image, upscale_factor
    )

    # rescale with upscale factor
    w, h = input_image.size
    control_image = input_image.resize((w * upscale_factor, h * upscale_factor))

    generator = torch.Generator().manual_seed(seed)

    gr.Info("Upscaling image...")
    image = pipe(
        prompt="",
        control_image=control_image,
        controlnet_conditioning_scale=controlnet_conditioning_scale,
        num_inference_steps=num_inference_steps,
        guidance_scale=3.5,
        height=control_image.size[1],
        width=control_image.size[0],
        generator=generator,
    ).images[0]

    if was_resized:
        gr.Info(
            f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
        )

    # resize to target desired size
    image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
    image.save("output.jpg")
    
    # convert to numpy
    return [true_input_image, image, seed]

with gr.Blocks() as demo:

    with gr.Row():
        button = gr.LoginButton("Sign in")
        gr.load("models/black-forest-labs/FLUX.1-dev", accept_token=button, provider="hf-inference")
    
    # 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="Number of Inference Steps",
    #             minimum=8,
    #             maximum=50,
    #             step=1,
    #             value=28,
    #         )
    #         upscale_factor = gr.Slider(
    #             label="Upscale Factor",
    #             minimum=1,
    #             maximum=4,
    #             step=1,
    #             value=4,
    #         )
    #         controlnet_conditioning_scale = gr.Slider(
    #             label="Controlnet Conditioning Scale",
    #             minimum=0.1,
    #             maximum=1.5,
    #             step=0.1,
    #             value=0.6,
    #         )
    #         seed = gr.Slider(
    #             label="Seed",
    #             minimum=0,
    #             maximum=MAX_SEED,
    #             step=1,
    #             value=42,
    #         )

    #         randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

    # with gr.Row():
    #     result = ImageSlider(label="Input / Output", type="pil", interactive=True)

    # examples = gr.Examples(
    #     examples=[
    #         [42, False, "z1.webp", 28, 4, 0.6],
    #         [42, False, "z2.webp", 28, 4, 0.6],
 
    #     ],
    #     inputs=[
    #         seed,
    #         randomize_seed,
    #         input_im,
    #         num_inference_steps,
    #         upscale_factor,
    #         controlnet_conditioning_scale,
    #     ],
    #     fn=infer,
    #     outputs=result,
    #     cache_examples="lazy",
    # )

    # 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,
    #     # show_progress="minimal",
    # )
    login_to_huggingface()

demo.queue().launch(share=True, show_api=True)

@app.get("/")
def read_root():
    return {"message": "Hello, World!"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}