domovoy_bot/services/gis_pdf_pipeline/evidence_analyzer.py

55 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import json
import base64
import google.generativeai as genai
from pathlib import Path
# Конфигурация API
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
def analyze_evidence(image_path):
"""
Использует Gemini 2.0 Flash для извлечения метаданных из скриншота ГИС ЖКХ.
"""
if not os.environ.get("GOOGLE_API_KEY"):
return {"error": "API Key missing"}
model = genai.GenerativeModel('gemini-2.0-flash')
with open(image_path, "rb") as f:
image_data = f.read()
prompt = """
Проанализируй этот скриншот личного кабинета ГИС ЖКХ.
Тебе нужно извлечь следующие данные в формате JSON:
{
"number": "номер обращения/дела",
"date": "дата опубликования или создания",
"time": "время",
"status": "текущий статус обращения",
"sender": "от кого",
"recipient": "кому"
}
Если какой-то из параметров не найден, напиши "null".
Ответ дай ТОЛЬКО в формате чистого JSON без лишнего текста.
"""
try:
response = model.generate_content([
prompt,
{'mime_type': 'image/png', 'data': image_data}
])
# Очистка от возможных ```json ... ```
clean_text = response.text.replace('```json', '').replace('```', '').strip()
return json.loads(clean_text)
except Exception as e:
print(f"Ошибка парсинга ответа Gemini: {e}")
return None
if __name__ == "__main__":
# Тестовый запуск на первом попавшемся скрине
# Путь скорректирован для сервера
test_path = "/volume1/web/legal/gis/raw/73-2026-14124/evidence.png"
if os.path.exists(test_path):
result = analyze_evidence(test_path)
print(json.dumps(result, indent=2, ensure_ascii=False))