Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -9,55 +9,62 @@ from translatepy import Translator
|
|
9 |
import numpy as np
|
10 |
import random
|
11 |
import os
|
12 |
-
hf_token = os.environ.get('HF_TOKEN')
|
13 |
-
from io import BytesIO
|
14 |
|
|
|
15 |
translator = Translator()
|
16 |
|
17 |
# Constants
|
18 |
model = "black-forest-labs/FLUX.1-dev"
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
MAX_SEED = np.iinfo(np.int32).max
|
23 |
MAX_IMAGE_SIZE = 2048
|
24 |
|
25 |
-
|
26 |
-
if torch.cuda.is_available()
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
pipe = FluxPipeline.from_pretrained(
|
32 |
-
model,
|
33 |
transformer=transformer,
|
34 |
-
torch_dtype=torch.bfloat16,
|
|
|
|
|
35 |
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
|
36 |
pipe.scheduler.config, use_beta_sigmas=True
|
37 |
)
|
38 |
-
pipe.to(
|
39 |
-
|
40 |
-
|
41 |
-
@spaces.GPU()
|
42 |
-
def infer(prompt, width, height, num_inference_steps, guidance_scale, nums, seed=42, randomize_seed=True, progress=gr.Progress(track_tqdm=True)):
|
43 |
if randomize_seed:
|
44 |
seed = random.randint(0, MAX_SEED)
|
45 |
-
generator = torch.Generator().manual_seed(seed)
|
46 |
-
image = pipe(
|
47 |
-
prompt = prompt,
|
48 |
-
width = width,
|
49 |
-
height = height,
|
50 |
-
num_inference_steps = num_inference_steps,
|
51 |
-
guidance_scale=guidance_scale,
|
52 |
-
num_images_per_prompt=nums,
|
53 |
-
generator = generator
|
54 |
-
).images
|
55 |
-
|
56 |
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
|
|
59 |
|
60 |
-
css="""
|
61 |
#col-container {
|
62 |
margin: 0 auto;
|
63 |
max-width: 1024px;
|
@@ -65,12 +72,10 @@ css="""
|
|
65 |
"""
|
66 |
|
67 |
with gr.Blocks(css=css) as demo:
|
68 |
-
|
69 |
with gr.Column(elem_id="col-container"):
|
70 |
gr.HTML("<h1><center>Model Testing</center></h1><p><center>Chroma</center></p>")
|
71 |
|
72 |
with gr.Row():
|
73 |
-
|
74 |
prompt = gr.Text(
|
75 |
label="Prompt",
|
76 |
show_label=False,
|
@@ -78,15 +83,12 @@ with gr.Blocks(css=css) as demo:
|
|
78 |
placeholder="Enter your prompt",
|
79 |
container=False,
|
80 |
)
|
81 |
-
|
82 |
run_button = gr.Button("Run", scale=0)
|
83 |
|
84 |
-
result = gr.Gallery(label="Gallery", format="png", columns
|
85 |
|
86 |
with gr.Accordion("Advanced Settings", open=False):
|
87 |
-
|
88 |
with gr.Row():
|
89 |
-
|
90 |
width = gr.Slider(
|
91 |
label="Width",
|
92 |
minimum=256,
|
@@ -94,7 +96,6 @@ with gr.Blocks(css=css) as demo:
|
|
94 |
step=32,
|
95 |
value=1024,
|
96 |
)
|
97 |
-
|
98 |
height = gr.Slider(
|
99 |
label="Height",
|
100 |
minimum=256,
|
@@ -104,7 +105,6 @@ with gr.Blocks(css=css) as demo:
|
|
104 |
)
|
105 |
|
106 |
with gr.Row():
|
107 |
-
|
108 |
num_inference_steps = gr.Slider(
|
109 |
label="Number of inference steps",
|
110 |
minimum=1,
|
@@ -112,7 +112,6 @@ with gr.Blocks(css=css) as demo:
|
|
112 |
step=1,
|
113 |
value=30,
|
114 |
)
|
115 |
-
|
116 |
guidance_scale = gr.Slider(
|
117 |
label="Guidance Scale",
|
118 |
minimum=0,
|
@@ -121,9 +120,7 @@ with gr.Blocks(css=css) as demo:
|
|
121 |
value=3.5,
|
122 |
)
|
123 |
|
124 |
-
|
125 |
with gr.Row():
|
126 |
-
|
127 |
nums = gr.Slider(
|
128 |
label="Number of Images",
|
129 |
minimum=1,
|
@@ -132,7 +129,6 @@ with gr.Blocks(css=css) as demo:
|
|
132 |
value=1,
|
133 |
scale=1,
|
134 |
)
|
135 |
-
|
136 |
seed = gr.Slider(
|
137 |
label="Seed",
|
138 |
minimum=0,
|
@@ -140,14 +136,13 @@ with gr.Blocks(css=css) as demo:
|
|
140 |
step=1,
|
141 |
value=-1,
|
142 |
)
|
143 |
-
|
144 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
145 |
|
146 |
gr.on(
|
147 |
triggers=[run_button.click, prompt.submit],
|
148 |
-
fn
|
149 |
-
inputs
|
150 |
-
outputs
|
151 |
)
|
152 |
-
|
153 |
demo.launch()
|
|
|
9 |
import numpy as np
|
10 |
import random
|
11 |
import os
|
|
|
|
|
12 |
|
13 |
+
hf_token = os.environ.get('HF_TOKEN')
|
14 |
translator = Translator()
|
15 |
|
16 |
# Constants
|
17 |
model = "black-forest-labs/FLUX.1-dev"
|
|
|
|
|
|
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
MAX_IMAGE_SIZE = 2048
|
20 |
|
21 |
+
@spaces.GPU()
|
22 |
+
def infer(prompt, width, height, num_inference_steps, guidance_scale, nums, seed=42, randomize_seed=True, progress雷神加速器梯子 = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
+
# Initialize model inside the GPU-enabled function
|
24 |
+
try:
|
25 |
+
transformer = FluxTransformer2DModel.from_single_file(
|
26 |
+
"https://huggingface.co/lodestones/Chroma/resolve/main/chroma-unlocked-v27.safetensors",
|
27 |
+
torch_dtype=torch.bfloat16,
|
28 |
+
token=hf_token
|
29 |
+
)
|
30 |
+
except KeyError as e:
|
31 |
+
print(f"Error loading chroma-unlocked-v27.safetensors: {e}. Falling back to pretrained model.")
|
32 |
+
transformer = FluxTransformer2DModel.from_pretrained(
|
33 |
+
"lodestones/Chroma",
|
34 |
+
subfolder="transformer",
|
35 |
+
torch_dtype=torch.bfloat16,
|
36 |
+
token=hf_token
|
37 |
+
)
|
38 |
+
|
39 |
pipe = FluxPipeline.from_pretrained(
|
40 |
+
model,
|
41 |
transformer=transformer,
|
42 |
+
torch_dtype=torch.bfloat16,
|
43 |
+
token=hf_token
|
44 |
+
)
|
45 |
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
|
46 |
pipe.scheduler.config, use_beta_sigmas=True
|
47 |
)
|
48 |
+
pipe.to(device)
|
49 |
+
|
50 |
+
# Generate images
|
|
|
|
|
51 |
if randomize_seed:
|
52 |
seed = random.randint(0, MAX_SEED)
|
53 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
images = pipe(
|
56 |
+
prompt=prompt,
|
57 |
+
width=width,
|
58 |
+
height=height,
|
59 |
+
num_inference_steps=num_inference_steps,
|
60 |
+
guidance_scale=guidance_scale,
|
61 |
+
num_images_per_prompt=nums,
|
62 |
+
generator=generator
|
63 |
+
).images
|
64 |
|
65 |
+
return images, seed
|
66 |
|
67 |
+
css = """
|
68 |
#col-container {
|
69 |
margin: 0 auto;
|
70 |
max-width: 1024px;
|
|
|
72 |
"""
|
73 |
|
74 |
with gr.Blocks(css=css) as demo:
|
|
|
75 |
with gr.Column(elem_id="col-container"):
|
76 |
gr.HTML("<h1><center>Model Testing</center></h1><p><center>Chroma</center></p>")
|
77 |
|
78 |
with gr.Row():
|
|
|
79 |
prompt = gr.Text(
|
80 |
label="Prompt",
|
81 |
show_label=False,
|
|
|
83 |
placeholder="Enter your prompt",
|
84 |
container=False,
|
85 |
)
|
|
|
86 |
run_button = gr.Button("Run", scale=0)
|
87 |
|
88 |
+
result = gr.Gallery(label="Gallery", format="png", columns=1, preview=True, height=400)
|
89 |
|
90 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
91 |
with gr.Row():
|
|
|
92 |
width = gr.Slider(
|
93 |
label="Width",
|
94 |
minimum=256,
|
|
|
96 |
step=32,
|
97 |
value=1024,
|
98 |
)
|
|
|
99 |
height = gr.Slider(
|
100 |
label="Height",
|
101 |
minimum=256,
|
|
|
105 |
)
|
106 |
|
107 |
with gr.Row():
|
|
|
108 |
num_inference_steps = gr.Slider(
|
109 |
label="Number of inference steps",
|
110 |
minimum=1,
|
|
|
112 |
step=1,
|
113 |
value=30,
|
114 |
)
|
|
|
115 |
guidance_scale = gr.Slider(
|
116 |
label="Guidance Scale",
|
117 |
minimum=0,
|
|
|
120 |
value=3.5,
|
121 |
)
|
122 |
|
|
|
123 |
with gr.Row():
|
|
|
124 |
nums = gr.Slider(
|
125 |
label="Number of Images",
|
126 |
minimum=1,
|
|
|
129 |
value=1,
|
130 |
scale=1,
|
131 |
)
|
|
|
132 |
seed = gr.Slider(
|
133 |
label="Seed",
|
134 |
minimum=0,
|
|
|
136 |
step=1,
|
137 |
value=-1,
|
138 |
)
|
|
|
139 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
140 |
|
141 |
gr.on(
|
142 |
triggers=[run_button.click, prompt.submit],
|
143 |
+
fn=infer,
|
144 |
+
inputs=[prompt, width, height, num_inference_steps, guidance_scale, nums, seed, randomize_seed],
|
145 |
+
outputs=[result, seed]
|
146 |
)
|
147 |
+
|
148 |
demo.launch()
|