nirajandhakal commited on
Commit
4318215
·
verified ·
1 Parent(s): a1097a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -59
app.py CHANGED
@@ -1,30 +1,30 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
  import torch
5
- from diffusers import DiffusionPipeline
 
 
6
  import os
7
 
8
- # Constants
9
- MAX_SEED = np.iinfo(np.int32).max
10
- MAX_IMAGE_SIZE = 2048
11
- DEFAULT_IMAGE_SIZE = 1024
12
-
13
- # Model setup
14
  dtype = torch.bfloat16
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
16
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
17
 
18
- pipe = DiffusionPipeline.from_pretrained(
19
- "black-forest-labs/FLUX.1-dev",
20
- torch_dtype=dtype,
21
- token=huggingface_token
22
- ).to(device)
23
 
24
- def generate_image(prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
- generator = torch.Generator().manual_seed(42)
28
  image = pipe(
29
  prompt=prompt,
30
  width=width,
@@ -35,15 +35,30 @@ def generate_image(prompt, seed, randomize_seed, width, height, guidance_scale,
35
  ).images[0]
36
  return image, seed
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  css = """
39
  body {
40
  background-color: #f4faff;
41
  color: #005662;
42
  font-family: 'Poppins', sans-serif;
43
  }
44
- .container {
45
  margin: 0 auto;
46
- max-width: 900px;
47
  padding: 20px;
48
  }
49
  .gr-button {
@@ -55,62 +70,128 @@ body {
55
  .gr-button:hover {
56
  background-color: #0277bd;
57
  }
58
- .gr-box {
59
- border-radius: 12px;
60
  border: 1px solid #eeeeee;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
  """
63
 
64
  with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
65
- gr.Markdown("""
66
- # FLUX.1 [dev] | A Text-To-Image Rectified Flow 12B Transformer
67
-
68
- Enter a text prompt below to generate an image. Click 'Generate' to create your image.
69
- """)
70
-
71
- with gr.Row():
72
- with gr.Column(scale=4):
73
- prompt = gr.Text(
74
- label="Prompt",
75
- placeholder="Enter your prompt here",
76
- lines=3
77
- )
78
- with gr.Column(scale=1):
79
- generate_button = gr.Button("Generate", variant="primary")
80
 
81
- result = gr.Image(label="Generated Image", type="pil")
82
-
83
- with gr.Accordion("Advanced Settings", open=False):
84
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
85
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
86
 
 
 
 
 
 
 
 
 
87
  with gr.Row():
88
- width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_IMAGE_SIZE)
89
- height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_IMAGE_SIZE)
 
 
 
 
 
 
90
 
91
- with gr.Row():
92
- guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
93
- num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
94
 
95
- gr.Examples(
96
- examples=[
97
- "a galaxy swirling with vibrant blue and purple hues",
98
- "a futuristic cityscape under a dark sky",
99
- "a serene forest with a magical glowing tree",
100
- "a portrait of a smiling woman with a colorful floral crown",
101
- "a fantastical creature with the body of a dragon and the wings of a butterfly",
102
- ],
103
- inputs=prompt,
104
- outputs=[result, seed],
105
- fn=generate_image,
106
- cache_examples=True,
107
- )
108
-
109
- generate_button.click(
110
- fn=generate_image,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
112
  outputs=[result, seed]
113
  )
114
 
 
 
 
 
 
115
 
116
  demo.launch(share=True)
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ import spaces
5
  import torch
6
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
7
+ from PIL import Image
8
+ import io
9
  import os
10
 
 
 
 
 
 
 
11
  dtype = torch.bfloat16
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ # Set your Hugging Face API token
15
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
16
 
17
+ # Load the diffusion pipeline with the Hugging Face API token
18
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, token=huggingface_token).to(device)
 
 
 
19
 
20
+ MAX_SEED = np.iinfo(np.int32).max
21
+ MAX_IMAGE_SIZE = 2048
22
+
23
+ @spaces.GPU(duration=200)
24
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
+ generator = torch.Generator().manual_seed(seed)
28
  image = pipe(
29
  prompt=prompt,
30
  width=width,
 
35
  ).images[0]
36
  return image, seed
37
 
38
+ def download_image(image, file_format):
39
+ img_byte_arr = io.BytesIO()
40
+ image.save(img_byte_arr, format=file_format)
41
+ img_byte_arr = img_byte_arr.getvalue()
42
+ return img_byte_arr
43
+
44
+ examples = [
45
+ "a galaxy swirling with vibrant blue and purple hues",
46
+ "a futuristic cityscape under a dark sky",
47
+ "a serene forest with a magical glowing tree",
48
+ "a futuristic cityscape with sleek skyscrapers and flying cars",
49
+ "a portrait of a smiling woman with a colorful floral crown",
50
+ "a fantastical creature with the body of a dragon and the wings of a butterfly",
51
+ ]
52
+
53
  css = """
54
  body {
55
  background-color: #f4faff;
56
  color: #005662;
57
  font-family: 'Poppins', sans-serif;
58
  }
59
+ #col-container {
60
  margin: 0 auto;
61
+ max-width: 100%;
62
  padding: 20px;
63
  }
64
  .gr-button {
 
70
  .gr-button:hover {
71
  background-color: #0277bd;
72
  }
73
+ .gr-examples-card {
 
74
  border: 1px solid #eeeeee;
75
+ border-radius: 12px;
76
+ padding: 16px;
77
+ margin-bottom: 12px;
78
+ }
79
+ .gr-examples-card:hover {
80
+ background-color: #f4faf2;
81
+ border-color: #0277bd;
82
+ color: #005662;
83
+ }
84
+ .gr-progress-bar, .gr-progress-bar-fill {
85
+ background-color: #0288d1 !important;
86
+ }
87
+ .gr-slider, .gr-slider-track {
88
+ background-color: #0288d1 !important;
89
+ }
90
+ .gr-slider-thumb {
91
+ background-color: #005662 !important;
92
+ }
93
+ .gr-text-input, .gr-image {
94
+ width: 100%;
95
+ box-sizing: border-box;
96
+ margin-bottom: 10px;
97
  }
98
  """
99
 
100
  with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ with gr.Column(elem_id="col-container"):
103
+ gr.Markdown(f"""# FLUX.1 [dev] | A Text-To-Image Rectified Flow 12B Transformer
 
 
 
104
 
105
+ <a href="https://huggingface.co/black-forest-labs/FLUX.1-dev" style="text-decoration:none;">
106
+ <div class="gr-examples-card">
107
+ <h3>View Model Details</h3>
108
+ <p>Explore more about this model on Hugging Face.</p>
109
+ </div>
110
+ </a>
111
+ """)
112
+
113
  with gr.Row():
114
+ with gr.Column(scale=4):
115
+ prompt = gr.Text(
116
+ label="Prompt",
117
+ placeholder="Enter your prompt here",
118
+ lines=2
119
+ )
120
+ with gr.Column(scale=1):
121
+ generate_button = gr.Button("Generate", variant="primary")
122
 
123
+ result = gr.Image(label="Generated Image", type="pil")
 
 
124
 
125
+ with gr.Accordion("Advanced Settings", open=False):
126
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
127
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
128
+
129
+ with gr.Row():
130
+ width = gr.Slider(
131
+ label="Width",
132
+ minimum=256,
133
+ maximum=MAX_IMAGE_SIZE,
134
+ step=32,
135
+ value=1024,
136
+ )
137
+ height = gr.Slider(
138
+ label="Height",
139
+ minimum=256,
140
+ maximum=MAX_IMAGE_SIZE,
141
+ step=32,
142
+ value=1024,
143
+ )
144
+
145
+ with gr.Row():
146
+ guidance_scale = gr.Slider(
147
+ label="Guidance Scale",
148
+ minimum=1,
149
+ maximum=15,
150
+ step=0.1,
151
+ value=3.5,
152
+ )
153
+ num_inference_steps = gr.Slider(
154
+ label="Number of inference steps",
155
+ minimum=1,
156
+ maximum=50,
157
+ step=1,
158
+ value=28,
159
+ )
160
+
161
+ download_format = gr.Radio(
162
+ label="Download Format",
163
+ choices=["PNG", "JPEG", "SVG", "WEBP"],
164
+ value="PNG",
165
+ type="value",
166
+ )
167
+
168
+ download_button = gr.Button("Download Image")
169
+
170
+ download_button.click(
171
+ fn=download_image,
172
+ inputs=[result, download_format],
173
+ outputs=gr.File(label="Download"),
174
+ )
175
+
176
+ gr.Examples(
177
+ examples=examples,
178
+ fn=infer,
179
+ inputs=[prompt],
180
+ outputs=[result, seed],
181
+ cache_examples="lazy"
182
+ )
183
+
184
+ gr.on(
185
+ triggers=[run_button.click, prompt.submit],
186
+ fn=infer,
187
  inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
188
  outputs=[result, seed]
189
  )
190
 
191
+ demo.load(
192
+ fn=lambda: None,
193
+ inputs=None,
194
+ outputs=None
195
+ )
196
 
197
  demo.launch(share=True)