File size: 5,352 Bytes
bc491a4
ac44e1f
bc491a4
ac44e1f
bc491a4
d1ed301
 
ac44e1f
 
 
bc491a4
 
 
 
 
 
 
 
 
 
 
 
 
 
3f2c596
bc491a4
 
 
 
 
 
f47ec65
 
bc491a4
 
 
 
 
 
 
 
 
 
4f440f1
bc491a4
3f2c596
 
 
 
 
 
a5ff012
3f2c596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc491a4
3f2c596
 
 
 
d1ed301
bc491a4
3f2c596
 
 
bc491a4
 
 
 
 
 
 
 
f47ec65
3f2c596
25916b6
 
 
 
 
 
 
 
a5ff012
 
25916b6
3f2c596
 
 
a5ff012
 
3f2c596
33c3877
d1ed301
bc491a4
af7d816
 
3f2c596
33c3877
af7d816
 
3f2c596
af7d816
d61b743
af7d816
 
bc491a4
3f2c596
 
cf1a19d
3f2c596
 
bc491a4
3f2c596
bc491a4
 
 
 
 
 
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
import gradio as gr
import os 
from gradio_client import Client, handle_file
from huggingface_hub import login

from gradio_imageslider import ImageSlider

hf_tkn = os.environ.get("HF_TKN")
login(hf_tkn)

def get_flux_image(prompt):
    client = Client("black-forest-labs/FLUX.1-schnell")
    result = client.predict(
		prompt=prompt,
		seed=0,
		randomize_seed=True,
		width=1024,
		height=1024,
		num_inference_steps=4,
		api_name="/infer"
    )
    print(result)
    return result[0]

def get_upscale_finegrain(prompt, img_path, upscale_factor):
    client = Client("finegrain/finegrain-image-enhancer")
    result = client.predict(
		input_image=handle_file(img_path),
		prompt=prompt,
		negative_prompt="",
		seed=42,
		upscale_factor=upscale_factor,
	    controlnet_scale=0.6,
		controlnet_decay=1,
		condition_scale=6,
		tile_width=112,
		tile_height=144,
		denoise_strength=0.35,
		num_inference_steps=18,
		solver="DDIM",
		api_name="/process"
    )
    print(result)
    return result[1]

def get_clarity_upscale(prompt, img_path, upscale_factor):
    client = Client("jbilcke-hf/clarity-upscaler")
    result = client.predict(
		img_path,	# filepath  in 'Image' Image component
		prompt,	# str  in 'Prompt' Textbox component
		"",	# str  in 'Negative Prompt' Textbox component
		upscale_factor,	# float  in 'Scale Factor' Number component
		1,	# float (numeric value between 1 and 50) in 'Dynamic' Slider component
		3,	# float  in 'Creativity' Number component
		3,	# float  in 'Resemblance' Number component
		"16",	# Literal['16', '32', '48', '64', '80', '96', '112', '128', '144', '160', '176', '192', '208', '224', '240', '256']  in 'tiling_width' Dropdown component
		"16",	# Literal['16', '32', '48', '64', '80', '96', '112', '128', '144', '160', '176', '192', '208', '224', '240', '256']  in 'tiling_height' Dropdown component
		"epicrealism_naturalSinRC1VAE.safetensors [84d76a0328]",	# Literal['epicrealism_naturalSinRC1VAE.safetensors [84d76a0328]', 'juggernaut_reborn.safetensors [338b85bc4f]', 'flat2DAnimerge_v45Sharp.safetensors']  in 'sd_model' Dropdown component
		"DPM++ 2M Karras",	# Literal['DPM++ 2M Karras', 'DPM++ SDE Karras', 'DPM++ 2M SDE Exponential', 'DPM++ 2M SDE Karras', 'Euler a', 'Euler', 'LMS', 'Heun', 'DPM2', 'DPM2 a', 'DPM++ 2S a', 'DPM++ 2M', 'DPM++ SDE', 'DPM++ 2M SDE', 'DPM++ 2M SDE Heun', 'DPM++ 2M SDE Heun Karras', 'DPM++ 2M SDE Heun Exponential', 'DPM++ 3M SDE', 'DPM++ 3M SDE Karras', 'DPM++ 3M SDE Exponential', 'DPM fast', 'DPM adaptive', 'LMS Karras', 'DPM2 Karras', 'DPM2 a Karras', 'DPM++ 2S a Karras', 'Restart', 'DDIM', 'PLMS', 'UniPC']  in 'scheduler' Dropdown component
		1,	# float (numeric value between 1 and 100) in 'Num Inference Steps' Slider component
		3,	# float  in 'Seed' Number component
		True,	# bool  in 'Downscaling' Checkbox component
		3,	# float  in 'Downscaling Resolution' Number component
		"Hello!!",	# str  in 'Lora Links' Textbox component
		"Hello!!",	# str  in 'Custom Sd Model' Textbox component
		api_name="/predict"
    )
    print(result)
    return result

def main(prompt, upscale_factor, upscale_provider):
    step_one_flux = get_flux_image(prompt)
    if upscale_provider == "finegrain image enhancer":
        step_two_upscale = get_upscale_finegrain(prompt, step_one_flux, upscale_factor)
    elif upscale_provider == "clarity upscale":
        step_two_upscale = get_clarity_upscale(prompt, step_one_flux, upscale_factor)
    return (step_one_flux, step_two_upscale)

def clean_previous():
    return gr.update(value=None)

css = """
#col-container{
    margin: 0 auto;
    max-width: 1024px;
}
"""
with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# Flux Upscaled")
        gr.Markdown("Step 1: Generate image with FLUX schnell; Step 2: UpScale with Finegrain Image-Enhancer OR Clarity UpScale;")
        with gr.Group():
            prompt_in = gr.Textbox(label="Prompt")
            with gr.Row():
                upscale_factor = gr.Radio(
                    label = "UpScale Factor",
                    choices = [
                        2, 3, 4
                    ],
                    value = 2,
                    scale=2
                )
                upscale_provider = gr.Dropdown(
                    label = "UpScale Provider",
                    choices = ["finegrain image enhancer", "clarity upscale"],
                    value = "clarity upscale",
                    scale=2
                )
                submit_btn = gr.Button("Submit", scale=1)
        output_res = ImageSlider(label="Flux / Upscaled")

        gr.Examples(
            examples = [
                ["a tiny astronaut hatching from an egg on the moon", 2, "clarity upscale"],
                ["a bright blue bird in the garden, natural photo cinematic, MM full HD", 2, "clarity upscale"]
            ],
            fn = main,
            inputs=[prompt_in, upscale_factor, upscale_provider],
            outputs=[output_res],
            cache_examples = "lazy"
        )

    submit_btn.click(
        fn = clean_previous,
        inputs = None,
        outputs = [output_res],
        queue=False
    ).then(
        fn=main,
        inputs=[prompt_in, upscale_factor, upscale_provider],
        outputs=[output_res],
        
    )

demo.queue().launch(show_api=False, show_error=True)