Spaces:
Sleeping
Sleeping
Commit
·
7a41953
1
Parent(s):
14ffee7
normal SD with concepts library
Browse files- app.py +87 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torch import autocast
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
# Initialize the model
|
| 8 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
# List of concept embeddings to use
|
| 12 |
+
concepts = [
|
| 13 |
+
"sd-concepts-library/sword-lily-flowers102",
|
| 14 |
+
"sd-concepts-library/azalea-flowers102",
|
| 15 |
+
"sd-concepts-library/samurai-jack",
|
| 16 |
+
"sd-concepts-library/wu-shi-art",
|
| 17 |
+
"sd-concepts-library/wu-shi"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
def load_learned_embed_in_clip(learned_embeds_path, text_encoder, tokenizer):
|
| 21 |
+
loaded_learned_embeds = torch.load(learned_embeds_path, map_location="cpu")
|
| 22 |
+
|
| 23 |
+
# Add the concept token to tokenizer
|
| 24 |
+
token = list(loaded_learned_embeds.keys())[0]
|
| 25 |
+
num_added_tokens = tokenizer.add_tokens(token)
|
| 26 |
+
|
| 27 |
+
# Resize token embeddings
|
| 28 |
+
text_encoder.resize_token_embeddings(len(tokenizer))
|
| 29 |
+
|
| 30 |
+
# Add the concept embedding
|
| 31 |
+
token_id = tokenizer.convert_tokens_to_ids(token)
|
| 32 |
+
text_encoder.get_input_embeddings().weight.data[token_id] = loaded_learned_embeds[token]
|
| 33 |
+
|
| 34 |
+
return token
|
| 35 |
+
|
| 36 |
+
def generate_images(prompt):
|
| 37 |
+
images = []
|
| 38 |
+
|
| 39 |
+
# Load base pipeline
|
| 40 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 41 |
+
model_id,
|
| 42 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 43 |
+
).to(device)
|
| 44 |
+
|
| 45 |
+
for concept in concepts:
|
| 46 |
+
# Load concept embedding
|
| 47 |
+
token = load_learned_embed_in_clip(
|
| 48 |
+
f"{concept}/learned_embeds.bin",
|
| 49 |
+
pipe.text_encoder,
|
| 50 |
+
pipe.tokenizer
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Generate random seed
|
| 54 |
+
seed = random.randint(1, 999999)
|
| 55 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 56 |
+
|
| 57 |
+
# Add concept token to prompt
|
| 58 |
+
concept_prompt = f"{token} {prompt}"
|
| 59 |
+
|
| 60 |
+
# Generate image
|
| 61 |
+
with autocast(device):
|
| 62 |
+
image = pipe(
|
| 63 |
+
concept_prompt,
|
| 64 |
+
num_inference_steps=50,
|
| 65 |
+
generator=generator,
|
| 66 |
+
guidance_scale=7.5
|
| 67 |
+
).images[0]
|
| 68 |
+
|
| 69 |
+
images.append(image)
|
| 70 |
+
|
| 71 |
+
# Clear concept from pipeline
|
| 72 |
+
pipe.tokenizer.remove_tokens([token])
|
| 73 |
+
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer))
|
| 74 |
+
|
| 75 |
+
return images
|
| 76 |
+
|
| 77 |
+
# Create Gradio interface
|
| 78 |
+
iface = gr.Interface(
|
| 79 |
+
fn=generate_images,
|
| 80 |
+
inputs=gr.Textbox(label="Enter your prompt"),
|
| 81 |
+
outputs=[gr.Image() for _ in range(5)],
|
| 82 |
+
title="Multi-Concept Stable Diffusion Generator",
|
| 83 |
+
description="Generate images using 5 different concepts from the SD Concepts Library"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch the app
|
| 87 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
diffusers
|
| 3 |
+
transformers
|
| 4 |
+
gradio
|