Coloring_Books / README.md
Walid-Ahmed's picture
Update README.md
d41174a verified

A newer version of the Gradio SDK is available: 6.2.0

Upgrade
metadata
title: Coloring Books
emoji: 🐠
colorFrom: pink
colorTo: green
sdk: gradio
sdk_version: 5.16.0
app_file: app.py
pinned: false

🌟 AI Coloring Book & Style Generator

A Hugging Face Space powered by Stable Diffusion XL, allowing users to generate AI-generated artwork in different styles. The app also supports LoRA fine-tuning, enabling specialized coloring book effects.

🧠 Concept

This application is built on Stable Diffusion XL (SDXL), a text-to-image generative AI model. Users can:

  • Enter a text prompt to describe the image they want.
  • Select a predefined artistic style (e.g., Neonpunk, Retro Cyberpunk, Dark Fantasy).
  • Enable LoRA (Low-Rank Adaptation) for Coloring Book Style, which enhances black-and-white outlines.

This makes the tool useful for: βœ… Artists looking for AI-generated inspiration.
βœ… Educators wanting customized coloring book images.
βœ… AI enthusiasts experimenting with fine-tuned models.


βš™οΈ How It Works

  1. User enters a text prompt describing the desired image.
  2. User selects an artistic style from the dropdown menu.
  3. User enables or disables LoRA (if they want a coloring book effect).
  4. The app processes the request using Stable Diffusion XL on a GPU.
  5. The final image is generated and displayed.

πŸš€ Runs on Hugging Face Spaces with ZeroGPU allocation!
⏳ GPU is only used during image generation, saving resources.


πŸ› οΈ Code Overview

πŸ“Œ 1. Loading the Stable Diffusion Model

The app loads the Stable Diffusion XL model and keeps it cached in memory.

@lru_cache(maxsize=1)
def load_pipeline(use_lora: bool):
    """Load Stable Diffusion XL pipeline and apply LoRA weights (if enabled)."""

    pipe = StableDiffusionXLPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
        use_safetensors=True
    )

    pipe.to("cpu")  # Keep on CPU until needed

    if use_lora:
        pipe.load_lora_weights(color_book_lora_path)

    return pipe

πŸ”Ή Uses LoRA for coloring book style when enabled.
πŸ”Ή Model stays on CPU until a request is made.


πŸ“Œ 2. Handling Image Generation

The generate_image() function processes user input, applies styles, and runs the Stable Diffusion pipeline.

@spaces.GPU  # Ensures GPU is only used during image generation
def generate_image(prompt: str, style_name: str, use_lora: bool):
    """Generate an image based on user input and selected style."""
    
    pipeline = load_pipeline(use_lora)
    pipeline.to("cuda")  # Move to GPU

    # Apply artistic style
    style_prompt = styles.get(style_name, {}).get("prompt", "")
    negative_prompt = styles.get(style_name, {}).get("negative_prompt", "")

    if use_lora:
        prompt += color_book_trigger  # Activates LoRA

    image = pipeline(
        prompt=prompt + " " + style_prompt,
        negative_prompt="blurred, ugly, watermark, low resolution, " + negative_prompt,
        num_inference_steps=20,
        guidance_scale=9.0
    ).images[0]

    pipeline.to("cpu")  # Free GPU memory

    return image

πŸ”Ή Uses predefined artistic styles.
πŸ”Ή LoRA modifies the image style for coloring book effects.
πŸ”Ή GPU is only used for generation, then freed for efficiency.


πŸ“Œ 3. Gradio UI for Hugging Face Spaces

The Gradio Interface provides a simple UI for users.

interface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Enter Your Prompt", placeholder="A cute lion"),
        gr.Dropdown(label="Select a Style", choices=list(styles.keys()), value="None"),
        gr.Checkbox(label="Use Coloring Book LoRA", value=False)
    ],
    outputs=gr.Image(label="Generated Image"),
    title="🎨 AI Coloring Book & Style Generator",
    description="Generate AI-powered art using Stable Diffusion XL. Choose a style or enable a LoRA fine-tuned coloring book effect."
)

if __name__ == "__main__":
    interface.launch()

πŸ”Ή User-friendly UI with textbox, dropdown, and checkbox controls.
πŸ”Ή Outputs a generated image based on user selections.
πŸ”Ή Runs efficiently on Hugging Face Spaces with ZeroGPU allocation.


πŸ“¦ Installation & Running Locally

If you want to run this locally, install dependencies:

pip install torch diffusers transformers accelerate gradio peft safetensors spaces

Then, run:

python app.py

πŸ“ Requirements (requirements.txt)

This app requires the following dependencies:

torch
diffusers
transformers
accelerate
gradiop
peft
safetensors
spaces

🌟 Future Improvements

βœ… Add more LoRA styles (e.g., Anime, Watercolor, Sketch).
βœ… Optimize GPU usage with mixed-precision inference.
βœ… Improve UI with real-time previews.


πŸ‘©β€πŸ’» Author

πŸ‘¨β€πŸ’» Developed by Walid Ahmed

πŸ“Œ Powered by Hugging Face πŸ€— and Stable Diffusion XL.

🌟 Try it out and start generating amazing AI art! 🎨