Spaces:
Runtime error
Runtime error
Create test.py
Browse files
test.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import ConvBertTokenizer, TFConvBertModel
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras import backend as K
|
5 |
+
from tensorflow.keras import regularizers
|
6 |
+
import tensorflow as tf
|
7 |
+
from tensorflow.keras.layers import *
|
8 |
+
from tensorflow.keras.models import *
|
9 |
+
from transformers import *
|
10 |
+
import os
|
11 |
+
from text_cleaning import clean_text
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
|
15 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
16 |
+
|
17 |
+
# gpu_number = 1 #### GPU number
|
18 |
+
# gpus = tf.config.experimental.list_physical_devices('GPU')
|
19 |
+
# if gpus:
|
20 |
+
# tf.config.experimental.set_visible_devices(gpus[gpu_number], 'GPU')
|
21 |
+
# logical_gpus = tf.config.experimental.list_logical_devices('GPU')
|
22 |
+
# print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
|
23 |
+
|
24 |
+
MAX_LENGTH = 32
|
25 |
+
BATCH_SIZE = 256
|
26 |
+
|
27 |
+
model_name = 'dbmdz/convbert-base-turkish-mc4-uncased'
|
28 |
+
tokenizer = ConvBertTokenizer.from_pretrained(model_name)
|
29 |
+
CUDA_VISIBLE_DEVICES=4
|
30 |
+
|
31 |
+
label_to_name = {0:"INSULT",
|
32 |
+
1:"OTHER",
|
33 |
+
2:"PROFANITY",
|
34 |
+
3:"RACIST",
|
35 |
+
4:"SEXIST"}
|
36 |
+
|
37 |
+
|
38 |
+
custom_object = {"TFConvBertModel": TFConvBertModel, "K":K}
|
39 |
+
|
40 |
+
second_model_1_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="2inci_model_mc4_emir_aug_data_dropout01_0.h5")
|
41 |
+
second_model_1 = tf.keras.models.load_model(second_model_1_path, custom_objects=custom_object, compile=False)
|
42 |
+
|
43 |
+
second_model_2_model_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="2inci_model_mc4_emir_aug_data_dropout01_1.h5")
|
44 |
+
second_model_2 = tf.keras.models.load_model(second_model_2_model_path, custom_objects=custom_object, compile=False)
|
45 |
+
|
46 |
+
second_model_3_model_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="2inci_model_mc4_emir_aug_data_dropout01_2.h5")
|
47 |
+
second_model_3 = tf.keras.models.load_model(second_model_3_model_path, custom_objects=custom_object, compile=False)
|
48 |
+
|
49 |
+
second_model_4_model_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="2inci_model_mc4_emir_aug_data_dropout01_3.h5")
|
50 |
+
second_model_4 = tf.keras.models.load_model(second_model_4_model_path, custom_objects=custom_object, compile=False)
|
51 |
+
|
52 |
+
second_model_5_model_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="2inci_model_mc4_emir_aug_data_dropout01_4.h5")
|
53 |
+
second_model_5 = tf.keras.models.load_model(second_model_5_model_path, custom_objects=custom_object, compile=False)
|
54 |
+
|
55 |
+
third_model_1_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="3uncu_model_mc4_emir_aug_data_0.h5")
|
56 |
+
third_model_1 = tf.keras.models.load_model(third_model_1_path, custom_objects=custom_object, compile=False)
|
57 |
+
|
58 |
+
third_model_2_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="3uncu_model_mc4_emir_aug_data_1.h5")
|
59 |
+
third_model_2 = tf.keras.models.load_model(third_model_2_path, custom_objects=custom_object, compile=False)
|
60 |
+
|
61 |
+
third_model_3_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="3uncu_model_mc4_emir_aug_data_2.h5")
|
62 |
+
third_model_3 = tf.keras.models.load_model(third_model_3_path, custom_objects=custom_object, compile=False)
|
63 |
+
|
64 |
+
third_model_4_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="3uncu_model_mc4_emir_aug_data_3.h5")
|
65 |
+
third_model_4 = tf.keras.models.load_model(third_model_4_path, custom_objects=custom_object, compile=False)
|
66 |
+
|
67 |
+
third_model_5_path = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="3uncu_model_mc4_emir_aug_data_4.h5")
|
68 |
+
third_model_5 = tf.keras.models.load_model(third_model_5_path, custom_objects=custom_object, compile=False)
|
69 |
+
|
70 |
+
model_path1 = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="model0.h5")
|
71 |
+
first_model_1 = tf.keras.models.load_model(model_path1, custom_objects=custom_object, compile=False)
|
72 |
+
|
73 |
+
model_path2 = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="model1.h5")
|
74 |
+
first_model_2 = tf.keras.models.load_model(model_path2, custom_objects=custom_object, compile=False)
|
75 |
+
|
76 |
+
model_path3 = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="model2.h5")
|
77 |
+
first_model_3 = tf.keras.models.load_model(model_path3, custom_objects=custom_object, compile=False)
|
78 |
+
|
79 |
+
model_path4 = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="model3.h5")
|
80 |
+
first_model_4 = tf.keras.models.load_model(model_path4, custom_objects=custom_object, compile=False)
|
81 |
+
|
82 |
+
model_path5 = hf_hub_download(repo_id="emirkocak/tekno_deneme_models", filename="model4.h5")
|
83 |
+
first_model_5 = tf.keras.models.load_model(model_path5, custom_objects=custom_object, compile=False)
|
84 |
+
|
85 |
+
def bert_encode(data):
|
86 |
+
tokens = tokenizer.batch_encode_plus(data, max_length=MAX_LENGTH, padding='max_length', truncation=True)
|
87 |
+
|
88 |
+
return tf.constant(tokens['input_ids'])
|
89 |
+
|
90 |
+
|
91 |
+
def test_predict(text):
|
92 |
+
|
93 |
+
test_encoded = bert_encode(text)
|
94 |
+
test_dataset = (
|
95 |
+
tf.data.Dataset
|
96 |
+
.from_tensor_slices((test_encoded))
|
97 |
+
.batch(BATCH_SIZE))
|
98 |
+
|
99 |
+
y_kfold_second = 0
|
100 |
+
y_kfold_third = 0
|
101 |
+
y_kfold_first = 0
|
102 |
+
|
103 |
+
for model in [second_model_1, second_model_2, second_model_3, second_model_4, second_model_5]:
|
104 |
+
y_kfold_second += model.predict(test_dataset)
|
105 |
+
|
106 |
+
for model in [third_model_1, third_model_2, third_model_3, third_model_4, third_model_5]:
|
107 |
+
y_kfold_third += model.predict(test_dataset)
|
108 |
+
|
109 |
+
for model in [first_model_1, first_model_2, first_model_3, first_model_4, first_model_5]:
|
110 |
+
y_kfold_first += model.predict(test_dataset)
|
111 |
+
|
112 |
+
y_pred_all = 0.39 * y_kfold_first / 5 + 0.38 * y_kfold_second / 5 + 0.23 * y_kfold_third / 5
|
113 |
+
|
114 |
+
# y_pred_all = y_kfold_first
|
115 |
+
preds = np.argmax(y_pred_all, 1)
|
116 |
+
|
117 |
+
preds_names = [label_to_name[pred] for pred in preds]
|
118 |
+
|
119 |
+
return preds_names
|