[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Machine learning and artificial intelligence](index.md) > Usage DataSphere > Image generation using the Stable Diffusion model

# Image generation using the Stable Diffusion model

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

[Stable Diffusion](https://github.com/CompVis/stable-diffusion) is an open-source text-to-image model developed by [stability.ai](https://stability.ai/).

In this tutorial, you will generate an image based on a text description by implementing the Stable Diffusion model in the [Diffusers](https://huggingface.co/docs/diffusers/index) library. This library prioritizes ease of use and customization over performance.

To generate an image using the Stable Diffusion model:

1. [Set up your infrastructure](#infra).
1. [Create a model and generate an image](#generate).

If you no longer need the resources you created, [delete them](#clear-out).

## Getting started {#before-you-begin}

Before getting started, register in Yandex Cloud, set up a [community](../../datasphere/concepts/community.md), and link your [billing account](../../billing/concepts/billing-account.md) to it.
1. [On the DataSphere home page](https://datasphere.yandex.cloud), click **Try for free** and select an account to log in with: Yandex ID or your working account with the identity federation (SSO).
1. Select the [Yandex Identity Hub organization](../../organization/index.md) you are going to use in Yandex Cloud.
1. [Create a community](../../datasphere/operations/community/create.md).
1. [Link your billing account](../../datasphere/operations/community/link-ba.md) to the DataSphere community you are going to work in. Make sure you have a linked billing account and its [status](../../billing/concepts/billing-account-statuses.md) is `ACTIVE` or `TRIAL_ACTIVE`. If you do not have a billing account yet, create one in the DataSphere interface.

{% note info %}

If you are using an [identity federation](../../organization/concepts/add-federation.md) to work with Yandex Cloud, you might not have access to billing details. In this case, contact your Yandex Cloud organization administrator.

{% endnote %}

### Required paid resources {#paid-resources}

The cost of using the model includes a fee for running code cells (see [DataSphere pricing](../../datasphere/pricing.md)).

## Set up your infrastructure {#infra}

### Create a project {#create-project}

1. Open the DataSphere [home page](https://datasphere.yandex.cloud).
1. In the left-hand panel, select ![image](../../_assets/console-icons/circles-concentric.svg) **Communities**.
1. Select the community where you want to create a project.
1. On the community page, click ![image](../../_assets/console-icons/folder-plus.svg) **Create project**.
1. In the window that opens, enter `Stable Diffusion` as your project name and add a description (optional).
1. Click **Create**.

### Create a notebook and install the libraries {#install-libraries}

{% note info %}

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

{% endnote %}

1. In the DataSphere interface, open the project you created.
1. Create a new notebook:

   1. In the top panel of the project window, click **File** → **New** → **Notebook**.
   1. In the window that opens, select **DataSphere Kernel**.

1. Install the [Diffusers](https://huggingface.co/docs/diffusers/index) library. Paste the below code into the cell and click ![run](../../_assets/datasphere/jupyterlab/run.svg):

   ```python
   %pip install diffusers
   ```

1. Install the [Transformers](https://huggingface.co/docs/transformers/index) library:

   ```python
   %pip install transformers
   ```

1. Once the installation is complete, select **Kernel** ⟶ **Restart kernel...** on the top panel.

## Create a model and generate an image {#generate}

1. Import the libraries to the project:

   ```python
   from diffusers import StableDiffusionPipeline
   import torch
   ```

1. Create a model:

   ```python
   model_id = "runwayml/stable-diffusion-v1-5"
   pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
   pipe = pipe.to("cuda")
   ```

1. Generate an image by its description:

   ```python
   prompt = "a photo of an astronaut riding a horse on mars"
   image = pipe(prompt).images[0]
   ```

1. Save the output image:

   ```python
   image.save("astronaut_rides_horse.png")
   ```

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

   ![generate-image](../../_assets/datasphere/generated-image.png)

## How to delete the resources you created {#clear-out}

If you no longer plan to use the `Stable Diffusion` project, [delete it](../../datasphere/operations/projects/delete.md#delete-project).

#### See also {#see-also}

* [Working with models](../../datasphere/operations/data/models.md)
* [Running computations in DataSphere using the API](../../datasphere/tutorials/batch-code-execution.md)