File size: 8,849 Bytes
41c0a9e
b4e9a74
 
41c0a9e
b4e9a74
a8aa400
 
41c0a9e
b4e9a74
 
41c0a9e
 
 
 
 
b4e9a74
fe386b4
b4e9a74
a8aa400
b4e9a74
fe386b4
b4e9a74
fe386b4
b4e9a74
d3c46a0
80a4e51
b4e9a74
 
 
a8aa400
b4e9a74
 
 
 
 
 
 
 
 
 
a8aa400
b4e9a74
 
a8aa400
b4e9a74
 
 
a8aa400
b4e9a74
 
 
a8aa400
b4e9a74
 
 
a8aa400
 
b4e9a74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8aa400
b4e9a74
fe386b4
b4e9a74
a8aa400
b4e9a74
 
 
 
 
 
 
a8aa400
41c0a9e
b4e9a74
a8aa400
80a4e51
b4e9a74
80a4e51
 
 
 
a8aa400
b4e9a74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8aa400
 
b4e9a74
 
a8aa400
b4e9a74
 
80a4e51
b4e9a74
 
d3c46a0
 
b4e9a74
 
 
 
 
 
 
 
 
a8aa400
b4e9a74
80a4e51
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
"""
app.py β€” GharScan HuggingFace Space
Uses gr.Blocks (ZeroGPU-compatible). Custom CSS for Off-Brand badge.
"""
import os
import spaces
import gradio as gr
from PIL import Image
from pathlib import Path

from inference import run_gharscan_pipeline
from agent_trace import AgentTraceLogger

trace_logger = AgentTraceLogger()

# ── ZeroGPU-compatible inference ──────────────────────────────────────────────
@spaces.GPU
def analyze_image(image: Image.Image, language: str) -> dict:
    if image is None:
        return {}
    session = trace_logger.start_trace()
    report  = run_gharscan_pipeline(image, language=language, trace_session=session)
    trace_logger.save_trace(session)
    return report

def analyze_and_render(image, language):
    if image is None:
        return "<p style='color:#6b7280;padding:20px'>Please upload or take a photo first.</p>"
    r = analyze_image(image, language)
    if not r.get("analysis_ok"):
        return f"<p style='color:#ef4444;padding:20px'>⚠️ {r.get('description','Analysis failed.')}</p>"
    
    color  = r.get("severity_color", "#6b7280")
    sev    = r.get("severity", 0)
    pct    = sev * 20

    struct_html = ""
    if r.get("is_structural"):
        struct_html = f"""<div style='background:rgba(127,29,29,0.25);border:1px solid #7f1d1d;border-radius:8px;padding:12px;margin-bottom:12px;color:#fca5a5'>
        ⚠️ <strong>STRUCTURAL RISK</strong> β€” {r.get("structural_reasoning","")}
        </div>"""
    else:
        struct_html = "<div style='background:rgba(21,128,61,0.2);border:1px solid #166534;border-radius:8px;padding:10px;margin-bottom:12px;color:#86efac'>βœ… Not Structural β€” No immediate safety risk</div>"

    liability = ""
    if r.get("show_liability_banner"):
        liability = f"<div style='background:rgba(239,68,68,0.1);border:1px solid rgba(239,68,68,0.3);border-radius:8px;padding:12px;color:#fca5a5;font-size:12px;margin-top:10px'>⚠️ {r.get('liability_text','')}</div>"

    disclaimer = ""
    if r.get("disclaimer"):
        disclaimer = f"<div style='border-left:3px solid #f59e0b;padding:10px 14px;font-size:12px;color:#9ca3af;margin-top:8px'>{r['disclaimer']}</div>"

    monsoon = ""
    if r.get("monsoon_risk"):
        monsoon = "<div style='background:rgba(234,179,8,0.1);border:1px solid rgba(234,179,8,0.3);border-radius:6px;padding:10px;color:#fde68a;font-size:13px;margin-top:10px'>🌧️ <strong>Monsoon Risk:</strong> This defect worsens during heavy rainfall. Address before June.</div>"

    return f"""
    <div style='background:#181b20;border:1px solid #2a2f38;border-radius:14px;padding:20px;font-family:Inter,sans-serif;color:#e8eaed;max-width:600px'>
        {struct_html}
        <div style='display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:14px'>
            <div>
                <div style='font-size:18px;font-weight:700'>{r.get("defect_display","")}</div>
                <div style='font-size:11px;color:#8b9099;margin-top:3px'>{r.get("defect_type","").replace("_"," ").upper()}</div>
            </div>
            <div style='background:#20242b;border:2px solid {color};border-radius:10px;padding:8px 14px;text-align:center'>
                <div style='font-size:22px;font-weight:700;color:{color}'>{sev}</div>
                <div style='font-size:10px;color:#8b9099'>{r.get("severity_label","").upper()}</div>
            </div>
        </div>
        <div style='background:#20242b;border-radius:4px;height:8px;margin-bottom:4px'>
            <div style='width:{pct}%;height:8px;border-radius:4px;background:{color}'></div>
        </div>
        <div style='display:flex;justify-content:space-between;font-size:10px;color:#555d6b;margin-bottom:14px'>
            <span>Cosmetic</span><span>Moderate</span><span>Critical</span>
        </div>
        <div style='height:1px;background:#1e2229;margin:12px 0'></div>
        <div style='margin-bottom:10px'>
            <div style='font-size:10px;color:#6fb3e0;font-family:monospace;letter-spacing:.06em'>WHAT IT IS</div>
            <div style='font-size:14px;margin-top:4px'>{r.get("description","")}</div>
        </div>
        <div style='margin-bottom:10px'>
            <div style='font-size:10px;color:#6fb3e0;font-family:monospace;letter-spacing:.06em'>WHY IT HAPPENS</div>
            <div style='font-size:14px;margin-top:4px'>{r.get("primary_cause","")}</div>
        </div>
        <div style='height:1px;background:#1e2229;margin:12px 0'></div>
        <div style='background:rgba(59,130,246,0.08);border:1px solid rgba(59,130,246,0.2);border-radius:10px;padding:12px;margin-bottom:10px'>
            <div style='font-size:10px;color:#6fb3e0;font-family:monospace'>WHAT TO DO</div>
            <div style='font-size:14px;font-weight:500;color:#93c5fd;margin-top:4px'>{r.get("immediate_action","")}</div>
        </div>
        <div style='margin-bottom:12px'>
            <div style='font-size:10px;color:#6fb3e0;font-family:monospace'>WHEN TO ACT</div>
            <div style='font-size:14px;font-weight:500;margin-top:4px'>{r.get("urgency_display","")}</div>
        </div>
        <div style='height:1px;background:#1e2229;margin:12px 0'></div>
        <div style='background:#20242b;border-radius:10px;padding:14px'>
            <div style='font-size:22px;font-weight:700;color:#22c55e'>{r.get("cost_range_inr","")}</div>
            <div style='font-size:13px;color:#8b9099;margin-top:6px'>πŸ‘· {r.get("professional_display","")}</div>
        </div>
        {monsoon}
        {liability}
        {disclaimer}
    </div>
    """

# ── Custom CSS ─────────────────────────────────────────────────────────────────
CSS = """
body, .gradio-container { background: #0f1114 !important; color: #e8eaed !important; }
.gradio-container { max-width: 700px !important; margin: 0 auto !important; }
.gr-button-primary { background: linear-gradient(135deg,#2563eb,#1d4ed8) !important; border: none !important; }
.gr-button-primary:hover { opacity: 0.9 !important; }
footer { display: none !important; }
#component-0 { padding: 20px !important; }
.dark { --background-fill-primary: #181b20; --background-fill-secondary: #20242b; --border-color-primary: #2a2f38; --color-text-body: #e8eaed; }
"""

# ── UI ──────────────────────────────────────────────────────────────────────────
with gr.Blocks(
    css=CSS,
    title="GharScan β€” Building Defect Inspector",
    theme=gr.themes.Base(
        primary_hue="blue",
        neutral_hue="slate",
    )
) as demo:
    gr.HTML("""
    <div style='display:flex;align-items:center;justify-content:space-between;
                padding:14px 0;border-bottom:1px solid #2a2f38;margin-bottom:20px'>
        <div style='display:flex;align-items:center;gap:10px'>
            <span style='font-size:26px'>πŸ—οΈ</span>
            <div>
                <div style='font-size:18px;font-weight:700;color:#e8eaed'>GharScan</div>
                <div style='font-size:11px;color:#8b9099'>AI Building Defect Inspector Β· India</div>
            </div>
        </div>
        <div style='background:#20242b;border:1px solid #2a2f38;border-radius:20px;padding:4px 12px;
                    font-size:11px;color:#6fb3e0;font-family:monospace'>
            ● Qwen2-VL-2B Β· 2.07B
        </div>
    </div>
    """)

    with gr.Row():
        image_input = gr.Image(
            sources=["upload", "webcam"],
            type="pil",
            label="πŸ“Έ Take Photo or Upload",
            height=300,
        )

    language = gr.Radio(
        choices=["en", "hi"],
        value="en",
        label="Output language",
    )

    analyze_btn = gr.Button("πŸ” Analyse Defect", variant="primary", size="lg")
    report_output = gr.HTML(label="Inspection Report")

    analyze_btn.click(
        fn=analyze_and_render,
        inputs=[image_input, language],
        outputs=report_output,
        api_name="analyze"
    )

    gr.HTML("""
    <div style='text-align:center;padding:20px 0;font-size:11px;color:#555d6b;
                border-top:1px solid #1e2229;margin-top:20px'>
        <p>Qwen2-VL-2B fine-tuned on Indian building defects Β· No cloud APIs</p>
        <p>πŸ—οΈ Built for <a href="https://huggingface.co/build-small-hackathon" 
           style='color:#8b9099'>Build Small Hackathon 2026</a> Β· Backyard AI Track</p>
        <p style='color:#374151'>GharScan is a triage aid, not a substitute for professional structural assessment.</p>
    </div>
    """)

demo.queue()
demo.launch()