LPX55 commited on
Commit
0fd9151
·
verified ·
1 Parent(s): 3facfbb

Create redux.py

Browse files
Files changed (1) hide show
  1. redux.py +114 -0
redux.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+
5
+ import spaces
6
+ import torch
7
+
8
+ from diffusers import FluxPriorReduxPipeline, FluxPipeline
9
+ from diffusers.utils import load_image
10
+
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+ MAX_IMAGE_SIZE = 2048
13
+
14
+ pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained(
15
+ "black-forest-labs/FLUX.1-Redux-dev",
16
+ torch_dtype=torch.bfloat16
17
+ ).to("cuda")
18
+
19
+ pipe = FluxPipeline.from_pretrained(
20
+ "LPX55/FLUX.1-merged_uncensored" ,
21
+ text_encoder=None,
22
+ text_encoder_2=None,
23
+ torch_dtype=torch.bfloat16
24
+ ).to("cuda")
25
+
26
+ @spaces.GPU
27
+ def infer(control_image, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
28
+ if randomize_seed:
29
+ seed = random.randint(0, MAX_SEED)
30
+ pipe_prior_output = pipe_prior_redux(control_image)
31
+ images = pipe(
32
+ guidance_scale=guidance_scale,
33
+ num_inference_steps=num_inference_steps,
34
+ generator=torch.Generator("cpu").manual_seed(seed),
35
+ **pipe_prior_output,
36
+ ).images[0]
37
+ return images, seed
38
+
39
+ css="""
40
+ #col-container {
41
+ margin: 0 auto;
42
+ max-width: 960px;
43
+ }
44
+ """
45
+
46
+ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
47
+
48
+ with gr.Column(elem_id="col-container"):
49
+ gr.Markdown(f"""# FLUX.1 Redux [dev]
50
+ An adapter for FLUX [dev] to create image variations
51
+ [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
52
+ """)
53
+ with gr.Row():
54
+ with gr.Column():
55
+ input_image = gr.Image(label="Image to create variations", type="pil")
56
+ run_button = gr.Button("Run")
57
+ result = gr.Image(label="Result", show_label=False)
58
+
59
+ with gr.Accordion("Advanced Settings", open=False):
60
+
61
+ seed = gr.Slider(
62
+ label="Seed",
63
+ minimum=0,
64
+ maximum=MAX_SEED,
65
+ step=1,
66
+ value=0,
67
+ )
68
+
69
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
70
+
71
+ with gr.Row():
72
+
73
+ width = gr.Slider(
74
+ label="Width",
75
+ minimum=256,
76
+ maximum=MAX_IMAGE_SIZE,
77
+ step=32,
78
+ value=1024,
79
+ )
80
+
81
+ height = gr.Slider(
82
+ label="Height",
83
+ minimum=256,
84
+ maximum=MAX_IMAGE_SIZE,
85
+ step=32,
86
+ value=1024,
87
+ )
88
+
89
+ with gr.Row():
90
+
91
+ guidance_scale = gr.Slider(
92
+ label="Guidance Scale",
93
+ minimum=1,
94
+ maximum=15,
95
+ step=0.1,
96
+ value=3.5,
97
+ )
98
+
99
+ num_inference_steps = gr.Slider(
100
+ label="Number of inference steps",
101
+ minimum=1,
102
+ maximum=50,
103
+ step=1,
104
+ value=28,
105
+ )
106
+
107
+ gr.on(
108
+ triggers=[run_button.click],
109
+ fn = infer,
110
+ inputs = [input_image, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
111
+ outputs = [result, seed]
112
+ )
113
+
114
+ demo.launch()