55import json
66from pathlib import Path
77
8+ import anyio
89from nonebot import logger
10+ import nonebot_plugin_localstore as store
911import numpy as np
1012from PIL import Image
1113
12- from nonebot_plugin_nyaturingtest .vlm import SiliconFlowVLM
13-
1414from .config import plugin_config
15+ from .vlm import SiliconFlowVLM
1516
16- IMAGE_CACHE_DIR = Path (" image_cache" )
17+ IMAGE_CACHE_DIR = Path (f" { store . get_plugin_cache_dir () } / image_cache" )
1718
1819
1920@dataclass
@@ -80,7 +81,7 @@ def __init__(self):
8081 IMAGE_CACHE_DIR .mkdir (parents = True , exist_ok = True )
8182 self ._initialized = True
8283
83- def get_image_description (self , image_base64 : str , is_sticker : bool ) -> ImageWithDescription | None :
84+ async def get_image_description (self , image_base64 : str , is_sticker : bool ) -> ImageWithDescription | None :
8485 """
8586 获取图片描述
8687 """
@@ -90,20 +91,21 @@ def get_image_description(self, image_base64: str, is_sticker: bool) -> ImageWit
9091 # 检查缓存
9192 cache = IMAGE_CACHE_DIR .joinpath (f"{ image_hash } .json" )
9293 if cache .exists ():
93- with open (cache , encoding = "utf-8" ) as f :
94- image_with_desc_raw = f .read ()
94+ async with await anyio . open_file (cache , encoding = "utf-8" ) as f :
95+ image_with_desc_raw = await f .read ()
9596 try :
9697 image_with_desc = ImageWithDescription .from_json (image_with_desc_raw )
9798 if image_with_desc .is_sticker != is_sticker :
9899 image_with_desc .is_sticker = is_sticker
99100 # 修改缓存文件
100- with open (cache , "w" , encoding = "utf-8" ) as f :
101- f .write (image_with_desc .to_json ())
101+ async with await anyio . open_file (cache , "w" , encoding = "utf-8" ) as f :
102+ await f .write (image_with_desc .to_json ())
102103 return image_with_desc
103104 except ValueError as e :
104105 logger .error (f"缓存文件({ cache } )格式错误,重新生成" )
105106 logger .error (e )
106107 cache .unlink () # 删除缓存文件
108+ cache .unlink () # 删除缓存文件
107109
108110 # 获取图片描述
109111 # 获取图片类型
@@ -120,30 +122,30 @@ def get_image_description(self, image_base64: str, is_sticker: bool) -> ImageWit
120122 return None
121123 prompt = """这是一个动态图,每一张图代表了动态图的某一帧,黑色背景代表透明。"请用中文描述这张图片的内容。如
122124 果有文字,请把文字都描述出来。并尝试猜测这个图片的含义。最多100个字"""
123- description = self ._vlm .request (
125+ description = await self ._vlm .request (
124126 prompt = prompt ,
125127 image_base64 = gif_transfromed ,
126128 image_format = "jpeg" ,
127129 )
128130 # 分析表达的情感
129131 prompt = """这是一个动态图,每一张图代表了动态图的某一帧,黑色背景代表透明。请分析这个表情包表达的情感,
130132 用中文给出'情感,类型,含义'的三元式描述,要求每个描述都是一个简单的词语"""
131- description_emotion = self ._vlm .request (
133+ description_emotion = await self ._vlm .request (
132134 prompt = prompt ,
133135 image_base64 = gif_transfromed ,
134136 image_format = "jpeg" ,
135137 )
136138 else :
137139 prompt = "请用中文描述这张图片的内容。如果有文字,请把文字都描述出来。并尝试猜测这个图片的含义。最多100个字"
138- description = self ._vlm .request (
140+ description = await self ._vlm .request (
139141 prompt = prompt ,
140142 image_base64 = image_base64 ,
141143 image_format = image_format ,
142144 )
143145 # 分析表达的情感
144146 prompt = """请分析这个表情包表达的情感,用中文给出'情感,类型,含义'的三元式描述,要求每个描述都是一个简单的
145147 词语"""
146- description_emotion = self ._vlm .request (
148+ description_emotion = await self ._vlm .request (
147149 prompt = prompt ,
148150 image_base64 = image_base64 ,
149151 image_format = "jpeg" ,
@@ -158,10 +160,9 @@ def get_image_description(self, image_base64: str, is_sticker: bool) -> ImageWit
158160 emotion = description_emotion ,
159161 is_sticker = is_sticker ,
160162 )
161-
162163 # 缓存结果
163- with open (cache , "w" , encoding = "utf-8" ) as f :
164- f .write (result .to_json ())
164+ async with await anyio . open_file (cache , "w" , encoding = "utf-8" ) as f :
165+ await f .write (result .to_json ())
165166
166167 return result
167168
0 commit comments