Spaces:
Running
Running
| 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!** π¨ | |