Create Your Own AI Art Generator Using Stable Diffusion and Gradio

Create Your Own AI Art Generator Using Stable Diffusion and Gradio

AI art generation has taken the creative world by storm, enabling anyone to create stunning images from text prompts. In this tutorial, we’ll build a simple AI art generator using the popular Stable Diffusion model and Gradio, a Python library that lets you quickly create web-based demos. By the end, you’ll have an interactive app that transforms your text ideas into beautiful artwork.

 

🔧 Step 1: Install Required Packages

pip install diffusers transformers torch gradio

This installs the Diffusers library (which includes Stable Diffusion), Hugging Face Transformers, PyTorch for model computation, and Gradio for creating the web interface.

 

📁 Step 2: Project Structure

.
├── app.py
├── requirements.txt

 

🔐 Step 3: Set Up Your Hugging Face Token

Stable Diffusion models require authentication via a Hugging Face API token. Create an account on huggingface.co, generate a token from your profile settings, and set it as an environment variable:

export HUGGINGFACE_TOKEN="your_huggingface_token_here"

Alternatively, you can pass the token directly in the code when loading the model.

 

🧠 Step 4: Create app.py with Stable Diffusion and Gradio Integration

import torch
from diffusers import StableDiffusionPipeline
import gradio as gr
import os

# Load your Hugging Face token from env variable
hf_token = os.getenv("HUGGINGFACE_TOKEN")

# Load the Stable Diffusion model pipeline
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
    use_auth_token=hf_token
).to("cuda" if torch.cuda.is_available() else "cpu")

def generate_image(prompt):
    with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
        image = pipe(prompt).images[0]
    return image

# Define the Gradio interface
interface = gr.Interface(
    fn=generate_image,
    inputs=gr.Textbox(lines=2, placeholder="Enter your art prompt here..."),
    outputs="image",
    title="AI Art Generator using Stable Diffusion",
    description="Enter a text prompt to generate an AI-powered artwork."
)

if __name__ == "__main__":
    interface.launch()

 

💬 Step 5: Run Your AI Art Generator

python app.py

After running, a local URL will be provided by Gradio. Open it in your browser, enter a creative text prompt, and watch your AI-generated art come to life!

 

📌 Notes:

  • Make sure you have a GPU for faster image generation; otherwise, the CPU mode will be slower but still functional.
  • You can experiment with different Stable Diffusion models available on Hugging Face by changing the model ID.
  • Adjust the prompt creatively to generate different art styles and subjects.
  • Gradio also supports sharing your app publicly via a link with interface.launch(share=True).

 

✅ Conclusion

You’ve just created your own AI art generator with Stable Diffusion and Gradio! This simple yet powerful setup opens doors to endless creative possibilities powered by AI. Whether you want to prototype art tools, create unique content, or simply experiment, this foundation can be extended with features like image saving, prompt history, or style customization. Dive in and let your imagination run wild!