{ "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", "
\n", "

šŸŽØ Stable Diffusion 3.5 Large

\n", "

Professional AI Image Generation - 8B Parameter Model

\n", "
\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", "
\n", "

Powered by Stable Diffusion 3.5 Large | Built with Gradio

\n", "

Model by Stability AI • Interface by Gradio

\n", "
\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 }