vilarin commited on
Commit
a5a56d7
1 Parent(s): 0dec378

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -50
app.py CHANGED
@@ -5,17 +5,15 @@ import numpy as np
5
  import random
6
  from diffusers import FluxPipeline, FluxTransformer2DModel
7
  import spaces
8
- from transformers import T5EncoderModel
9
- from optimum.quanto import QuantizedDiffusersModel
10
  from translatepy import Translator
11
- from huggingface_hub import hf_hub_download
12
 
13
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
14
  translator = Translator()
15
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
16
  # Constants
17
- r_model = "black-forest-labs/FLUX.1-dev"
18
- model = "./model/"
19
  MAX_SEED = np.iinfo(np.int32).max
20
 
21
  CSS = """
@@ -31,39 +29,16 @@ JS = """function () {
31
  }
32
  }"""
33
 
34
- filenames = [
35
- "flux1-dev-fp8.safetensors",
36
- "t5xxl_fp8_e4m3fn.safetensors",
37
- ]
38
-
39
- for filename in filenames:
40
- downloaded_model_path = hf_hub_download(
41
- repo_id="camenduru/FLUX.1-dev",
42
- filename=filename,
43
- local_dir="model"
44
- )
45
-
46
- class QuantizedFluxTransformer2DModel(QuantizedDiffusersModel):
47
- base_class = FluxTransformer2DModel
48
-
49
- transformer = QuantizedFluxTransformer2DModel.from_pretrained(model)
50
- transformer.to(device="cuda", dtype=torch.float16)
51
-
52
- text_encoder_2 = T5EncoderModel.from_pretrained(
53
- model,
54
- torch_dtype=torch.float16)
55
 
 
 
 
 
 
56
 
57
- pipe = FluxPipeline.from_pretrained(
58
- r_model,
59
- transformer=None,
60
- text_encoder_2=text_encoder_2,
61
- torch_dtype=torch.float16,).to("cuda")
62
-
63
- pipe.transformer = transformer
64
-
65
- # Function
66
- @spaces.GPU()
67
  def generate_image(
68
  prompt,
69
  width=1024,
@@ -78,11 +53,11 @@ def generate_image(
78
  seed = random.randint(0, MAX_SEED)
79
  seed = int(seed)
80
  print(f'prompt:{prompt}')
81
-
82
  text = str(translator.translate(prompt, 'English'))
83
 
84
  generator = torch.Generator().manual_seed(seed)
85
-
86
 
87
  image = pipe(
88
  prompt=text,
@@ -96,19 +71,17 @@ def generate_image(
96
  generator=generator,
97
  ).images
98
 
99
- print(image)
100
- print(seed)
101
  return image, seed
102
 
103
 
104
  examples = [
105
  "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",
106
- "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\".",
107
- "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\"",
108
- "selfie photo of a wizard with long beard and purple robes, he is apparently in the middle of Tokyo. Probably taken from a phone.",
109
  "A vibrant street wall covered in colorful graffiti, the centerpiece spells \"FLUX\", in a storm of colors",
110
- "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.",
111
- "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",
112
  "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.",
113
  "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.",
114
  "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"
@@ -119,8 +92,8 @@ examples = [
119
  # Gradio Interface
120
 
121
  with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
122
- gr.HTML("<h1><center>Flux Dev Quanto</center></h1>")
123
- 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>")
124
  with gr.Row():
125
  with gr.Column(scale=4):
126
  img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
@@ -172,7 +145,7 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
172
  step=1,
173
  value=1,
174
  scale=1,
175
- )
176
  gr.Examples(
177
  examples=examples,
178
  inputs=prompt,
@@ -181,7 +154,7 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
181
  cache_examples="lazy",
182
  examples_per_page=4,
183
  )
184
-
185
  sendBtn.click(fn=generate_image,
186
  inputs=[prompt, width, height, scales, steps, seed, nums],
187
  outputs=[img, seed],
@@ -191,5 +164,5 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
191
  outputs=[img, seed],
192
  )
193
 
194
-
195
  demo.queue().launch()
 
5
  import random
6
  from diffusers import FluxPipeline, FluxTransformer2DModel
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 = """
 
29
  }
30
  }"""
31
 
32
+ transformer = FluxTransformer2DModel.from_pretrained("sayakpaul/FLUX.1-merged", torch_dtype=torch.bfloat16)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ if torch.cuda.is_available():
35
+ pipe = FluxPipeline.from_pretrained(
36
+ model,
37
+ transformer=transformer,
38
+ torch_dtype=torch.bfloat16).to("cuda")
39
 
40
+ # Function
41
+ @spaces.GPU(duration=120)
 
 
 
 
 
 
 
 
42
  def generate_image(
43
  prompt,
44
  width=1024,
 
53
  seed = random.randint(0, MAX_SEED)
54
  seed = int(seed)
55
  print(f'prompt:{prompt}')
56
+
57
  text = str(translator.translate(prompt, 'English'))
58
 
59
  generator = torch.Generator().manual_seed(seed)
60
+
61
 
62
  image = pipe(
63
  prompt=text,
 
71
  generator=generator,
72
  ).images
73
 
 
 
74
  return image, seed
75
 
76
 
77
  examples = [
78
  "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",
79
+ "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\".",
80
+ "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\"",
81
+ "selfie photo of a wizard with long beard and purple robes, he is apparently in the middle of Tokyo. Probably taken from a phone.",
82
  "A vibrant street wall covered in colorful graffiti, the centerpiece spells \"FLUX\", in a storm of colors",
83
+ "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.",
84
+ "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",
85
  "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.",
86
  "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.",
87
  "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
  # Gradio Interface
93
 
94
  with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
95
+ gr.HTML("<h1><center>Flux Dev</center></h1>")
96
+ gr.HTML("<p><center><a href='https://huggingface.co/sayakpaul/FLUX.1-merged'>FLUX.1 Merged</a></center></p>")
97
  with gr.Row():
98
  with gr.Column(scale=4):
99
  img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
 
145
  step=1,
146
  value=1,
147
  scale=1,
148
+ )
149
  gr.Examples(
150
  examples=examples,
151
  inputs=prompt,
 
154
  cache_examples="lazy",
155
  examples_per_page=4,
156
  )
157
+
158
  sendBtn.click(fn=generate_image,
159
  inputs=[prompt, width, height, scales, steps, seed, nums],
160
  outputs=[img, seed],
 
164
  outputs=[img, seed],
165
  )
166
 
167
+
168
  demo.queue().launch()