Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,10 @@ from diffusers import StableDiffusionPipeline
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# --- Configuration ---
|
| 6 |
-
# Your Hugging Face repository ID where the LoRAs are stored
|
| 7 |
HF_REPO_ID = "aanchal77/Final-One"
|
| 8 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 9 |
|
| 10 |
-
# --- Define Available LoRAs
|
| 11 |
-
# The key is the display name in the dropdown.
|
| 12 |
-
# The value is the subfolder path inside your Hugging Face repository.
|
| 13 |
AVAILABLE_LORAS = {
|
| 14 |
"None (Base Model)": None,
|
| 15 |
# --- Artists ---
|
|
@@ -33,43 +30,13 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 33 |
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 34 |
print(f"Using device: {device}")
|
| 35 |
|
| 36 |
-
# --- Load the Base Model ---
|
| 37 |
-
# This will be cached in the Space for faster startups.
|
| 38 |
print(f"π¨ Loading base model: {BASE_MODEL_ID}")
|
| 39 |
pipe = StableDiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=dtype).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 |
-
# """
|
| 46 |
-
# Generates an image, dynamically loading the selected LoRA from the Hub.
|
| 47 |
-
# """
|
| 48 |
-
# # Unload any existing LoRA to reset to the base model
|
| 49 |
-
# pipe.unload_lora_weights()
|
| 50 |
-
|
| 51 |
-
# lora_subfolder = AVAILABLE_LORAS.get(lora_choice)
|
| 52 |
-
|
| 53 |
-
# if lora_subfolder:
|
| 54 |
-
# print(f"β¨ Downloading and applying LoRA: {lora_choice}")
|
| 55 |
-
# try:
|
| 56 |
-
# # Load LoRA directly from the Hugging Face Hub
|
| 57 |
-
# pipe.load_lora_weights(HF_REPO_ID, subfolder=lora_subfolder)
|
| 58 |
-
# except Exception as e:
|
| 59 |
-
# print(f"β Failed to load LoRA from Hub '{HF_REPO_ID}/{lora_subfolder}': {e}")
|
| 60 |
-
# else:
|
| 61 |
-
# print("π¨ Using base model (no LoRA selected)")
|
| 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(prompt, num_inference_steps=steps, guidance_scale=guidance_scale).images[0]
|
| 69 |
-
|
| 70 |
-
# return image
|
| 71 |
def generate(prompt, quality, lora_choice):
|
| 72 |
-
# Reset to base model
|
| 73 |
pipe.unload_lora_weights()
|
| 74 |
|
| 75 |
lora_subfolder = AVAILABLE_LORAS.get(lora_choice)
|
|
@@ -80,7 +47,7 @@ def generate(prompt, quality, lora_choice):
|
|
| 80 |
pipe.load_lora_weights(
|
| 81 |
HF_REPO_ID,
|
| 82 |
subfolder=lora_subfolder,
|
| 83 |
-
weight_name="adapter_model.safetensors" #
|
| 84 |
)
|
| 85 |
except Exception as e:
|
| 86 |
print(f"β Failed to load LoRA from Hub '{HF_REPO_ID}/{lora_subfolder}': {e}")
|
|
@@ -96,7 +63,6 @@ def generate(prompt, quality, lora_choice):
|
|
| 96 |
|
| 97 |
return image
|
| 98 |
|
| 99 |
-
|
| 100 |
# --- Build the Gradio UI ---
|
| 101 |
title = f"π¨ Stable Diffusion Gallery from {HF_REPO_ID}"
|
| 102 |
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."
|
|
|
|
| 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 ---
|
|
|
|
|
|
|
| 10 |
AVAILABLE_LORAS = {
|
| 11 |
"None (Base Model)": None,
|
| 12 |
# --- Artists ---
|
|
|
|
| 30 |
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)
|
|
|
|
| 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}")
|
|
|
|
| 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."
|