gokaygokay commited on
Commit
77ed278
·
verified ·
1 Parent(s): 747abf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -15,7 +15,8 @@ from pytorch_lightning import seed_everything
15
  from omegaconf import OmegaConf
16
  from einops import rearrange, repeat
17
  from tqdm import tqdm
18
- from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
 
19
  import gradio as gr
20
  import shutil
21
  import tempfile
@@ -32,6 +33,8 @@ from src.utils.camera_util import (
32
  from src.utils.mesh_util import save_obj, save_glb
33
  from src.utils.infer_util import remove_background, resize_foreground, images_to_video
34
 
 
 
35
  # Set up cache path
36
  cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
37
  os.environ["TRANSFORMERS_CACHE"] = cache_path
@@ -73,9 +76,29 @@ else:
73
 
74
 
75
  device = torch.device('cuda')
76
-
 
 
77
  base_model = "black-forest-labs/FLUX.1-dev"
78
- pipe = FluxPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16, token=huggingface_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Load and fuse LoRA BEFORE quantizing
81
  print('Loading and fusing lora, please wait...')
@@ -83,16 +106,7 @@ lora_path = hf_hub_download("gokaygokay/Flux-Game-Assets-LoRA-v2", "game_asst.sa
83
  pipe.load_lora_weights(lora_path)
84
  pipe.fuse_lora(lora_scale=1.0)
85
  pipe.unload_lora_weights()
86
- #pipe.transformer.to(device, dtype=torch.bfloat16)
87
-
88
- # Now quantize after LoRA is fused
89
- print('Quantizing, please wait...')
90
- # Try qint8 if qfloat8 produces invalid values
91
- quantize(pipe.transformer, qfloat8) # Consider changing to qint8 if you get invalid values
92
- freeze(pipe.transformer)
93
- print('Model quantized!')
94
- pipe.to(device)
95
-
96
 
97
  # Load 3D generation models
98
  config_path = 'configs/instant-mesh-large.yaml'
 
15
  from omegaconf import OmegaConf
16
  from einops import rearrange, repeat
17
  from tqdm import tqdm
18
+ from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler, AutoencoderTiny, AutoencoderKL, AutoPipelineForImage2Image
19
+ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
20
  import gradio as gr
21
  import shutil
22
  import tempfile
 
33
  from src.utils.mesh_util import save_obj, save_glb
34
  from src.utils.infer_util import remove_background, resize_foreground, images_to_video
35
 
36
+ from transformer_flux import FluxTransformer2DModel
37
+
38
  # Set up cache path
39
  cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
40
  os.environ["TRANSFORMERS_CACHE"] = cache_path
 
76
 
77
 
78
  device = torch.device('cuda')
79
+ # Initialize the base model
80
+ dtype = torch.bfloat16
81
+ device = "cuda" if torch.cuda.is_available() else "cpu"
82
  base_model = "black-forest-labs/FLUX.1-dev"
83
+
84
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
85
+ good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype, token=huggingface_token).to(device)
86
+ pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1, token=huggingface_token).to(device)
87
+ pipe_i2i = AutoPipelineForImage2Image.from_pretrained(
88
+ base_model,
89
+ vae=good_vae,
90
+ transformer=pipe.transformer,
91
+ text_encoder=pipe.text_encoder,
92
+ tokenizer=pipe.tokenizer,
93
+ text_encoder_2=pipe.text_encoder_2,
94
+ tokenizer_2=pipe.tokenizer_2,
95
+ torch_dtype=dtype,
96
+ token=huggingface_token
97
+ )
98
+
99
+ MAX_SEED = 2**32 - 1
100
+
101
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
102
 
103
  # Load and fuse LoRA BEFORE quantizing
104
  print('Loading and fusing lora, please wait...')
 
106
  pipe.load_lora_weights(lora_path)
107
  pipe.fuse_lora(lora_scale=1.0)
108
  pipe.unload_lora_weights()
109
+ pipe.transformer.to(device, dtype=torch.bfloat16)
 
 
 
 
 
 
 
 
 
110
 
111
  # Load 3D generation models
112
  config_path = 'configs/instant-mesh-large.yaml'