File size: 2,298 Bytes
60f5157
 
 
cbeb9ba
 
ccce713
60f5157
c7c2e8e
60f5157
 
 
 
 
ccce713
ea6cb86
60f5157
 
ea6cb86
60f5157
 
 
 
 
 
ea6cb86
ccce713
 
ea6cb86
 
ccce713
ea6cb86
 
 
 
 
 
 
 
 
 
 
 
 
cbeb9ba
 
bb07a1e
 
 
457f149
cbeb9ba
 
 
60f5157
cbeb9ba
 
 
60f5157
cbeb9ba
 
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
import torch
from diffusers import FluxPriorReduxPipeline, FluxPipeline
from diffusers.utils import load_image
import gradio as gr
import spaces
from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images

pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained("black-forest-labs/FLUX.1-Redux-dev", torch_dtype=torch.bfloat16).to("cuda")
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev" , 
    text_encoder=None,
    text_encoder_2=None,
    torch_dtype=torch.bfloat16
).to("cuda")
# pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
# pipe.enable_sequential_cpu_offload()

@spaces.GPU(duration=120)
def enhance_image(image_path, keep_aspect_ratio=False):
    print(image_path)
    image = load_image(image_path)
    print(image.size)
    width, height = image.size if keep_aspect_ratio else (None, None)
    pipe_prior_output = pipe_prior_redux(image)
    # for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
    #     guidance_scale=2.5,
    #     num_inference_steps=50,
    #     width=width,
    #     height=height,
    #     generator=torch.Generator("cpu").manual_seed(0),
    #     output_type="pil",
    #     **pipe_prior_output
    #     ):
    #     yield img
    images = pipe(
        height=height,
        width=width,
        guidance_scale=2.5,
        num_inference_steps=25,
        generator=torch.Generator("cpu").manual_seed(0),
        **pipe_prior_output,
    ).images
    return images[0]


with gr.Blocks(title="Flux.1 Dev Redux") as demo:
    gr.HTML("<center><h1>Flux.1 Dev Redux</h1></center>")
    gr.Markdown("[FLUX.1 Redux](https://huggingface.co/black-forest-labs/FLUX.1-Redux-dev) is an adapter for all FLUX.1 base models for image variation generation.")

    with gr.Row():
        with gr.Column():
            image_path = gr.Image(label="Image", type="filepath")
            keep_aspect_ratio = gr.Checkbox(label="Keep Aspect Ratio", value=False)
            submit_btn = gr.Button(value="Submit", variant="primary")
        enhanced_image = gr.Image(label="Enhanced Image", type="pil")

    submit_btn.click(enhance_image, inputs=[image_path, keep_aspect_ratio], outputs=enhanced_image)

demo.queue().launch(share=False)