Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,21 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
-
import hashlib
|
| 5 |
import joblib
|
|
|
|
| 6 |
from collections import Counter
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
# --- utils
|
| 10 |
def ngrams(sentence, n=1, lc=True):
|
| 11 |
-
ngram_l = []
|
| 12 |
sentence = sentence.lower()
|
| 13 |
-
for i in range(len(sentence) - n + 1)
|
| 14 |
-
ngram = sentence[i:i+n]
|
| 15 |
-
ngram_l.append(ngram)
|
| 16 |
-
return ngram_l
|
| 17 |
|
| 18 |
def all_ngrams(sentence, max_ngram=3, lc=True):
|
| 19 |
-
|
| 20 |
for i in range(1, max_ngram + 1):
|
| 21 |
-
|
| 22 |
-
return
|
| 23 |
|
| 24 |
MAX_CHARS = 521
|
| 25 |
MAX_BIGRAMS = 1031
|
|
@@ -28,78 +24,73 @@ MAXES = [MAX_CHARS, MAX_BIGRAMS, MAX_TRIGRAMS]
|
|
| 28 |
|
| 29 |
def reproducible_hash(string):
|
| 30 |
h = hashlib.md5(string.encode("utf-8"), usedforsecurity=False)
|
| 31 |
-
return int.from_bytes(h.digest()[0:8],
|
| 32 |
|
| 33 |
def hash_ngrams(ngrams, modulos):
|
| 34 |
-
|
| 35 |
for ngram_list, modulo in zip(ngrams, modulos):
|
| 36 |
codes = [(reproducible_hash(x) % modulo) for x in ngram_list]
|
| 37 |
-
|
| 38 |
-
return
|
| 39 |
|
| 40 |
def calc_rel_freq(codes):
|
| 41 |
cnt = Counter(codes)
|
| 42 |
-
total = sum(cnt.values())
|
| 43 |
-
for k in cnt
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
MAX_SHIFT
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def build_freq_dict(sentence, MAXES=MAXES, MAX_SHIFT=MAX_SHIFT):
|
| 59 |
hngrams = hash_ngrams(all_ngrams(sentence), MAXES)
|
| 60 |
-
|
| 61 |
-
return shift_keys(
|
| 62 |
|
| 63 |
-
# --- load
|
| 64 |
-
clf = joblib.load("nld.joblib")
|
| 65 |
vectorizer = joblib.load("nld_vectorizer.joblib")
|
| 66 |
idx2lang = joblib.load("nld_lang_codes.joblib")
|
| 67 |
|
| 68 |
input_dim = len(vectorizer.vocabulary_)
|
| 69 |
-
|
| 70 |
|
| 71 |
model = nn.Sequential(
|
| 72 |
nn.Linear(input_dim, 50),
|
| 73 |
nn.ReLU(),
|
| 74 |
-
nn.Linear(50,
|
| 75 |
)
|
| 76 |
model.load_state_dict(torch.load("nld.pth", map_location="cpu"))
|
| 77 |
model.eval()
|
| 78 |
|
| 79 |
-
# --- prediction
|
| 80 |
-
def detect_lang(
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
return
|
| 91 |
-
|
| 92 |
-
# ---
|
| 93 |
-
with gr.Blocks(title="
|
| 94 |
-
gr.Markdown("#
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
-
|
| 98 |
-
|
| 99 |
with gr.Column():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
btn = gr.Button("Guess the language!")
|
| 103 |
-
btn.click(fn=detect_lang, inputs=[src_sentence], outputs=[tgt_sentence])
|
| 104 |
|
| 105 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
|
|
|
| 3 |
import joblib
|
| 4 |
+
import hashlib
|
| 5 |
from collections import Counter
|
| 6 |
+
import numpy as np
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# --- utils ---
|
| 10 |
def ngrams(sentence, n=1, lc=True):
|
|
|
|
| 11 |
sentence = sentence.lower()
|
| 12 |
+
return [sentence[i:i+n] for i in range(len(sentence) - n + 1)]
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def all_ngrams(sentence, max_ngram=3, lc=True):
|
| 15 |
+
result = []
|
| 16 |
for i in range(1, max_ngram + 1):
|
| 17 |
+
result += [ngrams(sentence, n=i, lc=lc)]
|
| 18 |
+
return result
|
| 19 |
|
| 20 |
MAX_CHARS = 521
|
| 21 |
MAX_BIGRAMS = 1031
|
|
|
|
| 24 |
|
| 25 |
def reproducible_hash(string):
|
| 26 |
h = hashlib.md5(string.encode("utf-8"), usedforsecurity=False)
|
| 27 |
+
return int.from_bytes(h.digest()[0:8], "big", signed=True)
|
| 28 |
|
| 29 |
def hash_ngrams(ngrams, modulos):
|
| 30 |
+
out = []
|
| 31 |
for ngram_list, modulo in zip(ngrams, modulos):
|
| 32 |
codes = [(reproducible_hash(x) % modulo) for x in ngram_list]
|
| 33 |
+
out.append(codes)
|
| 34 |
+
return out
|
| 35 |
|
| 36 |
def calc_rel_freq(codes):
|
| 37 |
cnt = Counter(codes)
|
| 38 |
+
total = sum(cnt.values()) or 1
|
| 39 |
+
return {k: v / total for k, v in cnt.items()}
|
| 40 |
+
|
| 41 |
+
MAX_SHIFT = [0]
|
| 42 |
+
for i in range(1, len(MAXES)):
|
| 43 |
+
MAX_SHIFT.append(sum(MAXES[:i]))
|
| 44 |
+
|
| 45 |
+
def shift_keys(dicts, shift_list):
|
| 46 |
+
new = {}
|
| 47 |
+
for i, d in enumerate(dicts):
|
| 48 |
+
for k, v in d.items():
|
| 49 |
+
new[k + shift_list[i]] = v
|
| 50 |
+
return new
|
| 51 |
+
|
| 52 |
+
def build_freq_dict(sentence):
|
|
|
|
|
|
|
| 53 |
hngrams = hash_ngrams(all_ngrams(sentence), MAXES)
|
| 54 |
+
freqs = list(map(calc_rel_freq, hngrams))
|
| 55 |
+
return shift_keys(freqs, MAX_SHIFT)
|
| 56 |
|
| 57 |
+
# --- load artifacts ---
|
|
|
|
| 58 |
vectorizer = joblib.load("nld_vectorizer.joblib")
|
| 59 |
idx2lang = joblib.load("nld_lang_codes.joblib")
|
| 60 |
|
| 61 |
input_dim = len(vectorizer.vocabulary_)
|
| 62 |
+
num_classes = len(idx2lang)
|
| 63 |
|
| 64 |
model = nn.Sequential(
|
| 65 |
nn.Linear(input_dim, 50),
|
| 66 |
nn.ReLU(),
|
| 67 |
+
nn.Linear(50, num_classes)
|
| 68 |
)
|
| 69 |
model.load_state_dict(torch.load("nld.pth", map_location="cpu"))
|
| 70 |
model.eval()
|
| 71 |
|
| 72 |
+
# --- prediction ---
|
| 73 |
+
def detect_lang(text: str):
|
| 74 |
+
feat_dict = build_freq_dict(text)
|
| 75 |
+
X = vectorizer.transform([feat_dict])
|
| 76 |
+
if hasattr(X, "toarray"):
|
| 77 |
+
X = X.toarray()
|
| 78 |
+
X = torch.from_numpy(X.astype("float32"))
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
logits = model(X)
|
| 82 |
+
pred_idx = torch.argmax(logits, dim=-1).item()
|
| 83 |
+
return idx2lang[pred_idx]
|
| 84 |
+
|
| 85 |
+
# --- UI ---
|
| 86 |
+
with gr.Blocks(title="Language Detector") as demo:
|
| 87 |
+
gr.Markdown("# Language Detector")
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column():
|
| 90 |
+
src_text = gr.Textbox(label="Enter text", placeholder="Type here...")
|
| 91 |
+
btn = gr.Button("Detect Language")
|
| 92 |
with gr.Column():
|
| 93 |
+
out_lang = gr.Textbox(label="Predicted language", interactive=False)
|
| 94 |
+
btn.click(fn=detect_lang, inputs=src_text, outputs=out_lang)
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
demo.launch()
|