Spaces:
Running
on
Zero
Running
on
Zero
DjStompzone
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
|
5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
|
11 |
-
|
12 |
-
if torch.cuda.is_available():
|
13 |
-
torch_dtype = torch.float16
|
14 |
-
else:
|
15 |
-
torch_dtype = torch.float32
|
16 |
|
17 |
-
|
|
|
18 |
pipe = pipe.to(device)
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
-
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
-
|
24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
25 |
def infer(
|
26 |
prompt,
|
27 |
-
|
28 |
-
|
29 |
-
randomize_seed,
|
30 |
-
width,
|
31 |
-
height,
|
32 |
guidance_scale,
|
33 |
num_inference_steps,
|
34 |
-
|
|
|
35 |
):
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
|
39 |
-
generator = torch.
|
|
|
40 |
|
41 |
-
|
|
|
42 |
prompt=prompt,
|
43 |
-
|
44 |
-
|
45 |
num_inference_steps=num_inference_steps,
|
46 |
-
|
47 |
-
height=height,
|
48 |
generator=generator,
|
49 |
).images[0]
|
50 |
|
51 |
-
return
|
52 |
-
|
53 |
-
|
54 |
-
examples = [
|
55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
-
"An astronaut riding a green horse",
|
57 |
-
"A delicious ceviche cheesecake slice",
|
58 |
-
]
|
59 |
|
60 |
css = """
|
61 |
#col-container {
|
@@ -66,89 +54,75 @@ css = """
|
|
66 |
|
67 |
with gr.Blocks(css=css) as demo:
|
68 |
with gr.Column(elem_id="col-container"):
|
69 |
-
gr.Markdown("
|
70 |
|
71 |
with gr.Row():
|
72 |
-
prompt = gr.
|
73 |
label="Prompt",
|
74 |
-
show_label=False,
|
75 |
-
max_lines=1,
|
76 |
placeholder="Enter your prompt",
|
77 |
-
|
78 |
)
|
79 |
-
|
80 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
81 |
|
82 |
result = gr.Image(label="Result", show_label=False)
|
83 |
|
84 |
with gr.Accordion("Advanced Settings", open=False):
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
)
|
91 |
-
|
92 |
seed = gr.Slider(
|
93 |
label="Seed",
|
94 |
minimum=0,
|
95 |
maximum=MAX_SEED,
|
96 |
step=1,
|
97 |
-
value=0
|
98 |
)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024, # Replace with defaults that work for your model
|
117 |
-
)
|
118 |
-
|
119 |
-
with gr.Row():
|
120 |
-
guidance_scale = gr.Slider(
|
121 |
-
label="Guidance scale",
|
122 |
-
minimum=0.0,
|
123 |
-
maximum=10.0,
|
124 |
-
step=0.1,
|
125 |
-
value=0.0, # Replace with defaults that work for your model
|
126 |
-
)
|
127 |
-
|
128 |
-
num_inference_steps = gr.Slider(
|
129 |
-
label="Number of inference steps",
|
130 |
-
minimum=1,
|
131 |
-
maximum=50,
|
132 |
-
step=1,
|
133 |
-
value=2, # Replace with defaults that work for your model
|
134 |
-
)
|
135 |
-
|
136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
137 |
-
gr.on(
|
138 |
-
triggers=[run_button.click, prompt.submit],
|
139 |
-
fn=infer,
|
140 |
inputs=[
|
141 |
prompt,
|
142 |
-
|
143 |
-
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
guidance_scale,
|
148 |
num_inference_steps,
|
|
|
|
|
149 |
],
|
150 |
-
outputs=[result, seed]
|
151 |
)
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
+
from diffusers.utils import load_image
|
4 |
+
from diffusers.pipelines.flux.pipeline_flux_controlnet import FluxControlNetPipeline
|
5 |
+
from diffusers.models.controlnet_flux import FluxControlNetModel
|
6 |
+
import random
|
7 |
+
import numpy as np
|
8 |
|
9 |
+
# Initialize models
|
10 |
+
base_model = 'black-forest-labs/FLUX.1-dev'
|
11 |
+
controlnet_model = 'promeai/FLUX.1-controlnet-lineart-promeai'
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch_dtype)
|
16 |
+
pipe = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch_dtype)
|
17 |
pipe = pipe.to(device)
|
18 |
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
20 |
|
|
|
|
|
21 |
def infer(
|
22 |
prompt,
|
23 |
+
control_image_path,
|
24 |
+
controlnet_conditioning_scale,
|
|
|
|
|
|
|
25 |
guidance_scale,
|
26 |
num_inference_steps,
|
27 |
+
seed,
|
28 |
+
randomize_seed,
|
29 |
):
|
30 |
if randomize_seed:
|
31 |
seed = random.randint(0, MAX_SEED)
|
32 |
|
33 |
+
generator = torch.manual_seed(seed)
|
34 |
+
control_image = load_image(control_image_path) if control_image_path else None
|
35 |
|
36 |
+
# Generate image
|
37 |
+
result = pipe(
|
38 |
prompt=prompt,
|
39 |
+
control_image=control_image,
|
40 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
41 |
num_inference_steps=num_inference_steps,
|
42 |
+
guidance_scale=guidance_scale,
|
|
|
43 |
generator=generator,
|
44 |
).images[0]
|
45 |
|
46 |
+
return result, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
css = """
|
49 |
#col-container {
|
|
|
54 |
|
55 |
with gr.Blocks(css=css) as demo:
|
56 |
with gr.Column(elem_id="col-container"):
|
57 |
+
gr.Markdown("## Zero-shot Partial Style Transfer for Line Art Images, Powered by FLUX.1")
|
58 |
|
59 |
with gr.Row():
|
60 |
+
prompt = gr.Textbox(
|
61 |
label="Prompt",
|
|
|
|
|
62 |
placeholder="Enter your prompt",
|
63 |
+
max_lines=1,
|
64 |
)
|
65 |
+
run_button = gr.Button("Generate", variant="primary")
|
|
|
66 |
|
67 |
result = gr.Image(label="Result", show_label=False)
|
68 |
|
69 |
with gr.Accordion("Advanced Settings", open=False):
|
70 |
+
control_image = gr.Image(
|
71 |
+
source="upload",
|
72 |
+
type="filepath",
|
73 |
+
label="Control Image (Line Art)"
|
74 |
+
)
|
75 |
+
controlnet_conditioning_scale = gr.Slider(
|
76 |
+
label="ControlNet Conditioning Scale",
|
77 |
+
minimum=0.0,
|
78 |
+
maximum=1.0,
|
79 |
+
value=0.6,
|
80 |
+
step=0.1
|
81 |
+
)
|
82 |
+
guidance_scale = gr.Slider(
|
83 |
+
label="Guidance Scale",
|
84 |
+
minimum=1.0,
|
85 |
+
maximum=10.0,
|
86 |
+
value=3.5,
|
87 |
+
step=0.1
|
88 |
+
)
|
89 |
+
num_inference_steps = gr.Slider(
|
90 |
+
label="Number of Inference Steps",
|
91 |
+
minimum=1,
|
92 |
+
maximum=100,
|
93 |
+
value=28,
|
94 |
+
step=1
|
95 |
)
|
|
|
96 |
seed = gr.Slider(
|
97 |
label="Seed",
|
98 |
minimum=0,
|
99 |
maximum=MAX_SEED,
|
100 |
step=1,
|
101 |
+
value=0
|
102 |
)
|
103 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
104 |
+
|
105 |
+
gr.Examples(
|
106 |
+
examples=[
|
107 |
+
"Anime girl with fennec ears holding a cake",
|
108 |
+
"Victorian style mansion interior with candlelight"
|
109 |
+
],
|
110 |
+
inputs=[prompt]
|
111 |
+
)
|
112 |
+
|
113 |
+
run_button.click(
|
114 |
+
infer,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
inputs=[
|
116 |
prompt,
|
117 |
+
control_image,
|
118 |
+
controlnet_conditioning_scale,
|
|
|
|
|
|
|
119 |
guidance_scale,
|
120 |
num_inference_steps,
|
121 |
+
seed,
|
122 |
+
randomize_seed
|
123 |
],
|
124 |
+
outputs=[result, seed]
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
+
demo.launch()
|