forked from mlc-ai/web-llm-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support stable diffusion plugin (mlc-ai#49)
- Loading branch information
Showing
14 changed files
with
269 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/api/langchain-tools/stable_diffusion_image_generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Tool } from "langchain/tools"; | ||
import S3FileStorage from "../../utils/r2_file_storage"; | ||
|
||
export class StableDiffusionWrapper extends Tool { | ||
name = "stable_diffusion_image_generator"; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
/** @ignore */ | ||
async _call(prompt: string) { | ||
let url = process.env.STABLE_DIFFUSION_API_URL; | ||
const data = { | ||
prompt: prompt, | ||
negative_prompt: | ||
process.env.STABLE_DIFFUSION_NEGATIVE_PROMPT ?? | ||
"longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", | ||
seed: -1, | ||
subseed: -1, | ||
subseed_strength: 0, | ||
batch_size: 1, | ||
n_iter: 1, | ||
steps: process.env.STABLE_DIFFUSION_STEPS ?? 20, | ||
cfg_scale: process.env.STABLE_DIFFUSION_CFG_SCALE ?? 7, | ||
width: process.env.STABLE_DIFFUSION_WIDTH ?? 720, | ||
height: process.env.STABLE_DIFFUSION_HEIGHT ?? 720, | ||
restore_faces: process.env.STABLE_DIFFUSION_RESTORE_FACES ?? false, | ||
eta: 0, | ||
sampler_index: process.env.STABLE_DIFFUSION_SAMPLER_INDEX ?? "Euler a", | ||
}; | ||
console.log(`[${this.name}]`, data); | ||
const response = await fetch(`${url}/sdapi/v1/txt2img`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
const json = await response.json(); | ||
let imageBase64 = json.images[0]; | ||
if (!imageBase64) return "No image was generated"; | ||
const buffer = Buffer.from(imageBase64, "base64"); | ||
const filePath = await S3FileStorage.put(`${Date.now()}.png`, buffer); | ||
console.log(`[${this.name}]`, filePath); | ||
return filePath; | ||
} | ||
|
||
description = `stable diffusion is an ai art generation model similar to dalle-2. | ||
input requires english. | ||
output will be the image link url. | ||
use markdown to display images. like: `; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Stable Diffusion 插件配置指南 | ||
|
||
## 前置条件 | ||
|
||
1. 部署 [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui) 项目,并保证可以正常使用 (必须) | ||
|
||
2. 在 [Civitai](https://civitai.com/) 挑选喜欢的底座模型 (可选) | ||
|
||
3. 生成一张图片并记住相关的参数配置 (可选) | ||
|
||
 | ||
|
||
## 环境变量 | ||
|
||
- `STABLE_DIFFUSION_API_URL`(必填) | ||
|
||
stable-diffusion-webui 服务的 api 地址,示例:http://127.0.0.1:7860 | ||
|
||
- `STABLE_DIFFUSION_NEGATIVE_PROMPT`(可选) | ||
|
||
反向提示词(Negative Prompt),默认值:`longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality` | ||
|
||
- `STABLE_DIFFUSION_STEPS`(可选) | ||
|
||
采样迭代步数(Steps),默认值:`20` | ||
|
||
- `STABLE_DIFFUSION_CFG_SCALE`(可选) | ||
|
||
提示词相关性(CFG Scale),默认值:`7` | ||
|
||
- `STABLE_DIFFUSION_WIDTH`(可选) | ||
|
||
生成图像宽度,默认值:`720` | ||
|
||
- `STABLE_DIFFUSION_HEIGHT`(可选) | ||
|
||
生成图像高度,默认值:`720` | ||
|
||
- `STABLE_DIFFUSION_SAMPLER_INDEX`(可选) | ||
|
||
采样方法(Sampler),默认值:`Euler a` | ||
|
||
## 如何使用 | ||
|
||
由于 OpenAI 的函数描述输入字符数量有限,在描述中并不能很好的将 Stable Diffusion 的提示词规则告诉 GPT。 | ||
|
||
所以这里提供了一个 Stable Diffusion 面具用来间接告诉 GPT 该如何调用 Stable Diffusion 插件,详细的内容请查看该面具的提示内容,当前您也可以自行修改。 | ||
|
||
这里需要注意,R2 存储在中国网络环境下可能无法正常使用,请确保你的网络可以正常访问 R2 存储服务,否则将无法正常使用本插件。 | ||
|
||
1. 首先第一步根据上面的章节配置好插件的变量 | ||
|
||
2. 在插件中开启 StableDiffusion 插件 | ||
|
||
 | ||
|
||
3. 在面具中找到 StableDiffusion 面具并进行对话 | ||
|
||
 | ||
|
||
4. 使用愉快 | ||
|
||
## 示例 | ||
|
||
 | ||
|
||
模型:[GhostMix - v2.0-BakedVAE | Stable Diffusion Checkpoint | Civitai](https://civitai.com/models/36520/ghostmix) | ||
|
||
环境变量: | ||
|
||
``` | ||
STABLE_DIFFUSION_API_URL=http://127.0.0.1:7860 | ||
STABLE_DIFFUSION_WIDTH=512 | ||
STABLE_DIFFUSION_HEIGHT=768 | ||
STABLE_DIFFUSION_NEGATIVE_PROMPT=(worst quality, low quality:2), monochrome, zombie,overexposure, watermark,text,bad anatomy,bad hand,extra hands,extra fingers,too many fingers,fused fingers,bad arm,distorted arm,extra arms,fused arms,extra legs,missing leg,disembodied leg,extra nipples, detached arm, liquid hand,inverted hand,disembodied limb, small breasts, loli, oversized head,extra body,completely nude, extra navel,easynegative,(hair between eyes),sketch, duplicate, ugly, huge eyes, text, logo, worst face, (bad and mutated hands:1.3), (blurry:2.0), horror, geometry, bad_prompt, (bad hands), (missing fingers), multiple limbs, bad anatomy, (interlocked fingers:1.2), Ugly Fingers, (extra digit and hands and fingers and legs and arms:1.4), ((2girl)), (deformed fingers:1.2), (long fingers:1.2),(bad-artist-anime), bad-artist, bad hand, extra legs ,(ng_deepnegative_v1_75t) | ||
STABLE_DIFFUSION_STEPS=30 | ||
STABLE_DIFFUSION_CFG_SCALE=6 | ||
``` | ||
|
Oops, something went wrong.