freshcodestech commited on
Commit
d36e3ce
·
verified ·
1 Parent(s): f2d2936

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -1,15 +1,15 @@
1
- # app.py — SDXL + LoRA from Hugging Face (PEFT backend)
2
  import torch, gradio as gr
3
  from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
4
 
5
  # --- Config ---
6
  BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
7
- LORA_REPO = "freshcodestech/LingoSpace" # your HF LoRA repo
8
- LORA_FILE = "LingoSpace-10.safetensors" # exact filename in that repo
9
- ADAPTER = "lingospace" # local nickname; can be any safe string
10
- LORA_W = 0.8 # LoRA strength (0.6–1.0 is typical)
11
 
12
- # Device / dtype (GPU preferred)
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
  dtype = torch.float16 if device == "cuda" else torch.float32
15
 
@@ -24,8 +24,7 @@ pipe = StableDiffusionXLPipeline.from_pretrained(
24
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
25
  pipe = pipe.to(device)
26
 
27
- # --- Load LoRA from the Hub via PEFT backend ---
28
- # NOTE: This requires `peft` to be installed (see requirements.txt below).
29
  pipe.load_lora_weights(
30
  LORA_REPO,
31
  weight_name=LORA_FILE,
@@ -33,26 +32,44 @@ pipe.load_lora_weights(
33
  use_peft_backend=True
34
  )
35
 
36
- # Preferred scaling API (if available)
 
37
  if hasattr(pipe, "set_adapters"):
38
- pipe.set_adapters(ADAPTER, weight=LORA_W)
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Memory helpers (helpful on T4 small)
41
  if device == "cuda":
42
  if hasattr(pipe, "enable_xformers_memory_efficient_attention"):
43
  pipe.enable_xformers_memory_efficient_attention()
44
  pipe.enable_attention_slicing()
45
  pipe.enable_vae_tiling()
46
 
47
- # --- Inference ---
 
 
 
 
48
  def infer(prompt, steps, cfg, h, w, lora_strength):
49
- # Fallback strength control for older diffusers if set_adapters doesn’t exist:
50
  kwargs = {}
51
- if not hasattr(pipe, "set_adapters"):
52
  kwargs["cross_attention_kwargs"] = {"scale": float(lora_strength)}
53
  else:
54
- # If adapter API exists, update weight dynamically
55
- pipe.set_adapters(ADAPTER, weight=float(lora_strength))
 
 
 
56
 
57
  out = pipe(
58
  prompt,
@@ -64,12 +81,11 @@ def infer(prompt, steps, cfg, h, w, lora_strength):
64
  )
65
  return out.images[0]
66
 
67
- DEFAULT_PROMPT = "lingospace, cartoon, 2d illustration, flat colors, clean lineart, a raccoon in a compact utility space suit, holding a steaming bowl of rice, stars and soft nebula, cozy composition"
68
-
69
- # Use smaller defaults on CPU for speed
70
- DEF_STEPS = 30 if device == "cuda" else 15
71
- DEF_H = 1024 if device == "cuda" else 768
72
- DEF_W = 1024 if device == "cuda" else 768
73
 
74
  demo = gr.Interface(
75
  fn=infer,
@@ -83,7 +99,7 @@ demo = gr.Interface(
83
  ],
84
  outputs=gr.Image(label="Output"),
85
  title="SDXL + LoRA (freshcodestech/LingoSpace)",
86
- description=f"Device: {device.upper()} | Base: {BASE_MODEL} | LoRA: {LORA_REPO}/{LORA_FILE}"
87
  )
88
 
89
  if __name__ == "__main__":
 
1
+ # app.py — SDXL + LoRA from Hub (PEFT) with version-safe adapter weighting
2
  import torch, gradio as gr
3
  from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
4
 
5
  # --- Config ---
6
  BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
7
+ LORA_REPO = "freshcodestech/LingoSpace" # your HF LoRA repo
8
+ LORA_FILE = "LingoSpace-10.safetensors" # exact filename in that repo
9
+ ADAPTER = "lingospace" # local nickname; any safe string
10
+ LORA_W = 0.8 # 0.6–1.0 typical
11
 
12
+ # Device / dtype
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
  dtype = torch.float16 if device == "cuda" else torch.float32
15
 
 
24
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
25
  pipe = pipe.to(device)
26
 
27
+ # --- Load LoRA from Hub via PEFT backend ---
 
28
  pipe.load_lora_weights(
29
  LORA_REPO,
30
  weight_name=LORA_FILE,
 
32
  use_peft_backend=True
33
  )
34
 
35
+ # Try all known set_adapters signatures; else fall back to per-call scaling
36
+ USE_RUNTIME_SCALE = False
37
  if hasattr(pipe, "set_adapters"):
38
+ try:
39
+ # Newer diffusers: keyword
40
+ pipe.set_adapters(ADAPTER, adapter_weights=LORA_W)
41
+ except TypeError:
42
+ try:
43
+ # Older diffusers: 2 positional lists
44
+ pipe.set_adapters([ADAPTER], [LORA_W])
45
+ except Exception:
46
+ USE_RUNTIME_SCALE = True
47
+ else:
48
+ USE_RUNTIME_SCALE = True
49
 
50
+ # Memory helpers (T4)
51
  if device == "cuda":
52
  if hasattr(pipe, "enable_xformers_memory_efficient_attention"):
53
  pipe.enable_xformers_memory_efficient_attention()
54
  pipe.enable_attention_slicing()
55
  pipe.enable_vae_tiling()
56
 
57
+ # Defaults (smaller on CPU)
58
+ DEF_STEPS = 30 if device == "cuda" else 15
59
+ DEF_H = 1024 if device == "cuda" else 768
60
+ DEF_W = 1024 if device == "cuda" else 768
61
+
62
  def infer(prompt, steps, cfg, h, w, lora_strength):
63
+ # If we couldn’t set adapter weight globally, pass strength per call
64
  kwargs = {}
65
+ if USE_RUNTIME_SCALE:
66
  kwargs["cross_attention_kwargs"] = {"scale": float(lora_strength)}
67
  else:
68
+ # If API exists, update weight dynamically per request
69
+ try:
70
+ pipe.set_adapters(ADAPTER, adapter_weights=float(lora_strength))
71
+ except TypeError:
72
+ pipe.set_adapters([ADAPTER], [float(lora_strength)])
73
 
74
  out = pipe(
75
  prompt,
 
81
  )
82
  return out.images[0]
83
 
84
+ DEFAULT_PROMPT = (
85
+ "lingospace, cartoon, 2d illustration, flat colors, clean lineart, "
86
+ "a raccoon in a compact utility space suit, holding a steaming bowl of rice, "
87
+ "stars and soft nebula, cozy composition"
88
+ )
 
89
 
90
  demo = gr.Interface(
91
  fn=infer,
 
99
  ],
100
  outputs=gr.Image(label="Output"),
101
  title="SDXL + LoRA (freshcodestech/LingoSpace)",
102
+ description=f"Device: {device.upper()} • Base: {BASE_MODEL} • LoRA: {LORA_REPO}/{LORA_FILE}"
103
  )
104
 
105
  if __name__ == "__main__":