Mac一键启动stable diffsuion

529 阅读5分钟

前段时间研究在Mac上部署stable diffusion,部署成功之后发现每次都需要打开终端输入命令才能启动stable diffusion,所以经过查找资料研究之后,整出了使用Mac自带的快捷指令来一键启动stable diffsuion,下面详细介绍一下操作流程

  1. 首先我们在电脑上打开finder查找快捷指令app,没有的可以在右上角搜索框搜索

image.png 2. 双击打开快捷指令app,点击右上方的“+”按钮,新建快捷指令

image.png

image.png 3.在右侧菜单栏里的搜索框里搜索“applescript”,选择运行AppleScript双击就会生成一个默认的脚本

image.png

image.png 4.将脚本中锤子图标下方的代码全部清空替换如下代码,在下面代码中的“/Users/xxx/stablediffusion/stable-diffusion-webui/”此段代码为stable diffusion在本地电脑上的文件路径,这个需要每个人根据自己的路径来填写

on run {input, parameters}
	
	tell application "System Events"
		set isRunning to exists (processes where name is "Terminal")
		if isRunning then
			tell application "Terminal"
				quit
			end tell
			repeat until not isRunning
				tell application "System Events"
					set isRunning to exists (processes where name is "Terminal")
				end tell
			end repeat
		end if
	end tell
	
	
	-- 获取键盘布局的JSON数据
	set jsonString to do shell script "defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources"
	
	-- 移除 JSON 数据中的空格和换行符
	set cleanedString to do shell script "echo " & quoted form of jsonString & " | tr -d '[:space:]'"
	
	
	-- --------------------------输入法切换 start--------------------------
	-- 用中文模拟输命令系统难以识别会报错,将键盘布局设置为英文
	-- 判断当前输入法是否为指定的输入法(ABC 键盘布局)
	
	-- 重试次数
	set inputMethodRetryCount to 0
	
	-- 重试最大次数
	set maxInputMethodRetries to 5
	
	--  如果cleanedString 包含字符串 "KeyboardLayoutName"=ABC , 则isABCLayout为true,否则为false
	set isABCLayout to cleanedString contains "\"KeyboardLayoutName\"=ABC"
	
	-- 循环
	repeat while (not isABCLayout) and (inputMethodRetryCount < maxInputMethodRetries)
		-- 模拟按下 Command+空格组合键来切换输入法
		do shell script "osascript -e 'tell application \"System Events\" to keystroke space using {control down}'"
		
		-- 等待1s,确保输入法切换完成
		delay 1
		
		-- 再重新获取键盘布局的JSON数据
		set jsonString to do shell script "defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources"
		
		-- 移除 JSON 数据中的空格和换行符
		set cleanedString to do shell script "echo " & quoted form of jsonString & " | tr -d '[:space:]'"
		
		-- 判断当前输入法是否为指定的输入法(ABC 键盘布局)
		set isABCLayout to cleanedString contains "\"KeyboardLayoutName\"=ABC"
		
		-- 重试次数+1
		set inputMethodRetryCount to inputMethodRetryCount + 1
	end repeat
	
	if not isABCLayout then
		error "Error: 无法切换到 ABC 键盘布局。"
	end if
	-- --------------------------输入法切换 start--------------------------
	
	-- --------------------------声明项目变量 start--------------------------
	
	-- 当前时间
	set startTime to current date
	-- 项目全路径
	set webuiPath to "/Users/xxx/stablediffusion/stable-diffusion-webui/"
	-- 项目启动端口,变量不能定义为port
	set desiredPort to "7860"
	set command to "/Users/xxx/stablediffusion/stable-diffusion-webui/webui.sh"
	-- 项目启动命令
	set webuiCommand to "./webui.sh"
	-- 项目启动标志,默认false
	set serviceStartedFlag to false
	-- 设置项目启动超时时间(单位:秒)
	set timeoutDuration to 180 -- 3分钟
	-- 执行losf -i:{desiredPort}命令后终端打印的信息保存在 {webuiPath}/terminal_output.txt
	set lsofLog to "terminal_output.txt"
	set logPath to "/Users/xxx/stablediffusion/stable-diffusion-webui/terminal_output.txt"
	
	--------------------------声明项目变量--------------------------
	
	--do shell script quoted form of command
	tell application "Terminal"
		activate
		tell application "System Events"
			keystroke "cd " & quoted form of webuiPath
			keystroke return
			keystroke webuiCommand
			keystroke return
		end tell
	end tell
	
	-- 使用while循环,直到服务启动成功
	repeat while (not serviceStartedFlag)
		-- 每隔2秒查一次
		delay 2
		
		-- 获取当前时间戳
		set currentTime to (current date)
		
		-- 计算时间差(单位:秒)
		set timeDifference to (currentTime - startTime) as integer
		
		-- 如果超过超时时间,终止脚本
		if timeDifference > timeoutDuration then
			error "Timeout: Service did not start within " & (timeoutDuration as text) & " seconds."
		end if
		try
			-- 执行 lsof -i:{desiredPort} 命令 将输出结果保存到文件:{webuiPath}{lsofLog}
			do shell script "lsof -i:" & desiredPort & " >  " & quoted form of (webuiPath & lsofLog)
			-- 读取文件内容
			set terminalOutputFile to (POSIX file (webuiPath & lsofLog))
			set commandOutput to read terminalOutputFile
			
			-- 检查终端输出,判断服务是否启动成功
			set serviceStartedFlag to (commandOutput contains desiredPort)
		end try
	end repeat
	
end run

此时你粘贴完之后应该如下图所示,这个时候只需要点击锤子图标,编译一下就会变成正常的代码显示 image.png

image.png 5.编译完成后就可以点击上图中绿色框选的执行按钮,执行脚本来验证我们的脚本是否可以正常打开stable diffusion了,成功之后如下图,就自动打开终端了,并且会自动打开浏览器加载webUI页面

image.png 6.启动成功之后,我们就可以在左上角更换我们想要的图标和给脚本起一个名字,之后关闭页面,返回快捷指令列表

image.png 7.在快捷指令列表大家就会看见刚刚创建的快捷指令了,然后右键快捷指令弹出多选菜单,选择添加到程序坞,一键启动的程序就创建好了。

image.png

问题

点击刚创建的应用启动webUI时会报错,这个错误在我们编译完脚本,执行脚本时也会碰见,这个是隐私安全问题,我们需要去系统设置里修改一下配置,在系统设置->隐私与安全性->辅助功能,将对应的app后面的开关打开。在添加app时,由于我们的脚本程序不是一个正常的app应用所以在打开的文件管理页面的应用程序列表可能找不到我们添加的脚本,所以我们直接在右上角的搜索框里搜索我们脚本的名字即可,然后选中打开就可以了

image.png

image.png

image.png

推荐stable difffusion的学习资料还是b站的秋葉aaaki大佬,提供的sd的整合包以及lora的一键训练包是非常棒,喜欢炼丹的小伙伴们可以看看