Econogoat commited on
Commit
a563432
·
verified ·
1 Parent(s): 9a900af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -56
app.py CHANGED
@@ -10,48 +10,55 @@ import pandas as pd
10
  import random
11
  import time
12
 
13
- # --- Main Configuration ---
14
  KRYPTO_LORA = {
15
  "repo": "Econogoat/Krypt0_LORA",
16
  "trigger": "Krypt0",
17
  "adapter_name": "krypt0"
18
  }
19
 
20
- # Load prompts for the randomize button
21
- df = pd.read_csv('prompts.csv', header=None)
22
- prompt_values = df.values.flatten()
23
-
24
- # Get access token from Space secrets
25
- HF_TOKEN = os.getenv("HF_TOKEN")
26
- if not HF_TOKEN:
27
- print("WARNING: HF_TOKEN secret is not set. Gated model downloads may fail.")
28
-
29
- # --- Model Initialization ---
30
- # CORRECTION : On ne détecte plus le device ici, on charge tout sur CPU par défaut.
31
- print("Loading all models to CPU by default for ZeroGPU compatibility.")
32
  dtype = torch.bfloat16
33
  base_model = "black-forest-labs/FLUX.1-dev"
34
 
35
- # CORRECTION : Tous les `.to(device)` sont retirés. Les modèles restent sur le CPU au démarrage.
36
- taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype)
37
- good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype, token=HF_TOKEN)
38
- pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1, token=HF_TOKEN)
39
- print("Models loaded on CPU.")
40
 
41
- # Load the LoRA adapter once on startup
42
- print(f"Loading on-board LoRA: {KRYPTO_LORA['repo']}")
43
- pipe.load_lora_weights(
44
- KRYPTO_LORA['repo'],
45
- low_cpu_mem_usage=True,
46
- adapter_name=KRYPTO_LORA['adapter_name'],
47
- token=HF_TOKEN
48
- )
49
- print("LoRA loaded successfully.")
50
 
51
- MAX_SEED = 2**32 - 1
52
 
53
- # Monkey-patch the pipeline for live preview
54
- pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
  def calculate_dimensions(aspect_ratio, resolution):
@@ -70,7 +77,6 @@ def calculate_dimensions(aspect_ratio, resolution):
70
  height = (height // 64) * 64
71
  return width, height
72
 
73
- # CORRECTION : Cette fonction suppose maintenant que `pipe` est déjà sur le bon device.
74
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
75
  generator = torch.Generator(device="cuda").manual_seed(seed)
76
 
@@ -102,37 +108,28 @@ def update_history(new_image, history):
102
 
103
  @spaces.GPU(duration=75)
104
  def run_generation(prompt, lora_scale, cfg_scale, steps, randomize_seed, seed, aspect_ratio, base_resolution, progress=gr.Progress(track_tqdm=True)):
 
105
  if not prompt:
106
  raise gr.Error("Prompt cannot be empty.")
107
 
108
- # CORRECTION : C'est ici, à l'intérieur de la fonction @spaces.GPU, que l'on déplace les modèles sur le GPU.
109
- print("Moving models to GPU for generation...")
110
- pipe.to("cuda")
111
- good_vae.to("cuda") # Il faut aussi déplacer le VAE de haute qualité
112
-
113
- try:
114
- prompt_mash = f"{KRYPTO_LORA['trigger']}, {prompt}"
115
- print("Final prompt:", prompt_mash)
116
-
117
- pipe.set_adapters([KRYPTO_LORA['adapter_name']], adapter_weights=[lora_scale])
118
- print(f"Adapter '{KRYPTO_LORA['adapter_name']}' activated with weight {lora_scale}.")
119
-
120
- if randomize_seed:
121
- seed = random.randint(0, MAX_SEED)
122
-
123
- width, height = calculate_dimensions(aspect_ratio, base_resolution)
124
- print(f"Generating a {width}x{height} image.")
125
 
126
- for image, progress_update in generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
127
- yield image, seed, progress_update
128
 
129
- finally:
130
- # CORRECTION : Bonne pratique, on nettoie en déplaçant les modèles vers le CPU après usage.
131
- print("Moving models back to CPU.")
132
- pipe.to("cpu")
133
- good_vae.to("cpu")
134
- torch.cuda.empty_cache()
 
 
135
 
 
 
136
 
137
  run_generation.zerogpu = True
138
 
 
10
  import random
11
  import time
12
 
13
+ # --- Configuration Principale ---
14
  KRYPTO_LORA = {
15
  "repo": "Econogoat/Krypt0_LORA",
16
  "trigger": "Krypt0",
17
  "adapter_name": "krypt0"
18
  }
19
 
20
+ # --- Lazy Loading Setup ---
21
+ # CORRECTION : On ne charge rien ici. Les modèles sont initialisés à None.
22
+ pipe = None
23
+ good_vae = None
24
+ taef1 = None
 
 
 
 
 
 
 
25
  dtype = torch.bfloat16
26
  base_model = "black-forest-labs/FLUX.1-dev"
27
 
28
+ # Charger les prompts pour le bouton de randomisation
29
+ df = pd.read_csv('prompts.csv', header=None)
30
+ prompt_values = df.values.flatten()
 
 
31
 
32
+ # Récupérer le token d'accès depuis les secrets du Space
33
+ HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
 
 
 
 
34
 
 
35
 
36
+ def load_models():
37
+ """
38
+ Cette fonction charge tous les modèles et les place sur le GPU.
39
+ Elle n'est appelée qu'une seule fois, lors de la première génération.
40
+ """
41
+ global pipe, good_vae, taef1
42
+
43
+ print("Performing first-time setup: loading models to GPU...")
44
+
45
+ # On charge tout directement sur CUDA car cette fonction est appelée depuis un contexte GPU.
46
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to("cuda")
47
+ good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype, token=HF_TOKEN).to("cuda")
48
+ pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1, token=HF_TOKEN).to("cuda")
49
+
50
+ print(f"Loading on-board LoRA: {KRYPTO_LORA['repo']}")
51
+ pipe.load_lora_weights(
52
+ KRYPTO_LORA['repo'],
53
+ low_cpu_mem_usage=True,
54
+ adapter_name=KRYPTO_LORA['adapter_name'],
55
+ token=HF_TOKEN
56
+ )
57
+
58
+ # Monkey-patch the pipeline for live preview
59
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
60
+
61
+ print("Models loaded and ready on GPU.")
62
 
63
 
64
  def calculate_dimensions(aspect_ratio, resolution):
 
77
  height = (height // 64) * 64
78
  return width, height
79
 
 
80
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
81
  generator = torch.Generator(device="cuda").manual_seed(seed)
82
 
 
108
 
109
  @spaces.GPU(duration=75)
110
  def run_generation(prompt, lora_scale, cfg_scale, steps, randomize_seed, seed, aspect_ratio, base_resolution, progress=gr.Progress(track_tqdm=True)):
111
+ global pipe
112
  if not prompt:
113
  raise gr.Error("Prompt cannot be empty.")
114
 
115
+ # CORRECTION : On charge les modèles seulement si ce n'est pas déjà fait.
116
+ if pipe is None:
117
+ load_models()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
+ prompt_mash = f"{KRYPTO_LORA['trigger']}, {prompt}"
120
+ print("Final prompt:", prompt_mash)
121
 
122
+ pipe.set_adapters([KRYPTO_LORA['adapter_name']], adapter_weights=[lora_scale])
123
+ print(f"Adapter '{KRYPTO_LORA['adapter_name']}' activated with weight {lora_scale}.")
124
+
125
+ if randomize_seed:
126
+ seed = random.randint(0, MAX_SEED)
127
+
128
+ width, height = calculate_dimensions(aspect_ratio, base_resolution)
129
+ print(f"Generating a {width}x{height} image.")
130
 
131
+ for image, progress_update in generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
132
+ yield image, seed, progress_update
133
 
134
  run_generation.zerogpu = True
135