vilarin commited on
Commit
12ed65b
1 Parent(s): a24eb3b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +174 -170
  2. 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 translatepy import Translator
9
-
10
-
11
- os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
12
- translator = Translator()
13
- HF_TOKEN = os.environ.get("HF_TOKEN", None)
14
- # Constants
15
- model = "black-forest-labs/FLUX.1-dev"
16
- MAX_SEED = np.iinfo(np.int32).max
17
-
18
- CSS = """
19
- footer {
20
- visibility: hidden;
21
- }
22
- """
23
-
24
- JS = """function () {
25
- gradioURL = window.location.href
26
- if (!gradioURL.endsWith('?__theme=dark')) {
27
- window.location.replace(gradioURL + '?__theme=dark');
28
- }
29
- }"""
30
-
31
- if torch.cuda.is_available():
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
- # quantize(pipe.text_encoder_2, weights=qfloat8)
39
- # freeze(pipe.text_encoder_2)
40
-
41
- # Function
42
- @spaces.GPU()
43
- def generate_image(
44
- prompt,
45
- width=1024,
46
- height=1024,
47
- scales=5,
48
- steps=30,
49
- seed: int =-1,
50
- nums=1,
51
- progress=gr.Progress(track_tqdm=True)):
52
-
53
- if seed == -1:
54
- seed = random.randint(0, MAX_SEED)
55
- seed = int(seed)
56
- print(f'prompt:{prompt}')
57
-
58
- text = str(translator.translate(prompt, 'English'))
59
-
60
- generator = torch.Generator().manual_seed(seed)
61
-
62
-
63
- image = pipe(
64
- prompt=text,
65
- height=height,
66
- width=width,
67
- guidance_scale=scales,
68
- output_type="pil",
69
- num_inference_steps=steps,
70
- max_sequence_length=512,
71
- num_images_per_prompt=nums,
72
- generator=generator,
73
- ).images
74
-
75
- print(image)
76
- print(seed)
77
- return image, seed
78
-
79
-
80
- examples = [
81
- "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",
82
- "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\".",
83
- "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\"",
84
- "selfie photo of a wizard with long beard and purple robes, he is apparently in the middle of Tokyo. Probably taken from a phone.",
85
- "A vibrant street wall covered in colorful graffiti, the centerpiece spells \"FLUX\", in a storm of colors",
86
- "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.",
87
- "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",
88
- "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.",
89
- "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.",
90
- "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"
91
- ]
92
-
93
-
94
-
95
- # Gradio Interface
96
-
97
- with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
98
- gr.HTML("<h1><center>Flux Dev Quanto</center></h1>")
99
- 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>")
100
- with gr.Row():
101
- with gr.Column(scale=4):
102
- img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
103
- with gr.Row():
104
- prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
105
- sendBtn = gr.Button(scale=1, variant='primary')
106
- with gr.Accordion("Advanced Options", open=True):
107
- with gr.Column(scale=1):
108
- width = gr.Slider(
109
- label="Width",
110
- minimum=512,
111
- maximum=1280,
112
- step=8,
113
- value=1024,
114
- )
115
- height = gr.Slider(
116
- label="Height",
117
- minimum=512,
118
- maximum=1280,
119
- step=8,
120
- value=1024,
121
- )
122
- scales = gr.Slider(
123
- label="Guidance",
124
- minimum=3.5,
125
- maximum=7,
126
- step=0.1,
127
- value=3.5,
128
- )
129
- steps = gr.Slider(
130
- label="Steps",
131
- minimum=1,
132
- maximum=100,
133
- step=1,
134
- value=30,
135
- )
136
- seed = gr.Slider(
137
- label="Seeds",
138
- minimum=-1,
139
- maximum=MAX_SEED,
140
- step=1,
141
- value=-1,
142
- scale=2,
143
- )
144
- nums = gr.Slider(
145
- label="Image Numbers",
146
- minimum=1,
147
- maximum=4,
148
- step=1,
149
- value=1,
150
- scale=1,
151
- )
152
- gr.Examples(
153
- examples=examples,
154
- inputs=prompt,
155
- outputs=[img, seed],
156
- fn=generate_image,
157
- cache_examples="lazy",
158
- examples_per_page=4,
159
- )
160
-
161
- sendBtn.click(fn=generate_image,
162
- inputs=[prompt, width, height, scales, steps, seed, nums],
163
- outputs=[img, seed],
164
- )
165
- prompt.submit(fn=generate_image,
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