Azure99 commited on
Commit
a2a913e
·
verified ·
1 Parent(s): 4dc40a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -5,40 +5,57 @@ import spaces
5
  import torch
6
  import os
7
  import time
8
- from diffusers import FluxPipeline
 
 
9
 
10
- MAX_SEED = np.iinfo(np.int32).max
11
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to("cuda")
 
 
 
 
12
  torch.cuda.empty_cache()
 
 
13
  MAX_IMAGE_SIZE = 2048
14
 
 
15
 
16
  @spaces.GPU(duration=75)
17
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
18
  if randomize_seed:
19
  seed = random.randint(0, MAX_SEED)
20
-
21
- image = pipe(
22
- prompt,
23
- height=height,
24
- width=width,
25
- guidance_scale=guidance_scale,
26
- num_inference_steps=num_inference_steps,
27
- max_sequence_length=512,
28
- generator=torch.Generator().manual_seed(seed)
29
- ).images[0]
30
- image.save("flux-dev.png")
 
 
 
 
31
 
32
- if image is not None:
 
 
33
  timestamp = int(time.time())
34
  filename = f"flux_generated_{timestamp}_{seed}.png"
35
 
36
  # Save to /tmp directory (as requested)
37
  tmp_path = os.path.join("/tmp", filename)
38
  os.makedirs("/tmp", exist_ok=True)
39
- image.save(tmp_path)
40
 
41
- return image, seed, tmp_path
 
 
42
 
43
  examples = [
44
  "a tiny astronaut hatching from an egg on the moon",
 
5
  import torch
6
  import os
7
  import time
8
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
9
+ from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
10
+ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
11
 
12
+ dtype = torch.bfloat16
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+
15
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
16
+ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
17
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
18
  torch.cuda.empty_cache()
19
+
20
+ MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 2048
22
 
23
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
24
 
25
  @spaces.GPU(duration=75)
26
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
27
  if randomize_seed:
28
  seed = random.randint(0, MAX_SEED)
29
+ generator = torch.Generator().manual_seed(seed)
30
+
31
+ # Get the final image from the generator
32
+ final_img = None
33
+ for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
34
+ prompt=prompt,
35
+ guidance_scale=guidance_scale,
36
+ num_inference_steps=num_inference_steps,
37
+ width=width,
38
+ height=height,
39
+ generator=generator,
40
+ output_type="pil",
41
+ good_vae=good_vae,
42
+ ):
43
+ final_img = img
44
 
45
+ # Save image to /tmp directory
46
+ if final_img is not None:
47
+ # Create a unique filename with timestamp
48
  timestamp = int(time.time())
49
  filename = f"flux_generated_{timestamp}_{seed}.png"
50
 
51
  # Save to /tmp directory (as requested)
52
  tmp_path = os.path.join("/tmp", filename)
53
  os.makedirs("/tmp", exist_ok=True)
54
+ final_img.save(tmp_path)
55
 
56
+ return final_img, seed, tmp_path
57
+
58
+ return final_img, seed, ""
59
 
60
  examples = [
61
  "a tiny astronaut hatching from an egg on the moon",