Ffftdtd5dtft commited on
Commit
29b6509
·
verified ·
1 Parent(s): 86f96a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -17
app.py CHANGED
@@ -4,29 +4,46 @@ import random
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
 
7
 
 
8
  dtype = torch.bfloat16
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
- pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
 
 
 
 
 
 
 
 
 
 
12
 
13
  MAX_SEED = np.iinfo(np.int32).max
14
  MAX_IMAGE_SIZE = 2048
15
 
16
  @spaces.GPU()
17
- def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
18
  if randomize_seed:
19
  seed = random.randint(0, MAX_SEED)
20
  generator = torch.Generator().manual_seed(seed)
21
- image = pipe(
22
- prompt = prompt,
23
- width = width,
24
- height = height,
25
- num_inference_steps = num_inference_steps,
26
- generator = generator,
27
- guidance_scale=0.0
28
- ).images[0]
29
- return image, seed
 
 
 
 
 
30
 
31
  examples = [
32
  "a tiny astronaut hatching from an egg on the moon",
@@ -61,7 +78,7 @@ with gr.Blocks(css=css) as demo:
61
 
62
  run_button = gr.Button("Run", scale=0)
63
 
64
- result = gr.Image(label="Result", show_label=False)
65
 
66
  with gr.Accordion("Advanced Settings", open=False):
67
 
@@ -95,7 +112,6 @@ with gr.Blocks(css=css) as demo:
95
 
96
  with gr.Row():
97
 
98
-
99
  num_inference_steps = gr.Slider(
100
  label="Number of inference steps",
101
  minimum=1,
@@ -103,20 +119,28 @@ with gr.Blocks(css=css) as demo:
103
  step=1,
104
  value=4,
105
  )
 
 
 
 
 
 
 
 
106
 
107
  gr.Examples(
108
  examples = examples,
109
  fn = infer,
110
  inputs = [prompt],
111
- outputs = [result, seed],
112
  cache_examples="lazy"
113
  )
114
 
115
  gr.on(
116
  triggers=[run_button.click, prompt.submit],
117
  fn = infer,
118
- inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
119
- outputs = [result, seed]
120
  )
121
 
122
- demo.launch()
 
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
7
+ from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
 
9
+ # Configuración para usar bfloat16 y CUDA si está disponible
10
  dtype = torch.bfloat16
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
+ # Inicialización del modelo en la RAM
14
+ with init_empty_weights():
15
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype)
16
+
17
+ # Cargar el modelo en la RAM y despachar los pesos a la GPU
18
+ pipe = load_checkpoint_and_dispatch(
19
+ pipe,
20
+ "black-forest-labs/FLUX.1-schnell",
21
+ device_map="auto", # Automatiza el uso de RAM y GPU
22
+ offload_folder=None, # Evita que se almacenen los pesos temporalmente en el disco
23
+ ).to(device)
24
 
25
  MAX_SEED = np.iinfo(np.int32).max
26
  MAX_IMAGE_SIZE = 2048
27
 
28
  @spaces.GPU()
29
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, num_images=1, progress=gr.Progress(track_tqdm=True)):
30
  if randomize_seed:
31
  seed = random.randint(0, MAX_SEED)
32
  generator = torch.Generator().manual_seed(seed)
33
+
34
+ images = []
35
+ for _ in range(num_images):
36
+ image = pipe(
37
+ prompt = prompt,
38
+ width = width,
39
+ height = height,
40
+ num_inference_steps = num_inference_steps,
41
+ generator = generator,
42
+ guidance_scale=0.0
43
+ ).images[0]
44
+ images.append(image)
45
+
46
+ return images, seed
47
 
48
  examples = [
49
  "a tiny astronaut hatching from an egg on the moon",
 
78
 
79
  run_button = gr.Button("Run", scale=0)
80
 
81
+ results = gr.Gallery(label="Results", show_label=False, elem_id="image-gallery")
82
 
83
  with gr.Accordion("Advanced Settings", open=False):
84
 
 
112
 
113
  with gr.Row():
114
 
 
115
  num_inference_steps = gr.Slider(
116
  label="Number of inference steps",
117
  minimum=1,
 
119
  step=1,
120
  value=4,
121
  )
122
+
123
+ num_images = gr.Slider(
124
+ label="Number of images",
125
+ minimum=1,
126
+ maximum=300,
127
+ step=1,
128
+ value=1,
129
+ )
130
 
131
  gr.Examples(
132
  examples = examples,
133
  fn = infer,
134
  inputs = [prompt],
135
+ outputs = [results, seed],
136
  cache_examples="lazy"
137
  )
138
 
139
  gr.on(
140
  triggers=[run_button.click, prompt.submit],
141
  fn = infer,
142
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps, num_images],
143
+ outputs = [results, seed]
144
  )
145
 
146
+ demo.launch()