Browse Source

fix: fix 504 error

Shellmiao 1 year ago
parent
commit
ef7e8c97d7

+ 19 - 0
api/img2img.py

@@ -13,6 +13,9 @@ img_url = os.getenv('img_url')
 def img_2_img(prompt, mask_img, img_key, img_path, output_directory):
     with open(img_path, 'rb') as f:
         image_data = f.read()
+    input_image_pil = Image.open(io.BytesIO(image_data))
+    width, height = input_image_pil.size
+
     input_image = base64.b64encode(image_data).decode('utf-8')
     mask = base64.b64encode(mask_img).decode('utf-8')
     payload = {
@@ -20,7 +23,10 @@ def img_2_img(prompt, mask_img, img_key, img_path, output_directory):
             input_image
         ],
         "denoising_strength": 0.96,
+        "width": width,
+        "height": height,
         "mask": mask,
+        "mask_blur": 8,
         "prompt": prompt,
         "negative_prompt": "EasyNegativeV2,(badhandv4:1.2)",
         "batch_size": 1,
@@ -36,6 +42,11 @@ def img_2_img(prompt, mask_img, img_key, img_path, output_directory):
                         "ad_model": "face_yolov8n.pt",
                         "ad_prompt": prompt,
                         "ad_negative_prompt": "EasyNegativeV2,(badhandv4:1.2)",
+                    },
+                    {
+                        "ad_model": "hand_yolov8n.pt",
+                        "ad_prompt": prompt,
+                        "ad_negative_prompt": "EasyNegativeV2,(badhandv4:1.2)",
                     }
                 ]
             },
@@ -45,6 +56,14 @@ def img_2_img(prompt, mask_img, img_key, img_path, output_directory):
                         "input_image": input_image,
                         "module": "openpose_full",
                         "model": "control_v11p_sd15_openpose [cab727d4]",
+                    },
+                    {
+                        "input_image": input_image,
+                        "module": "canny",
+                        "model": "control_v11p_sd15_canny [d14c016b]",
+                        "weight": 0.36,
+                        "starting_control_step": 0,
+                        "ending_control_step": 0.36,
                     }
                 ]
             }

+ 8 - 6
handler/card_handler.py

@@ -15,6 +15,8 @@ ACTION_TO_TITLE = {
     'furry': 'Furry头像'
 }
 
+clothes_lock = threading.Lock()
+
 def handle_card(card_data):
     reply_message_id = card_data['open_message_id']
     action = card_data['action']
@@ -60,12 +62,12 @@ def start_clothes_img_theards(reply_message_id):
     threads = []
     for img_key in img_keys:
         logger.info("开始clothes处理线程: "+img_key)
-        # img_clothes(reply_message_id, img_key)
-        t = threading.Thread(target=img_clothes, args=(reply_message_id, img_key))
-        threads.append(t)
-        t.start()
-    for t in threads:
-        t.join()
+        with clothes_lock:
+            # 在锁的上下文中启动线程,确保一次只启动一个
+            t = threading.Thread(target=img_clothes, args=(reply_message_id, img_key))
+            threads.append(t)
+            t.start()
+            t.join()  # 等待当前线程完成再启动下一个
 
 def mark_img_fail(reply_message_id, img_key):
     config_data = load_config_from_json(reply_message_id)

+ 23 - 0
handler/meg_handler.py

@@ -46,9 +46,21 @@ def handle_meg(meg, event_id, user_open_id):
                             else:
                                 update_data = img_in_meg_card(rich_text_content, if_has_download = True, reply_message_id = reply_msg_id)
                             update_message(reply_msg_id, update_data)
+                    else:
+                        # 无图片
+                        return
+                        
                 else:
                     # 未@机器人
                     return
+            else:
+                # 为普通消息
+                if 'mentions' in meg:
+                    if any(item['name'] == 'Stable Diffusion Of HOXI' for item in meg['mentions']):
+                        logger.info('返回使用手册')
+                        response_data = no_img_in_meg_card()
+                        res = reply_message(message_id, response_data)
+                return
         else:
             # 处理过的消息
             logger.info(message_id + '已处理过')
@@ -89,6 +101,17 @@ def download_img_in_content(message_id, reply_message_id, content):
                     save_image(response.content, reply_message_id, img_key)
                     logger.info(img_key + "保存成功")
 
+
+# 生成无图片的卡片消息
+def no_img_in_meg_card():
+    response_data ={
+        "msg_type": "interactive",
+    }
+    card_content = load_template('no_img_card')
+    response_data['content'] = json.dumps(card_content)
+    return response_data
+
+
 # 生成交互卡片消息
 def img_in_meg_card(content, if_has_download = False, reply_message_id = None):
     response_data ={

+ 48 - 0
template/no_img_card.json

@@ -0,0 +1,48 @@
+{
+    "header": {
+        "title": {
+            "tag": "plain_text",
+            "content": "Stable Diffusion Of HOXI使用说明"
+        },
+        "template": "green"
+    },
+    "elements": [
+        {
+            "tag": "div",
+            "text": {
+                "content": "**使用方法**:@Stable Diffusion Of HOXI + 任意数量图片,然后根据对应指导操作",
+                "tag": "lark_md"
+            }
+        },
+        {
+            "tag": "div",
+            "text": {
+                "content": "- **试衣间功能**:开放\n- **Furry头像功能**:待开放\n",
+                "tag": "lark_md"
+            }
+        },
+        {
+            "tag": "hr"
+        },
+        {
+            "tag": "div",
+            "text": {
+                "content": "[记录文档](https://hoxigames.feishu.cn/docx/HGKNdE9rKo7qeOxOGKgcGwgQnie)",
+                "tag": "lark_md"
+            }
+        },
+        {
+            "tag": "note",
+            "elements": [
+                {
+                    "tag": "lark_md",
+                    "content": "**v1.0.6 后续更新:试衣间可选保留背景、上传图片自动化训练Lora并添加至选项**"
+                }
+            ]
+        }
+    ],
+    "config": {
+        "enable_forward": true,
+        "update_multi":true
+    }
+}

+ 1 - 1
template/waiting_card.json

@@ -42,7 +42,7 @@
             "elements": [
                 {
                     "tag": "plain_text",
-                    "content": "一般一张图5秒左右( •̀ ω •́ )✧"
+                    "content": "一般一张图20秒左右( •̀ ω •́ )✧"
                 }
             ]
         }

BIN
utils/mask1.png


BIN
utils/mask2.png


BIN
utils/result.png