-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
323 lines (270 loc) · 11.3 KB
/
processor.py
File metadata and controls
323 lines (270 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
Обробка зображень через AI-модель: витягування інформації про людей.
"""
import json
import logging
import re
import time
from pathlib import Path
from typing import Optional
from ai_clients import AIClient, api_error_detail_for_log, create_ai_client
_PROMPTS_DIR = Path(__file__).resolve().parent / "prompts"
_BASE_PROMPT_FILE = _PROMPTS_DIR / "_base.txt"
_prompt_template_cache: Optional[str] = None
# У prompts/_base.txt блок «дефолтний формат JSON» між маркерами; при розширеному промпті його прибираємо
# (формат задає сам файл розширеного промпта).
_DEFAULT_FORMAT_BEGIN = "<!--BEGIN_DEFAULT_FORMAT-->"
_DEFAULT_FORMAT_END = "<!--END_DEFAULT_FORMAT-->"
_DEFAULT_FORMAT_BLOCK_RE = re.compile(
re.escape(_DEFAULT_FORMAT_BEGIN) + r"\s*.*?\s*" + re.escape(_DEFAULT_FORMAT_END),
flags=re.DOTALL,
)
# Для --extended-prompt: якщо значення збігається з цим шаблоном, шукаємо файл
# prompts/<значення>.txt (розширений промпт), інакше — довільний текст.
_EXTENDED_PROMPT_FILE_STEM_RE = re.compile(r"^[A-Za-z0-9_-]+$")
def _load_prompt_template() -> str:
global _prompt_template_cache
if _prompt_template_cache is None:
_prompt_template_cache = _BASE_PROMPT_FILE.read_text(encoding="utf-8")
return _prompt_template_cache
# ------------------------------------------------------------------ #
# Витягування номеру з імені файлу #
# ------------------------------------------------------------------ #
def extract_number(filename: str) -> Optional[int]:
"""
Витягує номер зі стовбця файлу. Правила:
- "00023.jpg" → 23
- "scan_00645.jpg" → 645 (береться останній числовий блок)
- "abc.jpg" → None
"""
stem = Path(filename).stem
matches = re.findall(r"\d+", stem)
if not matches:
return None
return int(matches[-1]) # знімає ведучі нулі автоматично
# ------------------------------------------------------------------ #
# Базовий промпт: prompts/_base.txt (+ опційний блок prompts/<stem>.txt). #
# ------------------------------------------------------------------ #
def _extended_prompt_active(extended_prompt: Optional[str]) -> bool:
return bool(extended_prompt and str(extended_prompt).strip())
def _build_prompt(extended_prompt: Optional[str]) -> str:
extra = ""
if extended_prompt:
raw = extended_prompt.strip()
if raw:
if _EXTENDED_PROMPT_FILE_STEM_RE.fullmatch(raw):
extended_path = (_PROMPTS_DIR / f"{raw}.txt").resolve()
if extended_path.is_file():
extra = "\n" + extended_path.read_text(encoding="utf-8")
else:
extra = f"\nДодатковий контекст: {raw}"
else:
extra = f"\nДодатковий контекст: {raw}"
raw = _load_prompt_template().replace("{extra}", extra)
if _extended_prompt_active(extended_prompt):
raw = _DEFAULT_FORMAT_BLOCK_RE.sub("", raw)
else:
raw = raw.replace(_DEFAULT_FORMAT_BEGIN, "").replace(_DEFAULT_FORMAT_END, "")
return raw
# ------------------------------------------------------------------ #
# Виклик моделі #
# ------------------------------------------------------------------ #
_MIME_MAP = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".tif": "image/tiff",
".tiff": "image/tiff",
".webp": "image/webp",
".heic": "image/heic",
".heif": "image/heic",
}
# Клієнт ініціалізується один раз через init_client()
_client: Optional[AIClient] = None
_log = logging.getLogger(__name__)
def init_client(provider: str, api_key: Optional[str] = None):
"""Ініціалізує AI-клієнт. Для старого виклику init_client(key) лишається Gemini."""
global _client
if api_key is None:
api_key = provider
provider = "gemini"
_client = create_ai_client(provider, api_key)
def process_image(
local_path: str,
model_name: str,
temperature: float,
extended_prompt: Optional[str],
) -> tuple:
"""
Відправляє зображення в AI-модель. Повертає (persons, scan_meta):
persons — список осіб з name, surname, meta (усі інші поля з JSON особи);
scan_meta — dict для scans.meta або None (дата документа в режимі без розширеного промпта).
"""
if _client is None:
raise RuntimeError("AI-клієнт не ініціалізовано. Викличте init_client() спочатку.")
ext = Path(local_path).suffix.lower()
mime_type = _MIME_MAP.get(ext, "image/jpeg")
with open(local_path, "rb") as f:
image_bytes = f.read()
prompt = _build_prompt(extended_prompt)
_log.info(
" Запит до моделі (%s, %s)…",
_client.provider_name,
model_name,
)
last_exc: Optional[BaseException] = None
for attempt in range(1, _client.retry_max_attempts + 1):
try:
response_text = _client.generate_json_from_image(
model_name=model_name,
image_bytes=image_bytes,
mime_type=mime_type,
prompt=prompt,
temperature=temperature,
)
return _parse_response(
response_text,
extended_used=_extended_prompt_active(extended_prompt),
)
except Exception as e:
last_exc = e
if attempt >= _client.retry_max_attempts or not _client.is_retryable_error(e):
break
status = _client.http_status_from_exception(e)
delay = _client.retry_delay_seconds(e, attempt)
kind = (
f"HTTP {status}"
if status is not None
else "мережа"
)
_log.warning(
"Тимчасова помилка %s (%s, спроба %s/%s): %s — повтор через %.1f с",
_client.provider_name,
kind,
attempt,
_client.retry_max_attempts,
e,
delay,
)
time.sleep(delay)
assert last_exc is not None
_log.error(
"Помилка моделі без успішного повтору: %s",
api_error_detail_for_log(last_exc),
)
raise last_exc
def _flat_dict_to_scan_meta(d: dict) -> dict:
"""Плоскі поля (scan або scan.meta) → очищений dict для scans.meta."""
out: dict = {}
if "document_year" in d:
out["document_year"] = _clean_int(d.get("document_year"))
if "document_date" in d:
out["document_date"] = _clean_str(d.get("document_date"))
for k, v in d.items():
if k in ("document_year", "document_date"):
continue
if v is None or isinstance(v, (dict, list)):
continue
if isinstance(v, bool):
out[k] = v
elif isinstance(v, int):
out[k] = v
elif isinstance(v, float):
out[k] = int(v) if v == int(v) else v
else:
out[k] = _clean_str(v)
return out
def _scan_meta_from_block(block: dict) -> Optional[dict]:
"""
Об'єкт scan → JSON для scans.meta.
Підтримка: плоскі поля; або вкладений block['meta'] (має пріоритет при збігу ключів).
"""
out: dict = {}
top = {k: v for k, v in block.items() if k != "meta"}
out.update(_flat_dict_to_scan_meta(top))
if isinstance(block.get("meta"), dict):
out.update(_flat_dict_to_scan_meta(block["meta"]))
return out if any(x is not None for x in out.values()) else None
def _parse_response(text: str, *, extended_used: bool) -> tuple:
"""Парсить JSON: масив осіб або об'єкт з persons (+ опційно scan). Повертає (persons, scan_meta)."""
text = text.strip()
text = re.sub(r"^```(?:json)?\s*", "", text)
text = re.sub(r"\s*```$", "", text)
text = text.strip()
data = json.loads(text)
scan_meta: Optional[dict] = None
raw_list: Optional[list] = None
if isinstance(data, list):
raw_list = data
elif isinstance(data, dict):
raw_list = data.get("persons")
scan_block = data.get("scan")
if isinstance(scan_block, dict):
scan_meta = _scan_meta_from_block(scan_block)
if raw_list is None:
raise ValueError(
"Очікувався JSON-масив осіб або об'єкт з ключем 'persons' (масив осіб)"
)
else:
raise ValueError(f"Очікувався JSON-масив або об'єкт, отримано: {type(data)}")
persons = []
for item in raw_list:
if not isinstance(item, dict):
continue
surname = _clean_surname(item.get("surname"))
if surname is None:
continue
meta = _person_meta_from_item(item)
persons.append({
"name": _clean_str(item.get("name")),
"surname": surname,
"meta": meta if meta else None,
})
return persons, scan_meta
# Цілі поля в meta особи — нормалізуємо через _clean_int.
_META_INT_KEYS = frozenset({"yob", "children_count", "marriage_ordinal"})
def _meta_fields_from_flat_dict(d: dict) -> dict:
"""Плоскі поля meta (без name/surname на верхньому рівні особи)."""
out: dict = {}
for k, v in d.items():
if k in ("name", "surname"):
continue
if k in _META_INT_KEYS:
out[k] = _clean_int(v)
continue
if isinstance(v, dict) or isinstance(v, list):
continue
if isinstance(v, bool):
out[k] = v
elif isinstance(v, int):
out[k] = v
elif isinstance(v, float):
out[k] = int(v) if v == int(v) else v
else:
out[k] = _clean_str(v)
return out
def _person_meta_from_item(item: dict) -> dict:
"""
Якщо є вкладений item['meta'] — лише звідти (поля name/surname у meta ігноруємо).
Інакше — усі поля крім name/surname з кореня об'єкта (плоский формат).
"""
if isinstance(item.get("meta"), dict):
return _meta_fields_from_flat_dict(item["meta"])
return _meta_fields_from_flat_dict(item)
def _clean_str(val) -> Optional[str]:
if val is None:
return None
s = str(val).strip()
return s if s else None
def _clean_surname(val) -> Optional[str]:
s = _clean_str(val)
if s is None or s.casefold() == "null":
return None
return s
def _clean_int(val) -> Optional[int]:
if val is None:
return None
try:
return int(val)
except (ValueError, TypeError):
return None