thankfulcarp commited on
Commit
16d02f1
Β·
1 Parent(s): 9e04e04

Single File FusionX

Browse files
Files changed (1) hide show
  1. app.py +56 -44
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import spaces
2
  import torch
3
- from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, UniPCMultistepScheduler
4
  from diffusers.utils import export_to_video
5
  # Conditionally import T2V pipeline to handle different diffusers versions and prevent crashes.
6
  try:
@@ -10,8 +10,8 @@ except ImportError:
10
  WanTextToVideoPipeline = None # Define as None so later code doesn't raise NameError
11
  IS_T2V_AVAILABLE = False
12
  print("⚠️ Warning: 'WanTextToVideoPipeline' could not be imported. Your 'diffusers' version might be outdated (requires >= 0.25.0).")
13
- from transformers import CLIPVisionModel
14
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
15
  import tempfile
16
  import re
17
  import os
@@ -24,56 +24,68 @@ import gradio as gr
24
  import random
25
 
26
  # --- I2V (Image-to-Video) Configuration ---
27
- I2V_MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
28
- I2V_LORA_FILENAME = "FusionX_LoRa/Wan2.1_I2V_14B_FusionX_LoRA.safetensors"
29
 
30
  # --- T2V (Text-to-Video) Configuration ---
31
  T2V_MODEL_ID = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
32
- T2V_LORA_FILENAME = "FusionX_LoRa/Wan2.1_T2V_14B_FusionX_LoRA.safetensors"
33
 
34
- # --- Common LoRA Configuration ---
35
- LORA_REPO_ID = "vrgamedevgirl84/Wan14BT2VFusioniX"
36
-
37
- def load_and_fuse_pipeline(model_id, lora_filename, pipeline_class, lora_repo_id, **pipeline_kwargs):
38
- """Loads a pipeline, downloads and fuses a LoRA, and handles errors."""
39
- if pipeline_class is None:
40
- print(f"Skipping {model_id} as its pipeline class is not available in this environment.")
41
- return None
42
 
43
- print(f"πŸš€ Loading pipeline for {model_id}...")
44
- try:
45
- pipe = pipeline_class.from_pretrained(model_id, torch_dtype=torch.bfloat16, **pipeline_kwargs)
46
- pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=8.0)
47
- pipe.to("cuda")
48
- except Exception as e:
49
- print(f"❌ Critical Error: Failed to load base pipeline for {model_id}.")
50
- traceback.print_exc()
51
- return None
 
 
 
 
 
 
 
 
 
52
 
 
 
 
53
  try:
54
- print(f"βœ… Loading LoRA for {model_id}...")
55
- # Use the more direct loading method from the diffusers library
56
- pipe.load_lora_weights(lora_repo_id, weight_name=lora_filename, adapter_name="fusionx_lora")
57
- pipe.set_adapters(["fusionx_lora"], adapter_weights=[0.75])
58
- pipe.fuse_lora()
59
- print(f"βœ… FusionX LoRA loaded and fused for {model_id} with a weight of 0.75.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  except Exception as e:
61
- print(f"❌ Error during LoRA loading for {model_id}. The pipeline will be used without the LoRA.")
62
  traceback.print_exc()
63
-
64
- return pipe
65
-
66
- # --- Load Pipelines ---
67
- i2v_image_encoder = CLIPVisionModel.from_pretrained(I2V_MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float32)
68
- i2v_vae = AutoencoderKLWan.from_pretrained(I2V_MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
69
- i2v_pipe = load_and_fuse_pipeline(
70
- I2V_MODEL_ID, I2V_LORA_FILENAME, WanImageToVideoPipeline, LORA_REPO_ID,
71
- vae=i2v_vae, image_encoder=i2v_image_encoder
72
- )
73
-
74
- t2v_pipe = load_and_fuse_pipeline(
75
- T2V_MODEL_ID, T2V_LORA_FILENAME, WanTextToVideoPipeline, LORA_REPO_ID
76
- )
77
 
78
  # --- LLM Prompt Enhancer Setup ---
79
  print("\nπŸ€– Loading LLM for Prompt Enhancement (Qwen/Qwen3-8B)...")
 
1
  import spaces
2
  import torch
3
+ from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, UniPCMultistepScheduler, WanTransformer3DModel
4
  from diffusers.utils import export_to_video
5
  # Conditionally import T2V pipeline to handle different diffusers versions and prevent crashes.
6
  try:
 
10
  WanTextToVideoPipeline = None # Define as None so later code doesn't raise NameError
11
  IS_T2V_AVAILABLE = False
12
  print("⚠️ Warning: 'WanTextToVideoPipeline' could not be imported. Your 'diffusers' version might be outdated (requires >= 0.25.0).")
13
+ from transformers import CLIPVisionModel, CLIPTextModel, CLIPTokenizer
14
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline # noqa
15
  import tempfile
16
  import re
17
  import os
 
24
  import random
25
 
26
  # --- I2V (Image-to-Video) Configuration ---
27
+ I2V_MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers" # Used for VAE/encoder components
28
+ I2V_SINGLE_FILE_URL = "https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/resolve/main/Wan14Bi2vFusioniX.safetensors"
29
 
30
  # --- T2V (Text-to-Video) Configuration ---
31
  T2V_MODEL_ID = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
32
+ T2V_SINGLE_FILE_URL = "https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/resolve/main/WanT2V_MasterModel.safetensors"
33
 
34
+ # --- Load Pipelines ---
35
+ print("πŸš€ Loading I2V pipeline from single file...")
36
+ i2v_pipe = None
37
+ try:
38
+ # Load components needed for the pipeline from the base model repo
39
+ i2v_image_encoder = CLIPVisionModel.from_pretrained(I2V_MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float32)
40
+ i2v_vae = AutoencoderKLWan.from_pretrained(I2V_MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
 
41
 
42
+ # Load the main transformer from the single file URL
43
+ i2v_transformer = WanTransformer3DModel.from_single_file(
44
+ I2V_SINGLE_FILE_URL,
45
+ torch_dtype=torch.bfloat16
46
+ )
47
+
48
+ # Manually assemble the pipeline with the custom transformer
49
+ i2v_pipe = WanImageToVideoPipeline(
50
+ vae=i2v_vae,
51
+ image_encoder=i2v_image_encoder,
52
+ transformer=i2v_transformer
53
+ )
54
+ i2v_pipe.scheduler = UniPCMultistepScheduler.from_config(i2v_pipe.scheduler.config, flow_shift=8.0)
55
+ i2v_pipe.to("cuda")
56
+ print("βœ… I2V pipeline loaded successfully from single file.")
57
+ except Exception as e:
58
+ print(f"❌ Critical Error: Failed to load I2V pipeline from single file.")
59
+ traceback.print_exc()
60
 
61
+ print("\nπŸš€ Loading T2V pipeline from single file...")
62
+ t2v_pipe = None
63
+ if IS_T2V_AVAILABLE:
64
  try:
65
+ # Load components needed for the pipeline from the base model repo
66
+ t2v_vae = AutoencoderKLWan.from_pretrained(T2V_MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
67
+ t2v_text_encoder = CLIPTextModel.from_pretrained(T2V_MODEL_ID, subfolder="text_encoder", torch_dtype=torch.bfloat16)
68
+ t2v_tokenizer = CLIPTokenizer.from_pretrained(T2V_MODEL_ID, subfolder="tokenizer")
69
+
70
+ # Load the main transformer from the single file URL
71
+ t2v_transformer = WanTransformer3DModel.from_single_file(
72
+ T2V_SINGLE_FILE_URL,
73
+ torch_dtype=torch.bfloat16
74
+ )
75
+
76
+ # Manually assemble the pipeline with the custom transformer
77
+ t2v_pipe = WanTextToVideoPipeline(
78
+ vae=t2v_vae,
79
+ text_encoder=t2v_text_encoder,
80
+ tokenizer=t2v_tokenizer,
81
+ transformer=t2v_transformer
82
+ )
83
+ t2v_pipe.scheduler = UniPCMultistepScheduler.from_config(t2v_pipe.scheduler.config, flow_shift=8.0)
84
+ t2v_pipe.to("cuda")
85
+ print("βœ… T2V pipeline loaded successfully from single file.")
86
  except Exception as e:
87
+ print(f"❌ Critical Error: Failed to load T2V pipeline from single file.")
88
  traceback.print_exc()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # --- LLM Prompt Enhancer Setup ---
91
  print("\nπŸ€– Loading LLM for Prompt Enhancement (Qwen/Qwen3-8B)...")