File size: 16,203 Bytes
220f24a
5004307
220f24a
5004307
 
220f24a
5004307
 
 
220f24a
5004307
220f24a
a158874
 
220f24a
5004307
 
 
220f24a
5004307
 
 
220f24a
5004307
 
220f24a
a158874
5004307
 
220f24a
 
5004307
 
 
 
 
220f24a
 
5004307
a158874
220f24a
 
5004307
 
 
 
 
220f24a
 
5004307
 
a158874
220f24a
 
5004307
 
220f24a
5004307
 
 
 
220f24a
5004307
 
220f24a
5004307
 
 
 
220f24a
5004307
 
 
 
 
 
220f24a
5004307
220f24a
 
5004307
220f24a
a158874
5004307
 
 
220f24a
5004307
 
220f24a
 
5004307
 
 
 
 
 
220f24a
5004307
 
 
 
 
 
 
 
220f24a
 
5004307
 
 
220f24a
5004307
 
 
 
220f24a
5004307
 
220f24a
5004307
220f24a
5004307
 
220f24a
 
5004307
 
220f24a
5004307
 
 
 
220f24a
5004307
 
 
 
 
 
 
220f24a
5004307
220f24a
5004307
220f24a
5004307
 
 
 
220f24a
5004307
 
220f24a
5004307
a158874
220f24a
5004307
 
 
 
 
220f24a
5004307
 
220f24a
5004307
 
220f24a
5004307
 
 
220f24a
5004307
 
 
 
 
220f24a
5004307
 
 
a158874
5004307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220f24a
5004307
 
 
220f24a
5004307
220f24a
 
5004307
 
220f24a
5004307
a158874
220f24a
5004307
 
220f24a
5004307
220f24a
5004307
 
a158874
 
 
 
 
220f24a
5004307
220f24a
5004307
 
a158874
 
 
 
 
220f24a
5004307
220f24a
5004307
a158874
 
45c45ee
a158874
 
220f24a
5004307
220f24a
5004307
a158874
 
 
 
 
 
 
 
 
 
 
220f24a
 
45c45ee
 
 
c82ddba
a158874
45c45ee
 
c82ddba
0bc11b7
 
a158874
45c45ee
4b5d27d
45c45ee
a158874
 
c82ddba
 
 
 
45c45ee
 
c82ddba
 
77997d0
c82ddba
45c45ee
 
a158874
 
c82ddba
45c45ee
 
 
 
a158874
9afd17b
c82ddba
 
 
 
45c45ee
 
a158874
 
 
45c45ee
 
 
c82ddba
 
45c45ee
a158874
 
 
45c45ee
 
 
4b5d27d
 
4ae0ebd
 
45c45ee
c82ddba
45c45ee
 
 
4ae0ebd
 
45c45ee
 
 
 
 
 
c82ddba
45c45ee
4ae0ebd
 
45c45ee
 
 
 
 
4ae0ebd
 
45c45ee
 
 
 
 
 
 
77997d0
 
45c45ee
 
 
 
 
 
9afd17b
 
45c45ee
 
 
9afd17b
 
45c45ee
 
 
 
 
 
 
9afd17b
 
45c45ee
 
77997d0
 
45c45ee
 
 
 
 
 
 
4b5d27d
 
45c45ee
 
 
 
 
77997d0
 
45c45ee
 
 
c82ddba
45c45ee
 
 
 
77997d0
 
45c45ee
c82ddba
45c45ee
 
4ae0ebd
 
45c45ee
 
c82ddba
4b5d27d
 
c82ddba
 
 
45c45ee
9afd17b
 
c82ddba
 
45c45ee
c82ddba
77997d0
c82ddba
4b5d27d
 
c82ddba
 
0bc11b7
 
c82ddba
 
45c45ee
 
 
0bc11b7
 
c82ddba
 
a158874
 
c82ddba
45c45ee
95fb395
 
c82ddba
45c45ee
 
c82ddba
4ae0ebd
45c45ee
c82ddba
044718d
c82ddba
45c45ee
 
77997d0
a158874
 
220f24a
 
4b5d27d
 
45c45ee
4b5d27d
 
45c45ee
4b5d27d
 
 
 
45c45ee
4b5d27d
 
45c45ee
044718d
 
45c45ee
 
 
 
 
c82ddba
 
45c45ee
 
 
c82ddba
45c45ee
 
 
 
c82ddba
 
 
45c45ee
c82ddba
 
 
45c45ee
 
 
 
c82ddba
45c45ee
c82ddba
 
a158874
 
4b5d27d
45c45ee
4b5d27d
c82ddba
 
a158874
 
 
 
 
c82ddba
 
 
 
 
a158874
45c45ee
c82ddba
45c45ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c82ddba
 
 
 
9afd17b
45c45ee
95fb395
 
45c45ee
95fb395
c82ddba
95fb395
 
c82ddba
9afd17b
45c45ee
95fb395
 
 
 
 
 
 
 
 
 
 
c82ddba
95fb395
 
 
45c45ee
 
 
 
 
 
9afd17b
45c45ee
 
ae8f283
45c45ee
 
 
 
 
 
 
a158874
 
 
 
45c45ee
a158874
 
 
 
4b5d27d
45c45ee
4b5d27d
45c45ee
4b5d27d
 
 
45c45ee
4b5d27d
45c45ee
4b5d27d
 
a158874
c82ddba
a158874
45c45ee
a158874
220f24a
 
 
 
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629

import re
import numpy as np
import pandas as pd
import faiss
import gradio as gr
from pathlib import Path
from huggingface_hub import snapshot_download
from sentence_transformers import SentenceTransformer

MODEL_REPO_ID = "ElMETRICO/crosstalk-ai-full-artifacts"

print("Downloading artifacts from:", MODEL_REPO_ID)
artifact_dir = Path(snapshot_download(repo_id=MODEL_REPO_ID, repo_type="model"))

MODEL_DIR = artifact_dir / "model" / "e5_lexical_contrastive_finetuned"
INDEX_PATH = artifact_dir / "artifacts" / "trained_e5_source_to_meaning.index"
DF_PATH = artifact_dir / "artifacts" / "trained_e5_source_to_meaning_df.csv"

print("Loading fine-tuned E5 model...")
model = SentenceTransformer(str(MODEL_DIR))
model.max_seq_length = 128

print("Loading FAISS index...")
source_index = faiss.read_index(str(INDEX_PATH))

print("Loading dataframe...")
source_df = pd.read_csv(DF_PATH, encoding="utf-8-sig")
print("Rows loaded:", len(source_df))


def clean_text(x):
    x = "" if pd.isna(x) else str(x)
    x = re.sub(r"[\u200b-\u200d\ufeff]", "", x)
    x = re.sub(r"\s+", " ", x).strip()
    return x


def normalize_lookup_text(x):
    return clean_text(x).lower()


def remove_parentheses_text(x):
    x = clean_text(x)
    x = re.sub(r"\(.*?\)", "", x)
    x = re.sub(r"\s+", " ", x).strip().lower()
    return x


def count_source_files(x):
    x = "" if pd.isna(x) else str(x)
    return len([p for p in x.split("||") if p.strip()]) if x.strip() else 0


def add_quality_score(df):
    df = df.copy()

    for col in ["english_meaning", "bangla_meaning", "source_file"]:
        if col not in df.columns:
            df[col] = ""
        df[col] = df[col].fillna("").astype(str)

    if "duplicate_count" not in df.columns:
        df["duplicate_count"] = 1

    df["duplicate_count"] = pd.to_numeric(df["duplicate_count"], errors="coerce").fillna(1)
    df["has_english"] = df["english_meaning"].str.strip().ne("").astype(int)
    df["has_bangla"] = df["bangla_meaning"].str.strip().ne("").astype(int)
    df["source_file_count"] = df["source_file"].apply(count_source_files)

    df["quality_score"] = (
        df["has_english"] * 3.0 +
        df["has_bangla"] * 2.0 +
        np.log1p(df["duplicate_count"]) * 0.5 +
        np.log1p(df["source_file_count"]) * 0.5
    )

    return df


source_df = add_quality_score(source_df)

for col in ["language", "source_text", "english_meaning", "bangla_meaning", "part_of_speech"]:
    if col not in source_df.columns:
        source_df[col] = ""
    source_df[col] = source_df[col].apply(clean_text)

source_df["norm_source"] = source_df["source_text"].apply(normalize_lookup_text)
source_df["base_source"] = source_df["source_text"].apply(remove_parentheses_text)


def format_verified_result(df, query, method, top_k=10):
    result = df.copy()
    result = result.sort_values(
        by=["quality_score", "duplicate_count"],
        ascending=[False, False]
    )

    keep_cols = [
        "language",
        "source_text",
        "english_meaning",
        "bangla_meaning",
        "part_of_speech",
        "duplicate_count",
        "quality_score"
    ]

    for col in keep_cols:
        if col not in result.columns:
            result[col] = ""

    result = result[keep_cols].head(top_k).copy()
    result.insert(0, "query", query)
    result.insert(1, "score", 1.0)
    result.insert(2, "method", method)

    if result["language"].nunique() > 1 or result["source_text"].nunique() > 1:
        result["confidence"] = "high_but_ambiguous"
    else:
        result["confidence"] = "high"

    result["note"] = "Verified dictionary match."
    return result


def trained_semantic_fallback(query, top_k=5, search_k_per_language=20):
    languages = sorted(source_df["language"].dropna().unique().tolist())

    query_texts = [
        f"query: {lang} word: {query}"
        for lang in languages
    ]

    query_emb = model.encode(
        query_texts,
        batch_size=16,
        convert_to_numpy=True,
        normalize_embeddings=True,
        show_progress_bar=False
    ).astype("float32")

    scores, indices = source_index.search(query_emb, search_k_per_language)

    best_by_index = {}

    for lang_i, lang in enumerate(languages):
        for score, idx in zip(scores[lang_i], indices[lang_i]):
            idx = int(idx)
            score = float(score)

            if idx < 0:
                continue

            if idx not in best_by_index or score > best_by_index[idx]["score"]:
                best_by_index[idx] = {"score": score}

    ranked = sorted(
        best_by_index.items(),
        key=lambda x: x[1]["score"],
        reverse=True
    )[:top_k]

    if len(ranked) == 0:
        return pd.DataFrame()

    selected_indices = [idx for idx, _ in ranked]
    result = source_df.iloc[selected_indices].copy().reset_index(drop=True)

    result.insert(0, "query", query)
    result.insert(1, "score", [x["score"] for _, x in ranked])
    result.insert(2, "method", "fine_tuned_e5_semantic_fallback")

    def label_score(score):
        if score >= 0.88:
            return "medium_high_trained_semantic_candidate"
        elif score >= 0.80:
            return "medium_trained_semantic_candidate"
        else:
            return "low_trained_semantic_candidate"

    result["confidence"] = result["score"].apply(label_score)
    result["note"] = "Semantic candidate only; not a confirmed translation."

    keep_cols = [
        "query",
        "score",
        "method",
        "confidence",
        "language",
        "source_text",
        "english_meaning",
        "bangla_meaning",
        "part_of_speech",
        "duplicate_count",
        "quality_score",
        "note"
    ]

    for col in keep_cols:
        if col not in result.columns:
            result[col] = ""

    return result[keep_cols]


def safe_search(query):
    query = clean_text(query)

    if not query:
        return "⚠️ Please enter a word.", "No input provided.", pd.DataFrame()

    q_norm = normalize_lookup_text(query)
    q_base = remove_parentheses_text(query)

    exact = source_df[source_df["norm_source"] == q_norm].copy()

    if len(exact) > 0:
        result = format_verified_result(exact, query, "hybrid_exact_source_match", top_k=10)
        return (
            "✅ Verified dictionary match found.",
            f"High-confidence verified dictionary output for **{query}**.",
            result
        )

    base = source_df[source_df["base_source"] == q_base].copy()

    if len(base) > 0:
        result = format_verified_result(base, query, "hybrid_base_form_match", top_k=10)
        return (
            "✅ Verified base-form match found.",
            f"High-confidence base-form dictionary output for **{query}**.",
            result
        )

    semantic = trained_semantic_fallback(query, top_k=5, search_k_per_language=20)

    if len(semantic) == 0:
        return (
            "❌ No match found.",
            "No verified dictionary match or semantic candidate was found.",
            pd.DataFrame()
        )

    strong = semantic[semantic["score"] >= 0.88].copy()

    if len(strong) > 0:
        return (
            "🧠 Semantic candidates found.",
            "No verified dictionary match was found. Showing fine-tuned E5 semantic candidates only. These are suggestions, not confirmed translations.",
            strong
        )

    return (
        "⚠️ Low-confidence semantic candidates.",
        "No verified dictionary match was found. Semantic scores are below the safe verification threshold, so no translation is claimed.",
        semantic
    )





custom_css = """
.gradio-container {
    background: #212121 !important;
    color: #ECECEC !important;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif !important;
}

#shell {
    max-width: 980px;
    margin: 0 auto;
    padding: 34px 18px 42px 18px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 26px;
    color: #B4B4B4;
    font-size: 14px;
}

.brand {
    font-weight: 700;
    color: #ECECEC;
}

.model-badge {
    background: #2F2F2F;
    border: 1px solid #3A3A3A;
    color: #CFCFCF;
    padding: 7px 11px;
    border-radius: 999px;
    font-size: 12px;
}

.hero {
    text-align: center;
    padding: 22px 10px 20px 10px;
    margin-bottom: 18px;
}

.title {
    font-size: 38px;
    line-height: 1.12;
    font-weight: 750;
    letter-spacing: -0.035em;
    margin: 0;
    color: #F5F5F5;
}

.subtitle {
    max-width: 760px;
    margin: 14px auto 0 auto;
    color: #B4B4B4;
    font-size: 15px;
    line-height: 1.65;
}

.capabilities {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    margin: 26px 0 22px 0;
}

.cap-card {
    background: #2A2A2A;
    border: 1px solid #3A3A3A;
    border-radius: 16px;
    padding: 14px 15px;
    color: #D7D7D7;
    font-size: 13px;
    line-height: 1.45;
}

.cap-card strong {
    display: block;
    color: #FFFFFF;
    font-size: 14px;
    margin-bottom: 4px;
}

.search-panel {
    background: #2F2F2F !important;
    border: 1px solid #454545 !important;
    border-radius: 24px !important;
    padding: 14px 16px 16px 16px !important;
    box-shadow: 0 14px 38px rgba(0,0,0,0.20);
    margin-bottom: 18px !important;
}

textarea, input {
    background: #2F2F2F !important;
    color: #FFFFFF !important;
    border: 1px solid #4A4A4A !important;
    border-radius: 18px !important;
    font-size: 16px !important;
}

textarea:focus, input:focus {
    border-color: #6B7280 !important;
    box-shadow: 0 0 0 2px rgba(255,255,255,0.08) !important;
}

button.send-button {
    background: #ECECEC !important;
    color: #111111 !important;
    border: 1px solid #ECECEC !important;
    border-radius: 14px !important;
    font-weight: 800 !important;
    min-height: 44px !important;
}

button.send-button:hover {
    background: #FFFFFF !important;
}

button.clear-button {
    background: #262626 !important;
    color: #D1D5DB !important;
    border: 1px solid #414141 !important;
    border-radius: 14px !important;
    font-weight: 700 !important;
    min-height: 44px !important;
}

.output-panel {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
    margin-top: 8px !important;
}

.answer-card {
    background: #2A2A2A;
    border: 1px solid #3D3D3D;
    border-radius: 18px;
    padding: 18px 20px;
    margin-bottom: 14px;
    color: #ECECEC;
    line-height: 1.65;
}

.answer-title {
    font-weight: 800;
    color: #FFFFFF;
    margin-bottom: 8px;
}

.answer-note {
    color: #C7C7C7;
    font-size: 14px;
}

.dataframe {
    border-radius: 16px !important;
    overflow: hidden !important;
    border: 1px solid #3D3D3D !important;
}

.footer {
    text-align: center;
    color: #9CA3AF;
    font-size: 12.5px;
    line-height: 1.6;
    margin-top: 24px;
}

#examples-block {
    margin-top: 10px;
}

#examples-block button {
    border-radius: 999px !important;
    background: #2A2A2A !important;
    border: 1px solid #3D3D3D !important;
    color: #D1D5DB !important;
}

.block {
    background: transparent !important;
}

label {
    color: #CFCFCF !important;
}

@media (max-width: 900px) {
    #shell {
        padding: 24px 14px 34px 14px;
    }

    .title {
        font-size: 30px;
    }

    .capabilities {
        grid-template-columns: 1fr;
    }
}
"""


EMPTY_RESULTS = pd.DataFrame(
    columns=[
        "query",
        "score",
        "method",
        "confidence",
        "language",
        "source_text",
        "english_meaning",
        "bangla_meaning",
        "part_of_speech",
        "note"
    ]
)


def clean_markdown_summary(x):
    x = "" if x is None else str(x)
    x = x.replace('<div class="result-note">', "")
    x = x.replace("</div>", "")
    return x.strip()


def chat_style_search(query):
    status, summary, result = safe_search(query)
    summary = clean_markdown_summary(summary)

    answer_html = f"""
    <div class="answer-card">
        <div class="answer-title">{status}</div>
        <div class="answer-note">{summary}</div>
    </div>
    """

    return answer_html, result


def clear_interface():
    return "", """
    <div class="answer-card">
        <div class="answer-title">Ready.</div>
        <div class="answer-note">Enter a source word to retrieve verified dictionary matches or semantic candidates.</div>
    </div>
    """, EMPTY_RESULTS


with gr.Blocks(
    css=custom_css,
    theme=gr.themes.Base(
        primary_hue="slate",
        secondary_hue="slate",
        neutral_hue="zinc"
    )
) as demo:

    with gr.Column(elem_id="shell"):
        gr.HTML(
            """
            <div class="topbar">
                <div class="brand">CrossTalk AI</div>
                <div class="model-badge">Full trained retrieval system</div>
            </div>

            <div class="hero">
                <h1 class="title">What ethnic word would you like to search?</h1>
                <div class="subtitle">
                    CrossTalk AI identifies and retrieves meanings from low-resource ethnic language dictionaries using verified lexical matching,
                    fine-tuned E5 semantic retrieval, FAISS vector search, and confidence-aware output control.
                </div>
            </div>

            <div class="capabilities">
                <div class="cap-card">
                    <strong>Verified lookup</strong>
                    Exact and base-form dictionary matches are returned first.
                </div>
                <div class="cap-card">
                    <strong>Semantic fallback</strong>
                    Fine-tuned E5 suggests candidates when verified matches are unavailable.
                </div>
                <div class="cap-card">
                    <strong>Safe output</strong>
                    Low-confidence semantic results are not claimed as confirmed translations.
                </div>
            </div>
            """
        )

        with gr.Column(elem_classes=["search-panel"]):
            query = gr.Textbox(
                label="Source word",
                placeholder="Message CrossTalk AI...",
                lines=1,
                show_label=False
            )

            with gr.Row():
                clear_btn = gr.Button("Clear", elem_classes=["clear-button"])
                submit_btn = gr.Button("Search", elem_classes=["send-button"])

            gr.Examples(
                examples=[
                    ["kəkhyáŋ"],
                    ["Hula"],
                    ["Aina"],
                    ["aam"],
                    ["bajaoo"],
                    ["unknown tribal word"]
                ],
                inputs=query,
                label="Try examples",
                elem_id="examples-block"
            )

        with gr.Column(elem_classes=["output-panel"]):
            answer = gr.HTML(
                """
                <div class="answer-card">
                    <div class="answer-title">Ready.</div>
                    <div class="answer-note">Enter a source word to retrieve verified dictionary matches or semantic candidates.</div>
                </div>
                """
            )

            results = gr.Dataframe(
                label="Retrieved results",
                value=EMPTY_RESULTS,
                interactive=False,
                wrap=True,
                max_height=470
            )

        gr.HTML(
            """
            <div class="footer">
                Exact/base-form matches are verified dictionary outputs. Fine-tuned semantic fallback results are candidate suggestions only, not confirmed translations.
            </div>
            """
        )

    submit_btn.click(
        fn=chat_style_search,
        inputs=query,
        outputs=[answer, results]
    )

    query.submit(
        fn=chat_style_search,
        inputs=query,
        outputs=[answer, results]
    )

    clear_btn.click(
        fn=clear_interface,
        inputs=None,
        outputs=[query, answer, results]
    )


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