guibegotti commited on
Commit
f2eed99
·
verified ·
1 Parent(s): 4ebd397

Upload 2 files

Browse files
Files changed (2) hide show
  1. api_app.py +313 -0
  2. app.py +47 -325
api_app.py ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import random
4
+ import sys
5
+ from typing import Sequence, Mapping, Any, Union
6
+ import torch
7
+ import gradio as gr
8
+ from PIL import Image
9
+ from huggingface_hub import hf_hub_download
10
+ import spaces
11
+ from comfy import model_management
12
+
13
+ hf_hub_download(repo_id="black-forest-labs/FLUX.1-Redux-dev", filename="flux1-redux-dev.safetensors", local_dir="models/style_models")
14
+ hf_hub_download(repo_id="black-forest-labs/FLUX.1-Depth-dev", filename="flux1-depth-dev.safetensors", local_dir="models/diffusion_models")
15
+ hf_hub_download(repo_id="Comfy-Org/sigclip_vision_384", filename="sigclip_vision_patch14_384.safetensors", local_dir="models/clip_vision")
16
+ hf_hub_download(repo_id="Kijai/DepthAnythingV2-safetensors", filename="depth_anything_v2_vitl_fp32.safetensors", local_dir="models/depthanything")
17
+ hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev", filename="ae.safetensors", local_dir="models/vae/FLUX1")
18
+ hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="clip_l.safetensors", local_dir="models/text_encoders")
19
+ t5_path = hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="t5xxl_fp16.safetensors", local_dir="models/text_encoders/t5")
20
+
21
+ # Import all the necessary functions from the original script
22
+ def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
23
+ try:
24
+ return obj[index]
25
+ except KeyError:
26
+ return obj["result"][index]
27
+
28
+ # Add all the necessary setup functions from the original script
29
+ def find_path(name: str, path: str = None) -> str:
30
+ if path is None:
31
+ path = os.getcwd()
32
+ if name in os.listdir(path):
33
+ path_name = os.path.join(path, name)
34
+ print(f"{name} found: {path_name}")
35
+ return path_name
36
+ parent_directory = os.path.dirname(path)
37
+ if parent_directory == path:
38
+ return None
39
+ return find_path(name, parent_directory)
40
+
41
+ def add_comfyui_directory_to_sys_path() -> None:
42
+ comfyui_path = find_path("ComfyUI")
43
+ if comfyui_path is not None and os.path.isdir(comfyui_path):
44
+ sys.path.append(comfyui_path)
45
+ print(f"'{comfyui_path}' added to sys.path")
46
+
47
+ def add_extra_model_paths() -> None:
48
+ try:
49
+ from main import load_extra_path_config
50
+ except ImportError:
51
+ from utils.extra_config import load_extra_path_config
52
+ extra_model_paths = find_path("extra_model_paths.yaml")
53
+ if extra_model_paths is not None:
54
+ load_extra_path_config(extra_model_paths)
55
+ else:
56
+ print("Could not find the extra_model_paths config file.")
57
+
58
+ # Initialize paths
59
+ add_comfyui_directory_to_sys_path()
60
+ add_extra_model_paths()
61
+
62
+ def import_custom_nodes() -> None:
63
+ import asyncio
64
+ import execution
65
+ from nodes import init_extra_nodes
66
+ import server
67
+ loop = asyncio.new_event_loop()
68
+ asyncio.set_event_loop(loop)
69
+ server_instance = server.PromptServer(loop)
70
+ execution.PromptQueue(server_instance)
71
+ init_extra_nodes()
72
+
73
+ # Import all necessary nodes
74
+ from nodes import (
75
+ StyleModelLoader,
76
+ VAEEncode,
77
+ NODE_CLASS_MAPPINGS,
78
+ LoadImage,
79
+ CLIPVisionLoader,
80
+ SaveImage,
81
+ VAELoader,
82
+ CLIPVisionEncode,
83
+ DualCLIPLoader,
84
+ EmptyLatentImage,
85
+ VAEDecode,
86
+ UNETLoader,
87
+ CLIPTextEncode,
88
+ )
89
+
90
+ # Initialize all constant nodes and models in global context
91
+ import_custom_nodes()
92
+
93
+ # Global variables for preloaded models and constants
94
+ #with torch.inference_mode():
95
+ # Initialize constants
96
+ intconstant = NODE_CLASS_MAPPINGS["INTConstant"]()
97
+ CONST_1024 = intconstant.get_value(value=1024)
98
+
99
+ # Load CLIP
100
+ dualcliploader = DualCLIPLoader()
101
+ CLIP_MODEL = dualcliploader.load_clip(
102
+ clip_name1="t5/t5xxl_fp16.safetensors",
103
+ clip_name2="clip_l.safetensors",
104
+ type="flux",
105
+ )
106
+
107
+ # Load VAE
108
+ vaeloader = VAELoader()
109
+ VAE_MODEL = vaeloader.load_vae(vae_name="FLUX1/ae.safetensors")
110
+
111
+ # Load UNET
112
+ unetloader = UNETLoader()
113
+ UNET_MODEL = unetloader.load_unet(
114
+ unet_name="flux1-depth-dev.safetensors", weight_dtype="default"
115
+ )
116
+
117
+ # Load CLIP Vision
118
+ clipvisionloader = CLIPVisionLoader()
119
+ CLIP_VISION_MODEL = clipvisionloader.load_clip(
120
+ clip_name="sigclip_vision_patch14_384.safetensors"
121
+ )
122
+
123
+ # Load Style Model
124
+ stylemodelloader = StyleModelLoader()
125
+ STYLE_MODEL = stylemodelloader.load_style_model(
126
+ style_model_name="flux1-redux-dev.safetensors"
127
+ )
128
+
129
+ # Initialize samplers
130
+ ksamplerselect = NODE_CLASS_MAPPINGS["KSamplerSelect"]()
131
+ SAMPLER = ksamplerselect.get_sampler(sampler_name="euler")
132
+
133
+ # Initialize depth model
134
+ cr_clip_input_switch = NODE_CLASS_MAPPINGS["CR Clip Input Switch"]()
135
+ downloadandloaddepthanythingv2model = NODE_CLASS_MAPPINGS["DownloadAndLoadDepthAnythingV2Model"]()
136
+ DEPTH_MODEL = downloadandloaddepthanythingv2model.loadmodel(
137
+ model="depth_anything_v2_vitl_fp32.safetensors"
138
+ )
139
+
140
+ cliptextencode = CLIPTextEncode()
141
+ loadimage = LoadImage()
142
+ vaeencode = VAEEncode()
143
+ fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
144
+ instructpixtopixconditioning = NODE_CLASS_MAPPINGS["InstructPixToPixConditioning"]()
145
+ clipvisionencode = CLIPVisionEncode()
146
+ stylemodelapplyadvanced = NODE_CLASS_MAPPINGS["StyleModelApplyAdvanced"]()
147
+ emptylatentimage = EmptyLatentImage()
148
+ basicguider = NODE_CLASS_MAPPINGS["BasicGuider"]()
149
+ basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
150
+ randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
151
+ samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
152
+ vaedecode = VAEDecode()
153
+ cr_text = NODE_CLASS_MAPPINGS["CR Text"]()
154
+ saveimage = SaveImage()
155
+ getimagesizeandcount = NODE_CLASS_MAPPINGS["GetImageSizeAndCount"]()
156
+ depthanything_v2 = NODE_CLASS_MAPPINGS["DepthAnything_V2"]()
157
+ imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
158
+
159
+ model_loaders = [CLIP_MODEL, VAE_MODEL, UNET_MODEL, CLIP_VISION_MODEL]
160
+
161
+ model_management.load_models_gpu([
162
+ loader[0].patcher if hasattr(loader[0], 'patcher') else loader[0] for loader in model_loaders
163
+ ])
164
+
165
+ @spaces.GPU
166
+ def generate_image(prompt, structure_image, style_image, depth_strength=15, style_strength=0.5, progress=gr.Progress(track_tqdm=True)) -> str:
167
+ """Main generation function that processes inputs and returns the path to the generated image."""
168
+ with torch.inference_mode():
169
+ # Set up CLIP
170
+ clip_switch = cr_clip_input_switch.switch(
171
+ Input=1,
172
+ clip1=get_value_at_index(CLIP_MODEL, 0),
173
+ clip2=get_value_at_index(CLIP_MODEL, 0),
174
+ )
175
+
176
+ # Encode text
177
+ text_encoded = cliptextencode.encode(
178
+ text=prompt,
179
+ clip=get_value_at_index(clip_switch, 0),
180
+ )
181
+ empty_text = cliptextencode.encode(
182
+ text="",
183
+ clip=get_value_at_index(clip_switch, 0),
184
+ )
185
+
186
+ # Process structure image
187
+ structure_img = loadimage.load_image(image=structure_image)
188
+
189
+ # Resize image
190
+ resized_img = imageresize.execute(
191
+ width=get_value_at_index(CONST_1024, 0),
192
+ height=get_value_at_index(CONST_1024, 0),
193
+ interpolation="bicubic",
194
+ method="keep proportion",
195
+ condition="always",
196
+ multiple_of=16,
197
+ image=get_value_at_index(structure_img, 0),
198
+ )
199
+
200
+ # Get image size
201
+ size_info = getimagesizeandcount.getsize(
202
+ image=get_value_at_index(resized_img, 0)
203
+ )
204
+
205
+ # Encode VAE
206
+ vae_encoded = vaeencode.encode(
207
+ pixels=get_value_at_index(size_info, 0),
208
+ vae=get_value_at_index(VAE_MODEL, 0),
209
+ )
210
+
211
+ # Process depth
212
+ depth_processed = depthanything_v2.process(
213
+ da_model=get_value_at_index(DEPTH_MODEL, 0),
214
+ images=get_value_at_index(size_info, 0),
215
+ )
216
+
217
+ # Apply Flux guidance
218
+ flux_guided = fluxguidance.append(
219
+ guidance=depth_strength,
220
+ conditioning=get_value_at_index(text_encoded, 0),
221
+ )
222
+
223
+ # Process style image
224
+ style_img = loadimage.load_image(image=style_image)
225
+
226
+ # Encode style with CLIP Vision
227
+ style_encoded = clipvisionencode.encode(
228
+ crop="center",
229
+ clip_vision=get_value_at_index(CLIP_VISION_MODEL, 0),
230
+ image=get_value_at_index(style_img, 0),
231
+ )
232
+
233
+ # Set up conditioning
234
+ conditioning = instructpixtopixconditioning.encode(
235
+ positive=get_value_at_index(flux_guided, 0),
236
+ negative=get_value_at_index(empty_text, 0),
237
+ vae=get_value_at_index(VAE_MODEL, 0),
238
+ pixels=get_value_at_index(depth_processed, 0),
239
+ )
240
+
241
+ # Apply style
242
+ style_applied = stylemodelapplyadvanced.apply_stylemodel(
243
+ strength=style_strength,
244
+ conditioning=get_value_at_index(conditioning, 0),
245
+ style_model=get_value_at_index(STYLE_MODEL, 0),
246
+ clip_vision_output=get_value_at_index(style_encoded, 0),
247
+ )
248
+
249
+ # Set up empty latent
250
+ empty_latent = emptylatentimage.generate(
251
+ width=get_value_at_index(resized_img, 1),
252
+ height=get_value_at_index(resized_img, 2),
253
+ batch_size=1,
254
+ )
255
+
256
+ # Set up guidance
257
+ guided = basicguider.get_guider(
258
+ model=get_value_at_index(UNET_MODEL, 0),
259
+ conditioning=get_value_at_index(style_applied, 0),
260
+ )
261
+
262
+ # Set up scheduler
263
+ schedule = basicscheduler.get_sigmas(
264
+ scheduler="simple",
265
+ steps=28,
266
+ denoise=1,
267
+ model=get_value_at_index(UNET_MODEL, 0),
268
+ )
269
+
270
+ # Generate random noise
271
+ noise = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
272
+
273
+ # Sample
274
+ sampled = samplercustomadvanced.sample(
275
+ noise=get_value_at_index(noise, 0),
276
+ guider=get_value_at_index(guided, 0),
277
+ sampler=get_value_at_index(SAMPLER, 0),
278
+ sigmas=get_value_at_index(schedule, 0),
279
+ latent_image=get_value_at_index(empty_latent, 0),
280
+ )
281
+
282
+ # Decode VAE
283
+ decoded = vaedecode.decode(
284
+ samples=get_value_at_index(sampled, 0),
285
+ vae=get_value_at_index(VAE_MODEL, 0),
286
+ )
287
+
288
+ # Save image
289
+ prefix = cr_text.text_multiline(text="Flux_BFL_Depth_Redux")
290
+
291
+ saved = saveimage.save_images(
292
+ filename_prefix=get_value_at_index(prefix, 0),
293
+ images=get_value_at_index(decoded, 0),
294
+ )
295
+ saved_path = f"output/{saved['ui']['images'][0]['filename']}"
296
+ return saved_path
297
+
298
+ api_app = gr.Interface(
299
+ fn=generate_image,
300
+ inputs=[
301
+ gr.Textbox(label="Prompt"),
302
+ gr.Image(label="Structure Image", type="filepath"),
303
+ gr.Image(label="Style Image", type="filepath"),
304
+ gr.Slider(minimum=0, maximum=50, value=15, label="Depth Strength"),
305
+ gr.Slider(minimum=0, maximum=1, value=0.5, label="Style Strength"),
306
+ ],
307
+ outputs=gr.Image(label="Generated Image"),
308
+ title="FLUX Style Shaping API",
309
+ description="API para geração de imagens utilizando o modelo FLUX.",
310
+ )
311
+
312
+ if __name__ == "__main__":
313
+ api_app.launch()
app.py CHANGED
@@ -1,329 +1,51 @@
1
- import os
2
- import random
3
- import sys
4
- from typing import Sequence, Mapping, Any, Union
5
- import torch
6
  import gradio as gr
 
7
  from PIL import Image
8
- from huggingface_hub import hf_hub_download
9
- import spaces
10
- from comfy import model_management
11
-
12
- hf_hub_download(repo_id="black-forest-labs/FLUX.1-Redux-dev", filename="flux1-redux-dev.safetensors", local_dir="models/style_models")
13
- hf_hub_download(repo_id="black-forest-labs/FLUX.1-Depth-dev", filename="flux1-depth-dev.safetensors", local_dir="models/diffusion_models")
14
- hf_hub_download(repo_id="Comfy-Org/sigclip_vision_384", filename="sigclip_vision_patch14_384.safetensors", local_dir="models/clip_vision")
15
- hf_hub_download(repo_id="Kijai/DepthAnythingV2-safetensors", filename="depth_anything_v2_vitl_fp32.safetensors", local_dir="models/depthanything")
16
- hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev", filename="ae.safetensors", local_dir="models/vae/FLUX1")
17
- hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="clip_l.safetensors", local_dir="models/text_encoders")
18
- t5_path = hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="t5xxl_fp16.safetensors", local_dir="models/text_encoders/t5")
19
-
20
- # Import all the necessary functions from the original script
21
- def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
22
- try:
23
- return obj[index]
24
- except KeyError:
25
- return obj["result"][index]
26
-
27
- # Add all the necessary setup functions from the original script
28
- def find_path(name: str, path: str = None) -> str:
29
- if path is None:
30
- path = os.getcwd()
31
- if name in os.listdir(path):
32
- path_name = os.path.join(path, name)
33
- print(f"{name} found: {path_name}")
34
- return path_name
35
- parent_directory = os.path.dirname(path)
36
- if parent_directory == path:
37
- return None
38
- return find_path(name, parent_directory)
39
-
40
- def add_comfyui_directory_to_sys_path() -> None:
41
- comfyui_path = find_path("ComfyUI")
42
- if comfyui_path is not None and os.path.isdir(comfyui_path):
43
- sys.path.append(comfyui_path)
44
- print(f"'{comfyui_path}' added to sys.path")
45
-
46
- def add_extra_model_paths() -> None:
47
- try:
48
- from main import load_extra_path_config
49
- except ImportError:
50
- from utils.extra_config import load_extra_path_config
51
- extra_model_paths = find_path("extra_model_paths.yaml")
52
- if extra_model_paths is not None:
53
- load_extra_path_config(extra_model_paths)
54
- else:
55
- print("Could not find the extra_model_paths config file.")
56
-
57
- # Initialize paths
58
- add_comfyui_directory_to_sys_path()
59
- add_extra_model_paths()
60
-
61
- def import_custom_nodes() -> None:
62
- import asyncio
63
- import execution
64
- from nodes import init_extra_nodes
65
- import server
66
- loop = asyncio.new_event_loop()
67
- asyncio.set_event_loop(loop)
68
- server_instance = server.PromptServer(loop)
69
- execution.PromptQueue(server_instance)
70
- init_extra_nodes()
71
-
72
- # Import all necessary nodes
73
- from nodes import (
74
- StyleModelLoader,
75
- VAEEncode,
76
- NODE_CLASS_MAPPINGS,
77
- LoadImage,
78
- CLIPVisionLoader,
79
- SaveImage,
80
- VAELoader,
81
- CLIPVisionEncode,
82
- DualCLIPLoader,
83
- EmptyLatentImage,
84
- VAEDecode,
85
- UNETLoader,
86
- CLIPTextEncode,
87
- )
88
-
89
- # Initialize all constant nodes and models in global context
90
- import_custom_nodes()
91
-
92
- # Global variables for preloaded models and constants
93
- #with torch.inference_mode():
94
- # Initialize constants
95
- intconstant = NODE_CLASS_MAPPINGS["INTConstant"]()
96
- CONST_1024 = intconstant.get_value(value=1024)
97
-
98
- # Load CLIP
99
- dualcliploader = DualCLIPLoader()
100
- CLIP_MODEL = dualcliploader.load_clip(
101
- clip_name1="t5/t5xxl_fp16.safetensors",
102
- clip_name2="clip_l.safetensors",
103
- type="flux",
104
- )
105
-
106
- # Load VAE
107
- vaeloader = VAELoader()
108
- VAE_MODEL = vaeloader.load_vae(vae_name="FLUX1/ae.safetensors")
109
-
110
- # Load UNET
111
- unetloader = UNETLoader()
112
- UNET_MODEL = unetloader.load_unet(
113
- unet_name="flux1-depth-dev.safetensors", weight_dtype="default"
114
- )
115
-
116
- # Load CLIP Vision
117
- clipvisionloader = CLIPVisionLoader()
118
- CLIP_VISION_MODEL = clipvisionloader.load_clip(
119
- clip_name="sigclip_vision_patch14_384.safetensors"
120
- )
121
-
122
- # Load Style Model
123
- stylemodelloader = StyleModelLoader()
124
- STYLE_MODEL = stylemodelloader.load_style_model(
125
- style_model_name="flux1-redux-dev.safetensors"
126
- )
127
-
128
- # Initialize samplers
129
- ksamplerselect = NODE_CLASS_MAPPINGS["KSamplerSelect"]()
130
- SAMPLER = ksamplerselect.get_sampler(sampler_name="euler")
131
-
132
- # Initialize depth model
133
- cr_clip_input_switch = NODE_CLASS_MAPPINGS["CR Clip Input Switch"]()
134
- downloadandloaddepthanythingv2model = NODE_CLASS_MAPPINGS["DownloadAndLoadDepthAnythingV2Model"]()
135
- DEPTH_MODEL = downloadandloaddepthanythingv2model.loadmodel(
136
- model="depth_anything_v2_vitl_fp32.safetensors"
137
- )
138
-
139
- cliptextencode = CLIPTextEncode()
140
- loadimage = LoadImage()
141
- vaeencode = VAEEncode()
142
- fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
143
- instructpixtopixconditioning = NODE_CLASS_MAPPINGS["InstructPixToPixConditioning"]()
144
- clipvisionencode = CLIPVisionEncode()
145
- stylemodelapplyadvanced = NODE_CLASS_MAPPINGS["StyleModelApplyAdvanced"]()
146
- emptylatentimage = EmptyLatentImage()
147
- basicguider = NODE_CLASS_MAPPINGS["BasicGuider"]()
148
- basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
149
- randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
150
- samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
151
- vaedecode = VAEDecode()
152
- cr_text = NODE_CLASS_MAPPINGS["CR Text"]()
153
- saveimage = SaveImage()
154
- getimagesizeandcount = NODE_CLASS_MAPPINGS["GetImageSizeAndCount"]()
155
- depthanything_v2 = NODE_CLASS_MAPPINGS["DepthAnything_V2"]()
156
- imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
157
-
158
- model_loaders = [CLIP_MODEL, VAE_MODEL, UNET_MODEL, CLIP_VISION_MODEL]
159
-
160
- model_management.load_models_gpu([
161
- loader[0].patcher if hasattr(loader[0], 'patcher') else loader[0] for loader in model_loaders
162
- ])
163
-
164
- @spaces.GPU
165
- def generate_image(prompt, structure_image, style_image, depth_strength=15, style_strength=0.5, progress=gr.Progress(track_tqdm=True)) -> str:
166
- """Main generation function that processes inputs and returns the path to the generated image."""
167
- with torch.inference_mode():
168
- # Set up CLIP
169
- clip_switch = cr_clip_input_switch.switch(
170
- Input=1,
171
- clip1=get_value_at_index(CLIP_MODEL, 0),
172
- clip2=get_value_at_index(CLIP_MODEL, 0),
173
- )
174
-
175
- # Encode text
176
- text_encoded = cliptextencode.encode(
177
- text=prompt,
178
- clip=get_value_at_index(clip_switch, 0),
179
- )
180
- empty_text = cliptextencode.encode(
181
- text="",
182
- clip=get_value_at_index(clip_switch, 0),
183
- )
184
-
185
- # Process structure image
186
- structure_img = loadimage.load_image(image=structure_image)
187
-
188
- # Resize image
189
- resized_img = imageresize.execute(
190
- width=get_value_at_index(CONST_1024, 0),
191
- height=get_value_at_index(CONST_1024, 0),
192
- interpolation="bicubic",
193
- method="keep proportion",
194
- condition="always",
195
- multiple_of=16,
196
- image=get_value_at_index(structure_img, 0),
197
- )
198
-
199
- # Get image size
200
- size_info = getimagesizeandcount.getsize(
201
- image=get_value_at_index(resized_img, 0)
202
- )
203
-
204
- # Encode VAE
205
- vae_encoded = vaeencode.encode(
206
- pixels=get_value_at_index(size_info, 0),
207
- vae=get_value_at_index(VAE_MODEL, 0),
208
- )
209
-
210
- # Process depth
211
- depth_processed = depthanything_v2.process(
212
- da_model=get_value_at_index(DEPTH_MODEL, 0),
213
- images=get_value_at_index(size_info, 0),
214
- )
215
-
216
- # Apply Flux guidance
217
- flux_guided = fluxguidance.append(
218
- guidance=depth_strength,
219
- conditioning=get_value_at_index(text_encoded, 0),
220
- )
221
-
222
- # Process style image
223
- style_img = loadimage.load_image(image=style_image)
224
-
225
- # Encode style with CLIP Vision
226
- style_encoded = clipvisionencode.encode(
227
- crop="center",
228
- clip_vision=get_value_at_index(CLIP_VISION_MODEL, 0),
229
- image=get_value_at_index(style_img, 0),
230
- )
231
-
232
- # Set up conditioning
233
- conditioning = instructpixtopixconditioning.encode(
234
- positive=get_value_at_index(flux_guided, 0),
235
- negative=get_value_at_index(empty_text, 0),
236
- vae=get_value_at_index(VAE_MODEL, 0),
237
- pixels=get_value_at_index(depth_processed, 0),
238
- )
239
-
240
- # Apply style
241
- style_applied = stylemodelapplyadvanced.apply_stylemodel(
242
- strength=style_strength,
243
- conditioning=get_value_at_index(conditioning, 0),
244
- style_model=get_value_at_index(STYLE_MODEL, 0),
245
- clip_vision_output=get_value_at_index(style_encoded, 0),
246
- )
247
-
248
- # Set up empty latent
249
- empty_latent = emptylatentimage.generate(
250
- width=get_value_at_index(resized_img, 1),
251
- height=get_value_at_index(resized_img, 2),
252
- batch_size=1,
253
- )
254
-
255
- # Set up guidance
256
- guided = basicguider.get_guider(
257
- model=get_value_at_index(UNET_MODEL, 0),
258
- conditioning=get_value_at_index(style_applied, 0),
259
- )
260
-
261
- # Set up scheduler
262
- schedule = basicscheduler.get_sigmas(
263
- scheduler="simple",
264
- steps=28,
265
- denoise=1,
266
- model=get_value_at_index(UNET_MODEL, 0),
267
- )
268
-
269
- # Generate random noise
270
- noise = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
271
-
272
- # Sample
273
- sampled = samplercustomadvanced.sample(
274
- noise=get_value_at_index(noise, 0),
275
- guider=get_value_at_index(guided, 0),
276
- sampler=get_value_at_index(SAMPLER, 0),
277
- sigmas=get_value_at_index(schedule, 0),
278
- latent_image=get_value_at_index(empty_latent, 0),
279
- )
280
-
281
- # Decode VAE
282
- decoded = vaedecode.decode(
283
- samples=get_value_at_index(sampled, 0),
284
- vae=get_value_at_index(VAE_MODEL, 0),
285
- )
286
-
287
- # Save image
288
- prefix = cr_text.text_multiline(text="Flux_BFL_Depth_Redux")
289
-
290
- saved = saveimage.save_images(
291
- filename_prefix=get_value_at_index(prefix, 0),
292
- images=get_value_at_index(decoded, 0),
293
- )
294
- saved_path = f"output/{saved['ui']['images'][0]['filename']}"
295
- return saved_path
296
-
297
- # Create Gradio interface
298
-
299
- examples = [
300
- ["", "mona.png", "receita-tacos.webp", 15, 0.6],
301
- ["a woman looking at a house catching fire on the background", "disaster_girl.png", "abaporu.jpg", 15, 0.15],
302
- ["istanbul aerial, dramatic photography", "natasha.png", "istambul.jpg", 15, 0.5],
303
- ]
304
-
305
- app = gr.Interface(
306
- fn=generate_image,
307
- inputs=[
308
- gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
309
- gr.Image(label="Structure Image", type="filepath"),
310
- gr.Image(label="Style Image", type="filepath"),
311
- gr.Slider(minimum=0, maximum=50, value=15, label="Depth Strength"),
312
- gr.Slider(minimum=0, maximum=1, value=0.5, label="Style Strength"),
313
- ],
314
- outputs=gr.Image(label="Generated Image"),
315
- title="FLUX Style Shaping",
316
- description="""Flux[dev] Redux + Flux[dev] Depth ComfyUI workflow by [Nathan Shipley](https://x.com/CitizenPlain) running directly on Gradio.
317
- [workflow](https://gist.github.com/nathanshipley/7a9ac1901adde76feebe58d558026f68) – [how to convert your any comfy workflow to gradio](https://huggingface.co/blog/run-comfyui-workflows-on-spaces)
318
- """,
319
- examples=examples,
320
- cache_examples=True,
321
- cache_mode="lazy"
322
- )
323
-
324
- if __name__ == "__main__":
325
- app.launch() # Aqui você pode usar ssr=True se desejar
326
-
327
 
328
  if __name__ == "__main__":
329
- app.launch(share=True)
 
1
+
 
 
 
 
2
  import gradio as gr
3
+ import requests
4
  from PIL import Image
5
+ import tempfile
6
+
7
+ def call_api(prompt, structure_image, style_image, depth_strength, style_strength):
8
+ with tempfile.NamedTemporaryFile(suffix=".png") as structure_tmp, tempfile.NamedTemporaryFile(suffix=".png") as style_tmp:
9
+ structure_image.save(structure_tmp.name)
10
+ style_image.save(style_tmp.name)
11
+
12
+ files = {
13
+ 'structure_image': open(structure_tmp.name, 'rb'),
14
+ 'style_image': open(style_tmp.name, 'rb'),
15
+ }
16
+ data = {
17
+ 'prompt': prompt,
18
+ 'depth_strength': depth_strength,
19
+ 'style_strength': style_strength,
20
+ }
21
+ response = requests.post("http://localhost:7860", data=data, files=files)
22
+ return Image.open(response.raw)
23
+
24
+ with gr.Blocks() as app:
25
+ gr.Markdown("# FLUX Style Shaping")
26
+ gr.Markdown("Interface conectada à API para geração de imagem com estilo e estrutura.")
27
+
28
+ with gr.Row():
29
+ with gr.Column():
30
+ prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
31
+ with gr.Row():
32
+ with gr.Group():
33
+ structure_image = gr.Image(label="Structure Image")
34
+ depth_strength = gr.Slider(minimum=0, maximum=50, value=15, label="Depth Strength")
35
+ with gr.Group():
36
+ style_image = gr.Image(label="Style Image")
37
+ style_strength = gr.Slider(minimum=0, maximum=1, value=0.5, label="Style Strength")
38
+ generate_btn = gr.Button("Generate")
39
+ output_image = gr.Image(label="Generated Image")
40
+
41
+ with gr.Column():
42
+ output_image.render()
43
+
44
+ generate_btn.click(
45
+ fn=call_api,
46
+ inputs=[prompt_input, structure_image, style_image, depth_strength, style_strength],
47
+ outputs=[output_image]
48
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
+ app.launch(show_api=False)