File size: 6,779 Bytes
93d29b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Hugging Face Space — Indic Language Identifier

Supports 12 Indic languages + English.

Uses best available model: Ensemble > CharNgram > Unicode Heuristic fallback.

"""
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))

import gradio as gr
from src.utils.languages import LANGUAGE_REGISTRY, ALL_LANGUAGE_CODES
from src.data.preprocessor import IndicTextPreprocessor
from src.models.ensemble_model import EnsembleLIDModel, UnicodeHeuristicModel

# ── Model Loading ─────────────────────────────────────────────────────────────

preprocessor = IndicTextPreprocessor()
model = None

for mpath in ["./artifacts/models/ensemble.pkl", "./artifacts/models/ngram_model.pkl"]:
    if Path(mpath).exists():
        try:
            import pickle
            with open(mpath, "rb") as f:
                model = pickle.load(f)
            print(f"✓ Loaded: {mpath}")
            break
        except Exception as e:
            print(f"Could not load {mpath}: {e}")

if model is None:
    model = EnsembleLIDModel()
    print("⚠ No trained model found — using Unicode heuristic fallback")


# ── Inference ─────────────────────────────────────────────────────────────────

def predict(text: str) -> str:
    """Clean inference function: text in → language name out."""
    if not text or not text.strip():
        return "Please enter some text."
    processed = preprocessor.preprocess(text)
    if hasattr(model, "predict_with_confidence"):
        result = model.predict_with_confidence([processed])[0]
        lang = result.get("predicted_lang", "?")
        conf = result.get("confidence", 0.0)
    elif hasattr(model, "predict"):
        lang = model.predict([processed])[0]
        conf = 0.9
    else:
        lang, conf = "eng", 0.5

    info = LANGUAGE_REGISTRY.get(lang)
    name = info.name if info else lang
    native = info.native_name if info else ""
    script = info.script if info else "?"
    direction = info.direction if info else "LTR"
    confidence_icon = "🟢" if conf > 0.8 else "🟡" if conf > 0.6 else "🔴"

    return (
        f"**{name}** ({native})\n\n"
        f"Code: `{lang}` | Script: {script} | Direction: {direction}\n\n"
        f"Confidence: {conf:.2%} {confidence_icon}"
    )


def predict_detailed(text: str) -> tuple:
    """Returns (result_markdown, top5_table) for the full UI."""
    if not text or not text.strip():
        return "Please enter some text.", None

    processed = preprocessor.preprocess(text)

    if hasattr(model, "predict_with_confidence"):
        result = model.predict_with_confidence([processed])[0]
        lang = result.get("predicted_lang", "?")
        conf = result.get("confidence", 0.0)
        top5 = result.get("top_5", [])
    elif hasattr(model, "predict"):
        lang = model.predict([processed])[0]
        conf = 0.9
        top5 = [{"lang": lang, "probability": conf}]
    else:
        lang, conf = "eng", 0.5
        top5 = []

    info = LANGUAGE_REGISTRY.get(lang)
    confidence_icon = "🟢" if conf > 0.8 else "🟡" if conf > 0.6 else "🔴"

    result_md = f"""### 🎯 {info.name if info else lang} ({info.native_name if info else ''})



| | |

|---|---|

| Code | `{lang}` |

| Script | {info.script if info else '?'} |

| Direction | {info.direction if info else 'LTR'} |

| Family | {info.family if info else '?'} |

| Confidence | {conf:.2%} {confidence_icon} |

"""

    table = None
    if top5:
        import pandas as pd
        rows = []
        for t in top5:
            i = LANGUAGE_REGISTRY.get(t["lang"])
            rows.append({
                "Language": i.name if i else t["lang"],
                "Code": t["lang"],
                "Probability": f"{t['probability']:.2%}",
            })
        table = pd.DataFrame(rows)

    return result_md, table


# ── Sample Texts ──────────────────────────────────────────────────────────────

SAMPLES = [
    ["भारत एक विशाल देश है जहाँ अनेक भाषाएँ बोली जाती हैं।"],
    ["বাংলাদেশ এবং পশ্চিমবঙ্গে বাংলা ভাষা প্রচলিত।"],
    ["தமிழ் மொழி உலகின் மிகவும் பழமையான மொழிகளில் ஒன்றாகும்."],
    ["India is a diverse country with hundreds of languages."],
    ["اردو پاکستان کی قومی زبان ہے۔"],
    ["ಕನ್ನಡ ಭಾಷೆ ಕರ್ನಾಟಕ ರಾಜ್ಯದ ಅಧಿಕೃತ ಭಾಷೆ."],
]

# ── Gradio UI ─────────────────────────────────────────────────────────────────

with gr.Blocks(title="Indic Language Identifier") as demo:
    gr.Markdown("""

# 🔤 Indic Language Identifier

Detects language from text across **12 Indic languages + English**.

Powered by Unicode heuristics + CharNgram ensemble.

""")

    with gr.Row():
        with gr.Column(scale=2):
            text_input = gr.Textbox(
                label="Input Text",
                placeholder="Type or paste text in any supported language...",
                lines=5,
            )
            with gr.Row():
                clear_btn = gr.Button("Clear", variant="secondary")
                detect_btn = gr.Button("🔍 Detect Language", variant="primary")

            gr.Examples(examples=SAMPLES, inputs=text_input, label="Try a sample")

        with gr.Column(scale=2):
            result_md = gr.Markdown(label="Result")
            top5_table = gr.Dataframe(label="Top Predictions", visible=True)

    detect_btn.click(fn=predict_detailed, inputs=text_input, outputs=[result_md, top5_table])
    clear_btn.click(fn=lambda: ("", None), outputs=[result_md, top5_table])
    text_input.submit(fn=predict_detailed, inputs=text_input, outputs=[result_md, top5_table])

    gr.Markdown("""---

**Supported languages:** Bengali · English · Gujarati · Hindi · Kannada · Malayalam · Marathi · Odia · Punjabi · Tamil · Telugu · Urdu

""")

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