一键启动远程访问 Stable Diffusion 脚本
一键启动远程访问 Stable Diffusion 脚本
前情提要
若还未观看 SSH端口转发远程访问 Stable Diffusion 这篇文章,请先跳转稍后再来。
本文主要优化上文中远程访问启动比较繁琐的问题,尽量减少启动的流程。
- 远程访问文件夹优化
- ssh 优化
远程访问文件夹优化
代码打包
每次启动远程访问文件夹还要先打开 pycharm,十分麻烦,所以直接把 gradio 代码打包为 exe。
下载 pyinstaller
直接在 pycharm 的控制台里输入下面命令:
1 | pip install pyinstaller |
打包 gradio
正常打包 pyinstaller -F python_file_name
会出现问题,报错如下:
[Errno 2] No such file or directory: gradio_client\types.json
这是由于 pyinstaller 没有准确的识别出用于代码中 gradio_client 与 gradio 库的依赖项,很多的博客都是说需要再 pyinstaller 的 hook 目录下添加文件,实则没有必要。需要将打包命令修改下命令即可,也就是补充上 –collect-data=gradio_client –collect-data=gradio ,完整命令如下:
1 | pyinstaller -F python_file_name --collect-data=gradio_client --collect-data=gradio |
再次执行报以下错误:
FileNotFoundError: [Errno 2] No such file or directory: gradio\blocks_events.pyc
这是由于 gradio 库中的代码都是 pyi 文件,而 pyinstaller 在打包时默认库中的都是 pyc 文件,故而需要修改 spec 文件,指定对 gradio 库下的代码进行编译。具体操作如下:
- 生成spec文件
1
pyi-makespec --collect-data=gradio_client --collect-data=gradio python_file_name
- 打开与要打包py代码同名的spec文件,添加对gradio的编译
修改后,删除掉目录下的 build 文件夹,再次执行 pyinstaller python_file_name.spec
即可.
- 进入dist目录,找到exe文件,打包完成。
- 生成单个文件(可选)
使用以下 spec 文件可以生成单个 exe 文件。(记得把下面所有的main
替换为你的 python 文件名)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files
datas = []
datas += collect_data_files('gradio_client')
datas += collect_data_files('gradio')
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=datas,
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
module_collection_mode={ 'gradio': 'py',},
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='main',
)
ssh 脚本
直接新建一个 .bat 脚本,输入下面的内容。
1 | @echo off |
再新建两个 .vbs 文件,用于输入 ssh 密码。
1 | Set Wshell = CreateObject("Wscript.Shell") |
然后只需要点击脚本并打开启动器就可以愉快的远程画图了。