Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,9 @@ import torch
|
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
print("--- FINAL VERSION:
|
| 7 |
|
| 8 |
# --- Step 1: Define constants ---
|
| 9 |
-
# We revert to the neutral v1.5 base model to let the LoRA style dominate.
|
| 10 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 11 |
LORA_FILENAME = "MyStickmanProject-10.safetensors"
|
| 12 |
|
|
@@ -22,10 +21,10 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
| 22 |
print("Base model loaded successfully.")
|
| 23 |
|
| 24 |
# --- Step 3: Load and apply the LoRA weights ---
|
| 25 |
-
|
|
|
|
| 26 |
pipe.load_lora_weights(LORA_FILENAME)
|
| 27 |
-
|
| 28 |
-
print("LoRA weights fused successfully.")
|
| 29 |
|
| 30 |
|
| 31 |
# --- Step 4: Move to CPU and optimize ---
|
|
@@ -36,26 +35,35 @@ pipe.enable_attention_slicing()
|
|
| 36 |
print("--- MODEL SETUP COMPLETE ---")
|
| 37 |
|
| 38 |
# --- Step 5: Define the core generation function ---
|
| 39 |
-
def generate_image(prompt, negative_prompt, guidance_scale, num_steps):
|
| 40 |
print(f"Generating image with prompt: {prompt}")
|
|
|
|
|
|
|
|
|
|
| 41 |
image = pipe(
|
| 42 |
prompt=prompt,
|
| 43 |
negative_prompt=negative_prompt,
|
| 44 |
num_inference_steps=int(num_steps),
|
| 45 |
guidance_scale=float(guidance_scale),
|
|
|
|
| 46 |
).images[0]
|
|
|
|
| 47 |
print("Image generation complete.")
|
| 48 |
return image
|
| 49 |
|
| 50 |
# --- Step 6: Create the Gradio user interface ---
|
| 51 |
with gr.Blocks() as iface:
|
| 52 |
-
gr.Markdown("# My Custom
|
| 53 |
-
gr.Markdown("Enter a prompt to generate an image in my unique
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column(scale=70):
|
| 57 |
-
prompt_input = gr.Textbox(label="Prompt", placeholder="A stickman
|
| 58 |
-
negative_prompt_input = gr.Textbox(label="Negative Prompt", value="(worst quality, low quality:1.4), blurry, noisy, grainy,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
guidance_scale_slider = gr.Slider(minimum=1, maximum=20, step=0.5, value=7.5, label="Guidance Scale")
|
| 61 |
steps_slider = gr.Slider(minimum=10, maximum=100, step=1, value=25, label="Inference Steps")
|
|
@@ -66,7 +74,7 @@ with gr.Blocks() as iface:
|
|
| 66 |
submit_button = gr.Button("Generate", variant="primary")
|
| 67 |
submit_button.click(
|
| 68 |
fn=generate_image,
|
| 69 |
-
inputs=[prompt_input, negative_prompt_input, guidance_scale_slider, steps_slider],
|
| 70 |
outputs=image_output
|
| 71 |
)
|
| 72 |
|
|
|
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
print("--- FINAL VERSION: ADJUSTING LORA SCALE ---")
|
| 7 |
|
| 8 |
# --- Step 1: Define constants ---
|
|
|
|
| 9 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 10 |
LORA_FILENAME = "MyStickmanProject-10.safetensors"
|
| 11 |
|
|
|
|
| 21 |
print("Base model loaded successfully.")
|
| 22 |
|
| 23 |
# --- Step 3: Load and apply the LoRA weights ---
|
| 24 |
+
# This is the standard way to load LoRA weights.
|
| 25 |
+
print("Loading LoRA weights...")
|
| 26 |
pipe.load_lora_weights(LORA_FILENAME)
|
| 27 |
+
print("LoRA weights loaded.")
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
# --- Step 4: Move to CPU and optimize ---
|
|
|
|
| 35 |
print("--- MODEL SETUP COMPLETE ---")
|
| 36 |
|
| 37 |
# --- Step 5: Define the core generation function ---
|
| 38 |
+
def generate_image(prompt, negative_prompt, guidance_scale, num_steps, lora_scale):
|
| 39 |
print(f"Generating image with prompt: {prompt}")
|
| 40 |
+
|
| 41 |
+
# This is the KEY CHANGE: We are now controlling the LoRA strength.
|
| 42 |
+
# lora_scale=1.0 means full strength. lora_scale=0.7 means 70% strength.
|
| 43 |
image = pipe(
|
| 44 |
prompt=prompt,
|
| 45 |
negative_prompt=negative_prompt,
|
| 46 |
num_inference_steps=int(num_steps),
|
| 47 |
guidance_scale=float(guidance_scale),
|
| 48 |
+
cross_attention_kwargs={"scale": lora_scale}
|
| 49 |
).images[0]
|
| 50 |
+
|
| 51 |
print("Image generation complete.")
|
| 52 |
return image
|
| 53 |
|
| 54 |
# --- Step 6: Create the Gradio user interface ---
|
| 55 |
with gr.Blocks() as iface:
|
| 56 |
+
gr.Markdown("# My Custom Stickman LoRA Demo")
|
| 57 |
+
gr.Markdown("Enter a prompt to generate an image in my unique style.")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column(scale=70):
|
| 61 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="A stickman wearing a fedora hat, holding a magnifying glass")
|
| 62 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", value="(worst quality, low quality:1.4), blurry, noisy, grainy, 3d, realistic, photo")
|
| 63 |
+
|
| 64 |
+
# We add a new slider to control the LoRA strength!
|
| 65 |
+
lora_slider = gr.Slider(minimum=0, maximum=1.5, step=0.05, value=0.75, label="LoRA Strength (Style Intensity)")
|
| 66 |
+
|
| 67 |
with gr.Row():
|
| 68 |
guidance_scale_slider = gr.Slider(minimum=1, maximum=20, step=0.5, value=7.5, label="Guidance Scale")
|
| 69 |
steps_slider = gr.Slider(minimum=10, maximum=100, step=1, value=25, label="Inference Steps")
|
|
|
|
| 74 |
submit_button = gr.Button("Generate", variant="primary")
|
| 75 |
submit_button.click(
|
| 76 |
fn=generate_image,
|
| 77 |
+
inputs=[prompt_input, negative_prompt_input, guidance_scale_slider, steps_slider, lora_slider],
|
| 78 |
outputs=image_output
|
| 79 |
)
|
| 80 |
|