File size: 3,887 Bytes
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
8682034
1b4b6d2
 
 
 
 
 
8a25d8e
1b4b6d2
 
1997b5a
 
 
 
1b4b6d2
 
 
 
 
c1e71b3
 
 
8a25d8e
 
 
 
 
 
 
 
 
 
 
 
 
c1e71b3
 
 
 
 
0d71027
c1e71b3
 
 
 
 
0d71027
c1e71b3
 
 
 
 
 
 
 
 
 
 
0d71027
c1e71b3
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
b9ca909
 
 
1b4b6d2
 
b9ca909
 
 
1b4b6d2
 
 
0d71027
1b4b6d2
 
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
"""
STEP 2 — Classify Pages
AI reads each page title/text and assigns drawing type
User can correct any misclassification
"""
import pandas as pd
import os, io, re, json
from pdf_engine.smart_classifier import classify_all_pages, PAGE_ITEMS_MAP
from pdf_engine.pdf_loader import page_to_pil
from workflow.workflow_state import mark_step_done

# Candidate types per PDF kind — used by the AI-vision refiner
_STRUCT_TYPES = ["foundations", "tie_beam", "column_layout", "upper_columns", "neck_columns",
                 "columns_1f", "columns_2f", "columns_roof",
                 "slab_1st", "slab_2nd", "roof_slab"]
_ARCH_TYPES   = ["ground_floor_plan", "first_floor_plan", "second_floor_plan",
                 "roof_floor_plan", "elevations", "setting_out", "schedules"]


def _ai_classify(img_input, pdf_type: str):
    """Classify ONE page by vision when keyword matching is unsure. Returns a
    PAGE_ITEMS_MAP key or None."""
    from utils.key_manager import get_key_manager
    mgr = get_key_manager()
    api_key, current_model = mgr.get_key_and_model()
    if not api_key or api_key == "NO_API_KEY_FOUND":
        return None
    choices = _STRUCT_TYPES if pdf_type == "structural" else _ARCH_TYPES
    try:
        from google import genai
        from google.genai import types
        import time
        import io as _io

        if isinstance(img_input, bytes):
            img = img_input
        elif isinstance(img_input, str) and os.path.exists(img_input):
            with open(img_input, "rb") as f:
                img = f.read()
        elif isinstance(img_input, np.ndarray):
            pil_img = page_to_pil(img_input)
            buf = _io.BytesIO()
            pil_img.save(buf, format="PNG")
            img = buf.getvalue()
        else:
            return None

        prompt = (
            f"This is a construction drawing. Classify it as one of: {choices}. "
            "Reply ONLY with valid JSON: {\"type\": \"<chosen_type>\"}"
        )

        for attempt in range(2):
            api_key, current_model = mgr.get_key_and_model()
            if not api_key or api_key == "NO_API_KEY_FOUND":
                return None

            try:
                client = genai.Client(api_key=api_key, http_options={'timeout': 10})
                resp = client.models.generate_content(
                    model=current_model,
                    contents=[types.Part.from_bytes(data=img, mime_type="image/png"), prompt])
                raw = re.sub(r"```json|```", "", resp.text).strip()
                result = json.loads(raw).get("type")
                return result if result in PAGE_ITEMS_MAP else None
            except Exception as e:
                last_err = str(e)
                if any(code in last_err for code in ("429", "RESOURCE_EXHAUSTED", "quota")):
                    mgr.mark_rate_limited(api_key, current_model)
                    continue
                time.sleep(1)

        return None
    except Exception:
        return None




def _classify_single(text: str, pdf_type: str) -> dict:
    from pdf_engine.smart_classifier import PAGE_ITEMS_MAP
    text_lower = text.lower()
    scores = {}
    for ptype, config in PAGE_ITEMS_MAP.items():
        # Enforce PDF domain isolation (structural vs architectural)
        if config.get("pdf_type") and config.get("pdf_type") != pdf_type:
            continue
        score = sum(1 for kw in config.get("drawing_keywords", []) if kw.lower() in text_lower)
        scores[ptype] = score
    if not scores:
        return {"detected_type": "unknown", "confidence": "low", "items": []}
    best = max(scores, key=scores.get)
    score = scores[best]
    return {
        "detected_type": best if score > 0 else "unknown",
        "confidence": "high" if score >= 1 else "low",
        "items": PAGE_ITEMS_MAP.get(best, {}).get("extract_items", []) if score > 0 else [],
    }