Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
import spaces
|
| 5 |
import torch
|
| 6 |
-
from diffusers import
|
| 7 |
-
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
| 8 |
-
from live_preview_helpers import
|
| 9 |
|
|
|
|
| 10 |
dtype = torch.bfloat16
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
torch.cuda.empty_cache()
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
MAX_IMAGE_SIZE = 2048
|
| 20 |
|
|
|
|
| 21 |
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
if randomize_seed:
|
| 26 |
seed = random.randint(0, MAX_SEED)
|
| 27 |
generator = torch.Generator().manual_seed(seed)
|
| 28 |
-
|
|
|
|
| 29 |
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 30 |
prompt=prompt,
|
| 31 |
guidance_scale=guidance_scale,
|
|
@@ -36,31 +47,33 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
|
|
| 36 |
output_type="pil",
|
| 37 |
good_vae=good_vae,
|
| 38 |
):
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
examples = [
|
| 42 |
"a tiny astronaut hatching from an egg on the moon",
|
| 43 |
"a cat holding a sign that says hello world",
|
| 44 |
"an anime illustration of a wiener schnitzel",
|
| 45 |
]
|
| 46 |
|
| 47 |
-
css="""
|
| 48 |
#col-container {
|
| 49 |
margin: 0 auto;
|
| 50 |
max-width: 520px;
|
| 51 |
}
|
| 52 |
"""
|
| 53 |
|
|
|
|
| 54 |
with gr.Blocks(css=css) as demo:
|
| 55 |
-
|
| 56 |
with gr.Column(elem_id="col-container"):
|
| 57 |
gr.Markdown(f"""# FLUX.1 [dev]
|
| 58 |
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
| 59 |
-
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]
|
|
|
|
|
|
|
| 60 |
""")
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
|
| 64 |
prompt = gr.Text(
|
| 65 |
label="Prompt",
|
| 66 |
show_label=False,
|
|
@@ -68,72 +81,31 @@ with gr.Blocks(css=css) as demo:
|
|
| 68 |
placeholder="Enter your prompt",
|
| 69 |
container=False,
|
| 70 |
)
|
| 71 |
-
|
| 72 |
run_button = gr.Button("Run", scale=0)
|
| 73 |
-
|
| 74 |
result = gr.Image(label="Result", show_label=False)
|
| 75 |
-
|
| 76 |
with gr.Accordion("Advanced Settings", open=False):
|
| 77 |
-
|
| 78 |
-
seed = gr.Slider(
|
| 79 |
-
label="Seed",
|
| 80 |
-
minimum=0,
|
| 81 |
-
maximum=MAX_SEED,
|
| 82 |
-
step=1,
|
| 83 |
-
value=0,
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
minimum=256,
|
| 93 |
-
maximum=MAX_IMAGE_SIZE,
|
| 94 |
-
step=32,
|
| 95 |
-
value=1024,
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
height = gr.Slider(
|
| 99 |
-
label="Height",
|
| 100 |
-
minimum=256,
|
| 101 |
-
maximum=MAX_IMAGE_SIZE,
|
| 102 |
-
step=32,
|
| 103 |
-
value=1024,
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
with gr.Row():
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
label="Guidance Scale",
|
| 110 |
-
minimum=1,
|
| 111 |
-
maximum=15,
|
| 112 |
-
step=0.1,
|
| 113 |
-
value=3.5,
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
num_inference_steps = gr.Slider(
|
| 117 |
-
label="Number of inference steps",
|
| 118 |
-
minimum=1,
|
| 119 |
-
maximum=50,
|
| 120 |
-
step=1,
|
| 121 |
-
value=28,
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
gr.Examples(
|
| 125 |
-
examples = examples,
|
| 126 |
-
fn = infer,
|
| 127 |
-
inputs = [prompt],
|
| 128 |
-
outputs = [result, seed],
|
| 129 |
-
cache_examples="lazy"
|
| 130 |
-
)
|
| 131 |
|
| 132 |
gr.on(
|
| 133 |
triggers=[run_button.click, prompt.submit],
|
| 134 |
-
fn
|
| 135 |
-
inputs
|
| 136 |
-
outputs
|
| 137 |
)
|
| 138 |
|
| 139 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
import torch
|
| 5 |
+
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
|
| 6 |
+
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
| 7 |
+
from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
|
| 8 |
|
| 9 |
+
# 檢查設備是否可用 GPU
|
| 10 |
dtype = torch.bfloat16
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
+
# 載入模型,若需要 API Token,請加上 use_auth_token=True
|
| 14 |
+
try:
|
| 15 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
| 16 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae",
|
| 17 |
+
torch_dtype=dtype).to(device)
|
| 18 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"模型載入錯誤: {e}")
|
| 21 |
+
|
| 22 |
torch.cuda.empty_cache()
|
| 23 |
|
| 24 |
MAX_SEED = np.iinfo(np.int32).max
|
| 25 |
MAX_IMAGE_SIZE = 2048
|
| 26 |
|
| 27 |
+
# 確保 flux_pipe_call_that_returns_an_iterable_of_images 綁定到模型
|
| 28 |
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
| 29 |
|
| 30 |
+
# 定義推論函數
|
| 31 |
+
@gr.Interface.function(duration=75)
|
| 32 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
|
| 33 |
+
guidance_scale=3.5, num_inference_steps=28,
|
| 34 |
+
progress=gr.Progress(track_tqdm=True)):
|
| 35 |
if randomize_seed:
|
| 36 |
seed = random.randint(0, MAX_SEED)
|
| 37 |
generator = torch.Generator().manual_seed(seed)
|
| 38 |
+
|
| 39 |
+
# 逐步生成圖像
|
| 40 |
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 41 |
prompt=prompt,
|
| 42 |
guidance_scale=guidance_scale,
|
|
|
|
| 47 |
output_type="pil",
|
| 48 |
good_vae=good_vae,
|
| 49 |
):
|
| 50 |
+
yield img, seed
|
| 51 |
+
|
| 52 |
+
# 預設的範例
|
| 53 |
examples = [
|
| 54 |
"a tiny astronaut hatching from an egg on the moon",
|
| 55 |
"a cat holding a sign that says hello world",
|
| 56 |
"an anime illustration of a wiener schnitzel",
|
| 57 |
]
|
| 58 |
|
| 59 |
+
css = """
|
| 60 |
#col-container {
|
| 61 |
margin: 0 auto;
|
| 62 |
max-width: 520px;
|
| 63 |
}
|
| 64 |
"""
|
| 65 |
|
| 66 |
+
# 建立 Gradio 介面
|
| 67 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
gr.Markdown(f"""# FLUX.1 [dev]
|
| 70 |
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
| 71 |
+
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]
|
| 72 |
+
[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)]
|
| 73 |
+
[[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
| 74 |
""")
|
| 75 |
|
| 76 |
with gr.Row():
|
|
|
|
| 77 |
prompt = gr.Text(
|
| 78 |
label="Prompt",
|
| 79 |
show_label=False,
|
|
|
|
| 81 |
placeholder="Enter your prompt",
|
| 82 |
container=False,
|
| 83 |
)
|
|
|
|
| 84 |
run_button = gr.Button("Run", scale=0)
|
| 85 |
+
|
| 86 |
result = gr.Image(label="Result", show_label=False)
|
| 87 |
+
|
| 88 |
with gr.Accordion("Advanced Settings", open=False):
|
| 89 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 94 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 95 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
|
| 98 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
|
| 99 |
|
| 100 |
+
gr.Examples(examples=examples, fn=infer, inputs=[prompt], outputs=[result, seed], cache_examples="lazy")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
gr.on(
|
| 103 |
triggers=[run_button.click, prompt.submit],
|
| 104 |
+
fn=infer,
|
| 105 |
+
inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 106 |
+
outputs=[result, seed]
|
| 107 |
)
|
| 108 |
|
| 109 |
+
# 啟動 Gradio App
|
| 110 |
+
demo.launch(share=True)
|
| 111 |
+
|