Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
@@ -1,154 +1,132 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
|
5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
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 |
image = pipe(
|
42 |
-
prompt=
|
43 |
negative_prompt=negative_prompt,
|
44 |
-
guidance_scale=guidance_scale,
|
45 |
-
num_inference_steps=num_inference_steps,
|
46 |
width=width,
|
47 |
height=height,
|
|
|
|
|
48 |
generator=generator,
|
|
|
49 |
).images[0]
|
50 |
|
51 |
-
return image
|
52 |
-
|
53 |
-
|
54 |
-
examples = [
|
55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
-
"An astronaut riding a green horse",
|
57 |
-
"A delicious ceviche cheesecake slice",
|
58 |
-
]
|
59 |
-
|
60 |
-
css = """
|
61 |
-
#col-container {
|
62 |
-
margin: 0 auto;
|
63 |
-
max-width: 640px;
|
64 |
-
}
|
65 |
-
"""
|
66 |
-
|
67 |
-
with gr.Blocks(css=css) as demo:
|
68 |
-
with gr.Column(elem_id="col-container"):
|
69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
70 |
-
|
71 |
-
with gr.Row():
|
72 |
-
prompt = gr.Text(
|
73 |
-
label="Prompt",
|
74 |
-
show_label=False,
|
75 |
-
max_lines=1,
|
76 |
-
placeholder="Enter your prompt",
|
77 |
-
container=False,
|
78 |
-
)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
max_lines=1,
|
88 |
-
placeholder="Enter a negative prompt",
|
89 |
-
visible=False,
|
90 |
-
)
|
91 |
-
|
92 |
-
seed = gr.Slider(
|
93 |
-
label="Seed",
|
94 |
-
minimum=0,
|
95 |
-
maximum=MAX_SEED,
|
96 |
-
step=1,
|
97 |
-
value=0,
|
98 |
-
)
|
99 |
-
|
100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
|
|
|
|
|
|
|
|
|
|
|
102 |
with gr.Row():
|
103 |
-
|
104 |
-
label="
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024, # Replace with defaults that work for your model
|
117 |
-
)
|
118 |
|
119 |
with gr.Row():
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
-
guidance_scale,
|
148 |
-
num_inference_steps,
|
149 |
-
],
|
150 |
-
outputs=[result, seed],
|
151 |
)
|
152 |
|
153 |
-
|
154 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
+
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# --- Configuration ---
|
7 |
+
# The base model your LoRA was trained on.
|
8 |
+
base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
9 |
+
|
10 |
+
# The path to your LoRA file on the Hugging Face Hub.
|
11 |
+
lora_repo_id = "TuringsSolutions/EmilyH"
|
12 |
+
lora_filename = "emilyh.safetensors"
|
13 |
+
|
14 |
+
# --- Load the Pipeline ---
|
15 |
+
# Use a recommended VAE for SDXL
|
16 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
17 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
18 |
+
base_model_id,
|
19 |
+
vae=vae,
|
20 |
+
torch_dtype=torch.float16,
|
21 |
+
variant="fp16",
|
22 |
+
use_safetensors=True
|
23 |
+
)
|
24 |
+
|
25 |
+
# --- Load and Fuse the LoRA ---
|
26 |
+
# Download the LoRA file and load the state dict.
|
27 |
+
lora_file_path = hf_hub_download(repo_id=lora_repo_id, filename=lora_filename)
|
28 |
+
pipe.load_lora_weights(lora_file_path)
|
29 |
+
|
30 |
+
# It's recommended to fuse the LoRA weights for better performance,
|
31 |
+
# but this is optional. You can also use pipe.set_adapters(["default"], adapter_weights=[0.9])
|
32 |
+
# during inference if you prefer more dynamic control.
|
33 |
+
# pipe.fuse_lora(lora_scale=0.9) # Fusing is more efficient
|
34 |
+
|
35 |
+
# Move the pipeline to the GPU
|
36 |
+
pipe.to("cuda")
|
37 |
+
|
38 |
+
# --- Default Settings from your Recommendations ---
|
39 |
+
# These are pulled directly from your "Recomendations.txt".
|
40 |
+
default_positive_prompt = "masterpiece, best quality, ultra-detailed, realistic skin, intricate details, highres" #
|
41 |
+
default_negative_prompt = "low quality, worst quality, blurry, (deformed:1.3), extra fingers, cartoon, 3d, anime, bad anatomy" #
|
42 |
+
default_sampler = "DPM++ 2M Karras" #
|
43 |
+
default_cfg = 6.0 #
|
44 |
+
default_steps = 30 #
|
45 |
+
trigger_word = "emilyh" #
|
46 |
+
lora_tag_main = "<lora:emilyh:0.9>" #
|
47 |
+
|
48 |
+
|
49 |
+
# --- Define the Inference Function ---
|
50 |
+
def generate_image(prompt, negative_prompt, sampler, steps, cfg, width, height, seed):
|
51 |
+
"""
|
52 |
+
Function to generate an image based on user inputs.
|
53 |
+
"""
|
54 |
+
# Combine the user prompt with the trigger word and LoRA tag
|
55 |
+
full_prompt = f"{lora_tag_main}, {trigger_word}, {prompt}"
|
56 |
+
|
57 |
+
# Set the scheduler (sampler)
|
58 |
+
if sampler == "DPM++ 2M Karras":
|
59 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True)
|
60 |
+
elif sampler == "DPM++ SDE Karras":
|
61 |
+
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
62 |
+
else: # Default to DPM++ 2M Karras
|
63 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True)
|
64 |
+
|
65 |
+
# Set seed for reproducibility
|
66 |
+
generator = torch.Generator("cuda").manual_seed(seed) if seed != -1 else None
|
67 |
+
|
68 |
+
# Generate the image
|
69 |
image = pipe(
|
70 |
+
prompt=full_prompt,
|
71 |
negative_prompt=negative_prompt,
|
|
|
|
|
72 |
width=width,
|
73 |
height=height,
|
74 |
+
guidance_scale=cfg,
|
75 |
+
num_inference_steps=steps,
|
76 |
generator=generator,
|
77 |
+
cross_attention_kwargs={"scale": 0.9} # This is an alternative way to apply LoRA scale if not fused
|
78 |
).images[0]
|
79 |
|
80 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
# --- Create the Gradio Interface ---
|
83 |
+
with gr.Blocks(css="style.css") as demo:
|
84 |
+
gr.Markdown("# `emilyh` LoRA Image Generator")
|
85 |
+
gr.Markdown(
|
86 |
+
"A Gradio interface for the `emilyh` LoRA. "
|
87 |
+
"Based on the recommendations provided. "
|
88 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
prompt = gr.Textbox(label="Positive Prompt", value=default_positive_prompt, lines=3)
|
93 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
94 |
+
|
95 |
with gr.Row():
|
96 |
+
sampler = gr.Radio(
|
97 |
+
label="Sampler",
|
98 |
+
choices=["DPM++ 2M Karras", "DPM++ SDE Karras"],
|
99 |
+
value=default_sampler,
|
100 |
+
) #
|
101 |
+
steps = gr.Slider(label="Steps", minimum=15, maximum=50, value=default_steps, step=1) #
|
102 |
+
|
103 |
+
cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=10.0, value=default_cfg, step=0.5) #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
with gr.Row():
|
106 |
+
width = gr.Slider(label="Width", minimum=512, maximum=1024, value=1024, step=64)
|
107 |
+
height = gr.Slider(label="Height", minimum=512, maximum=1024, value=1024, step=64)
|
108 |
+
|
109 |
+
seed = gr.Slider(label="Seed", minimum=-1, maximum=999999999, step=1, value=-1, info="Use -1 for a random seed.")
|
110 |
+
|
111 |
+
generate_button = gr.Button("Generate Image", variant="primary")
|
112 |
+
|
113 |
+
with gr.Column():
|
114 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
115 |
+
gr.Markdown(
|
116 |
+
"""
|
117 |
+
### 🔧 Usage Guide
|
118 |
+
* The trigger word `emilyh` and the LoRA tag `<lora:emilyh:0.9>` are automatically added to your prompt.
|
119 |
+
* For best results, generate images in batches and choose the most consistent ones.
|
120 |
+
* The LoRA captures the subject's appearance well across various poses and outfits.
|
121 |
+
* A weight of 0.9 provides a good balance of likeness and flexibility. Using a weight closer to 1.0 can increase consistency but may cause stiffness.
|
122 |
+
* This interface does not include ADetailer, which is recommended for final face refinement.
|
123 |
+
"""
|
124 |
+
)
|
125 |
+
|
126 |
+
generate_button.click(
|
127 |
+
fn=generate_image,
|
128 |
+
inputs=[prompt, negative_prompt, sampler, steps, cfg, width, height, seed],
|
129 |
+
outputs=output_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
)
|
131 |
|
132 |
+
demo.launch()
|
|