diff --git a/Readme.md b/Readme.md index d68011a..e557d75 100644 --- a/Readme.md +++ b/Readme.md @@ -11,7 +11,9 @@ ## [机器人使用方法](#ch2) ### [例子](#ch21) -## [使用协议](#ch3) +## [更新日志](#ch3) + +## [使用协议](#ch4) ## 安装配置方法

@@ -24,7 +26,7 @@ python3需要再安装这些库,使用pip安装就可以: ``` -pip install torch flask openai transformers diffusers python-opencv accelerate +pip install torch flask openai transformers diffusers accelerate ``` 当然如果使用cuda加速建议按照pytorch官网提供的方法安装支持cuda加速的torch版本。 @@ -40,6 +42,10 @@ Apple Silicon的macbook上可以使用mps后端加速,我开发的时候使用 因为懒省事所以有一些参数是写死在代码里的(坏文明),也是可以调整的,比如超时时间可以在wechat_client.go的代码中修改,这样在生成高分辨率、迭代次数非常多的图片的时候留有更多的时间。总之就是代码太简单了,自己看着改一下就行了(就是作者懒)。还有bot.py运行的时候用WSGI什么的,也就是加两行代码。(懒+1) +UseFP16一般打开就可以了,能显著减少显存需求,并且对画质几乎没有影响。 + +NoNSFWChecker一般打开就可以了,用于过滤生成含有NSFW内容的图片,被过滤的图片会变为纯黑色。 + ### 然后就可以运行了

@@ -61,7 +67,16 @@ go run wechat_client.go 第一次运行时候会弹出网页扫码登录微信,登陆一次之后之后再登陆不需要扫码,但仍然需要在手机上点击确认登陆。 -第一次运行需要下载Stable Diffusion模型,默认的stabilityai/stable-diffusion-2-1有将近10GB,并且从外网下载,需要有比较快速稳定的网络条件。如果 +第一次运行需要下载Diffusion模型,文件很大,并且从外网下载,需要有比较快速稳定的网络条件。 + +使用iffusion系列模型生成图片对显存容量的需求很大,在默认开启16位浮点数、768x768分辨率的条件下,迭代20次需要14GB显存(参考:RTX4080仅有12GB显存)。搭载Apple Silicon的Macbook/Mac Studio因为统一内存,有比较好的表现。 + +Diffusion推荐使用的模型: + +``` +andite/anything-v4.0 : 二次元浓度很高,画人的水平不错 +stabilityai/stable-diffusion-2-1 : 比较通用,能生成各种图片,二次元风格真实风格都可以,但是画人的能力很差,经常出现崩坏的手,缺胳膊少腿等问题。。。 +``` bot.py会占用本地11111网络端口,如果发生冲突可以在bot.py中修改这个端口号(没弄配置文件里?没错还是作者懒,要不是作者不想把自己的API Key写代码里开源了,连config.json配置文件都不会有(笑)) @@ -107,16 +122,39 @@ bot.py会占用本地11111网络端口,如果发生冲突可以在bot.py中修 ![](examples/exp4.jpg) +下面这个例子使用 andite/anything-v4.0 模型 + ``` -生成图片(800 600 100):(((masterpiece))), best quality, illustration, (beautiful detailed girl), detailed ice, expressionles, azure hair, long hairs, (skyblue dress), detailed cute anime face, white pantyhose -ugly face, no nose, pale face, duplicate, blurry, bad, worst, low quality, normal quality, jpeg artifacts, cropped, missing fingers, bad hands, bad anatomy, too many fingers, missing arms, cloned face, too many legs +生成图片(800 720 25):otokonoko,masterpiece, best quality,best qualityc, maid uniform,white hair,very long hair,golden eyes,cat ears,cat tail,smile,long stocks,white stocks,bedroom,(looking at viewer) +low quality, dark, fuzzy, normal quality, ugly, twisted face, scary eyes, sexual implication ``` -![](examples/exp5.jpg) +![](examples/exp6.png) -### 使用协议 +## 更新日志

+ + + + + + + + + + + + + + + + +
版本 日期 说明
v1.1 2023.02.07 1.修改默认的Diffusion模型为andite/anything-v4.0
2.新增特殊指令“查看机器人信息” <3> 3.新增半精度浮点数开关,并默认开启,减少内存占用
4.新增内容安全性检查开关
v1.0 2023.02.05 初始版本
+ +## 使用协议 +

+ 1.作者sxysxy依法享有对本软件的软件著作权:非商业使用遵循MIT协议即可(见LICENSE文件),商业使用联系作者,邮箱sxysxygm@gmail.com或1441157749@qq.com。(The author sxysxy is legally entitled to the software copyright: for non-commercial use, please follow the MIT license(see LICENSE file). For commercial use, contact the author at sxysxygm@gmail.com or 1441157749@qq.com) 2.内容版权:我不具有模型训练数据的版权,也不具有其创作成果的版权,我不能保证机器人的创作成果商业使用的合理与合法性。因为机器人的创作内容产生的一切纠纷与本项目作者无关。(Content copyright: I do not have the copyright of the model training data, nor the copyright of its creation results. I cannot guarantee the reasonableness and legality of the commercial use of the robot's creation results. All disputes arising from the creation content of robots have nothing to do with the author of this project.) diff --git a/bot.py b/bot.py index 104bd12..16e8982 100644 --- a/bot.py +++ b/bot.py @@ -1,13 +1,10 @@ -from email.mime import image -from cv2 import grabCut -import flask -import requests import json import openai import re -from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler +from diffusers import DiffusionPipeline, StableDiffusionPipeline, DPMSolverMultistepScheduler import torch import argparse +import flask ps = argparse.ArgumentParser() ps.add_argument("--config", default="config.json", help="Configuration file") @@ -32,17 +29,28 @@ class GlobalData: GENERATE_PICTURE_MAX_ITS = 200 #最大迭代次数 app = flask.Flask(__name__) -sd_pipe = StableDiffusionPipeline.from_pretrained(config_json["Diffusion-Model"], torch_dtype=torch.float32) + + +# 这个用于放行生成的任何图片,替换掉默认的NSFW检查器,公共场合慎重使用 +def run_safety_nochecker(image, device, dtype): + print("警告:屏蔽了内容安全性检查,可能会产生有害内容") + return image, None + +sd_args = { + "pretrained_model_name_or_path" : config_json["Diffusion-Model"], + "torch_dtype" : (torch.float16 if config_json.get("UseFP16", True) else torch.float32) +} + +sd_pipe = StableDiffusionPipeline.from_pretrained(**sd_args) sd_pipe.scheduler = DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config) +if config_json["NoNSFWChecker"]: + setattr(sd_pipe, "run_safety_checker", run_safety_nochecker) + if torch.backends.mps.is_available(): sd_pipe = sd_pipe.to("mps") elif torch.cuda.is_available(): sd_pipe = sd_pipe.to("cuda") -def send_text_to_user(user_id : str, text : str): - requests.post(url="http://localhost:11110/send_text", - data=json.dumps({"user_id" : user_id, "text" : text, "in_group" : False})) - def call_gpt(prompt : str): try: res = openai.Completion.create( @@ -155,6 +163,12 @@ def app_draw(): except Exception as e: return json.dumps({"user_name" : data["user_name"], "filenames" : [], "error" : True, "error_msg" : str(e)}) +@app.route("/info", methods=['POST', 'GET']) +def app_info(): + return "\n".join([f"GPT模型:{config_json['GPT-Model']}", f"Diffusion模型:{config_json['Diffusion-Model']}", + "默认图片规格:768x768 RGB三通道", "Diffusion默认迭代轮数:20", + f"使用半精度浮点数 : {'是' if config_json.get('UseFP16', True) else '否'}", + f"屏蔽NSFW检查:{'是' if config_json['NoNSFWChecker'] else '否'}"]) if __name__ == "__main__": #openai.organization = GlobalData.OPENAI_ORGID diff --git a/config.json b/config.json index 6f4cc7c..1e67a8b 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,8 @@ { "OpenAI-API-Key" : "", "GPT-Model" : "text-davinci-003", - "Diffusion-Model" : "stabilityai/stable-diffusion-2-1", - "DefaultDiffutionIterations" : 20 + "Diffusion-Model" : "andite/anything-v4.0", + "DefaultDiffutionIterations" : 20, + "UseFP16" : true, + "NoNSFWChecker" : false } \ No newline at end of file diff --git a/examples/exp5.jpg b/examples/exp5.jpg deleted file mode 100644 index 36f4952..0000000 Binary files a/examples/exp5.jpg and /dev/null differ diff --git a/examples/exp6.png b/examples/exp6.png new file mode 100644 index 0000000..782813c Binary files /dev/null and b/examples/exp6.png differ diff --git a/wechat_client.go b/wechat_client.go index 226a823..e4738ba 100644 --- a/wechat_client.go +++ b/wechat_client.go @@ -35,9 +35,17 @@ type GenerateImageRequest struct { Prompt string `json:"prompt"` } +type GlobalConfig struct { + OpenAIKey string `json:"OpenAI-API-Key"` + GPTModel string `json:"GPT-Model"` + DiffusionModel string `json:"Diffusion-Model"` + DefaultDiffusionIteration int `json:"DefaultDiffutionIterations"` + UseFP16 bool `json:"UseFP16"` +} + func HttpPost(url string, data interface{}, timelim int) []byte { // 超时时间 - timeout, _ := time.ParseDuration(fmt.Sprintf("%ss", timelim)) + timeout, _ := time.ParseDuration(fmt.Sprintf("%ss", timelim)) //是的,这里有个bug,但是这里就是靠这个bug正常运行的!!!??? client := &http.Client{Timeout: timeout} jsonStr, _ := json.Marshal(data) @@ -102,8 +110,13 @@ func main() { } } //fmt.Println(content) + + content = strings.TrimRight(content, "  \t\n") + if content == "查看机器人信息" { + info := HttpPost("http://localhost:11111/info", nil, 20) + msg.ReplyText(string(info)) - if strings.HasPrefix(content, "生成图片") { + } else if strings.HasPrefix(content, "生成图片") { // 调用Stable Diffusion // msg.ReplyText("这个功能还没有实现,可以先期待一下~") sender, _ := msg.Sender()