Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
#112
by
meroitachi
- opened
app.py
CHANGED
@@ -1,30 +1,32 @@
|
|
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 |
|
8 |
dtype = torch.bfloat16
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
|
|
11 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
12 |
|
13 |
MAX_SEED = np.iinfo(np.int32).max
|
14 |
-
MAX_IMAGE_SIZE = 2048
|
15 |
|
16 |
@spaces.GPU()
|
17 |
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
|
|
18 |
if randomize_seed:
|
19 |
seed = random.randint(0, MAX_SEED)
|
20 |
generator = torch.Generator().manual_seed(seed)
|
|
|
|
|
21 |
image = pipe(
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
).images[0]
|
29 |
return image, seed
|
30 |
|
@@ -50,7 +52,6 @@ with gr.Blocks(css=css) as demo:
|
|
50 |
""")
|
51 |
|
52 |
with gr.Row():
|
53 |
-
|
54 |
prompt = gr.Text(
|
55 |
label="Prompt",
|
56 |
show_label=False,
|
@@ -64,7 +65,6 @@ with gr.Blocks(css=css) as demo:
|
|
64 |
result = gr.Image(label="Result", show_label=False)
|
65 |
|
66 |
with gr.Accordion("Advanced Settings", open=False):
|
67 |
-
|
68 |
seed = gr.Slider(
|
69 |
label="Seed",
|
70 |
minimum=0,
|
@@ -76,11 +76,10 @@ with gr.Blocks(css=css) as demo:
|
|
76 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
77 |
|
78 |
with gr.Row():
|
79 |
-
|
80 |
width = gr.Slider(
|
81 |
label="Width",
|
82 |
minimum=256,
|
83 |
-
maximum=
|
84 |
step=32,
|
85 |
value=1024,
|
86 |
)
|
@@ -88,14 +87,12 @@ with gr.Blocks(css=css) as demo:
|
|
88 |
height = gr.Slider(
|
89 |
label="Height",
|
90 |
minimum=256,
|
91 |
-
maximum=
|
92 |
step=32,
|
93 |
value=1024,
|
94 |
)
|
95 |
|
96 |
with gr.Row():
|
97 |
-
|
98 |
-
|
99 |
num_inference_steps = gr.Slider(
|
100 |
label="Number of inference steps",
|
101 |
minimum=1,
|
@@ -105,18 +102,18 @@ with gr.Blocks(css=css) as demo:
|
|
105 |
)
|
106 |
|
107 |
gr.Examples(
|
108 |
-
examples
|
109 |
-
fn
|
110 |
-
inputs
|
111 |
-
outputs
|
112 |
cache_examples="lazy"
|
113 |
)
|
114 |
|
115 |
gr.on(
|
116 |
triggers=[run_button.click, prompt.submit],
|
117 |
-
fn
|
118 |
-
inputs
|
119 |
-
outputs
|
120 |
)
|
121 |
|
122 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
4 |
import torch
|
5 |
from diffusers import DiffusionPipeline
|
6 |
|
7 |
dtype = torch.bfloat16
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
+
# Load the pipeline with no hard size restrictions
|
11 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
12 |
|
13 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
14 |
|
15 |
@spaces.GPU()
|
16 |
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
17 |
+
# Remove artificial size restrictions; allow the API to handle any size within hardware capacity
|
18 |
if randomize_seed:
|
19 |
seed = random.randint(0, MAX_SEED)
|
20 |
generator = torch.Generator().manual_seed(seed)
|
21 |
+
|
22 |
+
# Process the image
|
23 |
image = pipe(
|
24 |
+
prompt=prompt,
|
25 |
+
width=width,
|
26 |
+
height=height,
|
27 |
+
num_inference_steps=num_inference_steps,
|
28 |
+
generator=generator,
|
29 |
+
guidance_scale=0.0
|
30 |
).images[0]
|
31 |
return image, seed
|
32 |
|
|
|
52 |
""")
|
53 |
|
54 |
with gr.Row():
|
|
|
55 |
prompt = gr.Text(
|
56 |
label="Prompt",
|
57 |
show_label=False,
|
|
|
65 |
result = gr.Image(label="Result", show_label=False)
|
66 |
|
67 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
68 |
seed = gr.Slider(
|
69 |
label="Seed",
|
70 |
minimum=0,
|
|
|
76 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
77 |
|
78 |
with gr.Row():
|
|
|
79 |
width = gr.Slider(
|
80 |
label="Width",
|
81 |
minimum=256,
|
82 |
+
maximum=2048, # Set sliders to a large size but allow flexibility in API
|
83 |
step=32,
|
84 |
value=1024,
|
85 |
)
|
|
|
87 |
height = gr.Slider(
|
88 |
label="Height",
|
89 |
minimum=256,
|
90 |
+
maximum=2048,
|
91 |
step=32,
|
92 |
value=1024,
|
93 |
)
|
94 |
|
95 |
with gr.Row():
|
|
|
|
|
96 |
num_inference_steps = gr.Slider(
|
97 |
label="Number of inference steps",
|
98 |
minimum=1,
|
|
|
102 |
)
|
103 |
|
104 |
gr.Examples(
|
105 |
+
examples=examples,
|
106 |
+
fn=infer,
|
107 |
+
inputs=[prompt],
|
108 |
+
outputs=[result, seed],
|
109 |
cache_examples="lazy"
|
110 |
)
|
111 |
|
112 |
gr.on(
|
113 |
triggers=[run_button.click, prompt.submit],
|
114 |
+
fn=infer,
|
115 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
116 |
+
outputs=[result, seed]
|
117 |
)
|
118 |
|
119 |
demo.launch()
|