File size: 3,663 Bytes
7bf5221
15b9c89
7bf5221
 
39ecd57
15b9c89
39ecd57
 
39310c4
39ecd57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bf5221
39ecd57
7bf5221
39ecd57
 
 
 
 
 
15b9c89
 
 
 
 
 
39ecd57
 
 
 
 
15b9c89
39ecd57
15b9c89
39ecd57
 
 
 
 
61ee5f7
 
 
15b9c89
61ee5f7
39ecd57
 
 
 
 
 
 
15b9c89
39ecd57
 
15b9c89
 
 
 
 
 
39ecd57
 
 
 
15b9c89
 
 
 
39ecd57
 
 
 
 
 
 
 
15b9c89
39ecd57
 
 
 
 
 
 
 
 
15b9c89
 
39ecd57
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gradio as gr
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import torch

# --- Configuration ---
HF_REPO_ID = "aanchal77/Final-One"
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"

# --- Define Available LoRAs ---
AVAILABLE_LORAS = {
    "None (Base Model)": None,
    # --- Artists ---
    "Artist: Vincent van Gogh": "artists/Vincent_van_Gogh",
    "Artist: Claude Monet": "artists/Claude_Monet",
    "Artist: Rembrandt": "artists/Rembrandt",
    "Artist: Pablo Picasso": "artists/Pablo_Picasso",
    # --- Styles ---
    "Style: Impressionism": "styles/Impressionism",
    "Style: Baroque": "styles/Baroque",
    "Style: Cubism": "styles/Cubism",
    "Style: Abstract Expressionism": "styles/Abstract_Expressionism",
    "Style: Romanticism": "styles/Romanticism",
    "Style: Realism": "styles/Realism",
    "Style: Post Impressionism": "styles/Post_Impressionism",
}
print("βœ… LoRA models from your Hugging Face repo are configured.")

# --- Setup ---
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
print(f"Using device: {device}")

print(f"🎨 Loading base model: {BASE_MODEL_ID}")
pipe = StableDiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=dtype)

# πŸ”§ Replace the fragile PNDM scheduler with a robust one to avoid index/NoneType errors
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)

pipe = pipe.to(device)
if device == "cpu":
    pipe.enable_attention_slicing()

# --- The Core Generation Function ---
def generate(prompt, quality, lora_choice):
    # Reset to base weights
    pipe.unload_lora_weights()

    lora_subfolder = AVAILABLE_LORAS.get(lora_choice)

    if lora_subfolder:
        print(f"✨ Downloading and applying LoRA: {lora_choice}")
        try:
            pipe.load_lora_weights(
                HF_REPO_ID,
                subfolder=lora_subfolder,
                weight_name="adapter_model.safetensors"  # ensure exact file
            )
        except Exception as e:
            print(f"❌ Failed to load LoRA from Hub '{HF_REPO_ID}/{lora_subfolder}': {e}")
    else:
        print("🎨 Using base model (no LoRA selected)")

    steps = 25 if quality == "Fast" else 40
    guidance_scale = 7.5

    print(f"πŸš€ Generating with prompt: '{prompt}'")
    with torch.no_grad():
        image = pipe(
            prompt,
            num_inference_steps=steps,
            guidance_scale=guidance_scale
        ).images[0]

    return image

# --- Build the Gradio UI ---
title = f"🎨 Stable Diffusion Gallery from {HF_REPO_ID}"
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."
)

demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Enter your prompt", placeholder="A beautiful painting of a fantasy landscape..."),
        gr.Dropdown(["Fast", "High Quality"], value="Fast", label="Generation Quality"),
        gr.Dropdown(
            choices=list(AVAILABLE_LORAS.keys()),
            value="None (Base Model)",
            label="Select a Trained LoRA Model"
        )
    ],
    outputs=gr.Image(label="Generated Image"),
    title=title,
    description=description,
    examples=[
        ["A portrait of an astronaut, cinematic lighting, by vincent van gogh", "Fast", "Artist: Vincent van Gogh"],
        ["A peaceful village in the mountains, impressionism style", "High Quality", "Style: Impressionism"],
    ],
    cache_examples=False,   # πŸ”’ prevent startup 500s if an example errors
)

demo.launch()