vilarin commited on
Commit
426dbe8
1 Parent(s): 52a722f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +171 -166
  2. requirements.txt +8 -7
app.py CHANGED
@@ -1,167 +1,172 @@
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
- import transformers
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
- if torch.cuda.is_available():
33
- pipe = FluxPipeline.from_pretrained(
34
- model,
35
- torch_dtype=torch.bfloat16).to("cuda")
36
-
37
- # Function
38
- @spaces.GPU(duration=120)
39
- def generate_image(
40
- prompt,
41
- width=1024,
42
- height=1024,
43
- scales=5,
44
- steps=30,
45
- seed: int =-1,
46
- nums=1,
47
- progress=gr.Progress(track_tqdm=True)):
48
-
49
- if seed == -1:
50
- seed = random.randint(0, MAX_SEED)
51
- seed = int(seed)
52
- print(f'prompt:{prompt}')
53
-
54
- text = str(translator.translate(prompt, 'English'))
55
-
56
- generator = torch.Generator().manual_seed(seed)
57
-
58
-
59
- image = pipe(
60
- prompt=text,
61
- height=height,
62
- width=width,
63
- guidance_scale=scales,
64
- output_type="pil",
65
- num_inference_steps=steps,
66
- max_sequence_length=512,
67
- num_images_per_prompt=nums,
68
- generator=generator,
69
- ).images
70
-
71
- print(image)
72
- print(seed)
73
- return image, seed
74
-
75
-
76
- examples = [
77
- "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",
78
- "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\".",
79
- "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\"",
80
- "selfie photo of a wizard with long beard and purple robes, he is apparently in the middle of Tokyo. Probably taken from a phone.",
81
- "A vibrant street wall covered in colorful graffiti, the centerpiece spells \"FLUX\", in a storm of colors",
82
- "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.",
83
- "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",
84
- "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.",
85
- "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.",
86
- "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"
87
- ]
88
-
89
-
90
-
91
- # Gradio Interface
92
-
93
- with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
94
- gr.HTML("<h1><center>Flux Dev</center></h1>")
95
- gr.HTML("<p><center><a href='https://huggingface.co/black-forest-labs/FLUX.1-dev'>FLUX</a> text-to-image generation<br><b>Feature</b>: Support 512 token and multi-languages</center></p>")
96
- with gr.Row():
97
- with gr.Column(scale=4):
98
- img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
99
- with gr.Row():
100
- prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
101
- sendBtn = gr.Button(scale=1, variant='primary')
102
- with gr.Accordion("Advanced Options", open=True):
103
- with gr.Column(scale=1):
104
- width = gr.Slider(
105
- label="Width",
106
- minimum=512,
107
- maximum=1280,
108
- step=8,
109
- value=1024,
110
- )
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=512,
114
- maximum=1280,
115
- step=8,
116
- value=1024,
117
- )
118
- scales = gr.Slider(
119
- label="Guidance",
120
- minimum=3.5,
121
- maximum=7,
122
- step=0.1,
123
- value=3.5,
124
- )
125
- steps = gr.Slider(
126
- label="Steps",
127
- minimum=1,
128
- maximum=100,
129
- step=1,
130
- value=30,
131
- )
132
- seed = gr.Slider(
133
- label="Seeds",
134
- minimum=-1,
135
- maximum=MAX_SEED,
136
- step=1,
137
- value=-1,
138
- scale=2,
139
- )
140
- nums = gr.Slider(
141
- label="Image Numbers",
142
- minimum=1,
143
- maximum=4,
144
- step=1,
145
- value=1,
146
- scale=1,
147
- )
148
- gr.Examples(
149
- examples=examples,
150
- inputs=prompt,
151
- outputs=[img, seed],
152
- fn=generate_image,
153
- cache_examples="lazy",
154
- examples_per_page=4,
155
- )
156
-
157
- sendBtn.click(fn=generate_image,
158
- inputs=[prompt, width, height, scales, steps, seed, nums],
159
- outputs=[img, seed],
160
- )
161
- prompt.submit(fn=generate_image,
162
- inputs=[prompt, width, height, scales, steps, seed, nums],
163
- outputs=[img, seed],
164
- )
165
-
166
-
 
 
 
 
 
167
  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
+ if torch.cuda.is_available():
33
+ pipe = FluxPipeline.from_pretrained(
34
+ model,
35
+ torch_dtype=torch.bfloat16).to("cuda")
36
+
37
+ quantize(pipe.transformer, weights=qfloat8)
38
+ freeze(pipe.transformer)
39
+ quantize(pipe.text_encoder_2, weights=qfloat8)
40
+ freeze(pipe.text_encoder_2)
41
+
42
+ # Function
43
+ @spaces.GPU()
44
+ def generate_image(
45
+ prompt,
46
+ width=1024,
47
+ height=1024,
48
+ scales=5,
49
+ steps=30,
50
+ seed: int =-1,
51
+ nums=1,
52
+ progress=gr.Progress(track_tqdm=True)):
53
+
54
+ if seed == -1:
55
+ seed = random.randint(0, MAX_SEED)
56
+ seed = int(seed)
57
+ print(f'prompt:{prompt}')
58
+
59
+ text = str(translator.translate(prompt, 'English'))
60
+
61
+ generator = torch.Generator().manual_seed(seed)
62
+
63
+
64
+ image = pipe(
65
+ prompt=text,
66
+ height=height,
67
+ width=width,
68
+ guidance_scale=scales,
69
+ output_type="pil",
70
+ num_inference_steps=steps,
71
+ max_sequence_length=512,
72
+ num_images_per_prompt=nums,
73
+ generator=generator,
74
+ ).images
75
+
76
+ print(image)
77
+ print(seed)
78
+ return image, seed
79
+
80
+
81
+ examples = [
82
+ "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",
83
+ "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\".",
84
+ "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\"",
85
+ "selfie photo of a wizard with long beard and purple robes, he is apparently in the middle of Tokyo. Probably taken from a phone.",
86
+ "A vibrant street wall covered in colorful graffiti, the centerpiece spells \"FLUX\", in a storm of colors",
87
+ "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.",
88
+ "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",
89
+ "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.",
90
+ "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.",
91
+ "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"
92
+ ]
93
+
94
+
95
+
96
+ # Gradio Interface
97
+
98
+ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
99
+ gr.HTML("<h1><center>Flux Dev Quanto</center></h1>")
100
+ 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>")
101
+ with gr.Row():
102
+ with gr.Column(scale=4):
103
+ img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
104
+ with gr.Row():
105
+ prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
106
+ sendBtn = gr.Button(scale=1, variant='primary')
107
+ with gr.Accordion("Advanced Options", open=True):
108
+ with gr.Column(scale=1):
109
+ width = gr.Slider(
110
+ label="Width",
111
+ minimum=512,
112
+ maximum=1280,
113
+ step=8,
114
+ value=1024,
115
+ )
116
+ height = gr.Slider(
117
+ label="Height",
118
+ minimum=512,
119
+ maximum=1280,
120
+ step=8,
121
+ value=1024,
122
+ )
123
+ scales = gr.Slider(
124
+ label="Guidance",
125
+ minimum=3.5,
126
+ maximum=7,
127
+ step=0.1,
128
+ value=3.5,
129
+ )
130
+ steps = gr.Slider(
131
+ label="Steps",
132
+ minimum=1,
133
+ maximum=100,
134
+ step=1,
135
+ value=30,
136
+ )
137
+ seed = gr.Slider(
138
+ label="Seeds",
139
+ minimum=-1,
140
+ maximum=MAX_SEED,
141
+ step=1,
142
+ value=-1,
143
+ scale=2,
144
+ )
145
+ nums = gr.Slider(
146
+ label="Image Numbers",
147
+ minimum=1,
148
+ maximum=4,
149
+ step=1,
150
+ value=1,
151
+ scale=1,
152
+ )
153
+ gr.Examples(
154
+ examples=examples,
155
+ inputs=prompt,
156
+ outputs=[img, seed],
157
+ fn=generate_image,
158
+ cache_examples="lazy",
159
+ examples_per_page=4,
160
+ )
161
+
162
+ sendBtn.click(fn=generate_image,
163
+ inputs=[prompt, width, height, scales, steps, seed, nums],
164
+ outputs=[img, seed],
165
+ )
166
+ prompt.submit(fn=generate_image,
167
+ inputs=[prompt, width, height, scales, steps, seed, nums],
168
+ outputs=[img, seed],
169
+ )
170
+
171
+
172
  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