天天看點

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

作者:技術聯盟總壇

寒斜 阿裡雲雲原生 2023-05-05 18:31 發表于浙江

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

上一篇講了如何使用 Serverless Devs 和函數計算快速體驗部署 Stable Diffusion,本篇繼續聊聊如何解決動态模型加載的問題,從玩起來到用起來。

思路

Cloud Native

其實很簡單, 我們隻需要将鏡像裡面的動态路徑映射到 NAS[1] 檔案存儲裡面即可,利用 NAS 獨立存儲檔案模型,擴充,語言包等,并且我們可以為管理 NAS 單獨配置一個可視化的背景,用簡單的檔案上傳删除的方式管理我們的檔案,為此我們需要展開打鏡像的腳本,為了完成全套的流程我們接下來盤一下整體的準備項。

準備項

Cloud Native

1. 開通阿裡雲函數計算[2]

2. 檔案存儲 NAS(可以根據情況自己建立性能更好的執行個體)

3. 開通阿裡雲容器鏡像服務 ACR[3]

4. 安裝 Serverless Devs[4]

$ npm install @serverless-devs/s -g           

5. 使用 Serverless Devs 配置阿裡雲密鑰資訊[5]

快速開始

Cloud Native

初始化應用模闆

s init fc-stable-diffusion-plus           

選擇 region:cn-hangzhou

輸入鏡像:registry.cn-hangzhou.aliyuncs.com/serverlessdevshanxie/sd-auto-nas:v1

部署應用模闆

cd fc-stable-diffusion-plus && s deploy           

部署需要花費一些時間(預估 5 分鐘),之後你會收到兩個域名傳回。

fc-nas-init: 

  region:   cn-hangzhou

  service: 

    name: fc-stable-diffusion-plus

  function: 

    name:       nas-init

    runtime:    python3.9

    handler:    index.handler

    memorySize: 3072

    timeout:    1200

    cpu:        2

    diskSize:   512

fc-nas-filemgr: 

  region:   cn-hangzhou

  service: 

    name: fc-stable-diffusion-plus

  function: 

    name:       admin

    runtime:    custom

    handler:    index.handler

    memorySize: 3072

    timeout:    7200

    cpu:        2

    diskSize:   512

  url: 

    system_url:          https://xxx.cn-hangzhou.fcapp.run

    system_intranet_url: https://xxx.cn-hangzhou-vpc.fcapp.run

    custom_domain: 

      - 

        domain: http://admin.fc-stable-diffusion-plus.xxxx.cn-hangzhou.fc.devsapp.net

  triggers: 

    - 

      type: http

      name: httpTrigger

keep-warm: 

  region:   cn-hangzhou

  service: 

    name: fc-stable-diffusion-plus

  function: 

    name:       keep-warm

    runtime:    python3

    handler:    index.handler

    memorySize: 128

    timeout:    120

    cpu:        0.1

    diskSize:   512

  triggers: 

    - 

      type: timer

      name: timerTrigger

stable-diffusion-sd-server: 

  region:   cn-hangzhou

  service: 

    name: fc-stable-diffusion-plus

  function: 

    name:       sd

    runtime:    custom-container

    handler:    index.handler

    memorySize: 32768

    timeout:    600

    cpu:        8

    diskSize:   10240

  url: 

    system_url:          https://xxx.cn-hangzhou.fcapp.run

    system_intranet_url: https://xxx.cn-hangzhou-vpc.fcapp.run

    custom_domain: 

      - 

        domain: http://sd.fc-stable-diffusion-plus.xxxxx.cn-hangzhou.fc.devsapp.net

  triggers: 

    - 

      type: http

      name: defaultTrigger           

其中:

http://admin.fc-stable-diffusion-plus.xxxx.cn-hangzhou.fc.devsapp.net 是背景管理頁面

http://sd.fc-stable-diffusion-plus.xxxxx.cn-hangzhou.fc.devsapp.net 是 Stable Diffusion 的操作界面

上傳模型

因為上面容器鏡像中移除了模型部分,是以你現在還無法啟動 Stable Diffusion 的操作界面,需要上傳自己的模型,之後再去啟動 Stable Diffusion 操作界面,上傳模型有兩個方法:

方法一

通過可視化界面上傳,通路 http://admin.fc-stable-diffusion-plus.xxxx.cn-hangzhou.fc.devsapp.net,點點點,配好登入密碼之後,進來到這個界面:檔案管理,然後在路徑欄裡輸入"/mnt/auto/sd"

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?
【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?
【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

你就看到自己熟悉的本地目錄了~

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

然後再通路 models/Stable-diffusion/ 把模型拖進來就行。

不過值得注意的是,這種方式上傳的模型超過 2、3 個 G 的時候會不穩定。是以如果你的模型很大,可以考慮用我們提供的 nas 指令。

方法二

使用 nas 指令上傳,進入項目根目錄,執行↓

s fc-nas-init nas upload -r <你的模型位址> /mnt/auto/sd/models/Stable-diffusion/           

就可以,剩下的就是等待,可能要花費 30 分鐘。

注:其他的如 controlNet,extension 可以按照類似的方式,不過注意對應的 nas 位址變一下。

頁面預覽

準備就緒後我們開始使用頁面了,打開這個位址↓

http://sd.fc-stable-diffusion-plus.xxxxx.cn-hangzhou.fc.devsapp.net 冷啟動會有一定的加載時間。

文生圖

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

圖生圖

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

其他的自己可以多摸索摸索。

有獎體驗

Cloud Native

阿裡雲将提供免費 Serverless 函數計算産品資源,邀請你,體驗一把 AIGC 級的畢加索、達芬奇、梵高等大師作畫的快感。下面請盡情發揮你的想象空間!!雙重獎品設定,完成體驗場景可得社群 1000 積分兌換獎品,還可參加 AI 生成圖像比賽赢取 Airpods、500 元貓超卡及社群定制抱枕!

體驗位址:

你也可以輕松做出下方畫作

【自己更換模型】如何用 Serverless 一鍵部署 Stable Diffusion?

相關連結:

[1] NAS

[2] 函數計算

[3] 容器鏡像服務 ACR

[4] Serverless Devs

[5] 配置阿裡雲密鑰資訊