telcom commited on
Commit
4c2d0e3
·
verified ·
1 Parent(s): e1970ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -82
app.py CHANGED
@@ -1,3 +1,12 @@
 
 
 
 
 
 
 
 
 
1
  import os
2
  import random
3
  import gc
@@ -6,13 +15,6 @@ import gradio as gr
6
  import numpy as np
7
  from PIL import Image
8
 
9
- try:
10
- import spaces
11
- GPU_DECORATOR = spaces.GPU
12
- except Exception:
13
- def GPU_DECORATOR(fn):
14
- return fn
15
-
16
  import torch
17
  from diffusers import (
18
  StableDiffusionPipeline,
@@ -32,83 +34,61 @@ HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
32
  if HF_TOKEN:
33
  login(token=HF_TOKEN)
34
 
35
- cuda_available = torch.cuda.is_available()
36
- device = torch.device("cuda" if cuda_available else "cpu")
37
- dtype = torch.float16 if cuda_available else torch.float32
38
 
39
  MAX_SEED = np.iinfo(np.int32).max
40
- MAX_IMAGE_SIZE = 768 if not cuda_available else 1024
41
 
42
  pipe_txt2img = None
43
  pipe_img2img = None
44
- model_loaded = False
45
- load_error = None
46
 
47
  # ============================================================
48
- # Load model (FORCED tokenizer fix)
49
  # ============================================================
50
- try:
51
- pipe_txt2img = StableDiffusionPipeline.from_pretrained(
52
- MODEL_ID,
53
- revision=REVISION,
54
- torch_dtype=dtype,
55
- safety_checker=None,
56
- ).to(device)
57
-
58
- # 🔑 FORCE tokenizer + text encoder
59
- pipe_txt2img.tokenizer = CLIPTokenizer.from_pretrained(
60
- MODEL_ID, subfolder="tokenizer"
61
- )
62
- pipe_txt2img.text_encoder = CLIPTextModel.from_pretrained(
63
- MODEL_ID,
64
- subfolder="text_encoder",
65
- torch_dtype=dtype,
66
- ).to(device)
67
-
68
- # Scheduler
69
- pipe_txt2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
70
- pipe_txt2img.scheduler.config
71
- )
72
-
73
- # Memory optimisations
74
- try:
75
- pipe_txt2img.enable_attention_slicing()
76
- pipe_txt2img.enable_vae_slicing()
77
- except Exception:
78
- pass
79
-
80
- try:
81
- pipe_txt2img.enable_xformers_memory_efficient_attention()
82
- except Exception:
83
- pass
84
-
85
- pipe_txt2img.set_progress_bar_config(disable=True)
86
-
87
- # Img2Img pipeline (share components)
88
- pipe_img2img = StableDiffusionImg2ImgPipeline(**pipe_txt2img.components).to(device)
89
- pipe_img2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
90
- pipe_img2img.scheduler.config
91
- )
92
 
93
- # Defensive checks
94
- assert pipe_txt2img.tokenizer is not None
95
- assert pipe_txt2img.text_encoder is not None
96
 
97
- model_loaded = True
 
 
 
98
 
99
- except Exception as e:
100
- load_error = repr(e)
101
- model_loaded = False
102
 
103
- # ============================================================
104
- # Helpers
105
- # ============================================================
106
- def _make_error_image(w, h):
107
- return Image.new("RGB", (w, h), (30, 30, 40))
108
 
109
  # ============================================================
110
- # Inference
111
  # ============================================================
 
112
  def infer(
113
  prompt,
114
  negative_prompt,
@@ -124,9 +104,6 @@ def infer(
124
  width = int(width)
125
  height = int(height)
126
 
127
- if not model_loaded:
128
- return _make_error_image(width, height), load_error
129
-
130
  if randomize_seed:
131
  seed = random.randint(0, MAX_SEED)
132
 
@@ -157,22 +134,15 @@ def infer(
157
 
158
  return image, f"Seed: {seed}"
159
 
160
- except Exception as e:
161
- return _make_error_image(width, height), str(e)
162
-
163
  finally:
164
  gc.collect()
165
- if device.type == "cuda":
166
- torch.cuda.empty_cache()
167
 
168
  # ============================================================
169
  # UI
170
  # ============================================================
171
  with gr.Blocks(title="Stable Diffusion (Unlearning Model)") as demo:
172
- gr.Markdown("## Stable Diffusion Generator")
173
-
174
- if not model_loaded:
175
- gr.Markdown(f"⚠️ **Model failed to load**\n\n{load_error}")
176
 
177
  prompt = gr.Textbox(label="Prompt", lines=2)
178
  init_image = gr.Image(label="Initial image (optional)", type="pil")
@@ -208,4 +178,4 @@ with gr.Blocks(title="Stable Diffusion (Unlearning Model)") as demo:
208
  outputs=[result, status],
209
  )
210
 
211
- demo.queue().launch(ssr_mode=False)
 
1
+ # ============================================================
2
+ # Hugging Face Spaces GPU app
3
+ # IMPORTANT:
4
+ # - spaces MUST be imported first
5
+ # - @spaces.GPU MUST be used directly
6
+ # ============================================================
7
+
8
+ import spaces # MUST be first, no try/except
9
+
10
  import os
11
  import random
12
  import gc
 
15
  import numpy as np
16
  from PIL import Image
17
 
 
 
 
 
 
 
 
18
  import torch
19
  from diffusers import (
20
  StableDiffusionPipeline,
 
34
  if HF_TOKEN:
35
  login(token=HF_TOKEN)
36
 
37
+ device = torch.device("cuda")
38
+ dtype = torch.float16
 
39
 
40
  MAX_SEED = np.iinfo(np.int32).max
41
+ MAX_IMAGE_SIZE = 1024
42
 
43
  pipe_txt2img = None
44
  pipe_img2img = None
 
 
45
 
46
  # ============================================================
47
+ # Load model (once at startup)
48
  # ============================================================
49
+ pipe_txt2img = StableDiffusionPipeline.from_pretrained(
50
+ MODEL_ID,
51
+ revision=REVISION,
52
+ torch_dtype=dtype,
53
+ safety_checker=None,
54
+ ).to(device)
55
+
56
+ # 🔑 Force tokenizer + text encoder (fixes tokenize None bug)
57
+ pipe_txt2img.tokenizer = CLIPTokenizer.from_pretrained(
58
+ MODEL_ID, subfolder="tokenizer"
59
+ )
60
+ pipe_txt2img.text_encoder = CLIPTextModel.from_pretrained(
61
+ MODEL_ID,
62
+ subfolder="text_encoder",
63
+ torch_dtype=dtype,
64
+ ).to(device)
65
+
66
+ # Scheduler
67
+ pipe_txt2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
68
+ pipe_txt2img.scheduler.config
69
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ # Memory optimisations (safe on Spaces)
72
+ pipe_txt2img.enable_attention_slicing()
73
+ pipe_txt2img.enable_vae_slicing()
74
 
75
+ try:
76
+ pipe_txt2img.enable_xformers_memory_efficient_attention()
77
+ except Exception:
78
+ pass
79
 
80
+ pipe_txt2img.set_progress_bar_config(disable=True)
 
 
81
 
82
+ # Img2Img pipeline (reuse components)
83
+ pipe_img2img = StableDiffusionImg2ImgPipeline(**pipe_txt2img.components).to(device)
84
+ pipe_img2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
85
+ pipe_img2img.scheduler.config
86
+ )
87
 
88
  # ============================================================
89
+ # GPU INFERENCE FUNCTION (Spaces requires this)
90
  # ============================================================
91
+ @spaces.GPU
92
  def infer(
93
  prompt,
94
  negative_prompt,
 
104
  width = int(width)
105
  height = int(height)
106
 
 
 
 
107
  if randomize_seed:
108
  seed = random.randint(0, MAX_SEED)
109
 
 
134
 
135
  return image, f"Seed: {seed}"
136
 
 
 
 
137
  finally:
138
  gc.collect()
139
+ torch.cuda.empty_cache()
 
140
 
141
  # ============================================================
142
  # UI
143
  # ============================================================
144
  with gr.Blocks(title="Stable Diffusion (Unlearning Model)") as demo:
145
+ gr.Markdown("## Stable Diffusion Generator (GPU)")
 
 
 
146
 
147
  prompt = gr.Textbox(label="Prompt", lines=2)
148
  init_image = gr.Image(label="Initial image (optional)", type="pil")
 
178
  outputs=[result, status],
179
  )
180
 
181
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)