File size: 3,517 Bytes
eb5e2b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
HuggingFace Space: Turkish Diacritic Restoration
Loads the CRF model from the emircanerol/turkish-diacritic-crf model repo.
"""
import gradio as gr
import torch
from huggingface_hub import hf_hub_download

# ── Load model ────────────────────────────────────────────────────────────────

def _load_model():
    # Download inference code and weights from the model repo
    vocab_path   = hf_hub_download("emircanerol/turkish-diacritic-crf", "ldgc/vocab.py")
    crf_gpu_path = hf_hub_download("emircanerol/turkish-diacritic-crf", "crf_gpu.py")
    weights_path = hf_hub_download("emircanerol/turkish-diacritic-crf", "crf_gpu.safetensors")

    # Make ldgc.vocab importable from the cached path
    import importlib.util, sys, os
    ldgc_dir = os.path.dirname(vocab_path)
    pkg_dir  = os.path.dirname(ldgc_dir)

    # Install ldgc as a package if not already
    if pkg_dir not in sys.path:
        sys.path.insert(0, pkg_dir)
    init = os.path.join(ldgc_dir, "__init__.py")
    if not os.path.exists(init):
        open(init, "w").close()

    # Load crf_gpu module from cached path
    spec = importlib.util.spec_from_file_location("crf_gpu", crf_gpu_path)
    mod  = importlib.util.module_from_spec(spec)
    sys.modules["crf_gpu"] = mod
    spec.loader.exec_module(mod)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model  = mod.CRFGPUModel.from_pretrained(weights_path, device=device)
    return model, mod.predict_stream


MODEL, predict_stream = _load_model()

# ── Gradio interface ──────────────────────────────────────────────────────────

_EXAMPLES = [
    "turkce dogal dil isleme cok onemlidir",
    "bugun hava cok guzel, disari cikmak istiyorum",
    "universite ogrencileri kΓΌtΓΌphane de calisΔ±yor",
    "ruzgar bugun cok siddetli esiyor",
    "turkiye'nin baskenti ankara'dir",
]


def restore(text: str) -> str:
    if not text.strip():
        return ""
    lines  = [l for l in text.splitlines() if l.strip()]
    preds  = MODEL.predict(lines, batch_size=128)
    return "\n".join(preds)


with gr.Blocks(title="Turkish Diacritic Restoration") as demo:
    gr.Markdown(
        """
        # Turkish Diacritic Restoration
        Restores missing diacritics in Turkish text
        (ç, ğ, ı, â, ş, ü and circumflex variants).

        **Model**: Bidirectional CRF with Β±2 character context and bigram features,
        trained on 200k Wikipedia sentences.
        Β· [Model repo](https://huggingface.co/emircanerol/turkish-diacritic-crf)
        Β· [Code](https://github.com/emircanerol/tr-grammar)
        """
    )

    with gr.Row():
        with gr.Column():
            inp = gr.Textbox(
                label="Noisy Turkish text (one sentence per line)",
                placeholder="turkce cok guzel…",
                lines=6,
            )
            btn = gr.Button("Restore diacritics", variant="primary")
        with gr.Column():
            out = gr.Textbox(label="Restored text", lines=6, interactive=False)

    gr.Examples(
        examples=[[e] for e in _EXAMPLES],
        inputs=inp,
        outputs=out,
        fn=restore,
        cache_examples=True,
    )

    btn.click(restore, inputs=inp, outputs=out)
    inp.submit(restore, inputs=inp, outputs=out)

demo.launch()