--- 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. ```python @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**. ```python @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. ```python 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: ```bash pip install torch diffusers transformers accelerate gradio peft safetensors spaces ``` Then, run: ```bash python app.py ``` --- ## **📝 Requirements (`requirements.txt`)** This app requires the following dependencies: ```txt 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!** 🎨