File size: 7,881 Bytes
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
8682034
1b4b6d2
 
 
 
 
 
 
 
 
1997b5a
 
 
 
1b4b6d2
 
 
 
 
c1e71b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""
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(page_arr, 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

        pil_img = page_to_pil(page_arr)
        buf = _io.BytesIO()
        pil_img.save(buf, format="PNG")
        img = buf.getvalue()
        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(5):
            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': 60})
                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(2)

        return None
    except Exception:
        return None


from utils.i18n import t

def render_step2() -> bool:
    import streamlit as st
    st.markdown(t("classify_title"))
    st.caption(t("classify_caption"))

    str_pages  = st.session_state.get("str_pages", [])
    arch_pages = st.session_state.get("arch_pages", [])
    str_texts  = st.session_state.get("str_texts", [])
    arch_texts = st.session_state.get("arch_texts", [])

    # Auto-classify if not done yet
    if "classified_pages" not in st.session_state:
        with st.spinner(t("classify_running")):
            classified = []
            for i, txt in enumerate(str_texts):
                classified.append({
                    "pdf":        "structural",
                    "page_index": i,
                    "page_num":   i + 1,
                    "text_preview": txt[:100].replace("\n", " "),
                    **_classify_single(txt, "structural"),
                })
            for i, txt in enumerate(arch_texts):
                classified.append({
                    "pdf":        "architectural",
                    "page_index": i,
                    "page_num":   i + 1,
                    "text_preview": txt[:100].replace("\n", " "),
                    **_classify_single(txt, "architectural"),
                })

        # Verify EVERY page with AI Vision (more reliable than keywords on
        # unseen drawings). Vision wins when it returns a valid type.
        prog = st.progress(0.0, text="AI-verifying every page by image…")
        for n, p in enumerate(classified):
            src = str_pages if p["pdf"] == "structural" else arch_pages
            if p["page_index"] < len(src):
                g = _ai_classify(src[p["page_index"]], p["pdf"])
                if g:
                    p["detected_type"] = g
                    p["confidence"]    = "high(ai)"
                    p["items"] = PAGE_ITEMS_MAP.get(g, {}).get("extract_items", [])
            prog.progress((n + 1) / len(classified))
        prog.empty()

        st.session_state["classified_pages"] = classified

    classified = st.session_state["classified_pages"]

    # Show classification results as editable table
    st.markdown("### Auto-Classification Results | نتائج التصنيف التلقائي")

    drawing_types = ["unknown"] + list(PAGE_ITEMS_MAP.keys())

    for page in classified:
        col_img, col_info, col_edit = st.columns([2, 2, 2])

        pages_src = (
            st.session_state.get("str_pages", []) if page["pdf"] == "structural"
            else st.session_state.get("arch_pages", [])
        )

        with col_img:
            if page["page_index"] < len(pages_src):
                st.image(
                    page_to_pil(pages_src[page["page_index"]]),
                    caption=f"{page['pdf']} P{page['page_num']}",
                    use_container_width=True
                )

        with col_info:
            _conf = str(page.get("confidence", "low"))
            conf_icon = "" if _conf.startswith("high") else {"medium": "", "low": ""}.get(_conf, "⚪")
            ai_tag = " (AI vision)" if "ai" in _conf else ""
            st.markdown(f"**{conf_icon} Auto-detected{ai_tag}:**")
            st.markdown(f"`{page.get('detected_type','unknown')}`")
            st.caption(page["text_preview"])

        with col_edit:
            current_idx = drawing_types.index(page.get("detected_type","unknown")) if page.get("detected_type","unknown") in drawing_types else 0
            new_type = st.selectbox(
                "Drawing Type",
                options=drawing_types,
                index=current_idx,
                key=f"cls_{page['pdf']}_{page['page_index']}",
                format_func=lambda x: x.replace("_", " ").title() if x != "unknown" else " Unknown — select type"
            )
            page["detected_type"] = new_type
            if new_type != "unknown":
                page["items"] = PAGE_ITEMS_MAP.get(new_type, {}).get("extract_items", [])

        st.divider()

    # Update session state
    st.session_state["classified_pages"] = classified

    ready = [p for p in classified if p["detected_type"] != "unknown"]
    unknown = [p for p in classified if p["detected_type"] == "unknown"]

    if unknown:
        st.warning(f" {len(unknown)} pages still unclassified — please assign types above")

    st.info(f" {len(ready)} pages ready for extraction")

    if ready and st.button(t("classify_next_btn"), type="primary", use_container_width=True):
        mark_step_done("classify")
        return True

    return False



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():
        score = sum(1 for kw in config.get("drawing_keywords", []) if kw.lower() in text_lower)
        scores[ptype] = score
    best  = max(scores, key=scores.get)
    score = scores[best]
    return {
        "detected_type": best if score > 0 else "unknown",
        "confidence": "high" if score >= 2 else "medium" if score == 1 else "low",
        "items": PAGE_ITEMS_MAP.get(best, {}).get("extract_items", []) if score > 0 else [],
    }