freshcodestech commited on
Commit
9e64eec
·
verified ·
1 Parent(s): 9fd6a5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -48
app.py CHANGED
@@ -1,67 +1,76 @@
1
- import torch
2
- import gradio as gr
3
  from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
4
 
5
- # Step 1: Define models
6
- base_model_sdxl = "stabilityai/stable-diffusion-xl-base-1.0"
7
- lora_repo = "freshcodestech/LingoSpace"
8
- lora_file = "LingoSpace-10.safetensors"
 
9
 
10
- # Step 2: Load SDXL pipeline
 
 
 
 
 
 
 
 
 
 
11
  pipe = StableDiffusionXLPipeline.from_pretrained(
12
- base_model_sdxl,
13
- torch_dtype=torch.float16,
14
- use_safetensors=True
 
 
15
  )
16
-
17
- # Replace scheduler for better quality
18
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
 
19
 
20
- # Move to GPU
21
- pipe = pipe.to("cuda")
22
-
23
- # Step 3: Load LoRA weights
24
- pipe.load_lora_weights(lora_repo, weight_name=lora_file)
25
 
26
- # LoRA scaling factor (strength)
27
- lora_strength = 0.8 # Try 0.6–1.0
 
 
28
 
29
- # Step 4: Fixed prompts
30
- prompts = [
31
- "A cartoon raccoon in a space suit with some rice food, highly detailed, vibrant, futuristic art style",
32
- "A cartoon bear in a space suit with some meat food, cinematic lighting, sci-fi background",
33
- "A cartoon giraffe in a space suit with some pizza food, whimsical, colorful, space kitchen scene"
34
- ]
35
 
36
- # Step 5: Generation settings
37
- num_steps = 30 # inference steps
38
- cfg = 7.5 # classifier-free guidance scale
39
- seed = 12345 # base seed for reproducibility
 
40
 
41
- # Inference function (returns list of images)
42
  def generate_images():
43
- results = []
44
- for idx, prompt in enumerate(prompts):
45
- generator = torch.manual_seed(seed + idx) # unique seed for each prompt
46
- image = pipe(
47
- prompt,
48
- num_inference_steps=num_steps,
49
- guidance_scale=cfg,
50
- cross_attention_kwargs={"scale": lora_strength},
51
- generator=generator,
52
- height=1024,
53
- width=1024
54
- ).images[0]
55
- results.append(image)
56
- return results
57
 
58
- # Step 6: Gradio UI
59
  demo = gr.Interface(
60
  fn=generate_images,
61
  inputs=None,
62
- outputs=[gr.Image(label=f"Image {i+1}") for i in range(len(prompts))],
63
- title="SDXL + LoRA: Cartoon Animals in Space Suits",
64
- description="Generates fixed set of images using LoRA fine-tuning."
65
  )
66
 
67
  if __name__ == "__main__":
 
1
+ # app.py
2
+ import torch, gradio as gr
3
  from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
4
 
5
+ # Models
6
+ BASE = "stabilityai/stable-diffusion-xl-base-1.0"
7
+ LORA_REPO = "freshcodestech/LingoSpace"
8
+ LORA_FILE = "LingoSpace-10.safetensors"
9
+ LORA_STRENGTH = 0.8 # 0.6–1.0
10
 
11
+ PROMPTS = [
12
+ "A cartoon raccoon in a space suit with some rice food, highly detailed, vibrant, futuristic art style",
13
+ "A cartoon bear in a space suit with some meat food, cinematic lighting, sci-fi background",
14
+ "A cartoon giraffe in a space suit with some pizza food, whimsical, colorful, space kitchen scene",
15
+ ]
16
+
17
+ # Device / dtype
18
+ device = "cuda" if torch.cuda.is_available() else "cpu"
19
+ dtype = torch.float16 if device == "cuda" else torch.float32
20
+
21
+ # Load SDXL (disable watermarker to avoid extra deps on Spaces)
22
  pipe = StableDiffusionXLPipeline.from_pretrained(
23
+ BASE,
24
+ torch_dtype=dtype,
25
+ use_safetensors=True,
26
+ add_watermarker=False,
27
+ **({"variant": "fp16"} if device == "cuda" else {})
28
  )
 
 
29
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
30
+ pipe = pipe.to(device)
31
 
32
+ # Load LoRA from Hub
33
+ pipe.load_lora_weights(LORA_REPO, weight_name=LORA_FILE)
 
 
 
34
 
35
+ # If your diffusers supports adapter weights:
36
+ if hasattr(pipe, "set_adapters"):
37
+ # (adapter name is "default" when not specified in load_lora_weights)
38
+ pipe.set_adapters("default", weight=LORA_STRENGTH)
39
 
40
+ # Memory helpers
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
+ # Gen settings (smaller & fewer steps on CPU so it doesn’t crawl)
48
+ STEPS = 30 if device == "cuda" else 15
49
+ CFG = 7.5
50
+ H = 1024 if device == "cuda" else 768
51
+ W = 1024 if device == "cuda" else 768
52
 
 
53
  def generate_images():
54
+ images = []
55
+ for i, p in enumerate(PROMPTS):
56
+ out = pipe(
57
+ p,
58
+ num_inference_steps=STEPS,
59
+ guidance_scale=CFG,
60
+ height=H,
61
+ width=W,
62
+ # Works even if set_adapters isn't available (older diffusers):
63
+ cross_attention_kwargs={"scale": LORA_STRENGTH},
64
+ )
65
+ images.append(out.images[0])
66
+ return images
 
67
 
 
68
  demo = gr.Interface(
69
  fn=generate_images,
70
  inputs=None,
71
+ outputs=[gr.Image(label=f"Image {i+1}") for i in range(len(PROMPTS))],
72
+ title="SDXL + LoRA: LingoSpace",
73
+ description=f"Device: {device.upper()} | Base: SDXL | LoRA: {LORA_REPO}/{LORA_FILE}",
74
  )
75
 
76
  if __name__ == "__main__":