# Stable Diffusion

Stable Diffusion là một công cụ tạo hình ảnh dựa trên các văn bản gợi ý. ChatX đã triển khai giao diện để truy cập API Stable Diffusion WebUI, vì vậy bạn có thể sử dụng nó trực tiếp trong ChatX. Dưới đây là các bước để tích hợp Stable Diffusion trong ChatX.

## 1. Đảm bảo bạn có một máy tính với GPU <a href="#id-1.-make-sure-you-have-a-machine-with-a-gpu" id="id-1.-make-sure-you-have-a-machine-with-a-gpu"></a>

Stable Diffusion yêu cầu một máy tính với GPU để tạo hình ảnh. Tuy nhiên, nếu không có GPU, bạn có thể sử dụng CPU để tạo hình ảnh, nhưng sẽ chậm hơn.

## 2. Khởi động Stable Diffusion WebUI <a href="#id-2.-launch-stable-diffusion-webui" id="id-2.-launch-stable-diffusion-webui"></a>

Khởi động Stable Diffusion WebUI trên máy tính hoặc máy chủ của bạn.

#### 2.1. Clone repository Stable Diffusion WebUI <a href="#id-2.1.-clone-the-stable-diffusion-webui-repository" id="id-2.1.-clone-the-stable-diffusion-webui-repository"></a>

Clone repository Stable Diffusion WebUI từ [official repository](https://github.com/AUTOMATIC1111/stable-diffusion-webui)

```
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
```

#### 2.2. Khởi động WebUI cục bộ <a href="#id-2.2.-launch-it-locally" id="id-2.2.-launch-it-locally"></a>

Sau khi clone repository, bạn nên thay đổi thư mục đến repository đã clone và chạy lệnh sau để khởi động Stable Diffusion WebUI.

**Windows**

```
cd stable-diffusion-webui
./webui.bat --api --listen
```

**Linux**

```
cd stable-diffusion-webui
./webui.sh --api --listen
```

#### 2.3. Chuẩn bị Models <a href="#id-2.3.-prepare-models" id="id-2.3.-prepare-models"></a>

Bây giờ bạn có thể truy cập Stable Diffusion WebUI trên trình duyệt của mình theo địa chỉ hiển thị trong terminal, nhưng các models chưa sẵn sàng. Bạn cần tải các models từ HuggingFace hoặc các nguồn khác và đặt chúng vào thư mục `models` của Stable Diffusion WebUI.

Ví dụ, chúng ta sử dụng [pastel-mix](https://huggingface.co/JamesFlare/pastel-mix) làm model, sử dụng `git lfs` để tải model và đặt nó vào thư mục `models` trong `stable-diffusion-webui`.

```
git clone https://huggingface.co/JamesFlare/pastel-mix
```

#### 2.4 Lấy Tên Model <a href="#id-2.4-get-model-name" id="id-2.4-get-model-name"></a>

Bây giờ bạn có thể thấy `pastel-mix` trong danh sách model, nhưng chúng ta vẫn cần lấy tên model. Truy cập `http://your_id:port/sdapi/v1/sd-models`, bạn sẽ thấy tên model như bên dưới.

```
[
    {
        "title": "pastel-mix/pastelmix-better-vae-fp32.ckpt [943a810f75]",
        "model_name": "pastel-mix_pastelmix-better-vae-fp32",
        "hash": "943a810f75",
        "sha256": "943a810f7538b32f9d81dc5adea3792c07219964c8a8734565931fcec90d762d",
        "filename": "/home/takatost/stable-diffusion-webui/models/Stable-diffusion/pastel-mix/pastelmix-better-vae-fp32.ckpt",
        "config": null
    },
]
```

`model_name` là thông tin chúng ta cần, trong trường hợp này là `pastel-mix_pastelmix-better-vae-fp32`.

## 3. Tích hợp Stable Diffusion vào ChatX <a href="#id-3.-integrate-stable-diffusion-in-dify" id="id-3.-integrate-stable-diffusion-in-dify"></a>

Điền vào Cấu hình Authentication và Model trong `Tools > StableDiffusion > To Authorize` với thông tin bạn nhận được từ các bước trước.

## 4. Hoàn thành <a href="#id-4.-finish" id="id-4.-finish"></a>

Chỉ cần thử sử dụng nó trong ChatX!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.chatx.vn/iv-huong-dan-tich-hop-nang-cao/ket-noi-voi-cac-cong-cu-khac-nhau/stable-diffusion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
