Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import numpy as np
|
|
3 |
import random
|
4 |
import spaces
|
5 |
import torch
|
|
|
6 |
import time
|
7 |
from diffusers import DiffusionPipeline
|
8 |
|
@@ -13,7 +14,7 @@ except ImportError:
|
|
13 |
raise ImportError("The 'sentencepiece' library is required but not installed. Please add it to your environment.")
|
14 |
|
15 |
# Set the device and dtype
|
16 |
-
dtype = torch.float16
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
|
19 |
# Load the diffusion pipeline without requiring an API token
|
@@ -23,141 +24,82 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
23 |
MAX_IMAGE_SIZE = 2048
|
24 |
|
25 |
@spaces.GPU()
|
26 |
-
def infer(prompt, negative_prompt
|
27 |
start_time = time.time()
|
28 |
-
|
29 |
if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
|
30 |
raise ValueError("Image size exceeds the maximum allowed dimensions.")
|
31 |
-
|
32 |
if randomize_seed:
|
33 |
seed = random.randint(0, MAX_SEED)
|
34 |
generator = torch.Generator(device=device).manual_seed(seed)
|
35 |
-
|
36 |
try:
|
37 |
-
# Include negative prompts in the diffusion pipeline call
|
38 |
image = pipe(
|
39 |
-
prompt=prompt,
|
40 |
-
negative_prompt=negative_prompt,
|
41 |
width=width,
|
42 |
height=height,
|
43 |
-
num_inference_steps=num_inference_steps,
|
44 |
generator=generator,
|
45 |
guidance_scale=guidance_scale
|
46 |
).images[0]
|
47 |
except Exception as e:
|
48 |
print(f"Error generating image: {e}")
|
49 |
return None, seed, f"Error: {str(e)}"
|
50 |
-
|
51 |
-
if time.time() - start_time > 60:
|
52 |
return None, seed, "Image generation took too long and was cancelled."
|
53 |
|
54 |
return image, seed, None
|
55 |
|
56 |
examples = [
|
57 |
-
"a tiny astronaut hatching from an egg on the moon",
|
58 |
-
"a cat holding a sign that says hello world",
|
59 |
-
"an anime illustration of a wiener schnitzel",
|
60 |
]
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
padding: 10px 20px;
|
76 |
-
font-size: 16px;
|
77 |
-
border-radius: 5px;
|
78 |
-
}
|
79 |
-
#run-button:hover {
|
80 |
-
background-color: #0056b3;
|
81 |
-
}
|
82 |
-
"""
|
83 |
-
|
84 |
-
with gr.Blocks(css=css) as demo:
|
85 |
-
with gr.Column(elem_id="col-container"):
|
86 |
-
gr.Markdown("""
|
87 |
-
# Custom Image Creator
|
88 |
-
A 12B param rectified flow transformer from [FLUX.1](https://blackforestlabs.ai/) for 4-step generation.
|
89 |
-
""", elem_id="title")
|
90 |
-
|
91 |
-
prompt = gr.Textbox(
|
92 |
-
label="Prompt",
|
93 |
-
show_label=False,
|
94 |
-
max_lines=1,
|
95 |
-
placeholder="Enter your prompt...",
|
96 |
-
)
|
97 |
-
negative_prompt = gr.Textbox(
|
98 |
-
label="Negative Prompt",
|
99 |
-
show_label=False,
|
100 |
-
max_lines=1,
|
101 |
-
placeholder="Enter negative prompts (what to avoid)...",
|
102 |
-
)
|
103 |
-
run_button = gr.Button("Run", elem_id="run-button")
|
104 |
-
|
105 |
-
result = gr.Image(label="Result", show_label=False, interactive=True)
|
106 |
-
|
107 |
-
with gr.Accordion("Advanced Settings", open=False):
|
108 |
-
seed = gr.Slider(
|
109 |
-
label="Seed",
|
110 |
-
minimum=0,
|
111 |
-
maximum=MAX_SEED,
|
112 |
-
step=1,
|
113 |
-
value=0,
|
114 |
-
tooltip="Seed value for reproducibility. Randomize for unique results."
|
115 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
step=32,
|
132 |
-
value=1024,
|
133 |
-
tooltip="Adjust the height of the generated image."
|
134 |
-
)
|
135 |
-
|
136 |
-
with gr.Row():
|
137 |
-
num_inference_steps = gr.Slider(
|
138 |
-
label="Number of inference steps",
|
139 |
-
minimum=1,
|
140 |
-
maximum=50,
|
141 |
-
step=1,
|
142 |
-
value=4,
|
143 |
-
tooltip="Controls the quality and coherence of the output."
|
144 |
-
)
|
145 |
-
guidance_scale = gr.Slider(
|
146 |
-
label="Guidance Scale",
|
147 |
-
minimum=0.0,
|
148 |
-
maximum=20.0,
|
149 |
-
step=0.5,
|
150 |
-
value=7.5,
|
151 |
-
tooltip="Higher values result in outputs closer to the prompt."
|
152 |
-
)
|
153 |
-
|
154 |
-
gr.Examples(
|
155 |
-
examples=examples,
|
156 |
-
fn=infer,
|
157 |
-
inputs=[prompt],
|
158 |
-
outputs=[result, seed],
|
159 |
-
cache_examples="lazy"
|
160 |
-
)
|
161 |
|
162 |
run_button.click(
|
163 |
fn=infer,
|
@@ -169,5 +111,5 @@ with gr.Blocks(css=css) as demo:
|
|
169 |
## Save Your Image
|
170 |
Right-click on the image and select 'Save As' to download the generated image.
|
171 |
""")
|
172 |
-
|
173 |
demo.launch()
|
|
|
3 |
import random
|
4 |
import spaces
|
5 |
import torch
|
6 |
+
import os
|
7 |
import time
|
8 |
from diffusers import DiffusionPipeline
|
9 |
|
|
|
14 |
raise ImportError("The 'sentencepiece' library is required but not installed. Please add it to your environment.")
|
15 |
|
16 |
# Set the device and dtype
|
17 |
+
dtype = torch.float16
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
|
20 |
# Load the diffusion pipeline without requiring an API token
|
|
|
24 |
MAX_IMAGE_SIZE = 2048
|
25 |
|
26 |
@spaces.GPU()
|
27 |
+
def infer(prompt, negative_prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=7.5, progress=gr.Progress(track_tqdm=True)):
|
28 |
start_time = time.time()
|
29 |
+
|
30 |
if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
|
31 |
raise ValueError("Image size exceeds the maximum allowed dimensions.")
|
32 |
+
|
33 |
if randomize_seed:
|
34 |
seed = random.randint(0, MAX_SEED)
|
35 |
generator = torch.Generator(device=device).manual_seed(seed)
|
36 |
+
|
37 |
try:
|
|
|
38 |
image = pipe(
|
39 |
+
prompt=prompt,
|
40 |
+
negative_prompt=negative_prompt,
|
41 |
width=width,
|
42 |
height=height,
|
43 |
+
num_inference_steps=num_inference_steps,
|
44 |
generator=generator,
|
45 |
guidance_scale=guidance_scale
|
46 |
).images[0]
|
47 |
except Exception as e:
|
48 |
print(f"Error generating image: {e}")
|
49 |
return None, seed, f"Error: {str(e)}"
|
50 |
+
|
51 |
+
if time.time() - start_time > 60:
|
52 |
return None, seed, "Image generation took too long and was cancelled."
|
53 |
|
54 |
return image, seed, None
|
55 |
|
56 |
examples = [
|
57 |
+
["a tiny astronaut hatching from an egg on the moon", "blurry, low quality"],
|
58 |
+
["a cat holding a sign that says hello world", "dog, text, writing"],
|
59 |
+
["an anime illustration of a wiener schnitzel", "realistic, photograph"],
|
60 |
]
|
61 |
|
62 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
63 |
+
gr.Markdown("""
|
64 |
+
# Custom Image Creator
|
65 |
+
12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
|
66 |
+
[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1)]
|
67 |
+
""")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(scale=2):
|
71 |
+
prompt = gr.Textbox(
|
72 |
+
label="Prompt",
|
73 |
+
placeholder="Enter your prompt",
|
74 |
+
lines=3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
)
|
76 |
+
negative_prompt = gr.Textbox(
|
77 |
+
label="Negative Prompt",
|
78 |
+
placeholder="Enter things to avoid in the image",
|
79 |
+
lines=2
|
80 |
+
)
|
81 |
+
run_button = gr.Button("Generate Image")
|
82 |
+
with gr.Column(scale=3):
|
83 |
+
result = gr.Image(label="Generated Image")
|
84 |
+
|
85 |
+
with gr.Accordion("Advanced Settings", open=False):
|
86 |
+
with gr.Row():
|
87 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
88 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
89 |
+
with gr.Row():
|
90 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
91 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
92 |
+
with gr.Row():
|
93 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=4)
|
94 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=20.0, step=0.5, value=7.5)
|
95 |
+
|
96 |
+
gr.Examples(
|
97 |
+
examples=examples,
|
98 |
+
fn=infer,
|
99 |
+
inputs=[prompt, negative_prompt],
|
100 |
+
outputs=[result, seed],
|
101 |
+
cache_examples=True
|
102 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
run_button.click(
|
105 |
fn=infer,
|
|
|
111 |
## Save Your Image
|
112 |
Right-click on the image and select 'Save As' to download the generated image.
|
113 |
""")
|
114 |
+
|
115 |
demo.launch()
|