Walid-Ahmed commited on
Commit
acaf802
Β·
verified Β·
1 Parent(s): 532bae2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +167 -0
README.md CHANGED
@@ -10,3 +10,170 @@ pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+
15
+
16
+ # 🌟 AI Coloring Book & Style Generator
17
+
18
+ ![Stable Diffusion XL](https://huggingface.co/spaces/stabilityai/stable-diffusion/assets/sdxl_banner.jpg)
19
+
20
+ 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.
21
+
22
+ ## **🧠 Concept**
23
+ This application is built on **Stable Diffusion XL (SDXL)**, a **text-to-image** generative AI model. Users can:
24
+ - **Enter a text prompt** to describe the image they want.
25
+ - **Select a predefined artistic style** (e.g., Neonpunk, Retro Cyberpunk, Dark Fantasy).
26
+ - **Enable LoRA (Low-Rank Adaptation) for Coloring Book Style**, which enhances black-and-white outlines.
27
+
28
+ This makes the tool useful for:
29
+ βœ… Artists looking for AI-generated inspiration.
30
+ βœ… Educators wanting customized coloring book images.
31
+ βœ… AI enthusiasts experimenting with fine-tuned models.
32
+
33
+ ---
34
+
35
+ ## **βš™οΈ How It Works**
36
+ 1. **User enters a text prompt** describing the desired image.
37
+ 2. **User selects an artistic style** from the dropdown menu.
38
+ 3. **User enables or disables LoRA** (if they want a coloring book effect).
39
+ 4. **The app processes the request** using **Stable Diffusion XL** on a GPU.
40
+ 5. **The final image is generated** and displayed.
41
+
42
+ πŸš€ **Runs on Hugging Face Spaces with ZeroGPU allocation!**
43
+ ⏳ **GPU is only used during image generation, saving resources.**
44
+
45
+ ---
46
+
47
+ ## **πŸ› οΈ Code Overview**
48
+ ### **πŸ“Œ 1. Loading the Stable Diffusion Model**
49
+ The app **loads the Stable Diffusion XL model** and keeps it cached in memory.
50
+
51
+ ```python
52
+ @lru_cache(maxsize=1)
53
+ def load_pipeline(use_lora: bool):
54
+ """Load Stable Diffusion XL pipeline and apply LoRA weights (if enabled)."""
55
+
56
+ pipe = StableDiffusionXLPipeline.from_pretrained(
57
+ "stabilityai/stable-diffusion-xl-base-1.0",
58
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
59
+ use_safetensors=True
60
+ )
61
+
62
+ pipe.to("cpu") # Keep on CPU until needed
63
+
64
+ if use_lora:
65
+ pipe.load_lora_weights(color_book_lora_path)
66
+
67
+ return pipe
68
+ ```
69
+ πŸ”Ή Uses **LoRA** for **coloring book style** when enabled.
70
+ πŸ”Ή Model stays on **CPU** until a request is made.
71
+
72
+ ---
73
+
74
+ ### **πŸ“Œ 2. Handling Image Generation**
75
+ The `generate_image()` function processes user input, applies styles, and runs the **Stable Diffusion pipeline**.
76
+
77
+ ```python
78
+ @spaces.GPU # Ensures GPU is only used during image generation
79
+ def generate_image(prompt: str, style_name: str, use_lora: bool):
80
+ """Generate an image based on user input and selected style."""
81
+
82
+ pipeline = load_pipeline(use_lora)
83
+ pipeline.to("cuda") # Move to GPU
84
+
85
+ # Apply artistic style
86
+ style_prompt = styles.get(style_name, {}).get("prompt", "")
87
+ negative_prompt = styles.get(style_name, {}).get("negative_prompt", "")
88
+
89
+ if use_lora:
90
+ prompt += color_book_trigger # Activates LoRA
91
+
92
+ image = pipeline(
93
+ prompt=prompt + " " + style_prompt,
94
+ negative_prompt="blurred, ugly, watermark, low resolution, " + negative_prompt,
95
+ num_inference_steps=20,
96
+ guidance_scale=9.0
97
+ ).images[0]
98
+
99
+ pipeline.to("cpu") # Free GPU memory
100
+
101
+ return image
102
+ ```
103
+ πŸ”Ή Uses **predefined artistic styles**.
104
+ πŸ”Ή LoRA **modifies the image style** for coloring book effects.
105
+ πŸ”Ή **GPU is only used for generation, then freed** for efficiency.
106
+
107
+ ---
108
+
109
+ ### **πŸ“Œ 3. Gradio UI for Hugging Face Spaces**
110
+ The **Gradio Interface** provides a simple UI for users.
111
+
112
+ ```python
113
+ interface = gr.Interface(
114
+ fn=generate_image,
115
+ inputs=[
116
+ gr.Textbox(label="Enter Your Prompt", placeholder="A cute lion"),
117
+ gr.Dropdown(label="Select a Style", choices=list(styles.keys()), value="None"),
118
+ gr.Checkbox(label="Use Coloring Book LoRA", value=False)
119
+ ],
120
+ outputs=gr.Image(label="Generated Image"),
121
+ title="🎨 AI Coloring Book & Style Generator",
122
+ description="Generate AI-powered art using Stable Diffusion XL. Choose a style or enable a LoRA fine-tuned coloring book effect."
123
+ )
124
+
125
+ if __name__ == "__main__":
126
+ interface.launch()
127
+ ```
128
+ πŸ”Ή **User-friendly UI** with **textbox, dropdown, and checkbox controls**.
129
+ πŸ”Ή Outputs **a generated image** based on user selections.
130
+ πŸ”Ή Runs efficiently on **Hugging Face Spaces with ZeroGPU allocation**.
131
+
132
+ ---
133
+
134
+ ## **πŸ“¦ Installation & Running Locally**
135
+ If you want to run this locally, install dependencies:
136
+
137
+ ```bash
138
+ pip install torch diffusers transformers accelerate gradio peft safetensors spaces
139
+ ```
140
+
141
+ Then, run:
142
+
143
+ ```bash
144
+ python app.py
145
+ ```
146
+
147
+ ---
148
+
149
+ ## **πŸ“ Requirements (`requirements.txt`)**
150
+ This app requires the following dependencies:
151
+
152
+ ```txt
153
+ torch
154
+ diffusers
155
+ transformers
156
+ accelerate
157
+ gradiop
158
+ peft
159
+ safetensors
160
+ spaces
161
+ ```
162
+
163
+ ---
164
+
165
+ ## **🌟 Future Improvements**
166
+ βœ… Add **more LoRA styles** (e.g., Anime, Watercolor, Sketch).
167
+ βœ… Optimize GPU usage **with mixed-precision inference**.
168
+ βœ… Improve **UI with real-time previews**.
169
+
170
+ ---
171
+
172
+ ## **πŸ‘©β€πŸ’» Author**
173
+ πŸ‘¨β€πŸ’» Developed by **Walid Ahmed**
174
+
175
+ πŸ“Œ **Powered by Hugging Face πŸ€— and Stable Diffusion XL**.
176
+
177
+ 🌟 **Try it out and start generating amazing AI art!** 🎨
178
+
179
+