
In this hands-on tutorial, we will unlock the stability AI’s industry-municipality model, stable spread V1.5, stability AI’s V2-base, and state-of-the-art stable spread 3 mediums, the creative ability of stability to generate the eye-catching imagination. The gradio is running completely in Google Colab with the interface, we will experience side-by-side comparison compared to three powerful pipelines, rapid recurrences, and seamless GPU-quick conclusions. Whether we are looking to elevate the visual story of our brand or a developer eager to prototype the AI-operated content workflows, this tutorial shows how stability allows AI’s open-source model to be deployed immediately and on the cost of any infrastructure, allowing you to focus on the story, busy, and the consequences of the real war.
!pip install huggingface_hub
from huggingface_hub import notebook_login
notebook_login()
We install Hugging Fes_Hhab Library and then import and invite the notebook_Login () function, which motivates you to certify your notebook session with your hauging face account, which can basically access and manage models, datasets and other hub resources.
!pip uninstall -y torchvision
!pip install --upgrade torch torchvision --index-url https://download.pytorch.org/whl/cu118
!pip install --upgrade diffusers transformers accelerate safetensors gradio pillow
We first emphasize any existing torque to clean potential conflicts, then restore torches and torchs from CUDA 11.8-computable pitorch wheels, and finally upgrade the major libraries, defuser, transformer, speed, saffheters, and taks, and upgrade the torch, and to ensure that there are latest versions to ensure that the building-building is the latest.
import torch
from diffusers import StableDiffusionPipeline, StableDiffusion3Pipeline
import gradio as gr
device = "cuda" if torch.cuda.is_available() else "cpu"
We import Pytorch with both gradio to manufacture interactive demo with stable defusion V1 and V3 pipelines from the Depuis Library. It then checks for CUDA availability and if GPU is present, the device sets the variable to “cuda”; Otherwise, it returns to “CPU”, ensuring that your models run on optimal hardware.
pipe1 = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16,
safety_checker=None
).to(device)
pipe1.enable_attention_slicing()
We load the steady V1.5 model in half-clarity (Float16) without the underlying safety checker, transferring it to your selected device (GPU, if available), and then enables attention to reduce peak vram use during the image generation.
pipe2 = StableDiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-base",
torch_dtype=torch.float16,
safety_checker=None
).to(device)
pipe2.enable_attention_slicing()
We load the stable defusion V2 “base” model in the 16-bit proceeds without a default safety filter, transfer it to your chosen device, and activate the slicing that pay attention to customize memory use during estimates.
pipe3 = StableDiffusion3Pipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers",
torch_dtype=torch.float16,
safety_checker=None
).to(device)
pipe3.enable_attention_slicing()
We pull the stability of stability AI in 16-bit prisons (excluding the underlying security checker) 3 “medium” checkpoints, transfer it to your selected device, and enables attention to slicing to reduce the GPU memory use during generation.
def generate(prompt, steps, scale):
img1 = pipe1(prompt, num_inference_steps=steps, guidance_scale=scale).images[0]
img2 = pipe2(prompt, num_inference_steps=steps, guidance_scale=scale).images[0]
img3 = pipe3(prompt, num_inference_steps=steps, guidance_scale=scale).images[0]
return img1, img2, img3
Now, this function runs the same text prompt through all three loaded pipelines (Pipe1, PIPY2, Pipe3), which uses the specified estimate stages and guidance scale, then returns the first image from each, which is perfect to compare it to the output in V1.5, V2-BASE, and V3-Medium.
def choose(selection):
return f"✅ You selected: **{selection}**"
with gr.Blocks() as demo:
gr.Markdown("## AI Social-Post Generator with 3 Models")
with gr.Row():
prompt = gr.Textbox(label="Prompt", placeholder="A vibrant beach sunset…")
steps = gr.Slider( 1, 100, value=50, step=1, label="Inference Steps")
scale = gr.Slider( 1.0, 20.0, value=7.5, step=0.1, label="Guidance Scale")
btn = gr.Button("Generate Images")
with gr.Row():
out1 = gr.Image(label="Model 1: SD v1.5")
out2 = gr.Image(label="Model 2: SD v2-base")
out3 = gr.Image(label="Model 3: SD v3-medium")
sel = gr.Radio(
["Model 1: SD v1.5","Model 2: SD v2-base","Model 3: SD v3-medium"],
label="Select your favorite"
)
txt = gr.Markdown()
btn.click(fn=generate, inputs=[prompt, steps, scale], outputs=[out1, out2, out3])
sel.change(fn=choose, inputs=sel, outputs=txt)
demo.launch(share=True)
Finally, this Gradio app creates a three-pillar UI, where you can enter a text prompt, adjust the invention steps and guidance scale, then SD V1.5, V2-BASE and V3-Medium side can generate and display images. It also has a radio selector, which allows you to select your favorite model output, and displays a simple confirmation message when there is an option.
In the conclusion, by integrating the state-of-the-art spreading architecture of stability AI in an easy-to-use Gradio app, you have noticed how much prototype, comparison you can compare, compare and deploy the stunning scenes echoing on today’s platforms. To automate the A/B-testing creative directions to automatic campaign assets, stability AI provides performance, flexibility and lively community support to replace your content pipeline.
Check it Collab notebookDon’t forget to follow us Twitter And join us Wire And LinkedIn GROUPDon’t forget to join us 90K+ ML Subredit, For promotion and partnership, please talk to us.
[Register Now] Minicon Virtual Conference on Agentic AI: Free Registration + attendance Certificate + 4 hours small incident (May 21, 9 am- 1 am PST)
Nikhil is a trainee advisor in Marktekpost. He is chasing an integrated dual degree in materials at the Indian Institute of Technology, Kharagpur. Nikhil is an AI/mL enthusiast who is always researching applications in areas such as biometric and biomedical science. With a strong background in physics, he is searching for new progress and creating opportunities to contribute.