Spaces:
Sleeping
Sleeping
Asanaly commited on
Update app.py
Browse files11 unmodified lines12131415161718192021222324256 unmodified lines323334353637387 unmodified lines46474849505152536 unmodified lines606162636465666768697011 unmodified linestokenizer = AutoTokenizer.from_pretrained(model_checkpoint)model = AutoModelForTokenClassification.from_pretrained(model_checkpoint, num_labels=7)# Қазақ NER үшін label тізіміlabel_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]# ============================# 2. NER функциясы (бірнеше сөзді біріктіру)# ============================def predict_ner_entities(text):tokens = tokenizer(text.split(), return_tensors="pt", is_split_into_words=True)outputs = model(**tokens).logitspredictions = np.argmax(outputs.detach().numpy(), axis=2)[0]word_ids = tokens.word_ids(batch_index=0)6 unmodified linesif word_idx is None:continuelabel = label_list[predictions[idx]]word = text.split()[word_idx]if label.startswith("B-"):if current_entity:7 unmodified linesentities.append((" ".join(current_entity), current_label))current_entity = []current_label = Noneif current_entity:entities.append((" ".join(current_entity), current_label))return entities# ============================6 unmodified linesoutput_dict = {"PER": [], "ORG": [], "LOC": []}for word, label in entities:if label.endswith("PER"):output_dict["PER"].append(word)elif label.endswith("ORG"):output_dict["ORG"].append(word)elif label.endswith("LOC"):output_dict["LOC"].append(word)output_text = ""11 unmodified lines12131415161718192021222324256 unmodified lines32333435363738394041424344457 unmodified lines535455565758596061626 unmodified lines697071727374757677787911 unmodified linestokenizer = AutoTokenizer.from_pretrained(model_checkpoint)model = AutoModelForTokenClassification.from_pretrained(model_checkpoint, num_labels=7)# NER үшін label тізіміlabel_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]# ============================# 2. NER функциясы (B/I тегтерін біріктіру)# ============================def predict_ner_entities(text):tokens = tokenizer(text, return_tensors="pt", is_split_into_words=False)outputs = model(**tokens).logitspredictions = np.argmax(outputs.detach().numpy(), axis=2)[0]word_ids = tokens.word_ids(batch_index=0)6 unmodified linesif word_idx is None:continuelabel = label_list[predictions[idx]]word = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0][idx])# Токендерді біріктіруif word.startswith("##"):word = word[2:]if current_entity:current_entity[-1] += wordcontinueif label.startswith("B-"):if current_entity:7 unmodified linesentities.append((" ".join(current_entity), current_label))current_entity = []current_label = Noneif current_entity:entities.append((" ".join(current_entity), current_label))return entities# ============================6 unmodified linesoutput_dict = {"PER": [], "ORG": [], "LOC": []}for word, label in entities:if label == "PER":output_dict["PER"].append(word)elif label == "ORG":output_dict["ORG"].append(word)elif label == "LOC":output_dict["LOC"].append(word)output_text = ""
app.py+16-7