Spaces:
Sleeping
Sleeping
Asanaly
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,33 +16,48 @@ model = AutoModelForTokenClassification.from_pretrained(model_checkpoint, num_la
|
|
| 16 |
label_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]
|
| 17 |
|
| 18 |
# ============================
|
| 19 |
-
# 2. NER функциясы
|
| 20 |
# ============================
|
| 21 |
-
def
|
| 22 |
tokens = tokenizer(text.split(), return_tensors="pt", is_split_into_words=True)
|
| 23 |
outputs = model(**tokens).logits
|
| 24 |
predictions = np.argmax(outputs.detach().numpy(), axis=2)[0]
|
| 25 |
-
|
| 26 |
word_ids = tokens.word_ids(batch_index=0)
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
for idx, word_idx in enumerate(word_ids):
|
| 30 |
-
if word_idx is
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# ============================
|
| 39 |
-
# 3.
|
| 40 |
# ============================
|
| 41 |
def format_ner(text):
|
| 42 |
-
entities =
|
| 43 |
if not entities:
|
| 44 |
return "Атаулар табылған жоқ"
|
| 45 |
-
|
| 46 |
output_dict = {"PER": [], "ORG": [], "LOC": []}
|
| 47 |
for word, label in entities:
|
| 48 |
if label.endswith("PER"):
|
|
@@ -51,12 +66,11 @@ def format_ner(text):
|
|
| 51 |
output_dict["ORG"].append(word)
|
| 52 |
elif label.endswith("LOC"):
|
| 53 |
output_dict["LOC"].append(word)
|
| 54 |
-
|
| 55 |
-
# Бір қатарға қосып шығару
|
| 56 |
output_text = ""
|
| 57 |
for key, words in output_dict.items():
|
| 58 |
if words:
|
| 59 |
-
output_text += f"{key}: {' '.join(words)}\n"
|
| 60 |
return output_text.strip()
|
| 61 |
|
| 62 |
# ============================
|
|
@@ -64,14 +78,13 @@ def format_ner(text):
|
|
| 64 |
# ============================
|
| 65 |
iface = gr.Interface(
|
| 66 |
fn=format_ner,
|
| 67 |
-
inputs=gr.Textbox(lines=
|
| 68 |
outputs=gr.Textbox(label="Анықталған атаулар"),
|
| 69 |
title="Қазақ тіліндегі NER",
|
| 70 |
-
description="PER – адам, ORG – ұйым, LOC – орын.
|
| 71 |
)
|
| 72 |
|
| 73 |
# ============================
|
| 74 |
# 5. Іске қосу
|
| 75 |
# ============================
|
| 76 |
iface.launch()
|
| 77 |
-
|
|
|
|
| 16 |
label_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]
|
| 17 |
|
| 18 |
# ============================
|
| 19 |
+
# 2. NER функциясы (бірнеше сөзді біріктіру)
|
| 20 |
# ============================
|
| 21 |
+
def predict_ner_entities(text):
|
| 22 |
tokens = tokenizer(text.split(), return_tensors="pt", is_split_into_words=True)
|
| 23 |
outputs = model(**tokens).logits
|
| 24 |
predictions = np.argmax(outputs.detach().numpy(), axis=2)[0]
|
|
|
|
| 25 |
word_ids = tokens.word_ids(batch_index=0)
|
| 26 |
+
|
| 27 |
+
entities = []
|
| 28 |
+
current_entity = []
|
| 29 |
+
current_label = None
|
| 30 |
+
|
| 31 |
for idx, word_idx in enumerate(word_ids):
|
| 32 |
+
if word_idx is None:
|
| 33 |
+
continue
|
| 34 |
+
label = label_list[predictions[idx]]
|
| 35 |
+
word = text.split()[word_idx]
|
| 36 |
+
|
| 37 |
+
if label.startswith("B-"):
|
| 38 |
+
if current_entity:
|
| 39 |
+
entities.append((" ".join(current_entity), current_label))
|
| 40 |
+
current_entity = [word]
|
| 41 |
+
current_label = label[2:]
|
| 42 |
+
elif label.startswith("I-") and current_label == label[2:]:
|
| 43 |
+
current_entity.append(word)
|
| 44 |
+
else:
|
| 45 |
+
if current_entity:
|
| 46 |
+
entities.append((" ".join(current_entity), current_label))
|
| 47 |
+
current_entity = []
|
| 48 |
+
current_label = None
|
| 49 |
+
if current_entity:
|
| 50 |
+
entities.append((" ".join(current_entity), current_label))
|
| 51 |
+
return entities
|
| 52 |
|
| 53 |
# ============================
|
| 54 |
+
# 3. Форматтау – қарапайым текстпен шығару
|
| 55 |
# ============================
|
| 56 |
def format_ner(text):
|
| 57 |
+
entities = predict_ner_entities(text)
|
| 58 |
if not entities:
|
| 59 |
return "Атаулар табылған жоқ"
|
| 60 |
+
|
| 61 |
output_dict = {"PER": [], "ORG": [], "LOC": []}
|
| 62 |
for word, label in entities:
|
| 63 |
if label.endswith("PER"):
|
|
|
|
| 66 |
output_dict["ORG"].append(word)
|
| 67 |
elif label.endswith("LOC"):
|
| 68 |
output_dict["LOC"].append(word)
|
| 69 |
+
|
|
|
|
| 70 |
output_text = ""
|
| 71 |
for key, words in output_dict.items():
|
| 72 |
if words:
|
| 73 |
+
output_text += f"{key}: {'; '.join(words)}\n"
|
| 74 |
return output_text.strip()
|
| 75 |
|
| 76 |
# ============================
|
|
|
|
| 78 |
# ============================
|
| 79 |
iface = gr.Interface(
|
| 80 |
fn=format_ner,
|
| 81 |
+
inputs=gr.Textbox(lines=15, placeholder="Қазақ мәтінін осында енгізіңіз..."),
|
| 82 |
outputs=gr.Textbox(label="Анықталған атаулар"),
|
| 83 |
title="Қазақ тіліндегі NER",
|
| 84 |
+
description="PER – адам, ORG – ұйым, LOC – орын. Бірнеше сөйлемді бірден өңдейді."
|
| 85 |
)
|
| 86 |
|
| 87 |
# ============================
|
| 88 |
# 5. Іске қосу
|
| 89 |
# ============================
|
| 90 |
iface.launch()
|
|
|