In this practical tutorial, we will unlock the creative potential of Stability aiIndustry state -of -the -art diffusion models, Stable diffusion V1.5, stability of the AI V2 base and a stable 3 to point diffusion medium.To generate catchy images. Performing entirely in Google Colab with a Gradio interface, we will undergo comparisons side by side of three powerful pipelines, rapid rapid iteration and accelerated seamless inference without seam. Whether we are a marketing specialist who seeks to raise the visual story of our brand or a developer wishing to prototyper the workflows focused on AI, this tutorial shows how the stability of the open-source models of AI can be deployed instantly and without the cost of infrastructure, which allows you to focus on the narration, commitment and conduct of real results.
!pip install huggingface_hub
from huggingface_hub import notebook_login
notebook_login()
We install the Huggingface_Hub library, then import and invoke the Notebook_Login () function, which invites you to authenticate your laptop session with your Hugging Face account, allowing you to access and managing models, data data and other HUB resources and other 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 inquest all existing torchion to eliminate potential conflicts, then reinstall the torch and the Torchion from the compatible pytorch wheels Cuda 11.8, transformers, accelerate, backups, gradio and pillew, to ensure versions of the last versions for construction and the latest gpu-ocelerars.
import torch
from diffusers import StableDiffusionPipeline, StableDiffusion3Pipeline
import gradio as gr
device = "cuda" if torch.cuda.is_available() else "cpu"
We import Pytorch alongside the stable Diffusion pipelines V1 and V3 of the Diffusers library, as well as Gradio for the construction of interactive demos. It then checks the availability Cuda and defines the device variable on “Cuda” if a GPU is present; Otherwise, it falls back to “CPU”, ensuring that your models work on optimal equipment.
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 stable diffusion model V1.5 in half-registration (Float16) without the integrated safety verifier, transfers it to your selected device (GPU, if available), then allows you to decide the attention to reduce the use of maximum VRAM during the generation of images.
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 V2 diffusion “base” model in 16 -bit precision without the default safety filter, transfers it to your chosen device and activates the attention to optimize the use of memory during inference.
pipe3 = StableDiffusion3Pipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers",
torch_dtype=torch.float16,
safety_checker=None
).to(device)
pipe3.enable_attention_slicing()
We approach the stability of the stable diffusion of AI 3 “average” control in 16 -bit precision (jumping the integrated safety verifier), transfers it to your selected device and allows the cutting of attention to reduce the use of GPU memory 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 performs the same text invite through the three loaded pipelines (pipe1, pipe2, pipe3) using the specified inference steps and the guidance scale, then returns the first image of each, which makes it perfect to compare outings through stable diffusion 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 application builds a three-column user interface where you can enter a text prompt, adjust the inference steps and the guide scale, then generate and display images from SD V1.5, V2-Base and V3-Medium side by side. It also has a radio selector, allowing you to select your favorite model output and displays a simple confirmation message when a choice is made.
In conclusion, by integrating the AI stability cutting-edge diffusion architectures in an easy-to-use Gradio application, you have seen how much you can prototyper, compare and deploy amazing visuals that resonate on today's platforms. From creative A / B test instructions to the automation of large -scale campaign assets, AI stability offers performance, flexibility and dynamic community support to transform your content pipeline.
Discover the Colaab. Don't forget to follow us Twitter And join our Telegram And Linkedin Group. Don't forget to join our 90K + ML Subdreddit. For promotion and partnerships, Please talk to us.
Nikhil is an intern consultant at Marktechpost. It pursues a double degree integrated into materials at the Indian Kharagpur Institute of Technology. Nikhil is an IA / ML enthusiast who is still looking for applications in fields like biomaterials and biomedical sciences. With a strong experience in material science, he explores new progress and creates opportunities to contribute.
