File size: 9,729 Bytes
a8cdbd8
1bc6164
28b0196
a8cdbd8
1bc6164
a8cdbd8
 
1bc6164
 
 
28b0196
1bc6164
 
28b0196
1bc6164
28b0196
1bc6164
 
a8cdbd8
 
 
1bc6164
 
 
 
a8cdbd8
 
 
1bc6164
a8cdbd8
 
 
1bc6164
a8cdbd8
 
1bc6164
a8cdbd8
 
1bc6164
 
 
 
a8cdbd8
 
1bc6164
a8cdbd8
 
 
 
1bc6164
 
 
 
 
 
 
 
 
 
 
a8cdbd8
1bc6164
a8cdbd8
 
 
 
 
1bc6164
 
a8cdbd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bc6164
a8cdbd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bc6164
 
 
 
a8cdbd8
 
 
 
 
 
 
 
 
1bc6164
 
a8cdbd8
 
 
 
 
 
 
 
 
 
1bc6164
 
a8cdbd8
 
 
 
 
 
 
 
1bc6164
a8cdbd8
1bc6164
 
a8cdbd8
 
 
1bc6164
a8cdbd8
1bc6164
 
 
 
a8cdbd8
 
 
 
 
 
 
 
 
1bc6164
 
 
 
 
 
 
a8cdbd8
 
 
 
 
 
 
 
 
 
1bc6164
a8cdbd8
 
1bc6164
 
 
 
 
 
 
 
 
a8cdbd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bc6164
a8cdbd8
 
1bc6164
a8cdbd8
 
602ebb2
a8cdbd8
 
 
 
1bc6164
a8cdbd8
 
 
 
 
1bc6164
a8cdbd8
 
 
 
 
 
 
 
 
 
 
 
 
 
602ebb2
a8cdbd8
1bc6164
a8cdbd8
 
1bc6164
 
 
 
 
a8cdbd8
1bc6164
 
 
 
 
a8cdbd8
 
1bc6164
 
 
 
a8cdbd8
1bc6164
 
a8cdbd8
 
1bc6164
a8cdbd8
 
1bc6164
 
 
 
 
 
a8cdbd8
 
602ebb2
a8cdbd8
 
1bc6164
a8cdbd8
 
452efb4
 
 
a8cdbd8
 
 
1bc6164
a8cdbd8
 
 
 
 
 
 
28b0196
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
import gradio as gr
from sentence_transformers import CrossEncoder
import torch

# Load fine-tuned model from Hub
MODEL_NAME = "pujithapsx/address-crossencoder-bge-reranker-v2-m3-finetuned"

print("Loading model...")
# Use CrossEncoder directly - it handles the model format correctly
model = CrossEncoder(
    MODEL_NAME,
    trust_remote_code=True,
    device="cpu"  # Use "cuda" if GPU is available in your HF Space
)
print("Model loaded successfully!")

# STATIC THRESHOLD
THRESHOLD = 0.75


def predict_similarity(input1, input2):
    """
    Predict similarity between two addresses using static threshold.
    Returns: Similarity %, Match/No Match result, and confidence bar value.
    """
    if not input1.strip() or not input2.strip():
        return "—", "⚠️ Please provide both addresses", 0

    score = model.predict([[input1, input2]])[0]
    similarity_pct = score * 100

    if score >= THRESHOLD:
        result = "✅ MATCH"
        confidence_label = "High" if score > 0.85 else "Medium"
    else:
        result = "❌ NO MATCH"
        confidence_label = "High" if score < 0.40 else "Medium"

    similarity_str = f"{similarity_pct:.2f}%"
    result_str = f"{result}  •  Confidence: {confidence_label}"

    return similarity_str, result_str, float(similarity_pct)


# ── Custom CSS ──────────────────────────────────────────────────────────────
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=DM+Sans:wght@300;400;500;600&display=swap');

:root {
    --bg:        #0b0f1a;
    --surface:   #111827;
    --border:    #1f2d45;
    --accent:    #38bdf8;
    --accent2:   #818cf8;
    --green:     #34d399;
    --red:       #f87171;
    --yellow:    #fbbf24;
    --text:      #e2e8f0;
    --muted:     #64748b;
    --radius:    12px;
}

body, .gradio-container {
    background: var(--bg) !important;
    font-family: 'DM Sans', sans-serif !important;
    color: var(--text) !important;
}

/* ── header ── */
#header-box {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 36px 40px 28px;
    margin-bottom: 24px;
    position: relative;
    overflow: hidden;
}
#header-box::before {
    content: '';
    position: absolute;
    top: -60px; right: -60px;
    width: 200px; height: 200px;
    background: radial-gradient(circle, rgba(56,189,248,0.12) 0%, transparent 70%);
    pointer-events: none;
}
#header-box h1 {
    font-family: 'Space Mono', monospace !important;
    font-size: 1.9rem !important;
    font-weight: 700 !important;
    color: var(--accent) !important;
    margin: 0 0 8px !important;
    letter-spacing: -0.5px;
}
#header-box p {
    color: var(--muted) !important;
    font-size: 0.95rem !important;
    margin: 0 !important;
    line-height: 1.6;
}
.badge {
    display: inline-block;
    background: rgba(56,189,248,0.12);
    border: 1px solid rgba(56,189,248,0.3);
    color: var(--accent);
    font-family: 'Space Mono', monospace;
    font-size: 0.7rem;
    padding: 3px 10px;
    border-radius: 20px;
    margin-right: 8px;
    margin-top: 12px;
}

/* ── input cards ── */
.input-card textarea,
.input-card input {
    background: var(--surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius) !important;
    color: var(--text) !important;
    font-family: 'DM Sans', sans-serif !important;
    font-size: 0.95rem !important;
    padding: 14px 16px !important;
    transition: border-color 0.2s;
}
.input-card textarea:focus,
.input-card input:focus {
    border-color: var(--accent) !important;
    box-shadow: 0 0 0 3px rgba(56,189,248,0.1) !important;
}
label span {
    font-family: 'Space Mono', monospace !important;
    font-size: 0.75rem !important;
    color: var(--accent2) !important;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

/* ── button ── */
#run-btn {
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent2) 100%) !important;
    border: none !important;
    border-radius: var(--radius) !important;
    color: #0b0f1a !important;
    font-family: 'Space Mono', monospace !important;
    font-size: 0.9rem !important;
    font-weight: 700 !important;
    letter-spacing: 0.5px;
    padding: 14px 32px !important;
    cursor: pointer;
    transition: opacity 0.2s, transform 0.15s;
    width: 100%;
    margin-top: 8px;
}
#run-btn:hover { opacity: 0.9; transform: translateY(-1px); }
#run-btn:active { transform: translateY(0); }

/* ── output cards ── */
.output-card textarea,
.output-card input {
    background: #0d1424 !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius) !important;
    color: var(--text) !important;
    font-family: 'Space Mono', monospace !important;
    font-size: 1.1rem !important;
    font-weight: 700 !important;
    text-align: center;
}

/* ── slider (score bar) ── */
.score-slider input[type=range] {
    accent-color: var(--accent);
}

/* ── examples table ── */
.gr-samples-table {
    background: var(--surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius) !important;
}
.gr-samples-table th {
    font-family: 'Space Mono', monospace !important;
    font-size: 0.72rem !important;
    color: var(--muted) !important;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    background: #0d1424 !important;
}
.gr-samples-table td {
    color: var(--text) !important;
    font-size: 0.88rem !important;
}
.gr-samples-table tr:hover td {
    background: rgba(56,189,248,0.04) !important;
}

/* ── info footer ── */
#footer-info {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px 24px;
    margin-top: 20px;
}
#footer-info p {
    color: var(--muted) !important;
    font-family: 'Space Mono', monospace !important;
    font-size: 0.78rem !important;
    margin: 4px 0 !important;
    line-height: 1.7;
}
#footer-info span { color: var(--accent) !important; }
"""

# ── Gradio UI ────────────────────────────────────────────────────────────────
with gr.Blocks(css=custom_css, title="Address Entity Matcher") as demo:

    # Header
    gr.HTML("""
    <div id="header-box">
        <h1>Address Entity Matcher</h1>
        <p>
            Enter two addresses to determine whether they refer to the same location.<br>
            Powered by a fine-tuned <strong>BGE-Reranker-v2-m3</strong> cross-encoder model.
        </p>
        <span class="badge">CrossEncoder</span>
        <span class="badge">BGE-Reranker-v2-m3</span>
        <span class="badge">Threshold: 0.75</span>
    </div>
    """)

    # Inputs
    with gr.Row(equal_height=True):
        with gr.Column(elem_classes="input-card"):
            input1 = gr.Textbox(
                label="Address 1",
                placeholder="e.g., Flat 12-B Sector 5 Noida",
                lines=3,
            )
        with gr.Column(elem_classes="input-card"):
            input2 = gr.Textbox(
                label="Address 2",
                placeholder="e.g., Flat 23-B Sector 5 Noida",
                lines=3,
            )

    btn = gr.Button(" Check Match", elem_id="run-btn", variant="primary")

    # Outputs
    with gr.Row(equal_height=True):
        with gr.Column(elem_classes="output-card"):
            similarity_output = gr.Textbox(
                label="Similarity Score",
                interactive=False,
                placeholder="—",
            )
        with gr.Column(elem_classes="output-card"):
            result_output = gr.Textbox(
                label="Match Result",
                interactive=False,
                placeholder="—",
            )

    score_bar = gr.Slider(
        minimum=0,
        maximum=100,
        value=0,
        step=0.01,
        label="Score Visualisation  (threshold line: 75%)",
        interactive=False,
        elem_classes="score-slider",
    )

    # Examples
    gr.Examples(
        examples=[
            ["Flat 12-B Sector 5 Noida",                          "Flat 23-B Sector 5 Noida"],
            ["Phase 4 Whitefield Bangalore",                       "Whitefield Phase V Bangalore"],
            ["Thirteen I 7th Avenue Adyar Chennai",                "13 Seventh Avenue Adyar Chennai"],
            ["Twenty Nine A Second Cross Koramangala Bengaluru",   "47 Forty Seven B Third Street Indiranagar Bengaluru"],
            ["Plot 8 Banjara Hills Hyderabad",                     "Plot 8 Banjara Hills Hyderabad"],
            ["House No 4 Lane 2 DLF Phase 1 Gurugram",            "H.No 4/2 DLF Phase One Gurgaon"],
        ],
        inputs=[input1, input2],
        label="Examples",
    )

    # Footer info
    gr.HTML(f"""
    <div id="footer-info">
        <p> <strong>Model:</strong> <span>pujithapsx/address-crossencoder-bge-reranker-v2-m3-finetuned</span></p>
        <p><strong>Threshold:</strong> <span>{THRESHOLD}</span> — Score ≥ {THRESHOLD} → MATCH &nbsp;|&nbsp; Score &lt; {THRESHOLD} → NO MATCH</p>
        <p> <strong>Confidence:</strong> High (score &gt; 0.85 or &lt; 0.40) &nbsp;|&nbsp; Medium (otherwise)</p>
    </div>
    """)

    # Wiring
    btn.click(
        fn=predict_similarity,
        inputs=[input1, input2],
        outputs=[similarity_output, result_output, score_bar],
    )

if __name__ == "__main__":
    demo.launch()