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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d139ba53",
   "metadata": {},
   "source": [
    "# Stable Diffusion 3.5 Large - Professional Image Generator\n",
    "\n",
    "This notebook provides a clean, professional interface for generating high-quality images using Stability AI's Stable Diffusion 3.5 Large model.\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24d783a3",
   "metadata": {},
   "source": [
    "## πŸ“¦ Install Required Dependencies\n",
    "\n",
    "First, let's install all necessary packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "169bbf1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q accelerate diffusers torch transformers gradio sentencepiece"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c59eabf",
   "metadata": {},
   "source": [
    "## πŸ”§ Import Libraries and Setup\n",
    "\n",
    "Import all necessary libraries and configure the environment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d54d02dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "import numpy as np\n",
    "import random\n",
    "from diffusers import DiffusionPipeline\n",
    "import torch\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "print(\"βœ… Libraries imported successfully\")\n",
    "print(f\"πŸ”₯ CUDA Available: {torch.cuda.is_available()}\")\n",
    "if torch.cuda.is_available():\n",
    "    print(f\"πŸ’» GPU: {torch.cuda.get_device_name(0)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9d3f107",
   "metadata": {},
   "source": [
    "## πŸš€ Load Stable Diffusion 3.5 Large Model\n",
    "\n",
    "This will download and initialize the model (may take a few minutes on first run):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc662d4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "model_repo_id = \"stabilityai/stable-diffusion-3.5-large\"\n",
    "\n",
    "if torch.cuda.is_available():\n",
    "    torch_dtype = torch.bfloat16\n",
    "else:\n",
    "    torch_dtype = torch.float32\n",
    "\n",
    "print(\"πŸ“₯ Loading model... (this may take a few minutes)\")\n",
    "pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)\n",
    "pipe = pipe.to(device)\n",
    "print(\"βœ… Model loaded successfully!\")\n",
    "\n",
    "MAX_SEED = np.iinfo(np.int32).max\n",
    "MAX_IMAGE_SIZE = 1024"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd5d7f09",
   "metadata": {},
   "source": [
    "## 🎨 Define Image Generation Function\n",
    "\n",
    "Core function that handles the image generation process:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a8c69ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_image(\n",
    "    prompt,\n",
    "    negative_prompt=\"\",\n",
    "    seed=42,\n",
    "    randomize_seed=False,\n",
    "    width=1024,\n",
    "    height=1024,\n",
    "    guidance_scale=4.5,\n",
    "    num_inference_steps=40,\n",
    "    progress=gr.Progress(track_tqdm=True),\n",
    "):\n",
    "    \"\"\"\n",
    "    Generate an image based on the provided prompt and parameters.\n",
    "    \"\"\"\n",
    "    if randomize_seed:\n",
    "        seed = random.randint(0, MAX_SEED)\n",
    "\n",
    "    generator = torch.Generator(device=device).manual_seed(seed)\n",
    "\n",
    "    image = pipe(\n",
    "        prompt=prompt,\n",
    "        negative_prompt=negative_prompt,\n",
    "        guidance_scale=guidance_scale,\n",
    "        num_inference_steps=num_inference_steps,\n",
    "        width=width,\n",
    "        height=height,\n",
    "        generator=generator,\n",
    "    ).images[0]\n",
    "\n",
    "    return image, seed\n",
    "\n",
    "print(\"βœ… Image generation function ready\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52857ec3",
   "metadata": {},
   "source": [
    "## 🎯 Launch Professional Gradio Interface\n",
    "\n",
    "Creating a clean, modern white UI with professional styling:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14c54e8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Professional example prompts\n",
    "examples = [\n",
    "    \"A serene mountain landscape at sunset with crystal clear lake reflections, photorealistic, 8k quality\",\n",
    "    \"Modern minimalist office space with floor-to-ceiling windows, natural lighting, architectural photography\",\n",
    "    \"Portrait of a woman in elegant evening dress, studio lighting, fashion photography, high detail\",\n",
    "    \"Futuristic city skyline at night with neon lights, cyberpunk aesthetic, ultra detailed\",\n",
    "    \"Cozy coffee shop interior with warm ambient lighting, bokeh effect, professional photography\",\n",
    "]\n",
    "\n",
    "# Modern, clean CSS styling\n",
    "css = \"\"\"\n",
    "@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');\n",
    "\n",
    "* {\n",
    "    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;\n",
    "}\n",
    "\n",
    "#main-container {\n",
    "    max-width: 1400px;\n",
    "    margin: 0 auto;\n",
    "    padding: 2rem;\n",
    "    background: #ffffff;\n",
    "}\n",
    "\n",
    ".header-section {\n",
    "    text-align: center;\n",
    "    padding: 2rem 0;\n",
    "    border-bottom: 1px solid #e5e7eb;\n",
    "    margin-bottom: 2rem;\n",
    "}\n",
    "\n",
    ".header-section h1 {\n",
    "    font-size: 2.5rem;\n",
    "    font-weight: 700;\n",
    "    color: #111827;\n",
    "    margin-bottom: 0.5rem;\n",
    "    letter-spacing: -0.025em;\n",
    "}\n",
    "\n",
    ".header-section p {\n",
    "    font-size: 1.125rem;\n",
    "    color: #6b7280;\n",
    "    font-weight: 400;\n",
    "}\n",
    "\n",
    ".input-section {\n",
    "    background: #f9fafb;\n",
    "    padding: 2rem;\n",
    "    border-radius: 12px;\n",
    "    border: 1px solid #e5e7eb;\n",
    "    margin-bottom: 1.5rem;\n",
    "}\n",
    "\n",
    ".output-section {\n",
    "    background: #ffffff;\n",
    "    padding: 1.5rem;\n",
    "    border-radius: 12px;\n",
    "    border: 1px solid #e5e7eb;\n",
    "    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);\n",
    "}\n",
    "\n",
    "button.primary {\n",
    "    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;\n",
    "    border: none !important;\n",
    "    color: white !important;\n",
    "    font-weight: 600 !important;\n",
    "    padding: 0.75rem 2rem !important;\n",
    "    border-radius: 8px !important;\n",
    "    font-size: 1rem !important;\n",
    "    transition: all 0.3s ease !important;\n",
    "    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;\n",
    "}\n",
    "\n",
    "button.primary:hover {\n",
    "    transform: translateY(-2px);\n",
    "    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1) !important;\n",
    "}\n",
    "\n",
    ".accordion {\n",
    "    background: #ffffff !important;\n",
    "    border: 1px solid #e5e7eb !important;\n",
    "    border-radius: 8px !important;\n",
    "    margin-top: 1rem;\n",
    "}\n",
    "\n",
    "label {\n",
    "    font-weight: 500 !important;\n",
    "    color: #374151 !important;\n",
    "    font-size: 0.875rem !important;\n",
    "    text-transform: uppercase;\n",
    "    letter-spacing: 0.05em;\n",
    "    margin-bottom: 0.5rem !important;\n",
    "}\n",
    "\n",
    "input, textarea {\n",
    "    border: 1px solid #d1d5db !important;\n",
    "    border-radius: 8px !important;\n",
    "    padding: 0.75rem !important;\n",
    "    font-size: 1rem !important;\n",
    "    transition: border-color 0.2s ease !important;\n",
    "}\n",
    "\n",
    "input:focus, textarea:focus {\n",
    "    border-color: #667eea !important;\n",
    "    outline: none !important;\n",
    "    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;\n",
    "}\n",
    "\n",
    ".slider {\n",
    "    margin: 1rem 0;\n",
    "}\n",
    "\n",
    ".examples {\n",
    "    margin-top: 2rem;\n",
    "    padding-top: 2rem;\n",
    "    border-top: 1px solid #e5e7eb;\n",
    "}\n",
    "\n",
    ".footer {\n",
    "    text-align: center;\n",
    "    padding: 2rem 0;\n",
    "    color: #9ca3af;\n",
    "    font-size: 0.875rem;\n",
    "    margin-top: 3rem;\n",
    "    border-top: 1px solid #e5e7eb;\n",
    "}\n",
    "\n",
    "/* Clean scrollbar */\n",
    "::-webkit-scrollbar {\n",
    "    width: 8px;\n",
    "    height: 8px;\n",
    "}\n",
    "\n",
    "::-webkit-scrollbar-track {\n",
    "    background: #f1f1f1;\n",
    "}\n",
    "\n",
    "::-webkit-scrollbar-thumb {\n",
    "    background: #888;\n",
    "    border-radius: 4px;\n",
    "}\n",
    "\n",
    "::-webkit-scrollbar-thumb:hover {\n",
    "    background: #555;\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "# Create the Gradio interface\n",
    "with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue=\"indigo\", neutral_hue=\"slate\")) as demo:\n",
    "    with gr.Column(elem_id=\"main-container\"):\n",
    "        \n",
    "        # Header\n",
    "        with gr.Row(elem_classes=\"header-section\"):\n",
    "            gr.HTML(\"\"\"\n",
    "                <div>\n",
    "                    <h1>🎨 Stable Diffusion 3.5 Large</h1>\n",
    "                    <p>Professional AI Image Generation - 8B Parameter Model</p>\n",
    "                </div>\n",
    "            \"\"\")\n",
    "        \n",
    "        # Main content in two columns\n",
    "        with gr.Row():\n",
    "            # Left column - Input\n",
    "            with gr.Column(scale=1):\n",
    "                with gr.Group(elem_classes=\"input-section\"):\n",
    "                    prompt = gr.Textbox(\n",
    "                        label=\"✍️ Your Prompt\",\n",
    "                        placeholder=\"Describe the image you want to create in detail...\",\n",
    "                        lines=4,\n",
    "                        max_lines=8,\n",
    "                    )\n",
    "                    \n",
    "                    generate_btn = gr.Button(\n",
    "                        \"πŸš€ Generate Image\",\n",
    "                        variant=\"primary\",\n",
    "                        size=\"lg\",\n",
    "                        elem_classes=\"primary\"\n",
    "                    )\n",
    "                \n",
    "                # Advanced Settings\n",
    "                with gr.Accordion(\"βš™οΈ Advanced Settings\", open=False, elem_classes=\"accordion\"):\n",
    "                    negative_prompt = gr.Textbox(\n",
    "                        label=\"🚫 Negative Prompt\",\n",
    "                        placeholder=\"What to avoid in the image...\",\n",
    "                        lines=2,\n",
    "                        value=\"blurry, low quality, distorted, artifacts\"\n",
    "                    )\n",
    "                    \n",
    "                    with gr.Row():\n",
    "                        width = gr.Slider(\n",
    "                            label=\"Width (px)\",\n",
    "                            minimum=512,\n",
    "                            maximum=MAX_IMAGE_SIZE,\n",
    "                            step=64,\n",
    "                            value=1024,\n",
    "                        )\n",
    "                        \n",
    "                        height = gr.Slider(\n",
    "                            label=\"Height (px)\",\n",
    "                            minimum=512,\n",
    "                            maximum=MAX_IMAGE_SIZE,\n",
    "                            step=64,\n",
    "                            value=1024,\n",
    "                        )\n",
    "                    \n",
    "                    with gr.Row():\n",
    "                        guidance_scale = gr.Slider(\n",
    "                            label=\"Guidance Scale\",\n",
    "                            minimum=1.0,\n",
    "                            maximum=15.0,\n",
    "                            step=0.5,\n",
    "                            value=4.5,\n",
    "                            info=\"Higher = more adherence to prompt\"\n",
    "                        )\n",
    "                        \n",
    "                        num_inference_steps = gr.Slider(\n",
    "                            label=\"Quality Steps\",\n",
    "                            minimum=20,\n",
    "                            maximum=75,\n",
    "                            step=5,\n",
    "                            value=40,\n",
    "                            info=\"More steps = higher quality\"\n",
    "                        )\n",
    "                    \n",
    "                    with gr.Row():\n",
    "                        seed = gr.Slider(\n",
    "                            label=\"Seed\",\n",
    "                            minimum=0,\n",
    "                            maximum=MAX_SEED,\n",
    "                            step=1,\n",
    "                            value=42,\n",
    "                            info=\"For reproducible results\"\n",
    "                        )\n",
    "                        \n",
    "                        randomize_seed = gr.Checkbox(\n",
    "                            label=\"🎲 Randomize Seed\",\n",
    "                            value=True\n",
    "                        )\n",
    "            \n",
    "            # Right column - Output\n",
    "            with gr.Column(scale=1):\n",
    "                with gr.Group(elem_classes=\"output-section\"):\n",
    "                    result = gr.Image(\n",
    "                        label=\"Generated Image\",\n",
    "                        show_label=True,\n",
    "                        height=600,\n",
    "                        show_download_button=True,\n",
    "                        show_share_button=True\n",
    "                    )\n",
    "                    \n",
    "                    seed_output = gr.Number(\n",
    "                        label=\"Used Seed\",\n",
    "                        interactive=False,\n",
    "                        info=\"Save this to reproduce the exact image\"\n",
    "                    )\n",
    "        \n",
    "        # Example prompts\n",
    "        with gr.Row(elem_classes=\"examples\"):\n",
    "            gr.Examples(\n",
    "                examples=examples,\n",
    "                inputs=[prompt],\n",
    "                outputs=[result, seed_output],\n",
    "                fn=generate_image,\n",
    "                cache_examples=False,\n",
    "                label=\"πŸ’‘ Example Prompts - Click to try\"\n",
    "            )\n",
    "        \n",
    "        # Footer\n",
    "        gr.HTML(\"\"\"\n",
    "            <div class=\"footer\">\n",
    "                <p>Powered by Stable Diffusion 3.5 Large | Built with Gradio</p>\n",
    "                <p style=\"margin-top: 0.5rem; font-size: 0.75rem;\">Model by Stability AI β€’ Interface by Gradio</p>\n",
    "            </div>\n",
    "        \"\"\")\n",
    "    \n",
    "    # Event handlers\n",
    "    gr.on(\n",
    "        triggers=[generate_btn.click, prompt.submit],\n",
    "        fn=generate_image,\n",
    "        inputs=[\n",
    "            prompt,\n",
    "            negative_prompt,\n",
    "            seed,\n",
    "            randomize_seed,\n",
    "            width,\n",
    "            height,\n",
    "            guidance_scale,\n",
    "            num_inference_steps,\n",
    "        ],\n",
    "        outputs=[result, seed_output],\n",
    "    )\n",
    "\n",
    "# Launch with public URL\n",
    "print(\"\\nπŸš€ Launching Gradio interface...\")\n",
    "print(\"πŸ“‘ Generating public URL for sharing...\\n\")\n",
    "\n",
    "demo.launch(\n",
    "    share=True,           # Creates a public URL\n",
    "    debug=True,           # Enable debugging\n",
    "    show_error=True,      # Show errors in UI\n",
    "    inbrowser=False,      # Don't auto-open browser (for notebook)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9b4a3ae",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## πŸ“ Usage Instructions\n",
    "\n",
    "1. **Run all cells** in order from top to bottom\n",
    "2. The last cell will launch the interface and display:\n",
    "   - A **local URL** (for you)\n",
    "   - A **public URL** (shareable link valid for 72 hours)\n",
    "3. Share the public URL with anyone to access your app\n",
    "\n",
    "### Tips for Best Results:\n",
    "\n",
    "- **Be specific** in your prompts (describe lighting, style, composition)\n",
    "- Use **negative prompts** to exclude unwanted elements\n",
    "- Increase **quality steps** (50-75) for more detailed images\n",
    "- Adjust **guidance scale** (7-10) for stronger prompt adherence\n",
    "- Save the **seed number** to reproduce exact results\n",
    "\n",
    "### Example Professional Prompts:\n",
    "\n",
    "```\n",
    "Architectural photography of modern house with glass walls, \n",
    "surrounded by forest, golden hour lighting, ultra detailed, 8k\n",
    "```\n",
    "\n",
    "```\n",
    "Product photography of luxury watch on marble surface, \n",
    "studio lighting, reflections, commercial photography, high resolution\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "**Note**: The public URL expires after 72 hours. To get a new one, simply re-run the last cell."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}