Spaces:
Running
on
Zero
Running
on
Zero
Update
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
import random
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import PIL.Image
|
|
@@ -17,17 +15,34 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
| 17 |
MAX_IMAGE_SIZE = 2048
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@spaces.GPU(duration=75)
|
| 21 |
def infer(
|
| 22 |
prompt: str,
|
| 23 |
seed: int,
|
| 24 |
-
randomize_seed: bool,
|
| 25 |
width: int = 1024,
|
| 26 |
height: int = 1024,
|
| 27 |
guidance_scale: float = 3.5,
|
| 28 |
num_inference_steps: int = 28,
|
| 29 |
progress: gr.Progress = gr.Progress(track_tqdm=True), # noqa: ARG001, B008
|
| 30 |
-
) ->
|
| 31 |
"""Generate an image from a text prompt using the FLUX.1 [dev] model.
|
| 32 |
|
| 33 |
Note:
|
|
@@ -37,7 +52,6 @@ def infer(
|
|
| 37 |
Args:
|
| 38 |
prompt: A text prompt in English to guide the image generation. Limited to 77 tokens.
|
| 39 |
seed: The seed value used for reproducible image generation.
|
| 40 |
-
randomize_seed: If True, overrides the seed with a randomly generated one.
|
| 41 |
width: Width of the output image in pixels. Defaults to 1024.
|
| 42 |
height: Height of the output image in pixels. Defaults to 1024.
|
| 43 |
guidance_scale: Controls how strongly the model follows the prompt.
|
|
@@ -46,13 +60,11 @@ def infer(
|
|
| 46 |
progress: (Internal) Progress tracker for UI integration; should not be manually set by users.
|
| 47 |
|
| 48 |
Returns:
|
| 49 |
-
A
|
| 50 |
"""
|
| 51 |
-
if randomize_seed:
|
| 52 |
-
seed = random.randint(0, MAX_SEED) # noqa: S311
|
| 53 |
generator = torch.Generator().manual_seed(seed)
|
| 54 |
|
| 55 |
-
|
| 56 |
prompt=prompt,
|
| 57 |
width=width,
|
| 58 |
height=height,
|
|
@@ -60,11 +72,10 @@ def infer(
|
|
| 60 |
generator=generator,
|
| 61 |
guidance_scale=guidance_scale,
|
| 62 |
).images[0]
|
| 63 |
-
return image, seed
|
| 64 |
|
| 65 |
|
| 66 |
def run_example(prompt: str) -> tuple[PIL.Image.Image, int]:
|
| 67 |
-
return infer(prompt, seed=42
|
| 68 |
|
| 69 |
|
| 70 |
examples = [
|
|
@@ -145,13 +156,17 @@ with gr.Blocks(css=css) as demo:
|
|
| 145 |
examples=examples,
|
| 146 |
fn=run_example,
|
| 147 |
inputs=prompt,
|
| 148 |
-
outputs=
|
| 149 |
)
|
| 150 |
|
| 151 |
prompt.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
fn=infer,
|
| 153 |
-
inputs=[prompt, seed,
|
| 154 |
-
outputs=
|
| 155 |
)
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import PIL.Image
|
|
|
|
| 15 |
MAX_IMAGE_SIZE = 2048
|
| 16 |
|
| 17 |
|
| 18 |
+
def get_seed(randomize_seed: bool, seed: int) -> int:
|
| 19 |
+
"""Determine and return the random seed to use for model generation.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
randomize_seed (bool): If True, a random seed (an integer in [0, MAX_SEED)) is generated using NumPy's default random number generator. If False, the provided seed argument is returned as-is.
|
| 23 |
+
seed (int): The seed value to use if randomize_seed is False.
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
int: The selected seed value. If randomize_seed is True, a randomly generated integer; otherwise, the value of the seed argument.
|
| 27 |
+
|
| 28 |
+
Notes:
|
| 29 |
+
- MAX_SEED is the maximum value for a 32-bit integer (np.iinfo(np.int32).max).
|
| 30 |
+
- This function is typically used to ensure reproducibility or to introduce randomness in model generation.
|
| 31 |
+
"""
|
| 32 |
+
rng = np.random.default_rng()
|
| 33 |
+
return int(rng.integers(0, MAX_SEED)) if randomize_seed else seed
|
| 34 |
+
|
| 35 |
+
|
| 36 |
@spaces.GPU(duration=75)
|
| 37 |
def infer(
|
| 38 |
prompt: str,
|
| 39 |
seed: int,
|
|
|
|
| 40 |
width: int = 1024,
|
| 41 |
height: int = 1024,
|
| 42 |
guidance_scale: float = 3.5,
|
| 43 |
num_inference_steps: int = 28,
|
| 44 |
progress: gr.Progress = gr.Progress(track_tqdm=True), # noqa: ARG001, B008
|
| 45 |
+
) -> PIL.Image.Image:
|
| 46 |
"""Generate an image from a text prompt using the FLUX.1 [dev] model.
|
| 47 |
|
| 48 |
Note:
|
|
|
|
| 52 |
Args:
|
| 53 |
prompt: A text prompt in English to guide the image generation. Limited to 77 tokens.
|
| 54 |
seed: The seed value used for reproducible image generation.
|
|
|
|
| 55 |
width: Width of the output image in pixels. Defaults to 1024.
|
| 56 |
height: Height of the output image in pixels. Defaults to 1024.
|
| 57 |
guidance_scale: Controls how strongly the model follows the prompt.
|
|
|
|
| 60 |
progress: (Internal) Progress tracker for UI integration; should not be manually set by users.
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
+
A PIL.Image.Image object representing the generated image.
|
| 64 |
"""
|
|
|
|
|
|
|
| 65 |
generator = torch.Generator().manual_seed(seed)
|
| 66 |
|
| 67 |
+
return pipe(
|
| 68 |
prompt=prompt,
|
| 69 |
width=width,
|
| 70 |
height=height,
|
|
|
|
| 72 |
generator=generator,
|
| 73 |
guidance_scale=guidance_scale,
|
| 74 |
).images[0]
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
def run_example(prompt: str) -> tuple[PIL.Image.Image, int]:
|
| 78 |
+
return infer(prompt, seed=42)
|
| 79 |
|
| 80 |
|
| 81 |
examples = [
|
|
|
|
| 156 |
examples=examples,
|
| 157 |
fn=run_example,
|
| 158 |
inputs=prompt,
|
| 159 |
+
outputs=result,
|
| 160 |
)
|
| 161 |
|
| 162 |
prompt.submit(
|
| 163 |
+
fn=get_seed,
|
| 164 |
+
inputs=[randomize_seed, seed],
|
| 165 |
+
outputs=seed,
|
| 166 |
+
).then(
|
| 167 |
fn=infer,
|
| 168 |
+
inputs=[prompt, seed, width, height, guidance_scale, num_inference_steps],
|
| 169 |
+
outputs=result,
|
| 170 |
)
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|