From 26b543a3304c63ecd890d5d5541472ea7699f58e Mon Sep 17 00:00:00 2001 From: congxu Date: Mon, 17 Apr 2023 12:57:01 +0800 Subject: [PATCH 1/3] Update godcmd.py --- plugins/godcmd/godcmd.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index 4c4c1f99d..15341919a 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -24,8 +24,7 @@ }, "tips": { "alias": ["tips", "技巧"], - "desc": "1、输入内容中含‘每日新闻’可获取当日新闻;含‘每日摄影’可获取每日的一张摄影作品 \ - 2、关键字画开头将触发画图,目前需要以特殊的格式输入【画 <模型>:prompt】", + "desc": "1、输入内容中含‘每日新闻’可获取当日新闻;含‘每日摄影’可获取每日的一张摄影作品 \n2、关键字画开头将触发画图,目前需要以特殊的格式输入【画 <模型>:prompt】", }, "helpp": { "alias": ["help", "帮助"], # 与help指令共用别名,根据参数数量区分 From f8692ebaa50eae8e33cc6ac0049e3e169a2b06f5 Mon Sep 17 00:00:00 2001 From: congxu Date: Mon, 17 Apr 2023 13:04:07 +0800 Subject: [PATCH 2/3] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b6bdfde37..79ddcd3ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,4 +15,4 @@ replicate>=0.7.0 lxml==4.9.2 pre-commit - +webdriver_manager From e9a8511f1a2f5ac656258127c5669a3007e3bfb0 Mon Sep 17 00:00:00 2001 From: congxu Date: Mon, 17 Apr 2023 15:02:40 +0800 Subject: [PATCH 3/3] Update main.py --- plugins/plugin_replicate/main.py | 104 +++++++++++++++++++------------ 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/plugins/plugin_replicate/main.py b/plugins/plugin_replicate/main.py index 28d144a9b..5f192b390 100644 --- a/plugins/plugin_replicate/main.py +++ b/plugins/plugin_replicate/main.py @@ -8,13 +8,15 @@ from plugins import * from common.log import logger import replicate +from common.expired_dict import ExpiredDict -@plugins.register(name="replicate", desc="利用replicate api来画图", version="0.1", author="lanvent") -class SDWebUI(Plugin): +@plugins.register(name="replicate", desc="利用replicate api来画图", version="0.2", author="lanvent") +class Replicate(Plugin): def __init__(self): super().__init__() curdir = os.path.dirname(__file__) config_path = os.path.join(curdir, "config.json") + self.params_cache = ExpiredDict(60 * 60) if not os.path.exists(config_path): logger.info('[RP] 配置文件不存在,将使用config-template.json模板') config_path = os.path.join(curdir, "config.json.template") @@ -40,12 +42,12 @@ def __init__(self): if isinstance(e, FileNotFoundError): logger.warn(f"[RP] init failed, config.json not found.") else: - logger.warn("[RP] init failed.") + logger.warn("[RP] init failed." + str(e)) raise e def on_handle_context(self, e_context: EventContext): - if e_context['context'].type != ContextType.IMAGE_CREATE: + if e_context['context'].type not in [ContextType.IMAGE_CREATE, ContextType.IMAGE]: return logger.debug("[RP] on_handle_context. content: %s" %e_context['context'].content) @@ -53,48 +55,73 @@ def on_handle_context(self, e_context: EventContext): logger.info("[RP] image_query={}".format(e_context['context'].content)) reply = Reply() try: + user_id = e_context['context']["session_id"] content = e_context['context'].content[:] - # 解析用户输入 如"横版 高清 二次元:cat" - if ":" in content: - keywords, prompt = content.split(":", 1) - else: - keywords = content - prompt = "" + if e_context['context'].type == ContextType.IMAGE_CREATE: + # 解析用户输入 如"横版 高清 二次元:cat" + content = content.replace(",", ",").replace(":", ":") + if ":" in content: + keywords, prompt = content.split(":", 1) + else: + keywords = content + prompt = "" + + keywords = keywords.split() - keywords = keywords.split() + if "help" in keywords or "帮助" in keywords: + reply.type = ReplyType.INFO + reply.content = self.get_help_text(verbose = True) + else: + rule_params = {} + for keyword in keywords: + matched = False + for rule in self.rules: + if keyword in rule["keywords"]: + for key in rule["params"]: + rule_params[key] = rule["params"][key] + matched = True + break # 一个关键词只匹配一个规则 + if not matched: + logger.warning("[RP] keyword not matched: %s" % keyword) + params = {**self.default_params, **rule_params} + params["prompt"] = params.get("prompt", "")+f", {prompt}" + logger.info("[RP] params={}".format(params)) - if "help" in keywords or "帮助" in keywords: - reply.type = ReplyType.INFO - reply.content = self.get_help_text(verbose = True) + if params.get("image",None): + self.params_cache[user_id] = params + reply.type = ReplyType.INFO + reply.content = "请发送一张图片给我" + else: + model = self.client.models.get(params.pop("model")) + version = model.versions.get(params.pop("version")) + result = version.predict(**params) + reply.type = ReplyType.IMAGE_URL + reply.content = result[0] + e_context.action = EventAction.BREAK_PASS # 事件结束后,跳过处理context的默认逻辑 + e_context['reply'] = reply else: - rule_params = {} - for keyword in keywords: - matched = False - for rule in self.rules: - if keyword in rule["keywords"]: - for key in rule["params"]: - rule_params[key] = rule["params"][key] - matched = True - break # 一个关键词只匹配一个规则 - if not matched: - logger.warning("[RP] keyword not matched: %s" % keyword) - - params = {**self.default_params, **rule_params} - params["prompt"] = params.get("prompt", "")+f", {prompt}" - logger.info("[RP] params={}".format(params)) - model = self.client.models.get(params.pop("model")) - version = model.versions.get(params.pop("version")) - result = version.predict(**params) - reply.type = ReplyType.IMAGE_URL - reply.content = result[0] - e_context.action = EventAction.BREAK_PASS # 事件结束后,跳过处理context的默认逻辑 + cmsg = e_context['context']['msg'] + if user_id in self.params_cache: + params = self.params_cache[user_id] + del self.params_cache[user_id] + cmsg.prepare() + img_key = params.pop("image") + params[img_key]=open(content,"rb") + model = self.client.models.get(params.pop("model")) + version = model.versions.get(params.pop("version")) + result = version.predict(**params) + reply.type = ReplyType.IMAGE_URL + reply.content = result + logger.info("[RP] result={}".format(result)) + e_context['reply'] = reply + e_context.action = EventAction.BREAK_PASS # 事件结束后,跳过处理context的默认逻辑 + except Exception as e: reply.type = ReplyType.ERROR reply.content = "[RP] "+str(e) - logger.error("[RP] exception: %s" % e) - e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 - finally: e_context['reply'] = reply + logger.exception("[RP] exception: %s" % e) + e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 def get_help_text(self, verbose = False, **kwargs): if not conf().get('image_create_prefix'): @@ -115,4 +142,3 @@ def get_help_text(self, verbose = False, **kwargs): else: help_text += "\n" return help_text -