|
import os |
|
import torch |
|
import gradio as gr |
|
from tqdm import tqdm |
|
from PIL import Image |
|
import torch.nn.functional as F |
|
from torchvision import transforms as tfms |
|
from transformers import CLIPTextModel, CLIPTokenizer, logging |
|
from diffusers import AutoencoderKL, LMSDiscreteScheduler, UNet2DConditionModel, DiffusionPipeline |
|
|
|
HTML_TEMPLATE = """ |
|
<style> |
|
body { |
|
background: linear-gradient(135deg, #f5f7fa, #c3cfe2); |
|
} |
|
#app-header { |
|
text-align: center; |
|
background: rgba(255, 255, 255, 0.8); |
|
padding: 20px; |
|
border-radius: 10px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
position: relative; |
|
} |
|
#app-header h1 { |
|
color: #4CAF50; |
|
font-size: 2em; |
|
margin-bottom: 10px; |
|
} |
|
.concept { |
|
position: relative; |
|
transition: transform 0.3s; |
|
} |
|
.concept:hover { |
|
transform: scale(1.1); |
|
} |
|
.concept img { |
|
width: 100px; |
|
border-radius: 10px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
} |
|
.concept-description { |
|
position: absolute; |
|
bottom: -30px; |
|
left: 50%; |
|
transform: translateX(-50%); |
|
background-color: #4CAF50; |
|
color: white; |
|
padding: 5px 10px; |
|
border-radius: 5px; |
|
opacity: 0; |
|
transition: opacity 0.3s; |
|
} |
|
.concept:hover .concept-description { |
|
opacity: 1; |
|
} |
|
.artifact { |
|
position: absolute; |
|
background: rgba(76, 175, 80, 0.1); |
|
border-radius: 50%; |
|
} |
|
.artifact.large { |
|
width: 300px; |
|
height: 300px; |
|
top: -50px; |
|
left: -150px; |
|
} |
|
.artifact.medium { |
|
width: 200px; |
|
height: 200px; |
|
bottom: -50px; |
|
right: -100px; |
|
} |
|
.artifact.small { |
|
width: 100px; |
|
height: 100px; |
|
top: 50%; |
|
left: 50%; |
|
transform: translate(-50%, -50%); |
|
} |
|
</style> |
|
<div id="app-header"> |
|
<div class="artifact large"></div> |
|
<div class="artifact medium"></div> |
|
<div class="artifact small"></div> |
|
<h1>Generative Art with Textual Inversion and Guidance</h1> |
|
<p>Generate unique art using different styles and guidance methods.</p> |
|
<div style="display: flex; justify-content: center; gap: 20px; margin-top: 20px;"> |
|
<div class="concept"> |
|
<img src="https://example.com/illustration-style.jpg" alt="Illustration Style"> |
|
<div class="concept-description">Illustration Style</div> |
|
</div> |
|
<div class="concept"> |
|
<img src="https://example.com/line-art.jpg" alt="Line Art"> |
|
<div class="concept-description">Line Art</div> |
|
</div> |
|
<!-- Add more concepts here for each style in your style_token_dict --> |
|
</div> |
|
</div> |
|
""" |
|
|
|
torch_device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" |
|
if "mps" == torch_device: os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = "1" |
|
|
|
|
|
model_path = "CompVis/stable-diffusion-v1-4" |
|
sd_pipeline = DiffusionPipeline.from_pretrained( |
|
model_path, |
|
low_cpu_mem_usage=True, |
|
torch_dtype=torch.float32 |
|
).to(torch_device) |
|
|
|
|
|
sd_pipeline.load_textual_inversion("sd-concepts-library/illustration-style") |
|
sd_pipeline.load_textual_inversion("sd-concepts-library/line-art") |
|
sd_pipeline.load_textual_inversion("sd-concepts-library/hitokomoru-style-nao") |
|
sd_pipeline.load_textual_inversion("sd-concepts-library/style-of-marc-allante") |
|
sd_pipeline.load_textual_inversion("sd-concepts-library/midjourney-style") |
|
sd_pipeline.load_textual_inversion("sd-concepts-library/hanfu-anime-style") |
|
sd_pipeline.load_textual_inversion("sd-concepts-library/birb-style") |
|
|
|
|
|
style_token_dict = { |
|
"Illustration Style": '<illustration-style>', |
|
"Line Art":'<line-art>', |
|
"Hitokomoru Style":'<hitokomoru-style-nao>', |
|
"Marc Allante": '<Marc_Allante>', |
|
"Midjourney":'<midjourney-style>', |
|
"Hanfu Anime": '<hanfu-anime-style>', |
|
"Birb Style": '<birb-style>' |
|
} |
|
|
|
def apply_guidance(image, guidance_method, loss_scale): |
|
|
|
img_tensor = tfms.ToTensor()(image).unsqueeze(0).to(torch_device) |
|
|
|
if guidance_method == 'Grayscale': |
|
gray = tfms.Grayscale(3)(img_tensor) |
|
guided = img_tensor + (gray - img_tensor) * (loss_scale / 10000) |
|
elif guidance_method == 'Bright': |
|
bright = F.relu(img_tensor) |
|
guided = img_tensor + (bright - img_tensor) * (loss_scale / 10000) |
|
elif guidance_method == 'Contrast': |
|
mean = img_tensor.mean() |
|
contrast = (img_tensor - mean) * 2 + mean |
|
guided = img_tensor + (contrast - img_tensor) * (loss_scale / 10000) |
|
elif guidance_method == 'Symmetry': |
|
flipped = torch.flip(img_tensor, [3]) |
|
guided = img_tensor + (flipped - img_tensor) * (loss_scale / 10000) |
|
elif guidance_method == 'Saturation': |
|
saturated = tfms.functional.adjust_saturation(img_tensor, 2) |
|
guided = img_tensor + (saturated - img_tensor) * (loss_scale / 10000) |
|
else: |
|
return image |
|
|
|
|
|
guided = guided.squeeze(0).clamp(0, 1) |
|
guided = (guided * 255).byte().cpu().permute(1, 2, 0).numpy() |
|
return Image.fromarray(guided) |
|
|
|
def generate_with_guidance(prompt, num_inference_steps, guidance_scale, seed, guidance_method, loss_scale): |
|
|
|
generator = torch.Generator(device=torch_device).manual_seed(seed) |
|
image = sd_pipeline( |
|
prompt, |
|
num_inference_steps=num_inference_steps, |
|
guidance_scale=guidance_scale, |
|
generator=generator |
|
).images[0] |
|
|
|
|
|
guided_image = apply_guidance(image, guidance_method, loss_scale) |
|
|
|
return guided_image |
|
|
|
def inference(text, style, inference_step, guidance_scale, seed, guidance_method, loss_scale): |
|
prompt = text + " " + style_token_dict[style] |
|
|
|
|
|
image_pipeline = sd_pipeline( |
|
prompt, |
|
num_inference_steps=inference_step, |
|
guidance_scale=guidance_scale, |
|
generator=torch.Generator(device=torch_device).manual_seed(seed) |
|
).images[0] |
|
|
|
|
|
image_guide = generate_with_guidance(prompt, inference_step, guidance_scale, seed, guidance_method, loss_scale) |
|
|
|
return image_pipeline, image_guide |
|
|
|
title = "Generative with Textual Inversion and Guidance" |
|
description = "A Gradio interface to infer Stable Diffusion and generate images with different art styles and guidance methods" |
|
examples = [ |
|
["A majestic castle on a floating island", 'Illustration Style', 10, 7.5, 42, 'Grayscale', 200] |
|
] |
|
|
|
title = "Generative Art with Textual Inversion and Guidance" |
|
description = "Create unique artworks using Stable Diffusion with various styles and guidance methods." |
|
|
|
with gr.Blocks(css=HTML_TEMPLATE) as demo: |
|
gr.HTML(HTML_TEMPLATE) |
|
with gr.Row(): |
|
text = gr.Textbox(label="Prompt", placeholder="Enter your creative prompt here...") |
|
style = gr.Dropdown(label="Style", choices=list(style_token_dict.keys()), value="Illustration Style") |
|
with gr.Row(): |
|
inference_step = gr.Slider(1, 50, 10, step=1, label="Inference steps") |
|
guidance_scale = gr.Slider(1, 10, 7.5, step=0.1, label="Guidance scale") |
|
seed = gr.Slider(0, 10000, 42, step=1, label="Seed") |
|
with gr.Row(): |
|
guidance_method = gr.Dropdown(label="Guidance method", choices=['Grayscale', 'Bright', 'Contrast', 'Symmetry', 'Saturation'], value="Grayscale") |
|
loss_scale = gr.Slider(100, 10000, 200, step=100, label="Loss scale") |
|
with gr.Row(): |
|
generate_button = gr.Button("Generate Art") |
|
with gr.Row(): |
|
output_image = gr.Image(width=512, height=512, label="Generated art") |
|
output_image_guided = gr.Image(width=512, height=512, label="Generated art with guidance") |
|
|
|
generate_button.click( |
|
inference, |
|
inputs=[text, style, inference_step, guidance_scale, seed, guidance_method, loss_scale], |
|
outputs=[output_image, output_image_guided] |
|
) |
|
|
|
gr.Examples( |
|
examples=[ |
|
["A majestic castle on a floating island", 'Illustration Style', 10, 7.5, 42, 'Grayscale', 200] |
|
], |
|
inputs=[text, style, inference_step, guidance_scale, seed, guidance_method, loss_scale], |
|
outputs=[output_image, output_image_guided], |
|
fn=inference, |
|
cache_examples=True, |
|
) |
|
|
|
demo.launch() |