File size: 30,181 Bytes
6bce138 |
1 |
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## NovelAi stable-diffusion-webui+api 修复版(Kaggle云端部署项目,完全免费,无需任何配置!)\n**torch: 1.13.1+cu117 • xformers: 0.0.16rc425**\n","metadata":{}},{"cell_type":"markdown","source":"<a href=\"https://sm.ms/image/fnCzAYw1FksqZEW\" target=\"_blank\"><img src=\"https://s2.loli.net/2023/02/06/fnCzAYw1FksqZEW.png\" /></a>\nKaggle在线部署NovelAi(Webui)的生成演示效果,与本地部署效果几乎一致。模型:AbyssOrange_mix2_nsfw","metadata":{}},{"cell_type":"markdown","source":"# 注意事项:\n\n- ### 请不要使用一次性使用太久,否则有封号的可能\n- ### 首次运行,需要导入Python环境的数据集,在右边Add Data中搜索Python-of-NOVELAI-WEBUI,然后导入即可直接运行\n- ### 此项目为yiyiooo作者所写的,本人修改了一些内容,使用的是旧版的启动方式,新版无法启动,原因未知\n- ### 此项目为修复版内容,目前部分功能不可用,例如frpc,以及koishi的机器人部署,maple的tag选择器扩展\n- ### 将设置中的PERSISTENCE改为Files Only方便下次打开提高启动速度,第一次启动后下载Python环境包和模型后就不用下载第二次了","metadata":{}},{"cell_type":"code","source":"\n\n#第一个代码单元格,可以根据自己的需求进行更改\n#通过下载链接下载模型,此功能仍在测试中\nmodelurl = 'https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix3/AOM3A3.safetensors'\nname = 'AOM3A3.safetensors'\n\nuseNgrok=False # 非必填 是否使用ngrok作为公网访问地址\nngrokTokenFile='/kaggle/input/configs/ngrok_token.txt' # 非必填 存放ngrokToken的文件的路径\nuseFrpc=True\nfrpcConfigFile='/kaggle/input/configs/frpc_txt.ini' # 非必填 frp 配置文件\ndisableShared=False # 关闭默认的gradio.live穿透,如果没有配置ngrok或frp不要开启这个选项\n\n# 模型目录 \n# Stable Diffusion模型请放在这里(不用填模型的文件名,只填模型的目录即可)\nmodelDirs = []\n# 启动时默认加载的模型名称 可填全路径或者模型名称,名称建议带上文件名后缀\n# 如果你想加载7G以上的大模型,请填这个来加载,直接在Webui里切换模型容易爆内存\nusedCkpt = ''\n# Hypernetworks超网络模型路径请放在这里:\nhypernetworksModelDirs = []\n# embeddings(pt文件)请放在这里:\nembeddingsModelDirs = [] \n# Lora模型的数据集路径请写在这里:\nloraModelDirs = [] \n# 此配置内的目录下所有文件将加载到wildcards文件夹\nwildcardsFileDirs = [] \n\n# ControlNet模型请放在这里:\nControlnet = []\n\n# 将Lora模型放在可选附加网络扩展(占用内存高,拖慢启动速度。请注意!!)\nAdditionalNetworks = []\n\n# 插件列表: git仓库地址\n# 开启公网访问后将不能在webui界面安装插件,如果有需要安装的插件,把地址加入到下面\n# 不需要的插件在前面加 # ,插件地址之间需要用英语逗号隔开\nextensions = [\n # krita 绘图软件插件\n 'https://github.com/Interpause/auto-sd-paint-ext',\n # 汉化 对新功能没有完全覆盖\n 'https://github.com/2575044704/stable-diffusion-webui-localization-zh_CN', \n # 图片浏览器 不安装不能在webui界面查看图片\n 'https://github.com/yfszzx/stable-diffusion-webui-images-browser', \n # 自动tag补全插件 tag翻译文件在webui_settings这个配置里\n 'https://github.com/DominikDoom/a1111-sd-webui-tagcomplete', \n # 此插件会过滤重复标签和空标签、会将中文的逗号和括号替换成英文的逗号或括号,可能影响tag权重\n 'https://github.com/viyiviyi/filter-empty-prompts.git', \n # 可以使用__加文件名的方式从文件中随机读取一行加入到tag\n 'https://github.com/AUTOMATIC1111/stable-diffusion-webui-wildcards', \n # 提示词变迁插件 没用过 -_-\n 'https://github.com/Kahsolt/stable-diffusion-webui-prompt-travel.git' ,\n #美术风格\n 'https://github.com/AUTOMATIC1111/stable-diffusion-webui-aesthetic-gradients.git',\n # Lora的扩展(占内存,慎用)\n #'https://github.com/kohya-ss/sd-webui-additional-networks',\n # ControlNet\n 'https://github.com/Mikubill/sd-webui-controlnet',\n # OpenPose\n 'https://github.com/fkunn1326/openpose-editor'\n]\n\n# webui的配置文件 因为有比较多的人在用了,而个人喜好又不一样,所以这里可以使用自己的配置文件\n# 需要使用git仓库 需要将对应的文件放在仓库的 dist目录下,dist目录下的全部文件都会被复制到webui的根目录\nwebui_settings = 'https://github.com/2575044704/sd-configs2.git'\n# 设置文件路径 默认使用 webui_settings 里面的config.json 和 ui-config.json文件\nsetting_file = '/kaggle/input/configs/config.json'\nui_config_file = '/kaggle/input/configs/ui-config.json'\notherArgs = '' # '--api-auth=2333:6666 --gradio-auth=2333:6666' # 其他需要加载的webui启动参数\n#VAE相关,一般不用改,这个vae色彩度很高\nzheshiVAEwenjian = 'https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/VAEs/orangemix.vae.pt'\naddress = 'orangemix.vae.pt'\n# koishi相关\nuseKoishi = False # 是否开启Koishi机器人\nkoishiPort = 5140 # 使用7860将会占用webui的默认端口\n# 如果这个配置文件与上面的配置文件相同,可能会出问题\nkoishiFrpcConfig = '/kaggle/input/frpc-koishi/frpc_KOISHI.ini'\n# 可以将输出目录的文件下载后上传到数据集,下次启动就不用再配置一次\nkoishiPath = '/kaggle/input/koishiconfig/kaggle/koishi'","metadata":{"execution":{"iopub.status.busy":"2023-03-05T07:08:55.370650Z","iopub.execute_input":"2023-03-05T07:08:55.371019Z","iopub.status.idle":"2023-03-05T07:08:55.402774Z","shell.execute_reply.started":"2023-03-05T07:08:55.370983Z","shell.execute_reply":"2023-03-05T07:08:55.401784Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"#第二个代码单元格,此处的设置一般不用动\n\n# 手机端界面优化 使用了修改过界面布局顺序的webui,不定期同步到官方版本\nmobileOptimize=False\n# 安装目录 如果安装目录在输出目录(/kaggle/working)下,建议不要开启 Save Version 功能,因为每次结束运行都需要等待上传全部文件\ninstall_path=\"/kaggle\" \n# 输出目录 默认将 图片输出目录和图片收藏目录连接到此目录下 可以在下方 【配置文件链接】 处开启链接其他文件夹(完全没有必要)\noutput_path=\"/kaggle/working\"\n\n# 配置启动参数\nquickStart=True # 快速启动 使用下载好的python环境 开启后启动到可用需要4分钟,不开启需要8分钟\nvaeHalf=False # vae开启半精度,关闭效果更好,对速度没啥影响\nmodelHalf=True # 模型开启半精度,关闭效果更好,但生成速度减半\nconsoleProgressbars=False # 控制台显示进度条,关闭可以减少一些输出内容,查看日志时更快一点\nconsolePrompts=False # 同上 \nenableLoadByCopy=False # 是否使用copy的方式加载文件 启动变慢,且测试后没有提高模型切换速度\n\n# 配置文件链接\nlinkHypernetworksDir=False # 链接 hypernetworks 目录到输出目录 如果需要训练 hypernetworks 时建议开启\nlinkEmbeddingsDir=False # 链接 embeddings 目录到输出目录 如果需要训练 embeddings 时建议开启\nlinkTextual_inversionDir=False # 链接 textual_inversion 目录到输出目录 如果需要保存训练过程的产出文件时建议开启\n\n# 这下面的是用于初始化一些值或者环境变量的,轻易别改\nfrom pathlib import Path\nimport os\nimport time\nimport re\nif not Path(frpcConfigFile).exists(): useFrpc = False\nos.environ['install_path'] = install_path\nos.environ['output_path'] = output_path\nos.environ['webui_settings'] = webui_settings\nos.environ['frpcConfig'] = frpcConfigFile\n\nngrokToken=''\nif Path(ngrokTokenFile).exists():\n with open(ngrokTokenFile,encoding = \"utf-8\") as nkfile:\n ngrokToken = nkfile.readline()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T07:08:55.404957Z","iopub.execute_input":"2023-03-05T07:08:55.405358Z","iopub.status.idle":"2023-03-05T07:08:55.414176Z","shell.execute_reply.started":"2023-03-05T07:08:55.405318Z","shell.execute_reply":"2023-03-05T07:08:55.413033Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"# 重置变量 用于修改参数后重新安装\nreLoad = True\n# 如果需要重新安装,请注释这一行\nreLoad = False","metadata":{"execution":{"iopub.status.busy":"2023-03-05T07:08:55.415762Z","iopub.execute_input":"2023-03-05T07:08:55.416402Z","iopub.status.idle":"2023-03-05T07:08:55.426525Z","shell.execute_reply.started":"2023-03-05T07:08:55.416364Z","shell.execute_reply":"2023-03-05T07:08:55.424912Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"# Koishi https://koishi.chat/\ndef installKoishi():\n !echo '安装koishi和相关依赖'\n %cd $install_path\n !mkdir -p koishi\n %cd koishi\n !apt-get update\n !apt-get install chromium-browser -y\n if Path(koishiPath).exists():\n os.system('cp -f -n '+koishiPath+'/* ./')\n else:\n !npm init -y\n !npm i koishi -D\n !npx koishi init\n !npm i @koishijs/plugin-logger @koishijs/plugin-market @koishijs/plugin-console -D\n !npm i @koishijs/plugin-auth @koishijs/plugin-status @koishijs/plugin-insight -D\n !npm i\n \ndef startKoishi():\n %cd $install_path/koishi\n if Path('koishi.config.yml').exists():\n !pm2 del koishi\n !pm2 start npx -n koishi -- koishi start koishi.config.yml\n return\n content = f'''\n# 全局设置\nhost: localhost\n# 端口\nport: {koishiPort}\n# 插件列表\nplugins:\n # group 表示这是一个插件组\n group:console:\n # 波浪线前缀表示一个不启用的插件\n auth: \n console:\n logger:\n status:\n insight:\n market:\n'''\n !mkdir -p $install_path/koishi/\n with open('koishi.config.yml','w') as sh:\n sh.write(content)\n !pm2 del koishi\n !pm2 start npx -n koishi -- koishi start koishi.config.yml\n\ndef startKoishFrpc():\n !pm2 del frpc_koishi\n os.system('pm2 start $install_path/frpc/frpc -n frpc_koishi -- -c '+koishiFrpcConfig)\n \ndef saveKoishi():\n %cd /kaggle/working/\n !tar -cvf koishi.tar --exclude=koishi/node_modules --exclude=koishi/logs $install_path/koishi \n\n# 每10分钟保存一次koishi的数据\ndef autoZipKoishiData():\n %cd $install_path\n content = '''\nsetInterval(() => {\n try {\n const { exec } = require('child_process');\n exec('tar -cvf /kaggle/working/koishi.tar --exclude=koishi/node_modules --exclude=koishi/logs $install_path/koishi');\n console.log('每10分钟保存一次数据');\n } catch (error) {\n console.error('保存数据失败');\n }\n}, 1000 * 60 * 10);\n '''\n with open('autoZipKoishiData.js','w') as sh:\n sh.write(content)\n !pm2 del autoZipKoishiData\n !pm2 start autoZipKoishiData.js -n autoZipKoishiData","metadata":{"execution":{"iopub.status.busy":"2023-03-05T07:08:55.429261Z","iopub.execute_input":"2023-03-05T07:08:55.429892Z","iopub.status.idle":"2023-03-05T07:08:55.545648Z","shell.execute_reply.started":"2023-03-05T07:08:55.429850Z","shell.execute_reply":"2023-03-05T07:08:55.544598Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"# 功能函数\n# 绕过 os.systen 的限制执行命令\ndef run(shell:str):\n with open('run_cache.sh','w') as sh:\n sh.write(shell)\n !bash run_cache.sh\n\n# 连接多个路径字符串 让路径在shell命令中能正常的执行\ndef pathJoin(*paths:str):\n pathStr = ''\n for p in paths:\n pathStr += '\"'+p+'\"'\n pathStr = '\"*\"'.join(pathStr.split('*'))\n pathStr = '\"$\"'.join(pathStr.split('$'))\n pathStr = '\"(\"'.join(pathStr.split('('))\n pathStr = '\")\"'.join(pathStr.split(')'))\n pathStr = '\"{\"'.join(pathStr.split('{'))\n pathStr = '\"}\"'.join(pathStr.split('}'))\n pathStr = re.sub(r'\"\"','',pathStr)\n pathStr = re.sub(r'\\*{2,}','\"',pathStr)\n pathStr = re.sub(r'/{2,}','/',pathStr)\n pathStr = re.sub(r'/\\./','/',pathStr)\n return pathStr\n\n# 判断路径是不是一个文件或者可能指向一些文件\ndef pathIsFile(path):\n if Path(path).is_file():\n return True\n if re.search(r'\\.(ckpt|safetensors|png|jpg|txt|pt|pth|json|yaml|\\*)$',path):\n return True\n return False\n\ndef installEnv():\n !npm i -g pm2\n\n#安装webui\ndef install():\n %cd $install_path\n if reLoad:\n !rm -rf stable-diffusion-webui\n if Path(\"stable-diffusion-webui\").exists():\n %cd $install_path/stable-diffusion-webui/\n !git checkout .\n !git pull\n else:\n if mobileOptimize:\n !git clone https://github.com/viyiviyi/stable-diffusion-webui.git -b local # 修改了前端界面,手机使用更方便\n else:\n !git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui \n %cd $install_path/stable-diffusion-webui/\n if quickStart:\n !echo \"解压Python环境... unzip venv\"\n !tar -xf /kaggle/working/venv/venv.tar.bak -C ./\n\n# 链接输出目录\ndef link_dir():\n # 链接输出目录 因为sd被部署在了stable-diffusion-webui目录,运行结束后为了方便下载就只有outputs目录放在output_path/目录下\n !mkdir -p $output_path/outputs\n !rm -rf $install_path/stable-diffusion-webui/outputs\n !ln -s -r $output_path/outputs $install_path/stable-diffusion-webui/ # 输出目录\n !mkdir -p $output_path/log\n !rm -rf $install_path/stable-diffusion-webui/log\n !ln -s -r $output_path/log $install_path/stable-diffusion-webui/\n # 链接 hypernetworks 目录\n if linkHypernetworksDir:\n !rm -rf $install_path/stable-diffusion-webui/models/hypernetworks\n !mkdir -p $output_path/hypernetworks\n !ln -s -r $output_path/hypernetworks $install_path/stable-diffusion-webui/models/\n # 链接 embeddings 目录\n if linkEmbeddingsDir:\n !rm -rf $install_path/stable-diffusion-webui/embeddings\n !mkdir -p $output_path/embeddings\n !ln -s -r $output_path/embeddings $install_path/stable-diffusion-webui/\n # 链接训练输出目录 文件夹链接会导致功能不能用\n if linkTextual_inversionDir:\n !rm -rf $install_path/stable-diffusion-webui/textual_inversion\n !mkdir -p $output_path/textual_inversion/\n !ln -s -r $output_path/textual_inversion $install_path/stable-diffusion-webui/\n \n \ndef venvdownload():\n if os.access(\"/kaggle/working/venv/venv.tar.bak\", os.F_OK):\n print (\"正在安装Webui\")\n else:\n print(\"\\033[1;31m 首次启动需要一点时间,请耐心等待\\033[0m\")\n print(\"\\033[1;31m 自动下载Python环境包...,可能有点慢\\033[0m\")\n !apt -y update -qq\n !apt -y install -qq aria2\n !echo '正在自动下载Python环境包'\n !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/datasets/ACCA225/vene/resolve/main/venv.tar.bak -d /kaggle/working/venv -o venv.tar.bak\n !echo '下载成功,请重新点击Run All'\n \n# 链接模型文件\n# link models files\ndef link_models():\n print(('复制' if enableLoadByCopy else '链接') + '模型文件')\n if enableLoadByCopy:\n print('如果出现 No such file or directory 错误,可以忽略')\n !mkdir -p $install_path/stable-diffusion-webui/models/Stable-diffusion # 模型和文件目录\n !mkdir -p $install_path/stable-diffusion-webui/models/hypernetworks # 超网络模型目录\n !mkdir -p $install_path/stable-diffusion-webui/embeddings # 画风模型目录\n !mkdir -p $install_path/stable-diffusion-webui/models/Lora # lora 文件目录\n !mkdir -p $install_path/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards # wildcards 文件目录\n !mkdir -p $install_path/stable-diffusion-webui/extensions/sd-webui-controlnet/models/ \n !mkdir -p $install_path/stable-diffusion-webui/extensions/sd-webui-additional-networks/models/lora\n !mkdir -p $install_path/stable-diffusion-webui/extensions/sd-webui-additional-networks/models/lora # lora 文件目录\n \n for path in modelDirs:\n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/models/Stable-diffusion')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.ckpt') +' $install_path/stable-diffusion-webui/models/Stable-diffusion/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.safetensors') +' $install_path/stable-diffusion-webui/models/Stable-diffusion/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.png') +' $install_path/stable-diffusion-webui/models/Stable-diffusion/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.jpg') +' $install_path/stable-diffusion-webui/models/Stable-diffusion/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.pt') +' $install_path/stable-diffusion-webui/models/Stable-diffusion/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/hypernetworks*/*.*') +' $install_path/stable-diffusion-webui/models/hypernetworks/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/embeddings*/*.*') +' $install_path/stable-diffusion-webui/embeddings/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/lora*/*.*') +' $install_path/stable-diffusion-webui/models/Lora/')\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/wildcards*/*.*') +' $install_path/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards/')\n for path in wildcardsFileDirs:\n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards/')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.*') +' $install_path/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards/')\n for path in loraModelDirs: \n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/models/Lora/')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.*') +' $install_path/stable-diffusion-webui/models/Lora/')\n for path in embeddingsModelDirs: \n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/embeddings/')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.*') +' $install_path/stable-diffusion-webui/embeddings/')\n for path in hypernetworksModelDirs: \n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/models/hypernetworks/')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path,'/*.*') +' $install_path/stable-diffusion-webui/models/hypernetworks/')\n if Path(install_path+'/stable-diffusion-webui/extensions/sd-webui-controlnet').exists():\n for path in Controlnet: \n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/extensions/sd-webui-controlnet/models/')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f ' + pathJoin(path,'/*.*') +' $install_path/stable-diffusion-webui/extensions/sd-webui-controlnet/models/')\n if Path(install_path+'/stable-diffusion-webui/extensions/sd-webui-additional-networks').exists():\n for path in AdditionalNetworks: \n if pathIsFile(path):\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f '+ pathJoin(path) +' $install_path/stable-diffusion-webui/extensions/sd-webui-additional-networks/models/lora/')\n continue\n os.system(('cp -n' if enableLoadByCopy else 'ln -s')+' -f ' + pathJoin(path,'/*.*') +' $install_path/stable-diffusion-webui/extensions/sd-webui-additional-networks/models/lora/')\n \n \n %cd $install_path/stable-diffusion-webui/extensions/sd-webui-controlnet/models/\n !rm -f \\*.* # 删除因为文件夹没有对应文件导致复制了一个'*.*'的文件\n !echo controlnetModel文件列表\n !ls\n\n %cd $install_path/stable-diffusion-webui/models/Stable-diffusion\n !rm -f \\*.* & rm -f \\*.ckpt & rm -f \\*.safetensors & rm -f \\*.png & rm -f \\*.pt & rm -f \\*.jpg # 删除因为文件夹没有对应文件导致复制了一个'*.*'的文件\n !echo 模型文件列表\n !ls\n %cd $install_path/stable-diffusion-webui/models/hypernetworks\n !rm -f \\*.* # 删除因为文件夹没有对应文件导致复制了一个'*.*'的文件\n !echo hypernetworks超网络模型文件列表\n !ls\n %cd $install_path/stable-diffusion-webui/embeddings\n !rm -f \\*.* # 删除因为文件夹没有对应文件导致复制了一个'*.*'的文件\n !echo embedding文件列表\n !ls\n %cd $install_path/stable-diffusion-webui/models/Lora\n !rm -f \\*.* # 删除因为文件夹没有对应文件导致复制了一个'*.*'的文件\n !echo lora文件列表\n !ls\n %cd $install_path/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards\n !rm -f \\*.* # 删除因为文件夹没有对应文件导致复制了一个'*.*'的文件\n !echo wildcards文件列表\n !ls\n\n#下载模型\ndef modelsdownload():\n !apt -y update -qq\n !apt -y install -qq aria2\n !echo '正在下载模型...'\n !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {modelurl} -d $install_path/stable-diffusion-webui/models/Stable-diffusion -o {name}\n#下载VAE\ndef vaedownload():\n !echo '正在下载VAE...'\n !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {zheshiVAEwenjian} -d $install_path/stable-diffusion-webui/models/Stable-diffusion -o {address}\n \n#安装依赖\ndef install_dependencies():\n %cd $install_path/stable-diffusion-webui\n !python3 -m venv venv\n !source venv/bin/activate\n !COMMANDLINE_ARGS=\"--exit\" REQS_FILE=\"requirements.txt\" venv/bin/python3 launch.py\n\n# 安装插件\ndef install_extensions():\n print('安装插件')\n %cd $install_path/stable-diffusion-webui\n !mkdir -p extensions\n %cd extensions\n !rm -rf sd-webui-additional-networks\n !git clone https://github.com/kohya-ss/sd-webui-additional-networks #安装lora扩展\n for ex in extensions:\n os.system('git clone '+ex)\n #Tag选择器\n !cp -r /kaggle/input/tagextensions/maple-from-fall-and-flower $install_path/stable-diffusion-webui/extensions/maple-from-fall-and-flower\n \n# 个性化配置 \ndef use_config():\n %cd $install_path/stable-diffusion-webui\n !mkdir -p tmp\n %cd tmp\n !git clone $webui_settings sd-configs\n !cp -rf sd-configs/dist/* $install_path/stable-diffusion-webui\n \n# ngrok\ndef ngrok_start():\n if useNgrok == True and ngrokToken != '':\n print('use nrgok')\n !pip install pyngrok\n from pyngrok import conf, ngrok\n conf.get_default().auth_token = ngrokToken\n conf.get_default().monitor_thread = False\n ssh_tunnels = ngrok.get_tunnels(conf.get_default())\n if len(ssh_tunnels) == 0:\n ssh_tunnel = ngrok.connect(7860)\n print('address:'+ssh_tunnel.public_url)\n else:\n print('address:'+ssh_tunnels[0].public_url)\n else:\n print('skip start ngrok')\n \ndef installFrpc():\n %cd $install_path\n !mkdir -p frpc\n %cd frpc\n !cp -f -n /kaggle/input/net-tools/frpc ./frpc\n !chmod +x ./frpc\n !./frpc -v\n \ndef startFrpc():\n if useFrpc:\n !pm2 del frpc_webui\n !pm2 start $install_path/frpc/frpc -n frpc_webui -- -c $frpcConfig\n \ndef installBore():\n %cd $install_path\n !mkdir -p bore\n %cd bore\n !cp -f -n /kaggle/input/net-tools/bore ./bore\n !chmod +x ./bore\n\ndef startBore():\n !pm2 del bore\n !pm2 start $install_path/bore/bore -n bore -- local 5140 --to bore.pub\n \n# 打包输出图片到$output_path/outputs.tar文件\ndef zip_output():\n !echo \"打包输出图片到$output_path/outputs.tar文件\"\n %cd $install_path/stable-diffusion-webui/\n !tar -cf outputs.tar ./outputs\n !mv ./outputs.tar $output_path/\n \n# 启动\ndef start():\n %cd $install_path/stable-diffusion-webui\n !python3 -m venv venv\n !source venv/bin/activate\n args = '--disable-safe-unpickle --deepdanbooru'\n if not disableShared:\n args += ' --share'\n args += ' --skip-torch-cuda-test'\n args += ' --xformers' # 已经可用\n args += ' --api --listen' # 开放 api\n args += ' --lowram' # 降低爆内存几率\n if ui_config_file is not None and ui_config_file != '' and Path(ui_config_file).exists(): # ui配置文件\n args += ' --ui-config-file=' + pathJoin(ui_config_file)\n if setting_file is not None and setting_file != '' and Path(setting_file).exists(): # 设置配置文件\n args += ' --ui-settings-file=' + pathJoin(setting_file)\n if usedCkpt is not None and usedCkpt != '': # 设置启动时默认加载的模型\n if '.' in usedCkpt:\n if '/' in usedCkpt:\n args += ' --ckpt='+ pathJoin(usedCkpt)\n else:\n args += ' --ckpt='+ pathJoin(install_path, '/stable-diffusion-webui/models/Stable-diffusion/', usedCkpt)\n else:\n for x in ['.ckpt','.safetensors']:\n if Path(install_path+'/stable-diffusion-webui/models/Stable-diffusion/' + usedCkpt+x).exists():\n args += ' --ckpt='+ pathJoin(install_path, '/stable-diffusion-webui/models/Stable-diffusion/', usedCkpt, x)\n break\n if vaeHalf is False: \n args += ' --no-half-vae'\n if modelHalf is False:\n args += ' --no-half'\n if consoleProgressbars is False:\n args += ' --disable-console-progressbars'\n if consolePrompts is True:\n args += ' --enable-console-prompts'\n args += ' ' + otherArgs\n os.environ['COMMANDLINE_ARGS']=args\n !echo COMMANDLINE_ARGS=$COMMANDLINE_ARGS\n %env REQS_FILE=requirements.txt\n !pm2 log & venv/bin/python3 launch.py\n\ndef main():\n startTicks = time.time()\n isInstall = True if os.getenv('IsInstall','False') == 'True' else False\n if isInstall is False or reLoad:\n venvdownload()\n install()\n install_dependencies()\n install_extensions()\n installEnv()\n use_config()\n link_dir()\n installFrpc()\n if useKoishi:\n installKoishi()\n isInstall = True\n os.environ['IsInstall'] = 'True'\n modelsdownload()\n vaedownload()\n link_models()\n ticks = time.time()\n print(\"加载耗时:\",(ticks - startTicks),\"s\")\n ngrok_start()\n startFrpc()\n if useKoishi:\n startKoishFrpc()\n startKoishi()\n autoZipKoishiData()\n start()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-05T07:08:55.568734Z","iopub.execute_input":"2023-03-05T07:08:55.568998Z","iopub.status.idle":"2023-03-05T07:09:00.388375Z","shell.execute_reply.started":"2023-03-05T07:08:55.568974Z","shell.execute_reply":"2023-03-05T07:09:00.385657Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"# 启动的输出日志,等待约5分钟,最终会得到一个gradio.live的链接,但如果出现Error这个词,说明出错了。请联系管理员解决该问题\n# start\nmain()\n\nif useKoishi:\n saveKoishi()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-03-05T07:09:00.393633Z","iopub.execute_input":"2023-03-05T07:09:00.395329Z"},"trusted":true},"execution_count":null,"outputs":[{"name":"stdout","text":"正在安装Webui\n/kaggle\nCloning into 'stable-diffusion-webui'...\nremote: Enumerating objects: 16434, done.\u001b[K\nremote: Total 16434 (delta 0), reused 0 (delta 0), pack-reused 16434\u001b[K\nReceiving objects: 100% (16434/16434), 27.51 MiB | 31.72 MiB/s, done.\nResolving deltas: 100% (11495/11495), done.\n/kaggle/stable-diffusion-webui\n解压Python环境... unzip venv\n","output_type":"stream"}]},{"cell_type":"code","source":"# 如果需要关闭后台任务 比如frpc \n# 1 运行 pm2 ls 查看任务id\n# 2 运行 pm2 stop 任务id\n# !pm2 ls\n# !pm2 stop all # all 表示关闭全部","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 交流群 632428790 (遇到问题可以找群主或者管理员解决)","metadata":{}}]} |