-
Notifications
You must be signed in to change notification settings - Fork 703
/
T5TestSpec.scala
591 lines (496 loc) · 28.2 KB
/
T5TestSpec.scala
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* Copyright 2017-2022 John Snow Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.johnsnowlabs.nlp.annotators.seq2seq
import com.johnsnowlabs.nlp.annotator.SentenceDetectorDLModel
import com.johnsnowlabs.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.util.io.ResourceHelper
import com.johnsnowlabs.tags.SlowTest
import com.johnsnowlabs.util.Benchmark
import org.apache.spark.ml.Pipeline
import org.apache.spark.sql.functions.col
import org.scalatest.flatspec.AnyFlatSpec
class T5TestSpec extends AnyFlatSpec {
"google/t5-small-ssm-nq " should "run SparkNLP pipeline" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(1, "Which is the capital of France? Who was the first president of USA?"),
(1, "Which is the capital of Bulgaria ?"),
(2, "Who is Donald Trump?")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val sentenceDetector = SentenceDetectorDLModel
.pretrained()
.setInputCols(Array("documents"))
.setOutputCol("questions")
val t5 = T5Transformer
.pretrained("google_t5_small_ssm_nq")
.setInputCols(Array("questions"))
.setOutputCol("answers")
val pipeline = new Pipeline().setStages(Array(documentAssembler, sentenceDetector, t5))
val model = pipeline.fit(testData)
val results = model.transform(testData)
results.select("questions.result", "answers.result").show(truncate = false)
}
"t5-small" should "run SparkNLP pipeline with maxLength=200 " taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setMaxOutputLength(200)
.setOutputCol("summaries")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results = model.transform(testData)
results.select("summaries.result").show(truncate = false)
val dataframe = results.select("summaries.result").collect()
val result = dataframe.toSeq.head.getAs[Seq[String]](0).head
assert(
result == "the lamb fillet of fat and cut into slices the thickness of a chop . cut the kidneys in half and snip out the white core .")
}
"t5-small" should "run SparkNLP pipeline with doSample=true " taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setDoSample(true)
.setMaxOutputLength(50)
.setOutputCol("summaries")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val dataframe1 = model
.transform(testData)
.select("summaries.result")
.collect()
.toSeq
.head
.getAs[Seq[String]](0)
.head
println(dataframe1)
val dataframe2 = model
.transform(testData)
.select("summaries.result")
.collect()
.toSeq
.head
.getAs[Seq[String]](0)
.head
println(dataframe2)
assert(!dataframe1.equals(dataframe2))
}
"t5-small" should "run SparkNLP pipeline with doSample=true and fixed random seed " taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setDoSample(true)
.setMaxOutputLength(50)
.setRandomSeed(10L)
.setOutputCol("summaries")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val dataframe1 = model
.transform(testData)
.select("summaries.result")
.collect()
.toSeq
.head
.getAs[Seq[String]](0)
.head
println(dataframe1)
val dataframe2 = model
.transform(testData)
.select("summaries.result")
.collect()
.toSeq
.head
.getAs[Seq[String]](0)
.head
println(dataframe2)
assert(dataframe1.equals(dataframe2))
}
"t5-small" should "run SparkNLP pipeline with doSample=true, fixed random seed deactivated topK" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setOutputCol("summaries")
.setDoSample(true)
.setRandomSeed(10L)
.setMaxOutputLength(20)
.setTopK(0)
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results1 = model.transform(testData)
val dataframe1 =
results1.select("summaries.result").collect().toSeq.head.getAs[Seq[String]](0).head
assert(
dataframe1 == "cook 2 months uncovered and uncovered for 15-20 mins with more butter . heat over medium")
}
"t5-small" should "run SparkNLP pipeline with temperature to decrease the sensitivity to low probability candidates" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setOutputCol("summaries")
.setDoSample(true)
.setRandomSeed(10L)
.setMaxOutputLength(50)
.setTemperature(0.7)
.setTopK(50)
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results1 = model.transform(testData)
val dataframe1 =
results1.select("summaries.result").collect().toSeq.head.getAs[Seq[String]](0).head
println(dataframe1)
assert(
"dripping or 2 tablespoons of vegetable oil set aside, stirring constantly . add the onions and fry for about 10 minutes until softened ." == dataframe1)
}
"t5-small" should "run SparkNLP pipeline with doSample and TopP" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setOutputCol("summaries")
.setDoSample(true)
.setRandomSeed(10L)
.setMaxOutputLength(50)
.setTopP(0.7)
.setTopK(0)
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results1 = model.transform(testData)
val dataframe1 =
results1.select("summaries.result").collect().toSeq.head.getAs[Seq[String]](0).head
println(dataframe1)
assert(
"the lamb fillet is cut into slices the thickness of a chop . add the kidneys and cook for 1-2 minutes, turning once, until browned ." == dataframe1)
}
"t5-small" should "run SparkNLP pipeline with repetitionPenalty" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(
Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setOutputCol("summaries")
.setDoSample(false)
.setMaxOutputLength(50)
.setRepetitionPenalty(2)
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results1 = model.transform(testData)
val dataframe1 =
results1.select("summaries.result").collect().toSeq.head.getAs[Seq[String]](0).head
println(dataframe1)
assert(
dataframe1 == "the lamb fillet of fat and cut into slices the thickness of a chop . heat up to 220°C/fan140°C/gas 7 and cook for another 2 hours - uncover, and brush the potatoes with more")
}
"t5-small" should "run SparkNLP pipeline and ignore a token" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden."),
(
1,
"Donald John Trump (born June 14, 1946) is the 45th and current president of the United States. Before " +
"entering politics, he was a businessman and television personality. Born and raised in Queens, New York " +
"City, Trump attended Fordham University for two years and received a bachelor's degree in economics from the " +
"Wharton School of the University of Pennsylvania. He became president of his father Fred Trump's real " +
"estate business in 1971, renamed it The Trump Organization, and expanded its operations to building or " +
"renovating skyscrapers, hotels, casinos, and golf courses. Trump later started various side ventures," +
" mostly by licensing his name. Trump and his businesses have been involved in more than 4,000 state and" +
" federal legal actions, including six bankruptcies. He owned the Miss Universe brand of beauty pageants " +
"from 1996 to 2015, and produced and hosted the reality television series The Apprentice from 2004 to 2015.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setMaxOutputLength(200)
.setIgnoreTokenIds(Array(12065)) // ignore token "vegetable"
.setOutputCol("summaries")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results = model.transform(testData)
Benchmark.time("Time to save pipeline the first time") {
results.select("summaries.result").write.mode("overwrite").save("./tmp_t5_pipeline")
}
Benchmark.time("Time to save pipeline the second time") {
results.select("summaries.result").write.mode("overwrite").save("./tmp_t5_pipeline")
}
results.select("summaries.result").show(truncate = false)
assert(
results
.selectExpr("explode(summaries) AS summary")
.where(col("summary.result").contains(" vegetable "))
.count() == 0,
"should not include ignored tokens")
}
"t5-small" should "run SparkNLP pipeline for translation" taggedAs SlowTest in {
val testData = ResourceHelper.spark
.createDataFrame(Seq(
(
1,
"Preheat the oven to 220°C/ fan200°C/gas 7. Trim the lamb fillet of fat and cut into slices the thickness" +
" of a chop. Cut the kidneys in half and snip out the white core. Melt a knob of dripping or 2 tablespoons " +
"of vegetable oil in a heavy large pan. Fry the lamb fillet in batches for 3-4 minutes, turning once, until " +
"browned. Set aside. Fry the kidneys and cook for 1-2 minutes, turning once, until browned. Set aside." +
"Wipe the pan with kitchen paper, then add the butter. Add the onions and fry for about 10 minutes until " +
"softened. Sprinkle in the flour and stir well for 1 minute. Gradually pour in the stock, stirring all the " +
"time to avoid lumps. Add the herbs. Stir the lamb and kidneys into the onions. Season well. Transfer to a" +
" large 2.5-litre casserole. Slice the peeled potatoes thinly and arrange on top in overlapping rows. Brush " +
"with melted butter and season. Cover and bake for 30 minutes. Reduce the oven temperature to 160°C" +
"/fan140°C/gas 3 and cook for a further 2 hours. Then increase the oven temperature to 200°C/ fan180°C/gas 6," +
" uncover, and brush the potatoes with more butter. Cook uncovered for 15-20 minutes, or until golden."),
(
1,
"Donald John Trump (born June 14, 1946) is the 45th and current president of the United States. Before " +
"entering politics, he was a businessman and television personality. Born and raised in Queens, New York " +
"City, Trump attended Fordham University for two years and received a bachelor's degree in economics from the " +
"Wharton School of the University of Pennsylvania. He became president of his father Fred Trump's real " +
"estate business in 1971, renamed it The Trump Organization, and expanded its operations to building or " +
"renovating skyscrapers, hotels, casinos, and golf courses. Trump later started various side ventures," +
" mostly by licensing his name. Trump and his businesses have been involved in more than 4,000 state and" +
" federal legal actions, including six bankruptcies. He owned the Miss Universe brand of beauty pageants " +
"from 1996 to 2015, and produced and hosted the reality television series The Apprentice from 2004 to 2015.")))
.toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("summarize:")
.setInputCols(Array("documents"))
.setMaxOutputLength(200)
.setIgnoreTokenIds(Array(12065)) // ignore token "vegetable"
.setOutputCol("summaries")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results = model.transform(testData).cache()
Benchmark.time("Time to save pipeline the first time") {
results.select("summaries.result").write.mode("overwrite").save("./tmp_t5_pipeline")
}
Benchmark.time("Time to save pipeline the second time") {
results.select("summaries.result").write.mode("overwrite").save("./tmp_t5_pipeline")
}
assert(
results
.selectExpr("explode(summaries) AS summary")
.where(col("summary.result").contains(" vegetable "))
.count() == 0,
"should not include ignored tokens")
}
"Pretrained models" should "able to change task" taggedAs SlowTest in {
val testData =
ResourceHelper.spark.createDataFrame(Seq((1, "That is good."))).toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("translate:")
.setInputCols(Array("documents"))
.setMaxOutputLength(200)
.setOutputCol("translations")
t5.setTask("translate English to German:")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results = model.transform(testData).cache()
val collected = results.selectExpr("explode(translations.result)").collect().head.getString(0)
val expected = "Das ist gut."
assert(collected == expected, "translation should be correct")
}
"Pretrained models" should "be saved and loaded correctly" taggedAs SlowTest in {
val testData =
ResourceHelper.spark.createDataFrame(Seq((1, "That is good."))).toDF("id", "text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val t5 = T5Transformer
.pretrained("t5_small")
.setTask("translate:")
.setInputCols(Array("documents"))
.setMaxOutputLength(200)
.setOutputCol("translations")
t5.setTask("translate English to German:")
val pipeline = new Pipeline().setStages(Array(documentAssembler, t5))
val model = pipeline.fit(testData)
val results = model.transform(testData).cache()
results.selectExpr("explode(translations.result)").collect().head.getString(0)
model.stages.last.asInstanceOf[T5Transformer].write.overwrite().save("./tmp_t5_model")
val t5Loaded = T5Transformer.load("./tmp_t5_model")
val pipeline2 = new Pipeline().setStages(Array(documentAssembler, t5Loaded))
val model2 = pipeline2.fit(testData)
val results2 = model2.transform(testData)
results2.select("documents.result", "translations.result").show(truncate = false)
}
}