Spaces:
Runtime error
Runtime error
File size: 5,896 Bytes
aad7814 | 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 | """Vision analysis of selected section photos (max 2) for report generation."""
from __future__ import annotations
import base64
import logging
from dataclasses import dataclass
from pathlib import Path
from backend.config import settings
from backend.core import photo_store
from backend.llm import openai_client
from backend.prompts.vision_prompt import build_vision_messages
logger = logging.getLogger(__name__)
_VISION_MAX_ATTEMPTS = 2
@dataclass
class VisionSectionResult:
observations: list[str]
limitations: list[str]
ok: bool
error: str = ""
photos_analyzed: int = 0
def _data_url(path: Path, content_type: str) -> str:
raw = path.read_bytes()
b64 = base64.standard_b64encode(raw).decode("ascii")
return f"data:{content_type};base64,{b64}"
def _dedupe_observations(lines: list[str], *, max_items: int) -> list[str]:
seen: set[str] = set()
out: list[str] = []
for line in lines:
key = line.lower().strip()[:200]
if not key or key in seen:
continue
seen.add(key)
out.append(line.strip().rstrip(".").strip() + ".")
if len(out) >= max_items:
break
return out
def analyze_section_photos(
image_paths: list[tuple[Path, str]],
*,
section_label: str,
) -> VisionSectionResult:
"""Analyze up to two selected photos with engineer-grade vision prompts."""
if not image_paths:
return VisionSectionResult([], [], ok=True)
if not settings.section_photo_vision_enabled:
return VisionSectionResult(
[], [], ok=False,
error="Section photo vision is disabled.",
)
if not openai_client.is_available():
return VisionSectionResult(
[], [], ok=False,
error="OpenAI API key not configured for vision analysis.",
)
images: list[dict] = []
for path, ct in image_paths[: settings.max_section_photos_for_ai]:
try:
images.append({
"type": "image_url",
"image_url": {"url": _data_url(path, ct), "detail": "high"},
})
except OSError as exc:
logger.warning("Could not read photo %s: %s", path, exc)
if not images:
return VisionSectionResult(
[], [], ok=False,
error="Selected photos could not be read from storage.",
)
messages = build_vision_messages(
section_label=section_label,
image_count=len(images),
max_obs=min(10, settings.vision_max_observations),
)
user_content: list[dict] = [{"type": "text", "text": messages[-1]["content"]}]
user_content.extend(images)
messages[-1] = {"role": "user", "content": user_content}
last_error = ""
for attempt in range(_VISION_MAX_ATTEMPTS):
try:
data = openai_client.chat_vision_json(
messages,
model=settings.vision_model,
max_tokens=settings.vision_max_tokens,
)
raw_obs = data.get("observations")
if raw_obs is None:
return VisionSectionResult(
[], [], ok=False,
error="Vision response missing 'observations' key.",
)
if not isinstance(raw_obs, list):
return VisionSectionResult(
[], [], ok=False,
error="Vision 'observations' is not a list.",
)
limitations = [
str(x).strip() for x in (data.get("limitations") or [])
if str(x).strip()
]
clean = _dedupe_observations(
[str(o).strip() for o in raw_obs if str(o).strip()],
max_items=settings.vision_max_observations,
)
logger.info(
"Vision OK for %s: %d observations from %d photo(s)",
section_label, len(clean), len(images),
)
return VisionSectionResult(
clean, limitations, ok=True, photos_analyzed=len(images),
)
except Exception as exc: # noqa: BLE001
last_error = str(exc)
logger.warning(
"Vision attempt %d/%d failed for %s: %s",
attempt + 1, _VISION_MAX_ATTEMPTS, section_label, exc,
)
return VisionSectionResult(
[], [], ok=False,
error=last_error or "Vision analysis failed after retries.",
photos_analyzed=0,
)
def vision_observations_for_section(
tenant_id: str,
draft_id: str | None,
section_id: str,
section_label: str,
) -> tuple[list[str], str | None]:
"""Load selected photos for a draft section and return (observations, user_note)."""
if not draft_id:
return [], None
all_photos = photo_store.list_section_photos(tenant_id, draft_id, section_id)
if not all_photos:
return [], None
selected = [p for p in all_photos if p.selected_for_ai]
if not selected:
max_ai = settings.max_section_photos_for_ai
return [], (
f"{len(all_photos)} photo(s) uploaded but none selected for AI analysis. "
f"Select up to {max_ai} photo(s) before generating."
)
paths = photo_store.selected_photo_paths(tenant_id, draft_id, section_id)
result = analyze_section_photos(paths, section_label=section_label)
if result.ok:
note: str | None = None
if result.limitations:
note = "Photo limitations: " + " ".join(result.limitations[:2])
if not result.observations and not note:
note = "Selected photos did not yield usable observations; section is text-led."
return list(result.observations), note
return [], (
f"Photo analysis unavailable ({result.error}). "
"Section generated from notes and past-report text only."
)
|