Update photo_bot/handlers.py: preview after AI approval + dynamic log path + system_error_text

This commit is contained in:
2026-08-13 14:52:30 +07:00
parent 809f5f4980
commit 95803b2c3c
+59 -38
View File
@@ -1,3 +1,4 @@
# /opt/trueconf_bot/photo_bot/handlers.py
import os import os
import asyncio import asyncio
import logging import logging
@@ -14,19 +15,11 @@ from trueconf.types import FSInputFile
import config.config as config import config.config as config
from utils import states from utils import states
from utils.menu import MENU_TEXT from utils.menu import MENU_TEXT
from photo_bot.photo_ai_checker import verify_photo_with_ai from utils.texts import * # 👈 Импортируем все тексты и константы проекта
from utils.texts import (
EMOJI_DIGITS, # 🔌 Бизнес-логика обработки и ИИ-валидации
PHOTO_MAIN_MENU_TEXT, from photo_bot.photo_processor import prepare_ad_photo
PHOTO_UNKNOWN_CMD_TEXT, from photo_bot.ai_validator import validate_photo_ai
photo_error_handling,
PHOTO_UPLOADING_TEXT,
photo_success_sent,
photo_cancel_text,
PHOTO_PROCESSING_TEXT,
photo_preview_ready,
photo_error_processing,
)
# 🔌 Импортируем централизованную функцию сбора статистики из main # 🔌 Импортируем централизованную функцию сбора статистики из main
from utils.stats_logger import log_menu_stats from utils.stats_logger import log_menu_stats
@@ -47,7 +40,7 @@ def log_and_notify_photo_error(user_id: str, action: str, error_details: str):
"""Записывает системную ошибку в лог-файл и отправляет email-алерт администраторам""" """Записывает системную ошибку в лог-файл и отправляет email-алерт администраторам"""
global last_email_time global last_email_time
log_dir = "/opt/trueconf_bot/photo_bot/logs" log_dir = os.path.dirname(os.path.abspath(__file__))
os.makedirs(log_dir, exist_ok=True) os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, "photo_errors.log") log_file = os.path.join(log_dir, "photo_errors.log")
@@ -184,48 +177,76 @@ async def photo_module_handler(msg: Message):
return return
else: else:
await msg.answer(PHOTO_UNKNOWN_CMD_TEXT, parse_mode="html") await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html")
return return
# --- ПРИЕМ НОВОГО ФОТО (ТЕСТОВЫЙ РЕЖИМ: ТОЛЬКО ИИ) --- # --- ПРИЕМ НОВОГО ФОТО ---
if is_attachment: if is_attachment:
file_id = getattr(msg.content, "file_id", None) file_id = getattr(msg.content, "file_id", None)
raw_data = None ready_path = f"/tmp/{user_login}.bmp"
raw_path = f"/tmp/raw_{user_login}.webp"
await msg.answer("🤖 <i>Отправка фото на проверку в ИИ (srvkem-ii-03:8083)...</i>", parse_mode="html") await msg.answer(PHOTO_PROCESSING_TEXT, parse_mode="html")
try: try:
downloaded_file = await states.tc_bot.download_file_by_id(file_id) # 1. Скачиваем байты файла из TrueConf
raw_data = downloaded_file file_bytes = await states.tc_bot.download_file_by_id(file_id)
# Отправляем фото напрямую в ИИ Gemma 12B Vision # 2. Сохраняем байты во временный файл на диск
ai_result = await verify_photo_with_ai(raw_data) def save_raw_bytes(b_data, target_path):
with open(target_path, "wb") as f:
f.write(b_data)
# 🛡️ Защита от неожиданного формата (гарантирует отсутствие NameError) await asyncio.to_thread(save_raw_bytes, file_bytes, raw_path)
if not isinstance(ai_result, dict):
ai_result = {"is_valid": False, "reason": "Некорректный формат ответа от ИИ."}
is_valid = ai_result.get("is_valid", False) # 3. Техническая конвертация в BMP без геометрических проверок
reason = ai_result.get("reason", "Нет ответа от модели.") def convert_to_bmp_only(src_path, dst_path):
with Image.open(src_path) as img:
img.convert("RGB").save(dst_path, format="BMP")
await asyncio.to_thread(convert_to_bmp_only, raw_path, ready_path)
# 4. ИИ-ПРОВЕРКА ЧЕРЕЗ GEMMA 4 VISION (srvkem-ii-03)
ai_res = await validate_photo_ai(raw_path)
if ai_res["success"] and ai_res["approved"]:
# --- УСПЕШНАЯ ПРОВЕРКА ИИ ---
log_menu_stats(user_id, "Photo Bot", "Загрузка фото: Одобрено ИИ")
pending_photos[user_id] = ready_path
preview_path = f"/tmp/prev_{user_login}.jpg"
try:
Image.open(ready_path).save(preview_path, format="JPEG")
p_main = FSInputFile(preview_path, filename="preview.jpg")
p_thumb = FSInputFile(preview_path, filename="preview_thumb.jpg")
await msg.answer_photo(p_main, p_thumb)
except Exception as lib_err:
await asyncio.to_thread(log_and_notify_photo_error, user_id, "Генерация превью", str(lib_err))
finally:
if os.path.exists(preview_path):
os.remove(preview_path)
await asyncio.sleep(0.5)
await msg.answer(photo_preview_ready(), parse_mode="html")
if not is_valid:
await msg.answer(
f"❌ <b>[ИИ Отклонил]</b>\n\n<b>Причина:</b> {reason}",
parse_mode="html"
)
else: else:
# --- ИИ ОТКЛОНИЛ ФОТО ИЛИ ПРОИЗОШЛА ОШИБКА ---
log_menu_stats(user_id, "Photo Bot", "Загрузка фото: Отклонено ИИ")
reason_text = ai_res["text"] if ai_res["success"] else ai_res.get("error", "Не удалось проверить фото через ИИ.")
await msg.answer( await msg.answer(
f"<b>[ИИ Одобрил]</b>\n\n<b>Вердикт:</b> Фотография полностью соответствует требованиям.", f"<b>❌ Фотография не прошла проверку</b>\n\n{reason_text}",
parse_mode="html" parse_mode="html"
) )
except Exception as e: except Exception as e:
logger.exception(f"Ошибка при передаче фото в ИИ: {e}")
await handle_photo_error(msg, user_id, "Скачивание и обработка файла", str(e)) await handle_photo_error(msg, user_id, "Скачивание и обработка файла", str(e))
finally: finally:
if raw_data and isinstance(raw_data, str) and os.path.exists(raw_data): if raw_path and os.path.exists(raw_path):
try: try:
os.remove(raw_data) os.remove(raw_path)
except Exception: except Exception as cleanup_err:
pass await asyncio.to_thread(log_and_notify_photo_error, user_id, "Очистка временных файлов", str(cleanup_err))
return return