SpaceMonkey8-cloud commited on
Commit
eb186a1
Β·
1 Parent(s): 8d0de3a

Initial commit: Stable Diffusion image generator

Browse files
Files changed (4) hide show
  1. .gitignore +12 -0
  2. README.md +76 -7
  3. app.py +415 -0
  4. requirements.txt +10 -0
.gitignore ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.so
4
+ .Python
5
+ *.egg-info/
6
+ flagged/
7
+ gradio_cached_examples/
8
+ .DS_Store
9
+ *.png
10
+ *.jpg
11
+ *.jpeg
12
+
README.md CHANGED
@@ -1,13 +1,82 @@
1
- ---
2
- title: Spac3imag3
3
- emoji: πŸ†
4
- colorFrom: pink
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ title: Stable Diffusion Generator
2
+ emoji: 🎨
3
+ colorFrom: blue
4
+ colorTo: cyan
 
5
  sdk: gradio
6
+ sdk_version: 4.19.2
7
  app_file: app.py
8
  pinned: false
9
  license: apache-2.0
10
  ---
11
 
12
+ # 🎨 Stable Diffusion Image Generator
13
+
14
+ Generate stunning AI art from text descriptions using Stable Diffusion 2.1!
15
+
16
+ ## πŸš€ Features
17
+
18
+ - πŸ’¬ **Text-to-Image**: Create images from detailed descriptions
19
+ - 🎨 **Customizable**: Control size, quality, and style
20
+ - 🎲 **Reproducible**: Use seeds for consistent results
21
+ - ⚑ **Multiple Schedulers**: DPM++ 2M and Euler Ancestral
22
+ - πŸ–ΌοΈ **Batch Generation**: Create multiple variations at once
23
+
24
+ ## 🎯 How to Use
25
+
26
+ 1. **Write a Prompt**: Describe the image you want in detail
27
+ 2. **Add Negative Prompt**: Specify what to avoid
28
+ 3. **Adjust Settings**: Size, steps, guidance scale
29
+ 4. **Generate**: Click the button and wait
30
+ 5. **Download**: Save your AI-generated artwork!
31
+
32
+ ## πŸ’‘ Prompt Tips
33
+
34
+ ### Good Prompt Structure
35
+ `[Subject] + [Style] + [Lighting] + [Details] + [Quality]`
36
+
37
+ ### Examples
38
+ - "a serene japanese garden, cherry blossoms, koi pond, soft lighting, 4k"
39
+ - "portrait of a cyberpunk character, neon lights, detailed, digital art"
40
+ - "fantasy landscape with mountains, dramatic sunset, epic, highly detailed"
41
+
42
+ ### Style Keywords
43
+ - photorealistic, digital art, oil painting, watercolor, anime
44
+ - cinematic, ethereal, vibrant, dramatic, soft
45
+
46
+ ### Quality Modifiers
47
+ - highly detailed, 4k, 8k, sharp focus, intricate
48
+ - trending on artstation, masterpiece
49
+
50
+ ## πŸ”§ Settings Guide
51
+
52
+ | Parameter | Range | Recommended | Effect |
53
+ |-----------|-------|-------------|--------|
54
+ | **Steps** | 10-50 | 20-30 | Quality (more = better but slower) |
55
+ | **Guidance** | 1-20 | 7-10 | Prompt adherence |
56
+ | **Size** | 256-1024 | 512x512 | Image resolution |
57
+ | **Seed** | Any number | -1 (random) | Reproducibility |
58
+
59
+ ## πŸ“Š Performance
60
+
61
+ | Hardware | Resolution | Steps | Time |
62
+ |----------|-----------|-------|------|
63
+ | CPU Free | 512x512 | 20 | ~3-5 min |
64
+ | T4 GPU | 512x512 | 25 | ~15-20 sec |
65
+ | T4 GPU | 768x768 | 30 | ~30-40 sec |
66
+
67
+ ## 🎨 Model Info
68
+
69
+ - **Base**: Stable Diffusion 2.1
70
+ - **Resolution**: Optimized for 768px
71
+ - **Training**: LAION-5B dataset
72
+ - **License**: CreativeML Open RAIL++-M
73
+
74
+ ## πŸ“š Resources
75
+
76
+ - [Stable Diffusion Paper](https://arxiv.org/abs/2112.10752)
77
+ - [Model Card](https://huggingface.co/stabilityai/stable-diffusion-2-1)
78
+ - [Diffusers Docs](https://huggingface.co/docs/diffusers)
79
+
80
+ ---
81
+
82
+ **Made with ❀️ using HuggingFace Diffusers**
app.py ADDED
@@ -0,0 +1,415 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import (
4
+ StableDiffusionPipeline,
5
+ DPMSolverMultistepScheduler,
6
+ EulerAncestralDiscreteScheduler
7
+ )
8
+ from PIL import Image
9
+ import random
10
+
11
+ print("🎨 Initializing Stable Diffusion pipeline...")
12
+
13
+ # Configurazione
14
+ MODEL_ID = "stabilityai/stable-diffusion-2-1"
15
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
16
+ USE_SAFETENSORS = True
17
+
18
+ # Carica pipeline
19
+ print(f"πŸ“¦ Loading model: {MODEL_ID}")
20
+
21
+ pipe = StableDiffusionPipeline.from_pretrained(
22
+ MODEL_ID,
23
+ torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
24
+ use_safetensors=USE_SAFETENSORS,
25
+ safety_checker=None, # Disabilita per velocitΓ  (opzionale)
26
+ )
27
+
28
+ # Scheduler ottimizzato
29
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
30
+
31
+ pipe.to(DEVICE)
32
+
33
+ # Ottimizzazioni
34
+ if DEVICE == "cuda":
35
+ pipe.enable_model_cpu_offload()
36
+ pipe.enable_vae_slicing()
37
+ print("βœ… GPU optimizations enabled")
38
+
39
+ print(f"βœ… Pipeline loaded on {DEVICE}")
40
+
41
+
42
+ def generate_image(
43
+ prompt,
44
+ negative_prompt="",
45
+ width=512,
46
+ height=512,
47
+ num_inference_steps=25,
48
+ guidance_scale=7.5,
49
+ num_images=1,
50
+ seed=-1,
51
+ scheduler_type="DPM++ 2M",
52
+ progress=gr.Progress()
53
+ ):
54
+ """
55
+ Genera immagini da prompt testuale
56
+
57
+ Args:
58
+ prompt: Descrizione dell'immagine da generare
59
+ negative_prompt: Cosa evitare
60
+ width: Larghezza immagine (multiplo di 8)
61
+ height: Altezza immagine (multiplo di 8)
62
+ num_inference_steps: Step di qualitΓ  (15-50)
63
+ guidance_scale: Aderenza al prompt (5-15)
64
+ num_images: Numero di immagini da generare (1-4)
65
+ seed: Random seed (-1 per random)
66
+ scheduler_type: Tipo di scheduler
67
+ progress: Progress tracker
68
+ """
69
+
70
+ if not prompt or len(prompt.strip()) == 0:
71
+ return None, "❌ Inserisci un prompt!"
72
+
73
+ try:
74
+ progress(0, desc="🎨 Initializing generation...")
75
+
76
+ # Imposta seed
77
+ if seed == -1:
78
+ seed = random.randint(0, 2147483647)
79
+
80
+ generator = torch.Generator(device=DEVICE).manual_seed(seed)
81
+
82
+ print(f"πŸ“ Prompt: {prompt}")
83
+ print(f"🎲 Seed: {seed}")
84
+ print(f"πŸ“ Size: {width}x{height}")
85
+ print(f"🎨 Steps: {num_inference_steps}")
86
+
87
+ # Cambia scheduler se richiesto
88
+ if scheduler_type == "Euler a":
89
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
90
+ pipe.scheduler.config
91
+ )
92
+ elif scheduler_type == "DPM++ 2M":
93
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(
94
+ pipe.scheduler.config
95
+ )
96
+
97
+ progress(0.2, desc="πŸ–ΌοΈ Generating image...")
98
+
99
+ # Genera immagini
100
+ with torch.no_grad():
101
+ result = pipe(
102
+ prompt=prompt,
103
+ negative_prompt=negative_prompt if negative_prompt else None,
104
+ width=width,
105
+ height=height,
106
+ num_inference_steps=num_inference_steps,
107
+ guidance_scale=guidance_scale,
108
+ num_images_per_prompt=num_images,
109
+ generator=generator,
110
+ )
111
+
112
+ images = result.images
113
+
114
+ progress(1.0, desc="βœ… Complete!")
115
+
116
+ # Info
117
+ info = f"""
118
+ βœ… **Immagine generata con successo!**
119
+
120
+ πŸ“Š **Dettagli:**
121
+ - Prompt: "{prompt}"
122
+ - Negative: "{negative_prompt if negative_prompt else 'None'}"
123
+ - Risoluzione: {width}x{height}
124
+ - Steps: {num_inference_steps}
125
+ - Guidance Scale: {guidance_scale}
126
+ - Seed: {seed}
127
+ - Scheduler: {scheduler_type}
128
+ - Device: {DEVICE.upper()}
129
+ - Immagini generate: {len(images)}
130
+
131
+ πŸ’‘ **Tip:** Salva il seed per ricreare immagini simili!
132
+ """
133
+
134
+ # Restituisci prima immagine + gallery
135
+ return images[0], images if len(images) > 1 else None, info
136
+
137
+ except Exception as e:
138
+ error_msg = f"""
139
+ ❌ **Errore durante la generazione:**
140
+
141
+ {str(e)}
142
+
143
+ πŸ’‘ **Possibili soluzioni:**
144
+ - Riduci risoluzione (512x512 consigliato)
145
+ - Riduci inference steps (20-25)
146
+ - Semplifica il prompt
147
+ - Verifica che width e height siano multipli di 8
148
+ """
149
+ print(f"Error: {e}")
150
+ import traceback
151
+ traceback.print_exc()
152
+ return None, None, error_msg
153
+
154
+
155
+ # Esempi predefiniti
156
+ EXAMPLES = [
157
+ [
158
+ "a beautiful landscape with mountains and a lake at sunset, highly detailed, 4k",
159
+ "blurry, low quality, distorted, ugly",
160
+ 512, 512, 25, 7.5, 1, 42, "DPM++ 2M"
161
+ ],
162
+ [
163
+ "portrait of a cute cat wearing a wizard hat, digital art, detailed fur",
164
+ "low quality, blurry",
165
+ 512, 512, 30, 8.0, 1, 123, "DPM++ 2M"
166
+ ],
167
+ [
168
+ "futuristic city with flying cars, neon lights, cyberpunk style, detailed",
169
+ "blurry, low quality",
170
+ 768, 512, 25, 7.5, 1, 456, "DPM++ 2M"
171
+ ],
172
+ [
173
+ "medieval castle on a hill, dramatic lighting, fantasy art, intricate details",
174
+ "modern, contemporary",
175
+ 512, 768, 30, 7.5, 1, 789, "Euler a"
176
+ ],
177
+ ]
178
+
179
+
180
+ # Interfaccia Gradio
181
+ with gr.Blocks(
182
+ title="🎨 Stable Diffusion Generator",
183
+ theme=gr.themes.Soft(
184
+ primary_hue="blue",
185
+ secondary_hue="cyan"
186
+ ),
187
+ css="""
188
+ .gradio-container {max-width: 1400px !important}
189
+ """
190
+ ) as demo:
191
+
192
+ gr.Markdown("""
193
+ # 🎨 Stable Diffusion Image Generator
194
+ ### Create Stunning AI Art from Text Descriptions
195
+
196
+ Powered by **Stable Diffusion 2.1** - State-of-the-art text-to-image generation
197
+
198
+ πŸ’‘ **Tips for better results:**
199
+ - Be specific and descriptive
200
+ - Mention style, lighting, and details
201
+ - Use negative prompts to avoid unwanted elements
202
+ - Experiment with different seeds and settings
203
+ """)
204
+
205
+ with gr.Row():
206
+ # Colonna sinistra - Input
207
+ with gr.Column(scale=1):
208
+ prompt_input = gr.Textbox(
209
+ label="✨ Prompt (Describe what you want to create)",
210
+ placeholder="Example: a serene japanese garden with cherry blossoms, koi pond, soft lighting, highly detailed, 4k",
211
+ lines=4,
212
+ value="a beautiful landscape with mountains and a lake at sunset, highly detailed, 4k"
213
+ )
214
+
215
+ negative_prompt_input = gr.Textbox(
216
+ label="🚫 Negative Prompt (What to avoid)",
217
+ placeholder="Example: blurry, low quality, distorted, ugly, deformed",
218
+ lines=2,
219
+ value="blurry, low quality, distorted, ugly"
220
+ )
221
+
222
+ with gr.Row():
223
+ width = gr.Slider(
224
+ minimum=256,
225
+ maximum=1024,
226
+ value=512,
227
+ step=64,
228
+ label="πŸ“ Width",
229
+ info="Must be multiple of 64"
230
+ )
231
+
232
+ height = gr.Slider(
233
+ minimum=256,
234
+ maximum=1024,
235
+ value=512,
236
+ step=64,
237
+ label="πŸ“ Height",
238
+ info="Must be multiple of 64"
239
+ )
240
+
241
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
242
+ num_inference_steps = gr.Slider(
243
+ minimum=10,
244
+ maximum=50,
245
+ value=25,
246
+ step=5,
247
+ label="🎨 Inference Steps",
248
+ info="More = better quality but slower"
249
+ )
250
+
251
+ guidance_scale = gr.Slider(
252
+ minimum=1.0,
253
+ maximum=20.0,
254
+ value=7.5,
255
+ step=0.5,
256
+ label="🎯 Guidance Scale",
257
+ info="How closely to follow the prompt (7-10 recommended)"
258
+ )
259
+
260
+ num_images = gr.Slider(
261
+ minimum=1,
262
+ maximum=4,
263
+ value=1,
264
+ step=1,
265
+ label="πŸ–ΌοΈ Number of Images",
266
+ info="Generate multiple variations"
267
+ )
268
+
269
+ scheduler_type = gr.Dropdown(
270
+ choices=["DPM++ 2M", "Euler a"],
271
+ value="DPM++ 2M",
272
+ label="πŸ”§ Scheduler",
273
+ info="Different sampling methods"
274
+ )
275
+
276
+ seed = gr.Number(
277
+ value=-1,
278
+ label="🎲 Seed (-1 for random)",
279
+ info="Use same seed for consistent results",
280
+ precision=0
281
+ )
282
+
283
+ generate_btn = gr.Button(
284
+ "🎨 Generate Image",
285
+ variant="primary",
286
+ size="lg"
287
+ )
288
+
289
+ gr.Markdown("""
290
+ ### πŸ“Š Performance Guide
291
+
292
+ **CPU (Free tier):**
293
+ - Resolution: 512x512
294
+ - Steps: 15-20
295
+ - Time: ~2-5 min
296
+
297
+ **GPU T4 ($0.60/h):**
298
+ - Resolution: 768x768
299
+ - Steps: 25-35
300
+ - Time: ~10-30 sec
301
+ """)
302
+
303
+ # Colonna destra - Output
304
+ with gr.Column(scale=1):
305
+ image_output = gr.Image(
306
+ label="πŸ–ΌοΈ Generated Image",
307
+ type="pil",
308
+ height=512
309
+ )
310
+
311
+ gallery_output = gr.Gallery(
312
+ label="🎨 Image Variations",
313
+ columns=2,
314
+ rows=2,
315
+ height=400,
316
+ visible=False
317
+ )
318
+
319
+ info_output = gr.Markdown(
320
+ value="πŸ‘† Write a prompt and click 'Generate' to create your image!",
321
+ label="ℹ️ Generation Info"
322
+ )
323
+
324
+ # Sezione esempi
325
+ gr.Markdown("### 🎨 Example Prompts - Click to try")
326
+
327
+ gr.Examples(
328
+ examples=EXAMPLES,
329
+ inputs=[
330
+ prompt_input,
331
+ negative_prompt_input,
332
+ width,
333
+ height,
334
+ num_inference_steps,
335
+ guidance_scale,
336
+ num_images,
337
+ seed,
338
+ scheduler_type
339
+ ],
340
+ outputs=[image_output, gallery_output, info_output],
341
+ fn=generate_image,
342
+ cache_examples=False,
343
+ )
344
+
345
+ # Event handler
346
+ generate_btn.click(
347
+ fn=generate_image,
348
+ inputs=[
349
+ prompt_input,
350
+ negative_prompt_input,
351
+ width,
352
+ height,
353
+ num_inference_steps,
354
+ guidance_scale,
355
+ num_images,
356
+ seed,
357
+ scheduler_type
358
+ ],
359
+ outputs=[image_output, gallery_output, info_output],
360
+ )
361
+
362
+ # Footer
363
+ gr.Markdown("""
364
+ ---
365
+ ### πŸ“š Prompt Engineering Guide
366
+
367
+ **Structure:** `[Subject] + [Style] + [Lighting] + [Details] + [Quality]`
368
+
369
+ **Good Prompt Examples:**
370
+ - "a serene japanese garden with cherry blossoms, koi pond, soft golden hour lighting, highly detailed, 4k, photorealistic"
371
+ - "portrait of an astronaut floating in space, cinematic lighting, digital art, trending on artstation"
372
+ - "fantasy castle on a floating island, dramatic storm clouds, epic scale, concept art, octane render"
373
+
374
+ **Style Keywords:**
375
+ - photorealistic, digital art, oil painting, watercolor, anime, concept art
376
+ - cinematic, dramatic, ethereal, vibrant, muted, pastel
377
+
378
+ **Quality Modifiers:**
379
+ - highly detailed, 4k, 8k, ultra detailed, intricate, sharp focus
380
+ - trending on artstation, award winning, masterpiece
381
+
382
+ **Common Negative Prompts:**
383
+ - blurry, low quality, distorted, ugly, deformed, duplicate
384
+ - bad anatomy, poorly drawn, amateur, watermark, signature
385
+
386
+ ---
387
+
388
+ ### πŸ”§ Technical Details
389
+
390
+ - **Model**: Stable Diffusion 2.1 (768px base model)
391
+ - **Scheduler**: DPM++ 2M Karras / Euler Ancestral
392
+ - **Device**: {device}
393
+ - **VRAM**: Optimized with CPU offload and VAE slicing
394
+
395
+ ### πŸ’‘ Tips
396
+
397
+ - **Square images** (512x512) are fastest
398
+ - **Portrait** (512x768) or **Landscape** (768x512) for specific ratios
399
+ - Start with **guidance scale 7-8**, adjust if needed
400
+ - Use **20-25 steps** for good quality
401
+ - Save your **seed** to recreate variations
402
+
403
+ ---
404
+
405
+ **Made with ❀️ using HuggingFace Diffusers & Stable Diffusion**
406
+ """.replace("{device}", DEVICE.upper()))
407
+
408
+ # Launch
409
+ if __name__ == "__main__":
410
+ demo.queue(max_size=20)
411
+ demo.launch(
412
+ server_name="0.0.0.0",
413
+ server_port=7860,
414
+ share=False
415
+ )
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.27.2
2
+ transformers==4.38.1
3
+ accelerate==0.27.2
4
+ torch==2.2.0
5
+ torchvision==0.17.0
6
+ gradio==4.19.2
7
+ pillow==10.2.0
8
+ numpy==1.26.4
9
+ safetensors==0.4.2
10
+ huggingface-hub==0.21.4