Spaces:
Sleeping
Sleeping
Asanaly commited on
Update app.py
Browse files15 unmodified lines1617181919202121222324252527282627282930313031323334353632333435363738394041424344454647484950515253395455564257585945606162632 unmodified lines66676854556970717259737475761 unmodified line7879806781828370848586878889907715 unmodified lineslabel_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]
# ============================# 2. NER функциясы# 2. NER функциясы (бірнеше сөзді біріктіру)# ============================def predict_ner(text):def predict_ner_entities(text): tokens = tokenizer(text.split(), return_tensors="pt", is_split_into_words=True) outputs = model(**tokens).logits predictions = np.argmax(outputs.detach().numpy(), axis=2)[0]
word_ids = tokens.word_ids(batch_index=0) results = [] already_seen = set()
entities = [] current_entity = [] current_label = None
for idx, word_idx in enumerate(word_ids): if word_idx is not None and word_idx not in already_seen: label = label_list[predictions[idx]] word = text.split()[word_idx] if label != "O": results.append((word, label)) already_seen.add(word_idx) return results if word_idx is None: continue label = label_list[predictions[idx]] word = text.split()[word_idx]
if label.startswith("B-"): if current_entity: entities.append((" ".join(current_entity), current_label)) current_entity = [word] current_label = label[2:] elif label.startswith("I-") and current_label == label[2:]: current_entity.append(word) else: if current_entity: entities.append((" ".join(current_entity), current_label)) current_entity = [] current_label = None if current_entity: entities.append((" ".join(current_entity), current_label)) return entities
# ============================# 3. Атауларды текстпен шығару# 3. Форматтау – қарапайым текстпен шығару# ============================def format_ner(text): entities = predict_ner(text) entities = predict_ner_entities(text) if not entities: return "Атаулар табылған жоқ"
output_dict = {"PER": [], "ORG": [], "LOC": []} for word, label in entities: if label.endswith("PER"):2 unmodified lines output_dict["ORG"].append(word) elif label.endswith("LOC"): output_dict["LOC"].append(word) # Бір қатарға қосып шығару
output_text = "" for key, words in output_dict.items(): if words: output_text += f"{key}: {' '.join(words)}\n" output_text += f"{key}: {'; '.join(words)}\n" return output_text.strip()
# ============================1 unmodified line# ============================iface = gr.Interface( fn=format_ner, inputs=gr.Textbox(lines=10, placeholder="Қазақ мәтінін осында енгізіңіз..."), inputs=gr.Textbox(lines=15, placeholder="Қазақ мәтінін осында енгізіңіз..."), outputs=gr.Textbox(label="Анықталған атаулар"), title="Қазақ тіліндегі NER", description="PER – адам, ORG – ұйым, LOC – орын. Атаулар қарапайым текстпен шығарылады." description="PER – адам, ORG – ұйым, LOC – орын. Бірнеше сөйлемді бірден өңдейді.")
# ============================# 5. Іске қосу# ============================iface.launch()
app.py+34-21