-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
test_huggingface.py
380 lines (319 loc) · 13 KB
/
test_huggingface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import time
from unittest.mock import patch
import pandas as pd
from mindsdb_sql import parse_sql
from tests.unit.executor_test_base import BaseExecutorTest
# How to run:
# env PYTHONPATH=./ pytest -vx tests/unit/test_ml_handlers.py
# Warning: a big huggingface models will be downloaded
class TestHuggingface(BaseExecutorTest):
def run_sql(self, sql):
return self.command_executor.execute_command(parse_sql(sql, dialect="mindsdb"))
def hf_test_run(self, mock_handler, model_name, create_sql, predict_sql):
# prepare table
text_spammy = [
"It is the best time to launch the Robot to get more money. https:\\/\\/Gof.bode-roesch.de\\/Gof",
"Start making thousands of dollars every week just using this robot. https:\\/\\/Gof.coronect.de\\/Gof",
]
text_short = ["I want to dance", "Baking is the best"]
text_long = [
"Dance is a performing art form consisting of sequences of movement, either improvised or purposefully selected. This movement has aesthetic and often symbolic value.[nb 1] Dance can be categorized and described by its choreography, by its repertoire of movements, or by its historical period or place of origin.",
"Baking is a method of preparing food that uses dry heat, typically in an oven, but can also be done in hot ashes, or on hot stones. The most common baked item is bread but many other types of foods can be baked. Heat is gradually transferred from the surface of cakes, cookies, and pieces of bread to their center. As heat travels through, it transforms batters and doughs into baked goods and more with a firm dry crust and a softer center. Baking can be combined with grilling to produce a hybrid barbecue variant by using both methods simultaneously, or one after the other. Baking is related to barbecuing because the concept of the masonry oven is similar to that of a smoke pit.",
]
df = pd.DataFrame(data=[text_spammy, text_short, text_long]).T
df.columns = ["text_spammy", "text_short", "text_long"]
self.set_handler(mock_handler, name="pg", tables={"df": df})
# create predictor
ret = self.run_sql(create_sql)
assert ret.error_code is None
# wait
done = False
for attempt in range(900):
ret = self.run_sql(
f"select status from mindsdb.models where name='{model_name}'"
)
if len(ret.data) > 0:
if ret.data[0][0] == "complete":
done = True
break
elif ret.data[0][0] == "error":
break
time.sleep(0.5)
if not done:
raise RuntimeError("predictor not created")
# use predictor
ret = self.command_executor.execute_command(
parse_sql(predict_sql, dialect="mindsdb")
)
assert ret.error_code is None
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_classification_bin(self, mock_handler):
# create predictor
create_sql = """
CREATE PREDICTOR mindsdb.spam_classifier
predict PRED
USING
engine='huggingface',
join_learn_process=true,
task='text-classification',
model_name= "mrm8488/bert-tiny-finetuned-sms-spam-detection",
input_column = 'text_spammy',
labels=['ham','spam']
"""
model_name = "spam_classifier"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.spam_classifier as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
# one line prediction
predict_sql = """
SELECT * from mindsdb.spam_classifier
where text_spammy= 'It is the best time to launch the Robot to get more money. https:\\/\\/Gof.bode-roesch.de\\/Gof'
"""
# use predictor
ret = self.command_executor.execute_command(
parse_sql(predict_sql, dialect="mindsdb")
)
assert ret.error_code is None
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_classification_multy(self, mock_handler):
# create predictor
create_sql = """
CREATE PREDICTOR mindsdb.sentiment_classifier
predict PRED
USING
engine='huggingface',
join_learn_process=true,
task='text-classification',
model_name= "cardiffnlp/twitter-roberta-base-sentiment",
input_column = 'text_short',
labels=['neg','neu','pos']
"""
model_name = "sentiment_classifier"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.sentiment_classifier as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_zero_shot(self, mock_handler):
# create predictor
create_sql = """
CREATE PREDICTOR mindsdb.zero_shot_tcd
predict PREDZS
USING
engine='huggingface',
join_learn_process=true,
task="zero-shot-classification",
model_name= "facebook/bart-large-mnli",
input_column = "text_short",
candidate_labels=['travel', 'cooking', 'dancing']
"""
model_name = "zero_shot_tcd"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.zero_shot_tcd as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_summarization(self, mock_handler):
# create predictor
create_sql = """
CREATE MODEL mindsdb.hf_summarization
PREDICT summary
USING
engine = 'huggingface',
task = 'summarization',
model_name = 'sshleifer/distilbart-xsum-12-1',
input_column = 'text_long',
min_output_length = 5,
max_output_length = 20;
"""
model_name = "hf_summarization"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.hf_summarization as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_translation(self, mock_handler):
# create predictor
create_sql = """
CREATE PREDICTOR mindsdb.translator_en_fr
predict TRANSLATION
USING
engine='huggingface',
join_learn_process=true,
task = "translation",
model_name = "t5-base",
input_column = "text_short",
lang_input = "en",
lang_output = "fr"
"""
model_name = "translator_en_fr"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.translator_en_fr as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_text2text(self, mock_handler):
# create predictor
create_sql = """
CREATE MODEL mindsdb.text_generator
predict PREDICTION
USING
engine='huggingface',
join_learn_process=true,
task = "text2text-generation",
model_name = "google/flan-t5-base",
input_column = 'comment'
"""
model_name = "text_generator"
predict_sql = """
SELECT * FROM text_generator
WHERE comment='Question: Why did the chicken cross the road?'
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_text_classification_finetune(self, mock_handler):
create_sql = """
CREATE PREDICTOR mindsdb.spam_classifier
predict PRED
USING
engine='huggingface',
join_learn_process=true,
task='text-classification',
model_name= "mrm8488/bert-tiny-finetuned-sms-spam-detection",
input_column = 'text_spammy',
labels=['ham','spam']
"""
model_name = "spam_classifier"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.spam_classifier as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
# one line prediction
predict_sql = """
SELECT * from mindsdb.spam_classifier
where text_spammy= 'It is the best time to launch the Robot to get more money. https:\\/\\/Gof.bode-roesch.de\\/Gof'
"""
# use predictor
ret = self.command_executor.execute_command(
parse_sql(predict_sql, dialect="mindsdb")
)
assert ret.error_code is None
# fine tune
fine_tune_sql = """
FINETUNE mindsdb.spam_classifier
FROM pg (
SELECT label as PRED, text as text_spammy FROM df WHERE PRED <= 1
)
USING
tokenizer_from = 'bert-base-uncased';
"""
ret = self.command_executor.execute_command(
parse_sql(fine_tune_sql, dialect="mindsdb")
)
assert ret.error_code is None
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_zero_shot_classification_finetune(self, mock_handler):
# create predictor
create_sql = """
CREATE PREDICTOR mindsdb.zero_shot_tcd
predict PREDZS
USING
engine='huggingface',
join_learn_process=true,
task="zero-shot-classification",
model_name= "facebook/bart-large-mnli",
input_column = "text_short",
candidate_labels=['travel', 'cooking', 'dancing']
"""
model_name = "zero_shot_tcd"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.zero_shot_tcd as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
# fine tune
fine_tune_sql = """
FINETUNE mindsdb.zero_shot_tcd
FROM pg (SELECT label, hypothesis FROM df);
"""
ret = self.command_executor.execute_command(
parse_sql(fine_tune_sql, dialect="mindsdb")
)
assert ret.error_code is None
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_translation_finetune(self, mock_handler):
# create predictor
create_sql = """
CREATE PREDICTOR mindsdb.translator_en_fr
predict TRANSLATION
USING
engine='huggingface',
join_learn_process=true,
task = "translation",
model_name = "t5-base",
input_column = "text_short",
lang_input = "en",
lang_output = "fr"
"""
model_name = "translator_en_fr"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.translator_en_fr as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
# fine tune
fine_tune_sql = """
FINETUNE mindsdb.translator_en_fr
FROM pg (SELECT text_long, transl FROM df);
"""
ret = self.command_executor.execute_command(
parse_sql(fine_tune_sql, dialect="mindsdb")
)
assert ret.error_code is None
@patch("mindsdb.integrations.handlers.postgres_handler.Handler")
def test_hf_summarization_finetune(self, mock_handler):
# create predictor
create_sql = """
CREATE MODEL mindsdb.hf_summarization
PREDICT summary
USING
engine = 'huggingface',
task = 'summarization',
model_name = 'sshleifer/distilbart-xsum-12-1',
input_column = 'text_long',
min_output_length = 5,
max_output_length = 20;
"""
model_name = "hf_summarization"
predict_sql = """
SELECT h.*
FROM pg.df as t
JOIN mindsdb.hf_summarization as h
"""
self.hf_test_run(mock_handler, model_name, create_sql, predict_sql)
# fine tune
fine_tune_sql = """
FINETUNE mindsdb.hf_summarization
FROM pg (
SELECT text, summary FROM df
);
"""
ret = self.command_executor.execute_command(
parse_sql(fine_tune_sql, dialect="mindsdb")
)
assert ret.error_code is None