Spaces:
Running
Running
jerukperas
commited on
Commit
•
3a6953c
1
Parent(s):
eb3b48e
Add application files
Browse files- .gitignore +1 -0
- README.md +2 -2
- app.py +197 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.venv
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: purple
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
+
title: Fluxing
|
3 |
+
emoji: 🦢
|
4 |
colorFrom: purple
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces # type: ignore
|
2 |
+
import os
|
3 |
+
import time # noqa: F401
|
4 |
+
import uuid
|
5 |
+
from PIL import Image
|
6 |
+
import gradio as gr # type: ignore
|
7 |
+
import numpy as np
|
8 |
+
import random
|
9 |
+
import torch
|
10 |
+
from diffusers import FluxPipeline
|
11 |
+
|
12 |
+
dtype = torch.bfloat16
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
pipe = FluxPipeline.from_pretrained(
|
15 |
+
"black-forest-labs/FLUX.1-dev",
|
16 |
+
torch_dtype=dtype,
|
17 |
+
)
|
18 |
+
pipe.to(device)
|
19 |
+
|
20 |
+
MAX_SEED = np.iinfo(np.int32).max
|
21 |
+
MAX_IMAGE_SIZE = 2048
|
22 |
+
|
23 |
+
|
24 |
+
@spaces.GPU(duration=90)
|
25 |
+
def infer(
|
26 |
+
prompt: str,
|
27 |
+
seed=42,
|
28 |
+
randomize_seed=False,
|
29 |
+
width=1024,
|
30 |
+
height=1024,
|
31 |
+
guidance_scale=5.0,
|
32 |
+
num_inference_steps=28,
|
33 |
+
progress=gr.Progress(track_tqdm=True),
|
34 |
+
):
|
35 |
+
if randomize_seed:
|
36 |
+
seed = random.randint(0, MAX_SEED)
|
37 |
+
|
38 |
+
generator = torch.Generator().manual_seed(seed)
|
39 |
+
image = pipe(
|
40 |
+
prompt=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 |
+
|
48 |
+
assert isinstance(
|
49 |
+
image, Image.Image
|
50 |
+
), "The output is not an instance of Image.Image"
|
51 |
+
|
52 |
+
filepath = os.path.join("images", "{uuid}.png".format(uuid=str(uuid.uuid4().hex)))
|
53 |
+
image.save(filepath)
|
54 |
+
|
55 |
+
return (
|
56 |
+
image,
|
57 |
+
gr.DownloadButton(
|
58 |
+
label="Download PNG", value=filepath, size="sm", visible=True
|
59 |
+
),
|
60 |
+
seed,
|
61 |
+
)
|
62 |
+
|
63 |
+
|
64 |
+
examples = [
|
65 |
+
"a tiny astronaut hatching from an egg on the moon",
|
66 |
+
"a cat holding a sign that says hello world",
|
67 |
+
"an anime illustration of a wiener schnitzel",
|
68 |
+
]
|
69 |
+
|
70 |
+
css = """
|
71 |
+
#col-container {
|
72 |
+
margin: 0 auto;
|
73 |
+
max-width: 520px;
|
74 |
+
}
|
75 |
+
"""
|
76 |
+
|
77 |
+
with gr.Blocks(css=css) as demo:
|
78 |
+
with gr.Column(elem_id="col-container"):
|
79 |
+
gr.Markdown("""# FLUX.1
|
80 |
+
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
81 |
+
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
82 |
+
""")
|
83 |
+
|
84 |
+
with gr.Row(equal_height=False):
|
85 |
+
prompt = gr.TextArea(
|
86 |
+
label="Prompt",
|
87 |
+
show_label=False,
|
88 |
+
lines=3,
|
89 |
+
placeholder="Enter your prompt",
|
90 |
+
container=False,
|
91 |
+
)
|
92 |
+
|
93 |
+
run_button = gr.Button("Run", variant="primary", scale=0)
|
94 |
+
|
95 |
+
result = gr.Image(
|
96 |
+
format="webp",
|
97 |
+
type="pil",
|
98 |
+
label="Result",
|
99 |
+
show_label=False,
|
100 |
+
show_download_button=False,
|
101 |
+
show_share_button=False,
|
102 |
+
)
|
103 |
+
download = gr.DownloadButton(size="sm", visible=False)
|
104 |
+
|
105 |
+
with gr.Accordion("Advanced Settings", open=False):
|
106 |
+
seed = gr.Slider(
|
107 |
+
label="Seed",
|
108 |
+
minimum=0,
|
109 |
+
maximum=MAX_SEED,
|
110 |
+
step=1,
|
111 |
+
value=0,
|
112 |
+
)
|
113 |
+
|
114 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
width = gr.Slider(
|
118 |
+
label="Width",
|
119 |
+
minimum=256,
|
120 |
+
maximum=MAX_IMAGE_SIZE,
|
121 |
+
step=32,
|
122 |
+
value=832,
|
123 |
+
)
|
124 |
+
|
125 |
+
height = gr.Slider(
|
126 |
+
label="Height",
|
127 |
+
minimum=256,
|
128 |
+
maximum=MAX_IMAGE_SIZE,
|
129 |
+
step=32,
|
130 |
+
value=1216,
|
131 |
+
)
|
132 |
+
|
133 |
+
with gr.Row():
|
134 |
+
guidance_scale = gr.Slider(
|
135 |
+
label="Guidance Scale",
|
136 |
+
minimum=0,
|
137 |
+
maximum=15,
|
138 |
+
step=0.1,
|
139 |
+
value=3.5,
|
140 |
+
)
|
141 |
+
|
142 |
+
num_inference_steps = gr.Slider(
|
143 |
+
label="Number of inference steps",
|
144 |
+
minimum=1,
|
145 |
+
maximum=50,
|
146 |
+
step=1,
|
147 |
+
value=28,
|
148 |
+
)
|
149 |
+
|
150 |
+
# gr.Examples(
|
151 |
+
# examples=examples,
|
152 |
+
# fn=infer,
|
153 |
+
# inputs=[prompt],
|
154 |
+
# outputs=[result, seed],
|
155 |
+
# cache_examples="lazy",
|
156 |
+
# visible=False,
|
157 |
+
# )
|
158 |
+
|
159 |
+
run_button.click(fn=lambda: gr.update(visible=False),inputs=None, outputs=download)
|
160 |
+
|
161 |
+
gr.on(
|
162 |
+
triggers=[run_button.click, prompt.submit],
|
163 |
+
fn=infer,
|
164 |
+
inputs=[
|
165 |
+
prompt,
|
166 |
+
seed,
|
167 |
+
randomize_seed,
|
168 |
+
width,
|
169 |
+
height,
|
170 |
+
guidance_scale,
|
171 |
+
num_inference_steps,
|
172 |
+
],
|
173 |
+
outputs=[result, download, seed],
|
174 |
+
)
|
175 |
+
|
176 |
+
# def delete_packed_tensors(
|
177 |
+
# startup_time: int = time.time_ns(),
|
178 |
+
# zerogpu_offload_dir: str = "/home/user/.zerogpu/tensors",
|
179 |
+
# ) -> None:
|
180 |
+
# entries = os.scandir(zerogpu_offload_dir)
|
181 |
+
# for entry in entries:
|
182 |
+
# print(f"Found entry {entry.path}")
|
183 |
+
# if entry.stat().st_atime_ns < startup_time:
|
184 |
+
# try:
|
185 |
+
# file_path = entry.path
|
186 |
+
# os.remove(file_path)
|
187 |
+
# print(f"File deleted successfully: {file_path}")
|
188 |
+
# except FileNotFoundError:
|
189 |
+
# print(f"File not found: {file_path}")
|
190 |
+
# except OSError as e:
|
191 |
+
# print(f"Error deleting file: {e}")
|
192 |
+
|
193 |
+
|
194 |
+
if __name__ == "__main__":
|
195 |
+
# delete_packed_tensors()
|
196 |
+
os.makedirs("images", exist_ok=True)
|
197 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
git+https://github.com/huggingface/diffusers.git
|
3 |
+
torch
|
4 |
+
transformers==4.42.4
|
5 |
+
xformers
|
6 |
+
sentencepiece
|
7 |
+
bitsandbytes
|