| """ |
| 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 |
|
|
| |
|
|
| def _load_model(): |
| |
| 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") |
|
|
| |
| import importlib.util, sys, os |
| ldgc_dir = os.path.dirname(vocab_path) |
| pkg_dir = os.path.dirname(ldgc_dir) |
|
|
| |
| 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() |
|
|
| |
| 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() |
|
|
| |
|
|
| _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() |
|
|