Linhz commited on
Commit
548e229
·
verified ·
1 Parent(s): de512d1

Upload 3 files

Browse files
Model/NER/VLSP2016/Load_model.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import RobertaConfig, AutoConfig
2
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
3
+ from Model.NER.VLSP2021.Ner_CRF import PhoBertCrf,PhoBertSoftmax,PhoBertLstmCrf
4
+ from Model.NER.VLSP2021.Predict_Ner import ViTagger
5
+ import torch
6
+ from spacy import displacy
7
+ import re
8
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
9
+ MODEL_MAPPING = {
10
+ 'vinai/phobert-base': {
11
+ 'softmax': PhoBertSoftmax,
12
+ 'crf': PhoBertCrf,
13
+ 'lstm_crf': PhoBertLstmCrf
14
+ },
15
+ }
16
+ if device == 'cpu':
17
+ checkpoint_data = torch.load('E:/demo_datn/pythonProject1/Model/NER/VLSP2016/best_model.pt', map_location='cpu')
18
+ else:
19
+ checkpoint_data = torch.load('E:/demo_datn/pythonProject1/Model/NER/VLSP2016/best_model.pt')
20
+
21
+ configs = checkpoint_data['args']
22
+ print(configs.model_name_or_path)
23
+ tokenizer = AutoTokenizer.from_pretrained(configs.model_name_or_path)
24
+ model_clss = MODEL_MAPPING[configs.model_name_or_path][configs.model_arch]
25
+ config = AutoConfig.from_pretrained(configs.model_name_or_path,
26
+ num_labels=len(checkpoint_data['classes']),
27
+ finetuning_task=configs.task)
28
+ model = model_clss(config=config)
29
+ model.resize_token_embeddings(len(tokenizer))
30
+ model.to(device)
31
+ model.load_state_dict(checkpoint_data['model'],strict=False)
32
+ print(model)
33
+
34
+
Model/NER/VLSP2016/best_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:13751c02dcf0158d003899ff0aa11b0489772446f41e0ab9e57c3790756119ec
3
+ size 537745865
Model/NER/app_NER.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from spacy import displacy
3
+ from Model.NER.VLSP2021.Predict_Ner import ViTagger
4
+ import re
5
+
6
+ import torch
7
+
8
+
9
+
10
+
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+
13
+ def process_text(text):
14
+ # Loại bỏ dấu cách thừa và dấu cách ở đầu và cuối văn bản
15
+ processed_text = re.sub(r'\s+', ' ', text.strip())
16
+ return processed_text
17
+
18
+ def show_ner():
19
+
20
+ st.sidebar.title('Datasets')
21
+ dataset = st.sidebar.selectbox("Datasets", ("VLSP2016", "VLSP2021"))
22
+ st.header("NER")
23
+ text = st.text_area("Enter your text for NER:", height=300)
24
+ text = process_text(text)
25
+ if st.button("Process NER"):
26
+ if dataset == "VLSP2021":
27
+ tagger = ViTagger(model_path='E:/demo_datn/pythonProject1/Model/NER/VLSP2021/best_model.pt')
28
+ a = text
29
+ b = tagger.extract_entity_doc(a)
30
+
31
+ # Danh sách các từ và nhãn NER
32
+ # words_and_labels = [('Dự', 'O'), ('báo', 'O'), ('thời', 'O'), ('tiết', 'O'), ('2 ngày', 'DATETIME-DATERANGE'), ('tới', 'O'), ('Nam Bộ', 'LOCATION-GPE'), ('có', 'O'), ('mưa', 'O'), ('dông', 'O')]
33
+ words_and_labels = b
34
+ # Tạo danh sách từ
35
+ words = [word for word, _ in words_and_labels]
36
+ # Tạo danh sách thực thể và nhãn cho mỗi từ, loại bỏ nhãn 'O'
37
+ entities = [{'start': sum(len(word) + 1 for word, _ in words_and_labels[:i]),
38
+ 'end': sum(len(word) + 1 for word, _ in words_and_labels[:i + 1]), 'label': label} for
39
+ i, (word, label)
40
+ in enumerate(words_and_labels) if label != 'O']
41
+ # print(entities)
42
+
43
+ # Render the visualization without color for 'O' labels
44
+ html = displacy.render(
45
+ {"text": " ".join(words), "ents": entities, "title": None},
46
+ style="ent",
47
+ manual=True,
48
+ options={"colors": {"DATETIME-DATERANGE": "#66c2ff",
49
+ "LOCATION-GPE": "#ffcc99",
50
+ "O": None, # Màu cho nhãn 'O'
51
+ "QUANTITY-NUM": "#ffdf80",
52
+ "EVENT-CUL": "#bfbfbf",
53
+ "DATETIME": "#80ff80",
54
+ "PERSONTYPE": "#ff80ff",
55
+ "PERSON": "#bf80ff",
56
+ "QUANTITY-PER": "#80cccc",
57
+ "ORGANIZATION": "#ff6666",
58
+ "LOCATION-GEO": "#66cc66",
59
+ "LOCATION-STRUC": "#cccc66",
60
+ "PRODUCT-COM": "#ffff66",
61
+ "DATETIME-DATE": "#66cccc",
62
+ "QUANTITY-DIM": "#6666ff",
63
+ "PRODUCT": "#cc6666",
64
+ "QUANTITY": "#6666cc",
65
+ "DATETIME-DURATION": "#9966ff",
66
+ "QUANTITY-CUR": "#ff9966",
67
+ "DATETIME-TIME": "#cdbf93",
68
+ "QUANTITY-TEM": "#cc9966",
69
+ "DATETIME-TIMERANGE": "#cc8566",
70
+ "EVENT-GAMESHOW": "#8c8c5a",
71
+ "QUANTITY-AGE": "#70db70",
72
+ "QUANTITY-ORD": "#e699ff",
73
+ "PRODUCT-LEGAL": "#806699",
74
+ "LOCATION": "#993366",
75
+ "ORGANIZATION-MED": "#339933",
76
+ "URL": "#ff4d4d",
77
+ "PHONENUMBER": "#99cc99",
78
+ "ORGANIZATION-SPORTS": "#6666ff",
79
+ "EVENT-SPORT": "#ffff80",
80
+ "SKILL": "#b38f66",
81
+ "EVENT-NATURAL": "#ff9966",
82
+ "ADDRESS": "#cc9966",
83
+ "IP": "#b38f66",
84
+ "EMAIL": "#cc8566",
85
+ "ORGANIZATION-STOCK": "#666633",
86
+ "DATETIME-SET": "#70db70",
87
+ "PRODUCT-AWARD": "#e699ff",
88
+ "MISCELLANEOUS": "#806699",
89
+ "LOCATION-GPE-GEO": "#99ffff"}}
90
+ )
91
+ # print(html)
92
+ st.markdown(html, unsafe_allow_html=True)
93
+ elif dataset == "VLSP2016":
94
+ tagger = ViTagger(model_path='E:/demo_datn/pythonProject1/Model/NER/VLSP2016/best_model.pt')
95
+ a = text
96
+ b = tagger.extract_entity_doc(a)
97
+
98
+ # Danh sách các từ và nhãn NER
99
+ # words_and_labels = [('Dự', 'O'), ('báo', 'O'), ('thời', 'O'), ('tiết', 'O'), ('2 ngày', 'DATETIME-DATERANGE'), ('tới', 'O'), ('Nam Bộ', 'LOCATION-GPE'), ('có', 'O'), ('mưa', 'O'), ('dông', 'O')]
100
+ words_and_labels = b
101
+ # Tạo danh sách từ
102
+ words = [word for word, _ in words_and_labels]
103
+ # Tạo danh sách thực thể và nhãn cho mỗi từ, loại bỏ nhãn 'O'
104
+ entities = [{'start': sum(len(word) + 1 for word, _ in words_and_labels[:i]),
105
+ 'end': sum(len(word) + 1 for word, _ in words_and_labels[:i + 1]), 'label': label} for
106
+ i, (word, label)
107
+ in enumerate(words_and_labels) if label != 'O']
108
+ # print(entities)
109
+
110
+ # Render the visualization without color for 'O' labels
111
+ html = displacy.render(
112
+ {"text": " ".join(words), "ents": entities, "title": None},
113
+ style="ent",
114
+ manual=True,
115
+ options={"colors": {"MISC": "#806699",
116
+ "ORG": "#ff6666",
117
+ "LOC": "#66cc66",
118
+ "PER": "#bf80ff",
119
+ "O": None}}
120
+ )
121
+ # print(html)
122
+ st.markdown(html, unsafe_allow_html=True)
123
+
124
+
125
+
126
+
127
+
128
+ # Sử dụng widget st.html để hiển thị HTML
129
+
130
+ # Hiển thị văn bản đã nhập
131
+ # st.write("Văn bản đã nhập:", text)