import os import torch import gradio as gr from PIL import Image import torch.nn.functional as F from torchvision import transforms as tfms from diffusers import DiffusionPipeline # Determine the appropriate device and dtype torch_device = "cuda" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch_device == "cuda" else torch.float32 # Load the pipeline model_path = "CompVis/stable-diffusion-v1-4" sd_pipeline = DiffusionPipeline.from_pretrained( model_path, torch_dtype=torch_dtype, low_cpu_mem_usage=True if torch_device == "cpu" else False ).to(torch_device) # Load textual inversions 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") # Update style token dictionary style_token_dict = { "Illustration Style": '', "Line Art": '', "Hitokomoru Style": '', "Marc Allante": '', "Midjourney": '', "Hanfu Anime": '', "Birb Style": '' } def apply_guidance(image, guidance_method, loss_scale): # Convert PIL Image to tensor 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) # Simple brightness increase 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]) # Flip horizontally 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 # Convert back to PIL Image guided = guided.squeeze(0).clamp(0, 1) guided = (guided * 255).byte().cpu().permute(1, 2, 0).numpy() return Image.fromarray(guided) def inference(text, style, inference_step, guidance_scale, seed, guidance_method, loss_scale, image_size): prompt = text + " " + style_token_dict[style] # Convert image_size from string to tuple of integers size = tuple(map(int, image_size.split('x'))) # Generate image with pipeline image_pipeline = sd_pipeline( prompt, num_inference_steps=inference_step, guidance_scale=guidance_scale, generator=torch.Generator(device=torch_device).manual_seed(seed), height=size[1], width=size[0] ).images[0] # Apply guidance image_guide = apply_guidance(image_pipeline, guidance_method, loss_scale) return image_pipeline, image_guide # Your existing imports and model setup code here... css_and_html = """

Dreamscape Creator

Unleash your imagination with AI-powered generative art

🎨
Illustration Style
✏️
Line Art
🌌
Midjourney Style
👘
Hanfu Anime
""" with gr.Blocks(css=css_and_html) as demo: gr.HTML(css_and_html) with gr.Row(): text = gr.Textbox(label="Prompt", placeholder="Describe your dreamscape...") style = gr.Dropdown(label="Style", choices=list(style_token_dict.keys()), value="Illustration Style") with gr.Row(): inference_step = gr.Slider(1, 50, 20, 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(): image_size = gr.Radio(["256x256", "512x512"], label="Image Size", value="256x256") with gr.Row(): generate_button = gr.Button("Create Dreamscape", variant="primary") with gr.Row(): output_image = gr.Image(label="Your Dreamscape") output_image_guided = gr.Image(label="Guided Dreamscape") generate_button.click( inference, inputs=[text, style, inference_step, guidance_scale, seed, guidance_method, loss_scale, image_size], outputs=[output_image, output_image_guided] ) gr.Examples( examples=[ ["Magical Forest with Glowing Trees", 'Birb Style', 40, 7.5, 42, 'Grayscale', 200, "256x256"], [" Ancient Temple Ruins at Sunset", 'Midjourney', 30, 8.0, 123, 'Bright', 5678, "256x256"], ["Japanese garden with cherry blossoms", 'Hitokomoru Style', 40, 7.0, 789, 'Contrast', 250, "256x256"], ], inputs=[text, style, inference_step, guidance_scale, seed, guidance_method, loss_scale, image_size], outputs=[output_image, output_image_guided], fn=inference, cache_examples=True, examples_per_page=5 ) if __name__ == "__main__": demo.launch()