linoyts HF staff commited on
Commit
64fd258
·
verified ·
1 Parent(s): a1d2741

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +199 -0
app.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ import spaces
6
+ from PIL import Image
7
+ import os
8
+ from huggingface_hub import hf_hub_download
9
+ import torch
10
+ import diffusers
11
+ from pipeline_flux_rf_inversion import RFInversionFluxPipeline
12
+
13
+ # Constants
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 1024
16
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
+
18
+
19
+ pipe = RFInversionFluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev",
20
+ torch_dtype=torch.bfloat16)
21
+ pipe.to(DEVICE)
22
+
23
+ def reset_do_inversion():
24
+ return True
25
+
26
+ def resize_img(image, max_size=1024):
27
+ width, height = image.size
28
+ scaling_factor = min(max_size / width, max_size / height)
29
+ new_width = int(width * scaling_factor)
30
+ new_height = int(height * scaling_factor)
31
+ return image.resize((new_width, new_height), Image.LANCZOS)
32
+
33
+ def invert_and_edit(image,
34
+ prompt,
35
+ eta,
36
+ gamma,
37
+ start_timestep,
38
+ stop_timestep,
39
+ num_inversion_steps,
40
+ width,
41
+ height,
42
+ inverted_latents,
43
+ image_latents,
44
+ latent_image_ids,
45
+ do_inversion,
46
+ seed,
47
+ randomize_seed,
48
+ ):
49
+ if randomize_seed:
50
+ seed = random.randint(0, MAX_SEED)
51
+ if do_inversion:
52
+ inverted_latents_tensor, image_latents_tensor, latent_image_ids_tensor = pipe.invert(image, num_inversion_steps=num_inversion_steps, gamma=gamma)
53
+ inverted_latents = gr.State(value=inverted_latents_tensor)
54
+ image_latents = gr.State(value=image_latents_tensor)
55
+ latent_image_ids = gr.State(value=latent_image_ids_tensor)
56
+ do_inversion = False
57
+
58
+ else:
59
+ output = pipe(prompt,
60
+ inverted_latents=inverted_latents.value,
61
+ image_latents=image_latents.value,
62
+ latent_image_ids=latent_image_ids.value,
63
+ start_timestep=start_timestep,
64
+ stop_timestep=stop_timestep,
65
+ num_inference_steps=num_inversion_steps,
66
+ eta=eta,
67
+ ).images[0]
68
+
69
+ return output, inverted_latents, image_latents, latent_image_ids, do_inversion, seed
70
+
71
+ # UI CSS
72
+ css = """
73
+ #col-container {
74
+ margin: 0 auto;
75
+ max-width: 960px;
76
+ }
77
+ """
78
+
79
+ # Create the Gradio interface
80
+ with gr.Blocks(css=css) as demo:
81
+
82
+ inverted_latents = gr.State()
83
+ image_latents = gr.State()
84
+ latent_image_ids = gr.State()
85
+ do_inversion = gr.State(False)
86
+
87
+ with gr.Column(elem_id="col-container"):
88
+ gr.Markdown(f"""# RF inversion with FLUX.1 [dev] 🖌️🏞️
89
+ Edit real images with Flux, based on the algorithm proposed in [*Semantic Image Inversion and Editing using
90
+ Stochastic Rectified Differential Equations*](https://rf-inversion.github.io/data/rf-inversion.pdf)
91
+ [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[project page](https://rf-inversion.github.io/] [[arxiv](https://arxiv.org/pdf/2410.10792)]
92
+ """)
93
+
94
+ with gr.Row():
95
+ with gr.Column():
96
+ input_image = gr.Image(
97
+ label="Input Image",
98
+ type="pil"
99
+ )
100
+ eta = gr.Slider(
101
+ label="eta",
102
+ info = "lower eta to ehnace the edits",
103
+ minimum=0.0,
104
+ maximum=1.0,
105
+ step=0.1,
106
+ value=0.9,
107
+ )
108
+ prompt = gr.Text(
109
+ label="Edit Prompt",
110
+ max_lines=1,
111
+ placeholder="describe the edited output",
112
+ )
113
+ run_button = gr.Button("Edit", variant="primary")
114
+
115
+ with gr.Column():
116
+ result = gr.Image(label="Result")
117
+
118
+ with gr.Accordion("Advanced Settings", open=False):
119
+ seed = gr.Slider(
120
+ label="Seed",
121
+ minimum=0,
122
+ maximum=MAX_SEED,
123
+ step=1,
124
+ value=42,
125
+ )
126
+
127
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
128
+
129
+ with gr.Row():
130
+ width = gr.Slider(
131
+ label="Width",
132
+ minimum=256,
133
+ maximum=MAX_IMAGE_SIZE,
134
+ step=32,
135
+ value=1024,
136
+ )
137
+
138
+ height = gr.Slider(
139
+ label="Height",
140
+ minimum=256,
141
+ maximum=MAX_IMAGE_SIZE,
142
+ step=32,
143
+ value=1024,
144
+ )
145
+ with gr.Row():
146
+ gamma = gr.Slider(
147
+ label="gamma",
148
+ info = "lower gamma to ehnace the edits",
149
+ minimum=0.0,
150
+ maximum=1.0,
151
+ step=0.1,
152
+ value=0.9,
153
+ )
154
+ start_timestep = gr.Slider(
155
+ label="start timestep",
156
+ info = "lower gamma to ehnace the edits",
157
+ minimum=0.0,
158
+ maximum=1.0,
159
+ step=0.1,
160
+ value=0.9,
161
+ )
162
+ stop_timestep = gr.Slider(
163
+ label="stop timestep",
164
+ info = "lower gamma to ehnace the edits",
165
+ minimum=0.0,
166
+ maximum=1.0,
167
+ step=0.1,
168
+ value=0.9,
169
+ )
170
+
171
+ run_button.click(
172
+ fn=invert_and_edit,
173
+ inputs=[
174
+ input_image,
175
+ prompt,
176
+ eta,
177
+ gamma,
178
+ start_timestep,
179
+ stop_timestep,
180
+ num_inversion_steps,
181
+ width,
182
+ height,
183
+ inverted_latents,
184
+ image_latents,
185
+ latent_image_ids,
186
+ do_inversion,
187
+ seed,
188
+ randomize_seed
189
+ ],
190
+ outputs=[result, inverted_latents, image_latents, latent_image_ids, do_inversion, seed],
191
+ )
192
+
193
+ input_image.change(
194
+ fn=reset_do_inversion,
195
+ outputs=[do_inversion]
196
+ )
197
+
198
+ if __name__ == "__main__":
199
+ demo.launch()