toxic-debug

This commit is contained in:
Untone 2024-09-26 20:17:42 +03:00
parent 963865f482
commit 0a21993c5b

View File

@ -35,31 +35,36 @@ async def messages_routing(msg, state):
latest_toxic_message_id = await redis.get(f"toxic:{cid}") latest_toxic_message_id = await redis.get(f"toxic:{cid}")
pattern = f"toxic:{cid}:{uid}:*" pattern = f"toxic:{cid}:{uid}:*"
scores = [] scores = []
scoring_msg_id = 0
async for key in redis.scan_iter(pattern): async for key in redis.scan_iter(pattern):
scr = await redis.get(key) scr = await redis.get(key)
if isinstance(scr, int): if isinstance(scr, int):
scores.append(scr) scores.append(scr)
logger.debug(f'found {len(scores)} messages') logger.debug(f'found {len(scores)} messages')
toxic_score = math.floor(sum(scores)/len(scores)) if scores else 0 toxic_score = math.floor(sum(scores)/len(scores)) if scores else 0
scoring_msg_id = reply_msg.get("message_id", int(latest_toxic_message_id)) if reply_msg else int(latest_toxic_message_id) if reply_msg:
msg_toxic_key = f"toxic:{cid}:{uid}:{scoring_msg_id}" scoring_msg_id = reply_msg.get("message_id")
logger.debug('msg_toxic_key: ', msg_toxic_key) if not scoring_msg_id and latest_toxic_message_id:
one_score = await redis.get(msg_toxic_key) scoring_msg_id = int(latest_toxic_message_id)
logger.debug('one_score: ', one_score) if scoring_msg_id:
emoji = '😳' if toxic_score > 90 else '😟' if toxic_score > 80 else '😏' if toxic_score > 60 else '🙂' if toxic_score > 20 else '😇' msg_toxic_key = f"toxic:{cid}:{uid}:{scoring_msg_id}"
text = f"{int(one_score or 0)}% токсичности\nСредняя токсичность сообщений: {toxic_score}% {emoji}" logger.debug('msg_toxic_key: ', msg_toxic_key)
one_score = await redis.get(msg_toxic_key)
logger.debug('one_score: ', one_score)
emoji = '😳' if toxic_score > 90 else '😟' if toxic_score > 80 else '😏' if toxic_score > 60 else '🙂' if toxic_score > 20 else '😇'
text = f"{int(one_score or 0)}% токсичности\nСредняя токсичность сообщений: {toxic_score}% {emoji}"
if text:
await telegram_api(
"sendMessage",
chat_id=cid,
reply_to_message_id=scoring_msg_id,
text=text
)
await telegram_api( await telegram_api(
"deleteMessage", "deleteMessage",
chat_id=cid, chat_id=cid,
message_id=mid message_id=mid
) )
if text:
await telegram_api(
"sendMessage",
chat_id=cid,
reply_to_message_id=scoring_msg_id,
text=text
)
else: else:
toxic_score = detector(text) toxic_score = detector(text)
toxic_perc = math.floor(toxic_score*100) toxic_perc = math.floor(toxic_score*100)