Update Model/MultimodelNER/VLSP2016/MNER_2016.py
Browse files
Model/MultimodelNER/VLSP2016/MNER_2016.py
CHANGED
@@ -1,106 +1,106 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from spacy import displacy
|
3 |
-
from Model.NER.VLSP2021.Predict_Ner import ViTagger
|
4 |
-
import re
|
5 |
-
from thunghiemxuly import save_uploaded_image,convert_text_to_txt,add_string_to_txt
|
6 |
-
|
7 |
-
import os
|
8 |
-
from transformers import AutoTokenizer, BertConfig
|
9 |
-
from Model.MultimodelNER.VLSP2016.train_umt_2016 import load_model,predict
|
10 |
-
from Model.MultimodelNER.Ner_processing import format_predictions,process_predictions,combine_entities,remove_B_prefix,combine_i_tags
|
11 |
-
|
12 |
-
from Model.MultimodelNER.predict import get_test_examples_predict
|
13 |
-
from Model.MultimodelNER import resnet as resnet
|
14 |
-
from Model.MultimodelNER.resnet_utils import myResnet
|
15 |
-
import torch
|
16 |
-
import numpy as np
|
17 |
-
from Model.MultimodelNER.VLSP2016.dataset_roberta import MNERProcessor_2016
|
18 |
-
|
19 |
-
|
20 |
-
CONFIG_NAME = 'bert_config.json'
|
21 |
-
WEIGHTS_NAME = 'pytorch_model.bin'
|
22 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
-
|
24 |
-
|
25 |
-
net = getattr(resnet, 'resnet152')()
|
26 |
-
net.load_state_dict(torch.load(os.path.join('
|
27 |
-
encoder = myResnet(net, True, device)
|
28 |
-
def process_text(text):
|
29 |
-
# Loại bỏ dấu cách thừa và dấu cách ở đầu và cuối văn bản
|
30 |
-
processed_text = re.sub(r'\s+', ' ', text.strip())
|
31 |
-
return processed_text
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
def show_mner_2016():
|
36 |
-
multimodal_text = st.text_area("Enter your text for MNER:", height=300)
|
37 |
-
multimodal_text = process_text(multimodal_text) # Xử lý văn bản
|
38 |
-
image = st.file_uploader("Upload an image (only jpg):", type=["jpg"])
|
39 |
-
if st.button("Process Multimodal NER"):
|
40 |
-
save_image = '
|
41 |
-
save_txt = '
|
42 |
-
image_name = image.name
|
43 |
-
save_uploaded_image(image, save_image)
|
44 |
-
convert_text_to_txt(multimodal_text, save_txt)
|
45 |
-
add_string_to_txt(image_name, save_txt)
|
46 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
47 |
-
|
48 |
-
bert_model='vinai/phobert-base-v2'
|
49 |
-
output_dir='
|
50 |
-
output_model_file = os.path.join(output_dir, WEIGHTS_NAME)
|
51 |
-
output_encoder_file = os.path.join(output_dir, "pytorch_encoder.bin")
|
52 |
-
processor = MNERProcessor_2016()
|
53 |
-
label_list = processor.get_labels()
|
54 |
-
auxlabel_list = processor.get_auxlabels()
|
55 |
-
num_labels = len(label_list) + 1
|
56 |
-
auxnum_labels = len(auxlabel_list) + 1
|
57 |
-
trans_matrix = np.zeros((auxnum_labels, num_labels), dtype=float)
|
58 |
-
trans_matrix[0, 0] = 1 # pad to pad
|
59 |
-
trans_matrix[1, 1] = 1 # O to O
|
60 |
-
trans_matrix[2, 2] = 0.25 # B to B-MISC
|
61 |
-
trans_matrix[2, 4] = 0.25 # B to B-PER
|
62 |
-
trans_matrix[2, 6] = 0.25 # B to B-ORG
|
63 |
-
trans_matrix[2, 8] = 0.25 # B to B-LOC
|
64 |
-
trans_matrix[3, 3] = 0.25 # I to I-MISC
|
65 |
-
trans_matrix[3, 5] = 0.25 # I to I-PER
|
66 |
-
trans_matrix[3, 7] = 0.25 # I to I-ORG
|
67 |
-
trans_matrix[3, 9] = 0.25 # I to I-LOC
|
68 |
-
trans_matrix[4, 10] = 1 # X to X
|
69 |
-
trans_matrix[5, 11] = 1 # [CLS] to [CLS]
|
70 |
-
trans_matrix[6, 12] = 1
|
71 |
-
tokenizer = AutoTokenizer.from_pretrained(bert_model, do_lower_case=False)
|
72 |
-
model_umt, encoder_umt = load_model(output_model_file, output_encoder_file, encoder,num_labels,auxnum_labels)
|
73 |
-
eval_examples = get_test_examples_predict('
|
74 |
-
|
75 |
-
y_pred, a = predict(model_umt, encoder_umt, eval_examples, tokenizer, device,save_image,trans_matrix)
|
76 |
-
formatted_output = format_predictions(a, y_pred[0])
|
77 |
-
final = process_predictions(formatted_output)
|
78 |
-
final2 = combine_entities(final)
|
79 |
-
final3 = remove_B_prefix(final2)
|
80 |
-
final4 = combine_i_tags(final3)
|
81 |
-
words_and_labels = final4
|
82 |
-
# Tạo danh sách từ
|
83 |
-
words = [word for word, _ in words_and_labels]
|
84 |
-
# Tạo danh sách thực thể và nhãn cho mỗi từ, loại bỏ nhãn 'O'
|
85 |
-
entities = [{'start': sum(len(word) + 1 for word, _ in words_and_labels[:i]),
|
86 |
-
'end': sum(len(word) + 1 for word, _ in words_and_labels[:i + 1]), 'label': label} for
|
87 |
-
i, (word, label)
|
88 |
-
in enumerate(words_and_labels) if label != 'O']
|
89 |
-
# print(entities)
|
90 |
-
|
91 |
-
# Render the visualization without color for 'O' labels
|
92 |
-
html = displacy.render(
|
93 |
-
{"text": " ".join(words), "ents": entities, "title": None},
|
94 |
-
style="ent",
|
95 |
-
manual=True,
|
96 |
-
options={"colors": {"MISC": "#806699",
|
97 |
-
"ORG": "#ff6666",
|
98 |
-
"LOC": "#66cc66",
|
99 |
-
"PER": "#bf80ff",
|
100 |
-
"O": None}}
|
101 |
-
)
|
102 |
-
# print(html)
|
103 |
-
st.markdown(html, unsafe_allow_html=True)
|
104 |
-
|
105 |
-
|
106 |
###Ví dụ 1 : Một trận hỗn chiến đã xảy ra tại trận đấu khúc côn cầu giữa Penguins và Islanders ở Mỹ (image:penguin)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from spacy import displacy
|
3 |
+
from Model.NER.VLSP2021.Predict_Ner import ViTagger
|
4 |
+
import re
|
5 |
+
from thunghiemxuly import save_uploaded_image,convert_text_to_txt,add_string_to_txt
|
6 |
+
|
7 |
+
import os
|
8 |
+
from transformers import AutoTokenizer, BertConfig
|
9 |
+
from Model.MultimodelNER.VLSP2016.train_umt_2016 import load_model,predict
|
10 |
+
from Model.MultimodelNER.Ner_processing import format_predictions,process_predictions,combine_entities,remove_B_prefix,combine_i_tags
|
11 |
+
|
12 |
+
from Model.MultimodelNER.predict import get_test_examples_predict
|
13 |
+
from Model.MultimodelNER import resnet as resnet
|
14 |
+
from Model.MultimodelNER.resnet_utils import myResnet
|
15 |
+
import torch
|
16 |
+
import numpy as np
|
17 |
+
from Model.MultimodelNER.VLSP2016.dataset_roberta import MNERProcessor_2016
|
18 |
+
|
19 |
+
|
20 |
+
CONFIG_NAME = 'bert_config.json'
|
21 |
+
WEIGHTS_NAME = 'pytorch_model.bin'
|
22 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
+
|
24 |
+
|
25 |
+
net = getattr(resnet, 'resnet152')()
|
26 |
+
net.load_state_dict(torch.load(os.path.join('/Model/Resnet/', 'resnet152.pth')))
|
27 |
+
encoder = myResnet(net, True, device)
|
28 |
+
def process_text(text):
|
29 |
+
# Loại bỏ dấu cách thừa và dấu cách ở đầu và cuối văn bản
|
30 |
+
processed_text = re.sub(r'\s+', ' ', text.strip())
|
31 |
+
return processed_text
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
def show_mner_2016():
|
36 |
+
multimodal_text = st.text_area("Enter your text for MNER:", height=300)
|
37 |
+
multimodal_text = process_text(multimodal_text) # Xử lý văn bản
|
38 |
+
image = st.file_uploader("Upload an image (only jpg):", type=["jpg"])
|
39 |
+
if st.button("Process Multimodal NER"):
|
40 |
+
save_image = '/Model/MultimodelNER/VLSP2016/Image'
|
41 |
+
save_txt = '/Model/MultimodelNER/VLSP2016/Filetxt/test.txt'
|
42 |
+
image_name = image.name
|
43 |
+
save_uploaded_image(image, save_image)
|
44 |
+
convert_text_to_txt(multimodal_text, save_txt)
|
45 |
+
add_string_to_txt(image_name, save_txt)
|
46 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
47 |
+
|
48 |
+
bert_model='vinai/phobert-base-v2'
|
49 |
+
output_dir='/Model/MultimodelNER/VLSP2016/best_model'
|
50 |
+
output_model_file = os.path.join(output_dir, WEIGHTS_NAME)
|
51 |
+
output_encoder_file = os.path.join(output_dir, "pytorch_encoder.bin")
|
52 |
+
processor = MNERProcessor_2016()
|
53 |
+
label_list = processor.get_labels()
|
54 |
+
auxlabel_list = processor.get_auxlabels()
|
55 |
+
num_labels = len(label_list) + 1
|
56 |
+
auxnum_labels = len(auxlabel_list) + 1
|
57 |
+
trans_matrix = np.zeros((auxnum_labels, num_labels), dtype=float)
|
58 |
+
trans_matrix[0, 0] = 1 # pad to pad
|
59 |
+
trans_matrix[1, 1] = 1 # O to O
|
60 |
+
trans_matrix[2, 2] = 0.25 # B to B-MISC
|
61 |
+
trans_matrix[2, 4] = 0.25 # B to B-PER
|
62 |
+
trans_matrix[2, 6] = 0.25 # B to B-ORG
|
63 |
+
trans_matrix[2, 8] = 0.25 # B to B-LOC
|
64 |
+
trans_matrix[3, 3] = 0.25 # I to I-MISC
|
65 |
+
trans_matrix[3, 5] = 0.25 # I to I-PER
|
66 |
+
trans_matrix[3, 7] = 0.25 # I to I-ORG
|
67 |
+
trans_matrix[3, 9] = 0.25 # I to I-LOC
|
68 |
+
trans_matrix[4, 10] = 1 # X to X
|
69 |
+
trans_matrix[5, 11] = 1 # [CLS] to [CLS]
|
70 |
+
trans_matrix[6, 12] = 1
|
71 |
+
tokenizer = AutoTokenizer.from_pretrained(bert_model, do_lower_case=False)
|
72 |
+
model_umt, encoder_umt = load_model(output_model_file, output_encoder_file, encoder,num_labels,auxnum_labels)
|
73 |
+
eval_examples = get_test_examples_predict('/Model/MultimodelNER/VLSP2016/Filetxt/')
|
74 |
+
|
75 |
+
y_pred, a = predict(model_umt, encoder_umt, eval_examples, tokenizer, device,save_image,trans_matrix)
|
76 |
+
formatted_output = format_predictions(a, y_pred[0])
|
77 |
+
final = process_predictions(formatted_output)
|
78 |
+
final2 = combine_entities(final)
|
79 |
+
final3 = remove_B_prefix(final2)
|
80 |
+
final4 = combine_i_tags(final3)
|
81 |
+
words_and_labels = final4
|
82 |
+
# Tạo danh sách từ
|
83 |
+
words = [word for word, _ in words_and_labels]
|
84 |
+
# Tạo danh sách thực thể và nhãn cho mỗi từ, loại bỏ nhãn 'O'
|
85 |
+
entities = [{'start': sum(len(word) + 1 for word, _ in words_and_labels[:i]),
|
86 |
+
'end': sum(len(word) + 1 for word, _ in words_and_labels[:i + 1]), 'label': label} for
|
87 |
+
i, (word, label)
|
88 |
+
in enumerate(words_and_labels) if label != 'O']
|
89 |
+
# print(entities)
|
90 |
+
|
91 |
+
# Render the visualization without color for 'O' labels
|
92 |
+
html = displacy.render(
|
93 |
+
{"text": " ".join(words), "ents": entities, "title": None},
|
94 |
+
style="ent",
|
95 |
+
manual=True,
|
96 |
+
options={"colors": {"MISC": "#806699",
|
97 |
+
"ORG": "#ff6666",
|
98 |
+
"LOC": "#66cc66",
|
99 |
+
"PER": "#bf80ff",
|
100 |
+
"O": None}}
|
101 |
+
)
|
102 |
+
# print(html)
|
103 |
+
st.markdown(html, unsafe_allow_html=True)
|
104 |
+
|
105 |
+
|
106 |
###Ví dụ 1 : Một trận hỗn chiến đã xảy ra tại trận đấu khúc côn cầu giữa Penguins và Islanders ở Mỹ (image:penguin)
|