链载Ai

标题: Dify 谷歌翻译 使用中文提示词实现本地文生图 [打印本页]

作者: 链载Ai    时间: 2 小时前
标题: Dify 谷歌翻译 使用中文提示词实现本地文生图

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">文章主要内容:

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 20px;font-weight: bold;display: table;margin: 2em auto 1.5em;padding-top: 6px;padding-bottom: 6px;padding-left: 16.7448px;background-image: linear-gradient(135deg, rgb(113, 23, 234), rgba(113, 23, 234, 0.667), rgba(234, 96, 96, 0.533), rgba(217, 57, 205, 0.267), rgba(217, 57, 205, 0));background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;color: rgb(255, 255, 255);border-radius: 8px;width: 318.247px;">Stable Diffusion WebUI 安装

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">首先是Stable Diffusion WebUI[1]的安装,AUTOMATIC1111 / stable-diffusion-webui这个项目提供了各个平台的安装脚本,但是由于他的脚本使用了venv本地创建了一个 Python 虚拟环境,加之网络、证书等问题,还是出现了不少波折。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;font-weight: bold;margin: 2em 8px 0.75em;border-left: 5px solid rgb(161, 27, 218);color: rgb(63, 63, 63);padding: 6px 8px;">苹果芯片

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">我使用了 MacOS 系统(苹果的芯片),貌似比 Windows\Linux 复杂些,作者专门写了个页面来说明:

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">《Installation on Apple Silicon》[2]

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;font-weight: bold;margin: 2em 8px 0.75em;border-left: 5px solid rgb(161, 27, 218);color: rgb(63, 63, 63);padding: 6px 8px;">基础库安装

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">安装一些 SD WebUI 环境需要的一些基础库

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;overflow-x: auto;border-radius: 8px;margin: 10px 8px;">brewinstallcmakeprotobufrustpython@3.10wget

代码库下载

下载代码:

gitclonehttps://github.com/AUTOMATIC1111/stable-diffusion-webui

然后进入目录:

cdstable-diffusion-webui

然后执行./webui.sh命令就好了。

执行过程中遇到了些问题,可能少部分人也会遇到。

Pip ssl certificate error

在 SD WebUI 自己创建的环境中,执行pip install的时候说证书错误。

WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)'))': /simple/torch/ Could not fetch URLhttps://pypi.org/simple/torch/[3]: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/torch/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)'))) - skipping

对于这个问题最简单的方案是让pip信任对应的站点。

可以修改webui-macos-env.sh文件,

增加 pip install 参数:

修改后的命令如下:

exportTORCH_COMMAND="pipinstall--trusted-hostpypi.org--trusted-hostfiles.pythonhosted.orgtorch==2.3.1torchvision==0.18.1"

好处是执行pytorch安装成功了,不好的是还有别的安装失败了。

Github 网络问题

WebUI 安装clip等包的时候要从 Github 上下载 Zip 包,网络的问题自然是下载失败了。

我的解决方案是,手动下载 Zip 包放到本地,然后修改环境变量:

exportCLIP_PACKAGE="--trusted-hostpypi.org--trusted-hostfiles.pythonhosted.org~/Downloads/CLIP-d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip"
exportOPENCLIP_PACKAGE="--trusted-hostpypi.org--trusted-hostfiles.pythonhosted.org~/Downloads/open_clip-bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b.zip"

Pip 全局证书信任

后续其他包还有 SSL 证书不认的情况,我们可以把这个忽略SSL的指令加到全局。

执行pip config list -v查看 Pip 配置文件位置:

>pipconfiglist-v
Forvariant'global',willtryloading'/Library/ApplicationSupport/pip/pip.conf'
Forvariant'user',willtryloading'/Users/shuyi/.pip/pip.conf'
Forvariant'user',willtryloading'/Users/shuyi/.config/pip/pip.conf'

我们修改~/pip/pip.conf文件,信任这几个网址:

[global]
trusted-host=pypi.python.org
pypi.org
files.pythonhosted.org

Linux 和 Windows 也可以这样修改哈,文件位置是:

接下来再执行启动命令:

shwebui-macos-env.sh;shwebui.sh--no-half

系统会使用默认浏览器自动打开网址http://127.0.0.1:7860/,可以看到熟悉的界面了:

我们让它来画一只猫,提示就是简单的a cat

API 模式运行

如果要让 Dify 调用 SD Web UI ,必须让 SD Web UI 以 API 监听的模式运行,增加--api --listen参数:

shwebui-macos-env.sh;shwebui.sh--api--listen--no-half

Dify 安装

Dify 支持本地源码运行、容器运行,之前使用源码运行,过了很长一段时间发现也没啥修改源码的地方,所以推荐搭建全部用 容器运行。

我们可以参考Dify 容器部署文档[4]来部署。

克隆项目

首先克隆项目:

gitclonehttps://github.com/langgenius/dify.git

复制修改环境变量

进入目录复制 ENV 环境变量:

cddify/docker
cp.env.example.env

如果要修改环境变量,可以修改.env文件,比如:

启动 Dify 容器

使用 Compose 服务启动中间件和 Dify 服务:

dockercomposeup-d

可以看到一共有如下容器启动:

[+]Running11/11
✔Networkdocker_ssrf_proxy_network
✔Networkdocker_default
✔Containerdocker-redis-1
✔Containerdocker-ssrf_proxy-1
✔Containerdocker-sandbox-1
✔Containerdocker-web-1
✔Containerdocker-weaviate-1
✔Containerdocker-db-1
✔Containerdocker-api-1
✔Containerdocker-worker-1
✔Containerdocker-nginx-1

访问 Dify

启动成功, 访问http://localhost,设置完用户密码,即可看到 Dify 的界面了:

更新项目

如果要更新 Dify,也很简单,停止容器、更新代码、重新启动即可。

cddify/docker
dockercomposedown
gitpulloriginmain
dockercomposepull
dockercomposeup-d

集成 Stable Diffusion 工具

前面我们已经将 Stable Diffusion Web UI 使用 API 模式运行起来了,可以在 Dify 工具中直接配置即可。Dify 也提供了 SD 集成文档[5],大家有兴趣可以查看。

点击 Dify 的工具Tools页签,打开工具页面,搜索stable,选择Stable Diffusion

然后点击To Authorize按钮进行认证:

有两个参数Base URLModel

由于 Dify 是在容器中运行,Base URL 不能填写localhost,Docker 提供了主机名host.docker.internal来代替宿主机地址。我们填写:

接下来是模型名称, 模型列表可以浏览器访问如下网址获得:

格式化之后,可以清楚看到模型名称,

为了方便起见,我写个命令给大家获取模型名称,执行下面命令,

curlhttp://127.0.0.1:7860/sdapi/v1/sd-models|python-mjson.tool|grepmodel_name

可以直接提取到模型名称:

挑选一个我们喜欢的 模型,填入即可,比如:

realisticVisionV20_v20

点击确定,可以看到认证成功:

创建应用

接下来我们来创建应用:

在工作区中点击右键,选择添加块

然后搜索选择Stable Diffusion WebUI工具:

把开始节点连接到 WebUI 工具:

点击 WebUI 工具进行设置,Prompt 输入框输入/可以选择变量,我们选择sys.query作为提示词:

为了快速验证,我们把图片的 宽度和高度 都改成 512,这样绘图的时间不会太长。

然后选择默认生成的LLM节点,按DEL键删掉,查新连线如下图:

选中最后一个 「回答」 节点,设置回答内容内files

点击预览按钮进行测试,在聊天输入框中输入a tiger发送消息, 过一会可以看到 应用返回了一张老虎图片:

翻译

同样的方法,我们在画布右击,选择谷歌翻译这个工具,添加到画布上:

重新连线如下:

设置翻译工具的内容为sys.query,目标语言为English

调整 Stable diffusion WebUI 工具的提示词为Translate / {x} text

下面我们写一个复杂一些的提示:

一大群人举着巴勒斯坦旗帜和气球,自由巴勒斯坦,

应用也给出了可用的图片:

翻译的必要性

前面我没有讲到绘画时翻译的必要性。

Stable Diffusion 是可以理解简单的中文的,但是仅仅限于简单简单的词语,比如一只猫老虎等单个词语,

如果是一个句子,Stable Diffusion 就理解不了,比如我们上面的那句话,如果直接发送给 SD,结果可能很随机:

所以使用 Stable Diffusion 还是要英文提示词,所以我们在英文不好的时候,通过翻译自动翻译给 StableDiffusion 还是很有必要的。

翻译之后,虽然说不上多专业,但是一些普通的使用还是可以满足的。

发布

一切就绪,我们来发布应用。

点击发布按钮,在弹框中点击「发布」或「更新」即可:

使用

我们点击Explore或者 Dify Logo 回到首页,都可以看到我们刚刚创建的应用:

点击应用然后开始聊天吧:

总结

Dify 提示了众多的工具让我们使用,我们可以通过流程编排来实现一些工具的串联, 这样的话如果没有额外逻辑我们就不需要编写代码了,可以节省很大工作量。

如果有额外的需求,我们也可以编写服务,然后使用自定义工具的方式来实现,这个咱们下期再讲。







欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5