Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
import spaces
|
5 |
import torch
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
-
from PIL import Image
|
8 |
-
import io
|
9 |
import os
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
dtype = torch.bfloat16
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
-
|
14 |
-
# Set your Hugging Face API token
|
15 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
MAX_IMAGE_SIZE = 2048
|
22 |
-
|
23 |
-
@spaces.GPU(duration=200)
|
24 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
25 |
if randomize_seed:
|
26 |
seed = random.randint(0, MAX_SEED)
|
27 |
generator = torch.Generator().manual_seed(seed)
|
@@ -35,30 +35,15 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
|
|
35 |
).images[0]
|
36 |
return image, seed
|
37 |
|
38 |
-
def download_image(image, file_format):
|
39 |
-
img_byte_arr = io.BytesIO()
|
40 |
-
image.save(img_byte_arr, format=file_format)
|
41 |
-
img_byte_arr = img_byte_arr.getvalue()
|
42 |
-
return img_byte_arr
|
43 |
-
|
44 |
-
examples = [
|
45 |
-
"a galaxy swirling with vibrant blue and purple hues",
|
46 |
-
"a futuristic cityscape under a dark sky",
|
47 |
-
"a serene forest with a magical glowing tree",
|
48 |
-
"a futuristic cityscape with sleek skyscrapers and flying cars",
|
49 |
-
"a portrait of a smiling woman with a colorful floral crown",
|
50 |
-
"a fantastical creature with the body of a dragon and the wings of a butterfly",
|
51 |
-
]
|
52 |
-
|
53 |
css = """
|
54 |
body {
|
55 |
background-color: #f4faff;
|
56 |
color: #005662;
|
57 |
font-family: 'Poppins', sans-serif;
|
58 |
}
|
59 |
-
|
60 |
margin: 0 auto;
|
61 |
-
max-width:
|
62 |
padding: 20px;
|
63 |
}
|
64 |
.gr-button {
|
@@ -70,135 +55,62 @@ body {
|
|
70 |
.gr-button:hover {
|
71 |
background-color: #0277bd;
|
72 |
}
|
73 |
-
.gr-
|
74 |
-
border: 1px solid #eeeeee;
|
75 |
border-radius: 12px;
|
76 |
-
|
77 |
-
margin-bottom: 12px;
|
78 |
-
}
|
79 |
-
.gr-examples-card:hover {
|
80 |
-
background-color: #f4faf2;
|
81 |
-
border-color: #0277bd;
|
82 |
-
color: #005662;
|
83 |
-
}
|
84 |
-
.gr-progress-bar, .gr-progress-bar-fill {
|
85 |
-
background-color: #0288d1 !important;
|
86 |
-
}
|
87 |
-
.gr-slider, .gr-slider-track {
|
88 |
-
background-color: #0288d1 !important;
|
89 |
-
}
|
90 |
-
.gr-slider-thumb {
|
91 |
-
background-color: #005662 !important;
|
92 |
-
}
|
93 |
-
.gr-text-input, .gr-image {
|
94 |
-
width: 100%;
|
95 |
-
box-sizing: border-box;
|
96 |
-
margin-bottom: 10px;
|
97 |
}
|
98 |
"""
|
99 |
|
100 |
with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
|
|
|
|
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
<h3>View Model Details</h3>
|
108 |
-
<p>Explore more about this model on Hugging Face.</p>
|
109 |
-
</div>
|
110 |
-
</a>
|
111 |
-
""")
|
112 |
-
|
113 |
-
with gr.Row():
|
114 |
prompt = gr.Text(
|
115 |
label="Prompt",
|
116 |
-
|
117 |
-
|
118 |
-
placeholder="Enter your prompt",
|
119 |
-
container=False,
|
120 |
)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
125 |
|
126 |
-
with gr.
|
127 |
-
|
128 |
-
|
129 |
-
minimum=0,
|
130 |
-
maximum=MAX_SEED,
|
131 |
-
step=1,
|
132 |
-
value=0,
|
133 |
-
)
|
134 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
135 |
-
|
136 |
-
with gr.Row():
|
137 |
-
width = gr.Slider(
|
138 |
-
label="Width",
|
139 |
-
minimum=256,
|
140 |
-
maximum=MAX_IMAGE_SIZE,
|
141 |
-
step=32,
|
142 |
-
value=1024,
|
143 |
-
)
|
144 |
-
height = gr.Slider(
|
145 |
-
label="Height",
|
146 |
-
minimum=256,
|
147 |
-
maximum=MAX_IMAGE_SIZE,
|
148 |
-
step=32,
|
149 |
-
value=1024,
|
150 |
-
)
|
151 |
-
|
152 |
-
with gr.Row():
|
153 |
-
guidance_scale = gr.Slider(
|
154 |
-
label="Guidance Scale",
|
155 |
-
minimum=1,
|
156 |
-
maximum=15,
|
157 |
-
step=0.1,
|
158 |
-
value=3.5,
|
159 |
-
)
|
160 |
-
num_inference_steps = gr.Slider(
|
161 |
-
label="Number of inference steps",
|
162 |
-
minimum=1,
|
163 |
-
maximum=50,
|
164 |
-
step=1,
|
165 |
-
value=28,
|
166 |
-
)
|
167 |
|
168 |
-
|
169 |
-
label="
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
cache_examples="lazy"
|
189 |
-
)
|
190 |
-
|
191 |
-
gr.on(
|
192 |
-
triggers=[run_button.click, prompt.submit],
|
193 |
-
fn=infer,
|
194 |
inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
195 |
outputs=[result, seed]
|
196 |
)
|
197 |
|
198 |
-
demo.load(
|
199 |
-
fn=lambda: None,
|
200 |
-
inputs=None,
|
201 |
-
outputs=None
|
202 |
-
)
|
203 |
|
204 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
4 |
import torch
|
5 |
+
from diffusers import DiffusionPipeline
|
|
|
|
|
6 |
import os
|
7 |
|
8 |
+
# Constants
|
9 |
+
MAX_SEED = np.iinfo(np.int32).max
|
10 |
+
MAX_IMAGE_SIZE = 2048
|
11 |
+
DEFAULT_IMAGE_SIZE = 1024
|
12 |
+
|
13 |
+
# Model setup
|
14 |
dtype = torch.bfloat16
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
16 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
17 |
|
18 |
+
pipe = DiffusionPipeline.from_pretrained(
|
19 |
+
"black-forest-labs/FLUX.1-dev",
|
20 |
+
torch_dtype=dtype,
|
21 |
+
token=huggingface_token
|
22 |
+
).to(device)
|
23 |
|
24 |
+
def generate_image(prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
25 |
if randomize_seed:
|
26 |
seed = random.randint(0, MAX_SEED)
|
27 |
generator = torch.Generator().manual_seed(seed)
|
|
|
35 |
).images[0]
|
36 |
return image, seed
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
css = """
|
39 |
body {
|
40 |
background-color: #f4faff;
|
41 |
color: #005662;
|
42 |
font-family: 'Poppins', sans-serif;
|
43 |
}
|
44 |
+
.container {
|
45 |
margin: 0 auto;
|
46 |
+
max-width: 900px;
|
47 |
padding: 20px;
|
48 |
}
|
49 |
.gr-button {
|
|
|
55 |
.gr-button:hover {
|
56 |
background-color: #0277bd;
|
57 |
}
|
58 |
+
.gr-box {
|
|
|
59 |
border-radius: 12px;
|
60 |
+
border: 1px solid #eeeeee;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
}
|
62 |
"""
|
63 |
|
64 |
with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
|
65 |
+
gr.Markdown("""
|
66 |
+
# FLUX.1 [dev] | A Text-To-Image Rectified Flow 12B Transformer
|
67 |
|
68 |
+
Enter a text prompt below to generate an image. Click 'Generate' to create your image.
|
69 |
+
""")
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column(scale=4):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
prompt = gr.Text(
|
74 |
label="Prompt",
|
75 |
+
placeholder="Enter your prompt here",
|
76 |
+
lines=2
|
|
|
|
|
77 |
)
|
78 |
+
with gr.Column(scale=1):
|
79 |
+
generate_button = gr.Button("Generate", variant="primary")
|
80 |
+
|
81 |
+
result = gr.Image(label="Generated Image", type="pil")
|
82 |
+
|
83 |
+
with gr.Accordion("Advanced Settings", open=False):
|
84 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
85 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
86 |
|
87 |
+
with gr.Row():
|
88 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_IMAGE_SIZE)
|
89 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_IMAGE_SIZE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
with gr.Row():
|
92 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
|
93 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
|
94 |
+
|
95 |
+
gr.Examples(
|
96 |
+
examples=[
|
97 |
+
"a galaxy swirling with vibrant blue and purple hues",
|
98 |
+
"a futuristic cityscape under a dark sky",
|
99 |
+
"a serene forest with a magical glowing tree",
|
100 |
+
"a portrait of a smiling woman with a colorful floral crown",
|
101 |
+
"a fantastical creature with the body of a dragon and the wings of a butterfly",
|
102 |
+
],
|
103 |
+
inputs=prompt,
|
104 |
+
outputs=[result, seed],
|
105 |
+
fn=generate_image,
|
106 |
+
cache_examples=True,
|
107 |
+
)
|
108 |
+
|
109 |
+
generate_button.click(
|
110 |
+
fn=generate_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
112 |
outputs=[result, seed]
|
113 |
)
|
114 |
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
demo.launch(share=True)
|