新增指定sampler, hires的功能
修复tags中方括号编码不正常的问题
This commit is contained in:
78
extension/anlas.py
Normal file
78
extension/anlas.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
import aiofiles
|
||||
from nonebot.adapters.onebot.v11 import Bot,GroupMessageEvent, Message, MessageSegment
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot import on_command, get_driver
|
||||
|
||||
jsonpath = Path("data/novelai/anlas.json").resolve()
|
||||
setanlas = on_command(".anlas")
|
||||
|
||||
@setanlas.handle()
|
||||
async def anlas_handle(bot:Bot,event: GroupMessageEvent, args: Message = CommandArg()):
|
||||
atlist = []
|
||||
user_id = str(event.user_id)
|
||||
for seg in event.original_message["at"]:
|
||||
atlist.append(seg.data["qq"])
|
||||
messageraw = args.extract_plain_text().strip()
|
||||
if not messageraw or messageraw == "help":
|
||||
await setanlas.finish(f"点数计算方法(四舍五入):分辨率*数量*强度/45875\n.anlas+数字+@某人 将自己的点数分给对方\n.anlas check 查看自己的点数")
|
||||
elif messageraw == "check":
|
||||
if await SUPERUSER(bot,event):
|
||||
await setanlas.finish(f"Master不需要点数哦")
|
||||
else:
|
||||
anlas = await anlas_check(user_id)
|
||||
await setanlas.finish(f"你的剩余点数为{anlas}")
|
||||
if atlist:
|
||||
at = atlist[0]
|
||||
if messageraw.isdigit():
|
||||
anlas_change = int(messageraw)
|
||||
if anlas_change > 1000:
|
||||
await setanlas.finish(f"一次能给予的点数不超过1000")
|
||||
if await SUPERUSER(bot,event):
|
||||
_, result = await anlas_set(at, anlas_change)
|
||||
message = f"分配完成:" + \
|
||||
MessageSegment.at(at)+f"的剩余点数为{result}"
|
||||
else:
|
||||
result, user_anlas = await anlas_set(user_id, -anlas_change)
|
||||
if result:
|
||||
_, at_anlas = await anlas_set(at, anlas_change)
|
||||
message = f"分配完成:\n"+MessageSegment.at(
|
||||
user_id)+f"的剩余点数为{user_anlas}\n"+MessageSegment.at(at)+f"的剩余点数为{at_anlas}"
|
||||
await setanlas.finish(message)
|
||||
else:
|
||||
await setanlas.finish(f"分配失败:点数不足,你的剩余点数为{user_anlas}")
|
||||
await setanlas.finish(message)
|
||||
else:
|
||||
await setanlas.finish(f"请以正整数形式输入点数")
|
||||
else:
|
||||
await setanlas.finish(f"请@你希望给予点数的人")
|
||||
|
||||
|
||||
async def anlas_check(user_id):
|
||||
if not jsonpath.exists():
|
||||
jsonpath.parent.mkdir(parents=True, exist_ok=True)
|
||||
async with aiofiles.open(jsonpath, "w+")as f:
|
||||
await f.write("{}")
|
||||
async with aiofiles.open(jsonpath, "r") as f:
|
||||
jsonraw = await f.read()
|
||||
anlasdict: dict = json.loads(jsonraw)
|
||||
anlas = anlasdict.get(user_id, 0)
|
||||
return anlas
|
||||
|
||||
|
||||
async def anlas_set(user_id, change):
|
||||
oldanlas = await anlas_check(user_id)
|
||||
newanlas = oldanlas+change
|
||||
if newanlas < 0:
|
||||
return False, oldanlas
|
||||
anlasdict = {}
|
||||
async with aiofiles.open(jsonpath, "r") as f:
|
||||
jsonraw = await f.read()
|
||||
anlasdict: dict = json.loads(jsonraw)
|
||||
anlasdict[user_id] = newanlas
|
||||
async with aiofiles.open(jsonpath, "w+") as f:
|
||||
jsonnew = json.dumps(anlasdict)
|
||||
await f.write(jsonnew)
|
||||
return True, newanlas
|
||||
20
extension/daylimit.py
Normal file
20
extension/daylimit.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import time
|
||||
from ..config import config
|
||||
|
||||
|
||||
class DayLimit():
|
||||
day: int = time.localtime(time.time()).tm_yday
|
||||
data: dict = {}
|
||||
|
||||
@classmethod
|
||||
def count(cls, user: str, num):
|
||||
day_ = time.localtime(time.time()).tm_yday
|
||||
if day_ != cls.day:
|
||||
cls.day = day_
|
||||
cls.data = {}
|
||||
count: int = cls.data.get(user, 0)+num
|
||||
if count > config.novelai_daylimit:
|
||||
return -1
|
||||
else:
|
||||
cls.data[user] = count
|
||||
return config.novelai_daylimit-count
|
||||
41
extension/deepdanbooru.py
Normal file
41
extension/deepdanbooru.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import aiohttp
|
||||
import base64
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment
|
||||
from nonebot.log import logger
|
||||
from .translation import translate
|
||||
|
||||
deepdanbooru = on_command(".gettag", aliases={"鉴赏", "查书"})
|
||||
|
||||
|
||||
@deepdanbooru.handle()
|
||||
async def deepdanbooru_handle(event: GroupMessageEvent):
|
||||
url = ""
|
||||
for seg in event.message['image']:
|
||||
url = seg.data["url"]
|
||||
if url:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
logger.info(f"正在获取图片")
|
||||
async with session.get(url) as resp:
|
||||
bytes = await resp.read()
|
||||
str_img = str(base64.b64encode(bytes), "utf-8")
|
||||
message = MessageSegment.at(event.user_id)
|
||||
start = "data:image/jpeg;base64,"
|
||||
str0 = start+str_img
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post('https://mayhug-rainchan-anime-image-label.hf.space/api/predict/', json={"data": [str0, 0.6,"ResNet101"]}) as resp:
|
||||
if resp.status != 200:
|
||||
await deepdanbooru.finish(f"识别失败,错误代码为{resp.status}")
|
||||
jsonresult = await resp.json()
|
||||
data = jsonresult['data'][0]
|
||||
logger.info(f"TAG查询完毕")
|
||||
tags = ""
|
||||
for label in data['confidences']:
|
||||
tags = tags+label["label"]+","
|
||||
tags_ch = await translate(tags.replace("_", " "), "zh")
|
||||
if tags_ch == tags.replace("_", " "):
|
||||
message = message+tags
|
||||
message = message+tags+f"\n机翻结果:"+tags_ch
|
||||
await deepdanbooru.finish(message)
|
||||
else:
|
||||
await deepdanbooru.finish(f"未找到图片")
|
||||
113
extension/translation.py
Normal file
113
extension/translation.py
Normal file
@@ -0,0 +1,113 @@
|
||||
import aiohttp
|
||||
from ..config import config
|
||||
from nonebot.log import logger
|
||||
|
||||
|
||||
async def translate(text: str, to: str):
|
||||
# en,jp,zh
|
||||
result = await translate_deepl(text, to) or await translate_bing(text, to) or await translate_google_proxy(text, to) or await translate_youdao(text, to)
|
||||
if result:
|
||||
return result
|
||||
else:
|
||||
logger.error(f"未找到可用的翻译引擎!")
|
||||
return text
|
||||
|
||||
|
||||
async def translate_bing(text: str, to: str):
|
||||
"""
|
||||
en,jp,zh_Hans
|
||||
"""
|
||||
if to == "zh":
|
||||
to = "zh-Hans"
|
||||
key = config.bing_key
|
||||
if not key:
|
||||
return None
|
||||
header = {
|
||||
"Ocp-Apim-Subscription-Key": key,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
async with aiohttp.ClientSession() as session:
|
||||
body = [{'text': text}]
|
||||
params = {
|
||||
"api-version": "3.0",
|
||||
"to": to,
|
||||
"profanityAction": "Deleted",
|
||||
}
|
||||
async with session.post('https://api.cognitive.microsofttranslator.com/translate', json=body, params=params, headers=header) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"Bing翻译接口调用失败,错误代码{resp.status},{await resp.text()}")
|
||||
return None
|
||||
jsonresult = await resp.json()
|
||||
result=jsonresult[0]["translations"][0]["text"]
|
||||
logger.debug(f"Bing翻译启动,获取到{text},翻译后{result}")
|
||||
return result
|
||||
|
||||
|
||||
async def translate_deepl(text: str, to: str):
|
||||
"""
|
||||
EN,JA,ZH
|
||||
"""
|
||||
to = to.upper()
|
||||
key = config.deepl_key
|
||||
if not key:
|
||||
return None
|
||||
async with aiohttp.ClientSession() as session:
|
||||
params = {
|
||||
"auth_key":key,
|
||||
"text": text,
|
||||
"target_lang": to,
|
||||
}
|
||||
async with session.get('https://api-free.deepl.com/v2/translate', params=params) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"DeepL翻译接口调用失败,错误代码{resp.status},{await resp.text()}")
|
||||
return None
|
||||
jsonresult = await resp.json()
|
||||
result=jsonresult["translations"][0]["text"]
|
||||
logger.debug(f"DeepL翻译启动,获取到{text},翻译后{result}")
|
||||
return result
|
||||
|
||||
|
||||
async def translate_youdao(input: str, type: str):
|
||||
"""
|
||||
默认auto
|
||||
ZH_CH2EN 中译英
|
||||
EN2ZH_CN 英译汉
|
||||
"""
|
||||
if type == "zh":
|
||||
type = "EN2ZH_CN"
|
||||
elif type == "en":
|
||||
type = "ZH_CH2EN"
|
||||
async with aiohttp.ClientSession() as session:
|
||||
data = {
|
||||
'doctype': 'json',
|
||||
'type': type,
|
||||
'i': input
|
||||
}
|
||||
async with session.post("http://fanyi.youdao.com/translate", data=data) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"有道翻译接口调用失败,错误代码{resp.status},{await resp.text()}")
|
||||
return None
|
||||
result = await resp.json()
|
||||
result=result["translateResult"][0][0]["tgt"]
|
||||
logger.debug(f"有道翻译启动,获取到{input},翻译后{result}")
|
||||
return result
|
||||
|
||||
|
||||
async def translate_google_proxy(input: str, to: str):
|
||||
"""
|
||||
en,jp,zh 需要来源语言
|
||||
"""
|
||||
if to == "zh":
|
||||
from_ = "en"
|
||||
else:
|
||||
from_="zh"
|
||||
async with aiohttp.ClientSession()as session:
|
||||
data = {"data": [input, from_, to]}
|
||||
async with session.post("https://hf.space/embed/mikeee/gradio-gtr/+/api/predict", json=data)as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"谷歌代理翻译接口调用失败,错误代码{resp.status},{await resp.text()}")
|
||||
return None
|
||||
result = await resp.json()
|
||||
result=result["data"][0]
|
||||
logger.debug(f"谷歌代理翻译启动,获取到{input},翻译后{result}")
|
||||
return result
|
||||
Reference in New Issue
Block a user