Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HuggingFace Space: Turkish Diacritic Restoration
|
| 3 |
+
Loads the CRF model from the emircanerol/turkish-diacritic-crf model repo.
|
| 4 |
+
"""
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
|
| 9 |
+
# ββ Load model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 10 |
+
|
| 11 |
+
def _load_model():
|
| 12 |
+
# Download inference code and weights from the model repo
|
| 13 |
+
vocab_path = hf_hub_download("emircanerol/turkish-diacritic-crf", "ldgc/vocab.py")
|
| 14 |
+
crf_gpu_path = hf_hub_download("emircanerol/turkish-diacritic-crf", "crf_gpu.py")
|
| 15 |
+
weights_path = hf_hub_download("emircanerol/turkish-diacritic-crf", "crf_gpu.safetensors")
|
| 16 |
+
|
| 17 |
+
# Make ldgc.vocab importable from the cached path
|
| 18 |
+
import importlib.util, sys, os
|
| 19 |
+
ldgc_dir = os.path.dirname(vocab_path)
|
| 20 |
+
pkg_dir = os.path.dirname(ldgc_dir)
|
| 21 |
+
|
| 22 |
+
# Install ldgc as a package if not already
|
| 23 |
+
if pkg_dir not in sys.path:
|
| 24 |
+
sys.path.insert(0, pkg_dir)
|
| 25 |
+
init = os.path.join(ldgc_dir, "__init__.py")
|
| 26 |
+
if not os.path.exists(init):
|
| 27 |
+
open(init, "w").close()
|
| 28 |
+
|
| 29 |
+
# Load crf_gpu module from cached path
|
| 30 |
+
spec = importlib.util.spec_from_file_location("crf_gpu", crf_gpu_path)
|
| 31 |
+
mod = importlib.util.module_from_spec(spec)
|
| 32 |
+
sys.modules["crf_gpu"] = mod
|
| 33 |
+
spec.loader.exec_module(mod)
|
| 34 |
+
|
| 35 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
+
model = mod.CRFGPUModel.from_pretrained(weights_path, device=device)
|
| 37 |
+
return model, mod.predict_stream
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
MODEL, predict_stream = _load_model()
|
| 41 |
+
|
| 42 |
+
# ββ Gradio interface ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
+
|
| 44 |
+
_EXAMPLES = [
|
| 45 |
+
"turkce dogal dil isleme cok onemlidir",
|
| 46 |
+
"bugun hava cok guzel, disari cikmak istiyorum",
|
| 47 |
+
"universite ogrencileri kΓΌtΓΌphane de calisΔ±yor",
|
| 48 |
+
"ruzgar bugun cok siddetli esiyor",
|
| 49 |
+
"turkiye'nin baskenti ankara'dir",
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def restore(text: str) -> str:
|
| 54 |
+
if not text.strip():
|
| 55 |
+
return ""
|
| 56 |
+
lines = [l for l in text.splitlines() if l.strip()]
|
| 57 |
+
preds = MODEL.predict(lines, batch_size=128)
|
| 58 |
+
return "\n".join(preds)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
with gr.Blocks(title="Turkish Diacritic Restoration") as demo:
|
| 62 |
+
gr.Markdown(
|
| 63 |
+
"""
|
| 64 |
+
# Turkish Diacritic Restoration
|
| 65 |
+
Restores missing diacritics in Turkish text
|
| 66 |
+
(Γ§, Δ, Δ±, ΓΆ, Ε, ΓΌ and circumflex variants).
|
| 67 |
+
|
| 68 |
+
**Model**: Bidirectional CRF with Β±2 character context and bigram features,
|
| 69 |
+
trained on 200k Wikipedia sentences.
|
| 70 |
+
Β· [Model repo](https://huggingface.co/emircanerol/turkish-diacritic-crf)
|
| 71 |
+
Β· [Code](https://github.com/emircanerol/tr-grammar)
|
| 72 |
+
"""
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column():
|
| 77 |
+
inp = gr.Textbox(
|
| 78 |
+
label="Noisy Turkish text (one sentence per line)",
|
| 79 |
+
placeholder="turkce cok guzelβ¦",
|
| 80 |
+
lines=6,
|
| 81 |
+
)
|
| 82 |
+
btn = gr.Button("Restore diacritics", variant="primary")
|
| 83 |
+
with gr.Column():
|
| 84 |
+
out = gr.Textbox(label="Restored text", lines=6, interactive=False)
|
| 85 |
+
|
| 86 |
+
gr.Examples(
|
| 87 |
+
examples=[[e] for e in _EXAMPLES],
|
| 88 |
+
inputs=inp,
|
| 89 |
+
outputs=out,
|
| 90 |
+
fn=restore,
|
| 91 |
+
cache_examples=True,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
btn.click(restore, inputs=inp, outputs=out)
|
| 95 |
+
inp.submit(restore, inputs=inp, outputs=out)
|
| 96 |
+
|
| 97 |
+
demo.launch()
|