aanchal77 commited on
Commit
15b9c89
Β·
verified Β·
1 Parent(s): 39310c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
  # --- Configuration ---
6
- HF_REPO_ID = "aanchal77/Final-One"
7
  BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
8
 
9
  # --- Define Available LoRAs ---
@@ -31,14 +31,20 @@ dtype = torch.float16 if device == "cuda" else torch.float32
31
  print(f"Using device: {device}")
32
 
33
  print(f"🎨 Loading base model: {BASE_MODEL_ID}")
34
- pipe = StableDiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=dtype).to(device)
 
 
 
 
 
35
  if device == "cpu":
36
  pipe.enable_attention_slicing()
37
 
38
  # --- The Core Generation Function ---
39
  def generate(prompt, quality, lora_choice):
 
40
  pipe.unload_lora_weights()
41
-
42
  lora_subfolder = AVAILABLE_LORAS.get(lora_choice)
43
 
44
  if lora_subfolder:
@@ -47,7 +53,7 @@ def generate(prompt, quality, lora_choice):
47
  pipe.load_lora_weights(
48
  HF_REPO_ID,
49
  subfolder=lora_subfolder,
50
- weight_name="adapter_model.safetensors" # βœ… Explicit LoRA file
51
  )
52
  except Exception as e:
53
  print(f"❌ Failed to load LoRA from Hub '{HF_REPO_ID}/{lora_subfolder}': {e}")
@@ -56,16 +62,23 @@ def generate(prompt, quality, lora_choice):
56
 
57
  steps = 25 if quality == "Fast" else 40
58
  guidance_scale = 7.5
59
-
60
  print(f"πŸš€ Generating with prompt: '{prompt}'")
61
  with torch.no_grad():
62
- image = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance_scale).images[0]
63
-
 
 
 
 
64
  return image
65
 
66
  # --- Build the Gradio UI ---
67
  title = f"🎨 Stable Diffusion Gallery from {HF_REPO_ID}"
68
- description = "Select a trained LoRA model from your Hugging Face repository to apply its style. The first time you select a LoRA, it may take a moment to download."
 
 
 
69
 
70
  demo = gr.Interface(
71
  fn=generate,
@@ -74,7 +87,7 @@ demo = gr.Interface(
74
  gr.Dropdown(["Fast", "High Quality"], value="Fast", label="Generation Quality"),
75
  gr.Dropdown(
76
  choices=list(AVAILABLE_LORAS.keys()),
77
- value="None (Base Model)",
78
  label="Select a Trained LoRA Model"
79
  )
80
  ],
@@ -84,7 +97,8 @@ demo = gr.Interface(
84
  examples=[
85
  ["A portrait of an astronaut, cinematic lighting, by vincent van gogh", "Fast", "Artist: Vincent van Gogh"],
86
  ["A peaceful village in the mountains, impressionism style", "High Quality", "Style: Impressionism"],
87
- ]
 
88
  )
89
 
90
  demo.launch()
 
1
  import gradio as gr
2
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
3
  import torch
4
 
5
  # --- Configuration ---
6
+ HF_REPO_ID = "aanchal77/Final-One"
7
  BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
8
 
9
  # --- Define Available LoRAs ---
 
31
  print(f"Using device: {device}")
32
 
33
  print(f"🎨 Loading base model: {BASE_MODEL_ID}")
34
+ pipe = StableDiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=dtype)
35
+
36
+ # πŸ”§ Replace the fragile PNDM scheduler with a robust one to avoid index/NoneType errors
37
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
38
+
39
+ pipe = pipe.to(device)
40
  if device == "cpu":
41
  pipe.enable_attention_slicing()
42
 
43
  # --- The Core Generation Function ---
44
  def generate(prompt, quality, lora_choice):
45
+ # Reset to base weights
46
  pipe.unload_lora_weights()
47
+
48
  lora_subfolder = AVAILABLE_LORAS.get(lora_choice)
49
 
50
  if lora_subfolder:
 
53
  pipe.load_lora_weights(
54
  HF_REPO_ID,
55
  subfolder=lora_subfolder,
56
+ weight_name="adapter_model.safetensors" # ensure exact file
57
  )
58
  except Exception as e:
59
  print(f"❌ Failed to load LoRA from Hub '{HF_REPO_ID}/{lora_subfolder}': {e}")
 
62
 
63
  steps = 25 if quality == "Fast" else 40
64
  guidance_scale = 7.5
65
+
66
  print(f"πŸš€ Generating with prompt: '{prompt}'")
67
  with torch.no_grad():
68
+ image = pipe(
69
+ prompt,
70
+ num_inference_steps=steps,
71
+ guidance_scale=guidance_scale
72
+ ).images[0]
73
+
74
  return image
75
 
76
  # --- Build the Gradio UI ---
77
  title = f"🎨 Stable Diffusion Gallery from {HF_REPO_ID}"
78
+ description = (
79
+ "Select a trained LoRA model from your Hugging Face repository to apply its style. "
80
+ "The first time you select a LoRA, it may take a moment to download."
81
+ )
82
 
83
  demo = gr.Interface(
84
  fn=generate,
 
87
  gr.Dropdown(["Fast", "High Quality"], value="Fast", label="Generation Quality"),
88
  gr.Dropdown(
89
  choices=list(AVAILABLE_LORAS.keys()),
90
+ value="None (Base Model)",
91
  label="Select a Trained LoRA Model"
92
  )
93
  ],
 
97
  examples=[
98
  ["A portrait of an astronaut, cinematic lighting, by vincent van gogh", "Fast", "Artist: Vincent van Gogh"],
99
  ["A peaceful village in the mountains, impressionism style", "High Quality", "Style: Impressionism"],
100
+ ],
101
+ cache_examples=False, # πŸ”’ prevent startup 500s if an example errors
102
  )
103
 
104
  demo.launch()