Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Tutorials
    • All tutorials
      • Deploying a service in DataSphere from an ONNX model
      • Deploying a service in DataSphere from a Docker image
      • Deploying a service in DataSphere from a Docker image with FastAPI
      • Deploying a gRPC service based on a Docker image
      • Image generation using the Stable Diffusion model

In this article:

  • Getting started
  • Required paid resources
  • Prepare the infrastructure
  • Create a project
  • Create a notebook and install the libraries
  • Create a model and generate an image
  • How to delete the resources you created
  1. Machine learning and artificial intelligence
  2. Usage DataSphere
  3. Image generation using the Stable Diffusion model

Image generation using the Stable Diffusion model

Written by
Yandex Cloud
Updated at March 6, 2025
  • Getting started
    • Required paid resources
  • Prepare the infrastructure
    • Create a project
    • Create a notebook and install the libraries
  • Create a model and generate an image
  • How to delete the resources you created

In DataSphere, you can deploy a neural network based on the Stable Diffusion model and generate images based on text descriptions.

Stable Diffusion is an open-source text-to-image model developed by stability.ai.

In this tutorial, you will generate an image based on text description by implementing the Stable Diffusion model in the Diffusers library. This library prioritizes ease of use and customization over performance.

To generate an image using the Stable Diffusion model:

  1. Prepare your infrastructure.
  2. Create a model and generate an image.

If you no longer need the resources you created, delete them.

Getting started

Before getting started, register in Yandex Cloud, set up a community, and link your billing account to it.

  1. On the DataSphere home page, click Try for free and select an account to log in with: Yandex ID or your working account with the identity federation (SSO).
  2. Select the Yandex Cloud Organization organization you are going to use in Yandex Cloud.
  3. Create a community.
  4. Link your billing account to the DataSphere community you are going to work in. Make sure you have a linked billing account and its status is ACTIVE or TRIAL_ACTIVE. If you do not have a billing account yet, create one in the DataSphere interface.

Note

If you use an identity federation to access Yandex Cloud, billing details might be unavailable to you. In this case, contact your Yandex Cloud organization administrator.

Required paid resources

The cost of using the model includes a fee for running code cells (see DataSphere pricing).

Prepare the infrastructure

Create a project

  1. Open the DataSphere home page.
  2. In the left-hand panel, select Communities.
  3. Select the community to create a project in.
  4. On the community page, click Create project.
  5. In the window that opens, enter Stable Diffusion as your project name and add a description (optional).
  6. Click Create.

Create a notebook and install the libraries

Note

In this tutorial, all computations use the g1.1 configuration. However, you can run the model on other configurations as well.

  1. In the DataSphere interface, open the project you created.

  2. Create a new notebook:

    1. In the top panel of the project window, click File → New → Notebook.
    2. In the window that opens, select DataSphere Kernel.
  3. Install the Diffusers library. Paste the below code into the cell and click :

    %pip install diffusers
    
  4. Install the Transformers library:

    %pip install transformers
    
  5. Once the installation is complete, select Kernel ⟶ Restart kernel... on the top panel.

Create a model and generate an image

  1. Import the libraries to the project:

    from diffusers import StableDiffusionPipeline
    import torch
    
  2. Create a model:

    model_id = "runwayml/stable-diffusion-v1-5"
    pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
    pipe = pipe.to("cuda")
    
  3. Generate an image by its description:

    prompt = "a photo of an astronaut riding a horse on mars"
    image = pipe(prompt).images[0]
    
  4. Save the output image:

    image.save("astronaut_rides_horse.png")
    

    The image file will appear next to the notebook. Result:

    generate-image

How to delete the resources you created

If you no longer plan to use the Stable Diffusion project, delete it.

See also

  • Working with models
  • Running computations in DataSphere using the API

Was the article helpful?

Previous
Deploying a gRPC service based on a Docker image
Next
Analyzing data with Query
Yandex project
© 2025 Yandex.Cloud LLC