Spaces:
Running
Running
Upload 2 files
Browse files- app.py +174 -170
- requirements.txt +8 -7
app.py
CHANGED
@@ -1,171 +1,175 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
import torch
|
4 |
-
import numpy as np
|
5 |
-
import random
|
6 |
-
from diffusers import FluxPipeline
|
7 |
-
import spaces
|
8 |
-
from
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
}
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
"photo of
|
89 |
-
"
|
90 |
-
"photo of a woman
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
with gr.
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
inputs=[prompt, width, height, scales, steps, seed, nums],
|
167 |
-
outputs=[img, seed],
|
168 |
-
)
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
|
|
171 |
demo.queue().launch()
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
import random
|
6 |
+
from diffusers import FluxPipeline
|
7 |
+
import spaces
|
8 |
+
from optimum.quanto import freeze, qfloat8, quantize
|
9 |
+
from translatepy import Translator
|
10 |
+
|
11 |
+
|
12 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
13 |
+
translator = Translator()
|
14 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
15 |
+
# Constants
|
16 |
+
model = "black-forest-labs/FLUX.1-dev"
|
17 |
+
MAX_SEED = np.iinfo(np.int32).max
|
18 |
+
|
19 |
+
CSS = """
|
20 |
+
footer {
|
21 |
+
visibility: hidden;
|
22 |
+
}
|
23 |
+
"""
|
24 |
+
|
25 |
+
JS = """function () {
|
26 |
+
gradioURL = window.location.href
|
27 |
+
if (!gradioURL.endsWith('?__theme=dark')) {
|
28 |
+
window.location.replace(gradioURL + '?__theme=dark');
|
29 |
+
}
|
30 |
+
}"""
|
31 |
+
|
32 |
+
pipe = FluxPipeline.from_pretrained(
|
33 |
+
model,
|
34 |
+
torch_dtype=torch.bfloat16).to("cuda")
|
35 |
+
|
36 |
+
quantize(pipe.transformer, weights=qfloat8)
|
37 |
+
freeze(pipe.transformer)
|
38 |
+
|
39 |
+
quantize(pipe.text_encoder_2, weights=qfloat8)
|
40 |
+
freeze(pipe.text_encoder_2)
|
41 |
+
|
42 |
+
pipe.enable_model_cpu_offload()
|
43 |
+
pipe.to("cuda")
|
44 |
+
|
45 |
+
# Function
|
46 |
+
@spaces.GPU()
|
47 |
+
def generate_image(
|
48 |
+
prompt,
|
49 |
+
width=1024,
|
50 |
+
height=1024,
|
51 |
+
scales=5,
|
52 |
+
steps=30,
|
53 |
+
seed: int =-1,
|
54 |
+
nums=1,
|
55 |
+
progress=gr.Progress(track_tqdm=True)):
|
56 |
+
|
57 |
+
if seed == -1:
|
58 |
+
seed = random.randint(0, MAX_SEED)
|
59 |
+
seed = int(seed)
|
60 |
+
print(f'prompt:{prompt}')
|
61 |
+
|
62 |
+
text = str(translator.translate(prompt, 'English'))
|
63 |
+
|
64 |
+
generator = torch.Generator().manual_seed(seed)
|
65 |
+
|
66 |
+
|
67 |
+
image = pipe(
|
68 |
+
prompt=text,
|
69 |
+
height=height,
|
70 |
+
width=width,
|
71 |
+
guidance_scale=scales,
|
72 |
+
output_type="pil",
|
73 |
+
num_inference_steps=steps,
|
74 |
+
max_sequence_length=512,
|
75 |
+
num_images_per_prompt=nums,
|
76 |
+
generator=generator,
|
77 |
+
).images
|
78 |
+
|
79 |
+
print(image)
|
80 |
+
print(seed)
|
81 |
+
return image, seed
|
82 |
+
|
83 |
+
|
84 |
+
examples = [
|
85 |
+
"a female character with long, flowing hair that appears to be made of ethereal, swirling patterns resembling the Northern Lights or Aurora Borealis. The background is dominated by deep blues and purples, creating a mysterious and dramatic atmosphere. The character's face is serene, with pale skin and striking features. She wears a dark-colored outfit with subtle patterns. The overall style of the artwork is reminiscent of fantasy or supernatural genres",
|
86 |
+
"Digital art, portrait of an anthropomorphic roaring Tiger warrior with full armor, close up in the middle of a battle, behind him there is a banner with the text \"Open Source\".",
|
87 |
+
"photo of a dog and a cat both standing on a red box, with a blue ball in the middle with a parrot standing on top of the ball. The box has the text \"FLUX\"",
|
88 |
+
"selfie photo of a wizard with long beard and purple robes, he is apparently in the middle of Tokyo. Probably taken from a phone.",
|
89 |
+
"A vibrant street wall covered in colorful graffiti, the centerpiece spells \"FLUX\", in a storm of colors",
|
90 |
+
"photo of a young woman with long, wavy brown hair tied in a bun and glasses. She has a fair complexion and is wearing subtle makeup, emphasizing her eyes and lips. She is dressed in a black top. The background appears to be an urban setting with a building facade, and the sunlight casts a warm glow on her face.",
|
91 |
+
"anime art of a steampunk inventor in their workshop, surrounded by gears, gadgets, and steam. He is holding a blue potion and a red potion, one in each hand",
|
92 |
+
"photo of picturesque scene of a road surrounded by lush green trees and shrubs. The road is wide and smooth, leading into the distance. On the right side of the road, there's a blue sports car parked with the license plate spelling \"FLUX\". The sky above is partly cloudy, suggesting a pleasant day. The trees have a mix of green and brown foliage. There are no people visible in the image. The overall composition is balanced, with the car serving as a focal point.",
|
93 |
+
"photo of young man in a black suit, white shirt, and black tie. He has a neatly styled haircut and is looking directly at the camera with a neutral expression. The background consists of a textured wall with horizontal lines. The photograph is in black and white, emphasizing contrasts and shadows. The man appears to be in his late twenties or early thirties, with fair skin and short, dark hair.",
|
94 |
+
"photo of a woman on the beach, shot from above. She is facing the sea, while wearing a white dress. She has long blonde hair"
|
95 |
+
]
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
# Gradio Interface
|
100 |
+
|
101 |
+
with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
102 |
+
gr.HTML("<h1><center>Flux Dev Quanto</center></h1>")
|
103 |
+
gr.HTML("<p><center><a href='https://huggingface.co/black-forest-labs/FLUX.1-dev'>FLUX.1 DEV</a> with <a href='https://huggingface.co/blog/quanto-diffusers'>Quanto</a></center></p>")
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column(scale=4):
|
106 |
+
img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
|
107 |
+
with gr.Row():
|
108 |
+
prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
|
109 |
+
sendBtn = gr.Button(scale=1, variant='primary')
|
110 |
+
with gr.Accordion("Advanced Options", open=True):
|
111 |
+
with gr.Column(scale=1):
|
112 |
+
width = gr.Slider(
|
113 |
+
label="Width",
|
114 |
+
minimum=512,
|
115 |
+
maximum=1280,
|
116 |
+
step=8,
|
117 |
+
value=1024,
|
118 |
+
)
|
119 |
+
height = gr.Slider(
|
120 |
+
label="Height",
|
121 |
+
minimum=512,
|
122 |
+
maximum=1280,
|
123 |
+
step=8,
|
124 |
+
value=1024,
|
125 |
+
)
|
126 |
+
scales = gr.Slider(
|
127 |
+
label="Guidance",
|
128 |
+
minimum=3.5,
|
129 |
+
maximum=7,
|
130 |
+
step=0.1,
|
131 |
+
value=3.5,
|
132 |
+
)
|
133 |
+
steps = gr.Slider(
|
134 |
+
label="Steps",
|
135 |
+
minimum=1,
|
136 |
+
maximum=100,
|
137 |
+
step=1,
|
138 |
+
value=30,
|
139 |
+
)
|
140 |
+
seed = gr.Slider(
|
141 |
+
label="Seeds",
|
142 |
+
minimum=-1,
|
143 |
+
maximum=MAX_SEED,
|
144 |
+
step=1,
|
145 |
+
value=-1,
|
146 |
+
scale=2,
|
147 |
+
)
|
148 |
+
nums = gr.Slider(
|
149 |
+
label="Image Numbers",
|
150 |
+
minimum=1,
|
151 |
+
maximum=4,
|
152 |
+
step=1,
|
153 |
+
value=1,
|
154 |
+
scale=1,
|
155 |
+
)
|
156 |
+
gr.Examples(
|
157 |
+
examples=examples,
|
158 |
+
inputs=prompt,
|
159 |
+
outputs=[img, seed],
|
160 |
+
fn=generate_image,
|
161 |
+
cache_examples="lazy",
|
162 |
+
examples_per_page=4,
|
163 |
+
)
|
164 |
+
|
165 |
+
sendBtn.click(fn=generate_image,
|
166 |
+
inputs=[prompt, width, height, scales, steps, seed, nums],
|
167 |
+
outputs=[img, seed],
|
168 |
+
)
|
169 |
+
prompt.submit(fn=generate_image,
|
170 |
+
inputs=[prompt, width, height, scales, steps, seed, nums],
|
171 |
+
outputs=[img, seed],
|
172 |
+
)
|
173 |
+
|
174 |
+
|
175 |
demo.queue().launch()
|
requirements.txt
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
git+https://github.com/huggingface/diffusers
|
2 |
-
git+https://github.com/huggingface/transformers
|
3 |
-
numpy
|
4 |
-
torch
|
5 |
-
translatepy
|
6 |
-
accelerate
|
7 |
-
sentencepiece
|
|
|
|
1 |
+
git+https://github.com/huggingface/diffusers
|
2 |
+
git+https://github.com/huggingface/transformers
|
3 |
+
numpy
|
4 |
+
torch
|
5 |
+
translatepy
|
6 |
+
accelerate
|
7 |
+
sentencepiece
|
8 |
+
optimum-quanto
|