Spaces:
Runtime error
Runtime error
try to inport inside gpu
Browse files
app.py
CHANGED
@@ -8,101 +8,101 @@ import gradio as gr
|
|
8 |
from PIL import Image
|
9 |
import os
|
10 |
|
11 |
-
from src.flux.xflux_pipeline import XFluxPipeline
|
12 |
import random
|
13 |
|
14 |
-
def run_xflux_pipeline(
|
15 |
-
prompt, image, repo_id, name, device,
|
16 |
-
model_type, width, height, timestep_to_start_cfg, num_steps, true_gs, guidance,
|
17 |
-
neg_prompt="",
|
18 |
-
negative_image=None,
|
19 |
-
save_path='results', control_type='depth', use_controlnet=False, seed=None, num_images_per_prompt=1, use_lora=False, lora_weight=0.7, lora_repo_id="XLabs-AI/flux-lora-collection", lora_name="realism_lora.safetensors", use_ip=False
|
20 |
-
):
|
21 |
-
# Montando os argumentos simulando a linha de comando
|
22 |
-
class Args:
|
23 |
-
def __init__(self):
|
24 |
-
self.prompt = prompt
|
25 |
-
self.image = image
|
26 |
-
self.control_type = control_type
|
27 |
-
self.repo_id = repo_id
|
28 |
-
self.name = name
|
29 |
-
self.device = device
|
30 |
-
self.use_controlnet = use_controlnet
|
31 |
-
self.model_type = model_type
|
32 |
-
self.width = width
|
33 |
-
self.height = height
|
34 |
-
self.timestep_to_start_cfg = timestep_to_start_cfg
|
35 |
-
self.num_steps = num_steps
|
36 |
-
self.true_gs = true_gs
|
37 |
-
self.guidance = guidance
|
38 |
-
self.num_images_per_prompt = num_images_per_prompt
|
39 |
-
self.seed = seed if seed else 123456789
|
40 |
-
self.neg_prompt = neg_prompt
|
41 |
-
self.img_prompt = Image.open(image)
|
42 |
-
self.neg_img_prompt = Image.open(negative_image) if negative_image else None
|
43 |
-
self.ip_scale = 1.0
|
44 |
-
self.neg_ip_scale = 1.0
|
45 |
-
self.local_path = None
|
46 |
-
self.ip_repo_id = "XLabs-AI/flux-ip-adapter"
|
47 |
-
self.ip_name = "flux-ip-adapter.safetensors"
|
48 |
-
self.ip_local_path = None
|
49 |
-
self.lora_repo_id = lora_repo_id
|
50 |
-
self.lora_name = lora_name
|
51 |
-
self.lora_local_path = None
|
52 |
-
self.offload = False
|
53 |
-
self.use_ip = use_ip
|
54 |
-
self.use_lora = use_lora
|
55 |
-
self.lora_weight = lora_weight
|
56 |
-
self.save_path = save_path
|
57 |
-
|
58 |
-
args = Args()
|
59 |
-
|
60 |
-
# Carregar a imagem se fornecida
|
61 |
-
if args.image:
|
62 |
-
image = Image.open(args.image)
|
63 |
-
else:
|
64 |
-
image = None
|
65 |
-
|
66 |
-
# Inicializar o pipeline com os parâmetros necessários
|
67 |
-
xflux_pipeline = XFluxPipeline(args.model_type, args.device, args.offload)
|
68 |
-
|
69 |
-
# Configurar ControlNet se necessário
|
70 |
-
if args.use_controlnet:
|
71 |
-
print('Loading ControlNet:', args.local_path, args.repo_id, args.name)
|
72 |
-
xflux_pipeline.set_controlnet(args.control_type, args.local_path, args.repo_id, args.name)
|
73 |
-
if args.use_ip:
|
74 |
-
print('load ip-adapter:', args.ip_local_path, args.ip_repo_id, args.ip_name)
|
75 |
-
xflux_pipeline.set_ip(args.ip_local_path, args.ip_repo_id, args.ip_name)
|
76 |
-
if args.use_lora:
|
77 |
-
print('load lora:', args.lora_local_path, args.lora_repo_id, args.lora_name)
|
78 |
-
xflux_pipeline.set_lora(args.lora_local_path, args.lora_repo_id, args.lora_name, args.lora_weight)
|
79 |
-
|
80 |
-
# Laço para gerar imagens
|
81 |
-
images = []
|
82 |
-
for _ in range(args.num_images_per_prompt):
|
83 |
-
seed = random.randint(0, 2147483647)
|
84 |
-
result = xflux_pipeline(
|
85 |
-
prompt=args.prompt,
|
86 |
-
controlnet_image=image,
|
87 |
-
width=args.width,
|
88 |
-
height=args.height,
|
89 |
-
guidance=args.guidance,
|
90 |
-
num_steps=args.num_steps,
|
91 |
-
seed=seed,
|
92 |
-
true_gs=args.true_gs,
|
93 |
-
neg_prompt=args.neg_prompt,
|
94 |
-
timestep_to_start_cfg=args.timestep_to_start_cfg,
|
95 |
-
image_prompt=args.img_prompt,
|
96 |
-
neg_image_prompt=args.neg_img_prompt,
|
97 |
-
ip_scale=args.ip_scale,
|
98 |
-
neg_ip_scale=args.neg_ip_scale,
|
99 |
-
)
|
100 |
-
images.append(result)
|
101 |
-
|
102 |
-
return images
|
103 |
-
|
104 |
@spaces.GPU(duration=300)
|
105 |
def process_image(image, prompt, steps, use_lora, use_controlnet, use_depth, use_hed, use_ip, lora_name, lora_path, lora_weight, negative_image, neg_prompt, true_gs, guidance, cfg):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
return run_xflux_pipeline(
|
107 |
prompt=prompt,
|
108 |
neg_prompt=neg_prompt,
|
|
|
8 |
from PIL import Image
|
9 |
import os
|
10 |
|
|
|
11 |
import random
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
@spaces.GPU(duration=300)
|
14 |
def process_image(image, prompt, steps, use_lora, use_controlnet, use_depth, use_hed, use_ip, lora_name, lora_path, lora_weight, negative_image, neg_prompt, true_gs, guidance, cfg):
|
15 |
+
from src.flux.xflux_pipeline import XFluxPipeline
|
16 |
+
def run_xflux_pipeline(
|
17 |
+
prompt, image, repo_id, name, device,
|
18 |
+
model_type, width, height, timestep_to_start_cfg, num_steps, true_gs, guidance,
|
19 |
+
neg_prompt="",
|
20 |
+
negative_image=None,
|
21 |
+
save_path='results', control_type='depth', use_controlnet=False, seed=None, num_images_per_prompt=1, use_lora=False, lora_weight=0.7, lora_repo_id="XLabs-AI/flux-lora-collection", lora_name="realism_lora.safetensors", use_ip=False
|
22 |
+
):
|
23 |
+
# Montando os argumentos simulando a linha de comando
|
24 |
+
class Args:
|
25 |
+
def __init__(self):
|
26 |
+
self.prompt = prompt
|
27 |
+
self.image = image
|
28 |
+
self.control_type = control_type
|
29 |
+
self.repo_id = repo_id
|
30 |
+
self.name = name
|
31 |
+
self.device = device
|
32 |
+
self.use_controlnet = use_controlnet
|
33 |
+
self.model_type = model_type
|
34 |
+
self.width = width
|
35 |
+
self.height = height
|
36 |
+
self.timestep_to_start_cfg = timestep_to_start_cfg
|
37 |
+
self.num_steps = num_steps
|
38 |
+
self.true_gs = true_gs
|
39 |
+
self.guidance = guidance
|
40 |
+
self.num_images_per_prompt = num_images_per_prompt
|
41 |
+
self.seed = seed if seed else 123456789
|
42 |
+
self.neg_prompt = neg_prompt
|
43 |
+
self.img_prompt = Image.open(image)
|
44 |
+
self.neg_img_prompt = Image.open(negative_image) if negative_image else None
|
45 |
+
self.ip_scale = 1.0
|
46 |
+
self.neg_ip_scale = 1.0
|
47 |
+
self.local_path = None
|
48 |
+
self.ip_repo_id = "XLabs-AI/flux-ip-adapter"
|
49 |
+
self.ip_name = "flux-ip-adapter.safetensors"
|
50 |
+
self.ip_local_path = None
|
51 |
+
self.lora_repo_id = lora_repo_id
|
52 |
+
self.lora_name = lora_name
|
53 |
+
self.lora_local_path = None
|
54 |
+
self.offload = False
|
55 |
+
self.use_ip = use_ip
|
56 |
+
self.use_lora = use_lora
|
57 |
+
self.lora_weight = lora_weight
|
58 |
+
self.save_path = save_path
|
59 |
+
|
60 |
+
args = Args()
|
61 |
+
|
62 |
+
# Carregar a imagem se fornecida
|
63 |
+
if args.image:
|
64 |
+
image = Image.open(args.image)
|
65 |
+
else:
|
66 |
+
image = None
|
67 |
+
|
68 |
+
# Inicializar o pipeline com os parâmetros necessários
|
69 |
+
xflux_pipeline = XFluxPipeline(args.model_type, args.device, args.offload)
|
70 |
+
|
71 |
+
# Configurar ControlNet se necessário
|
72 |
+
if args.use_controlnet:
|
73 |
+
print('Loading ControlNet:', args.local_path, args.repo_id, args.name)
|
74 |
+
xflux_pipeline.set_controlnet(args.control_type, args.local_path, args.repo_id, args.name)
|
75 |
+
if args.use_ip:
|
76 |
+
print('load ip-adapter:', args.ip_local_path, args.ip_repo_id, args.ip_name)
|
77 |
+
xflux_pipeline.set_ip(args.ip_local_path, args.ip_repo_id, args.ip_name)
|
78 |
+
if args.use_lora:
|
79 |
+
print('load lora:', args.lora_local_path, args.lora_repo_id, args.lora_name)
|
80 |
+
xflux_pipeline.set_lora(args.lora_local_path, args.lora_repo_id, args.lora_name, args.lora_weight)
|
81 |
+
|
82 |
+
# Laço para gerar imagens
|
83 |
+
images = []
|
84 |
+
for _ in range(args.num_images_per_prompt):
|
85 |
+
seed = random.randint(0, 2147483647)
|
86 |
+
result = xflux_pipeline(
|
87 |
+
prompt=args.prompt,
|
88 |
+
controlnet_image=image,
|
89 |
+
width=args.width,
|
90 |
+
height=args.height,
|
91 |
+
guidance=args.guidance,
|
92 |
+
num_steps=args.num_steps,
|
93 |
+
seed=seed,
|
94 |
+
true_gs=args.true_gs,
|
95 |
+
neg_prompt=args.neg_prompt,
|
96 |
+
timestep_to_start_cfg=args.timestep_to_start_cfg,
|
97 |
+
image_prompt=args.img_prompt,
|
98 |
+
neg_image_prompt=args.neg_img_prompt,
|
99 |
+
ip_scale=args.ip_scale,
|
100 |
+
neg_ip_scale=args.neg_ip_scale,
|
101 |
+
)
|
102 |
+
images.append(result)
|
103 |
+
|
104 |
+
return images
|
105 |
+
|
106 |
return run_xflux_pipeline(
|
107 |
prompt=prompt,
|
108 |
neg_prompt=neg_prompt,
|