Pradeep Kumar commited on
Commit
32e8749
·
verified ·
1 Parent(s): 4ff849e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import tensorflow as tf
3
+ import tensorflow_hub as hub
4
+ import tf_keras as keras
5
+ import pandas as pd
6
+ from tensorflow.keras.models import load_model
7
+ from official.nlp.data import classifier_data_lib
8
+ from official.nlp.tools import tokenization
9
+ import joblib
10
+
11
+ model = load_model('best_model.h5', custom_objects={'KerasLayer': hub.KerasLayer})
12
+
13
+
14
+ vocab_file = model.resolved_object.vocab_file.asset_path.numpy()
15
+ do_lower_case = model.resolved_object.do_lower_case.numpy()
16
+ tokenizer = tokenization.FullTokenizer(vocab_file,do_lower_case)
17
+
18
+ # Parameters
19
+ max_seq_length = 128
20
+ label_list = 424
21
+ dummy_label = 100
22
+
23
+
24
+ # Define a function to preprocess the new data
25
+ def get_feature_new(text, max_seq_length, tokenizer, dummy_label):
26
+ example = classifier_data_lib.InputExample(guid=None,
27
+ text_a=text.numpy().decode('utf-8'),
28
+ text_b=None,
29
+ label=dummy_label) # Use a valid dummy label
30
+ feature = classifier_data_lib.convert_single_example(0, example, label_list, max_seq_length, tokenizer)
31
+ return feature.input_ids, feature.input_mask, feature.segment_ids
32
+
33
+ def get_feature_map_new(text):
34
+ input_ids, input_mask, segment_ids = tf.py_function(
35
+ lambda text: get_feature_new(text, max_seq_length, tokenizer, dummy_label),
36
+ inp=[text],
37
+ Tout=[tf.int32, tf.int32, tf.int32]
38
+ )
39
+ input_ids.set_shape([max_seq_length])
40
+ input_mask.set_shape([max_seq_length])
41
+ segment_ids.set_shape([max_seq_length])
42
+
43
+ x = {'input_word_ids': input_ids,
44
+ 'input_mask': input_mask,
45
+ 'input_type_ids': segment_ids}
46
+
47
+ return x
48
+
49
+ def preprocess_new_data(texts):
50
+ dataset = tf.data.Dataset.from_tensor_slices((texts,))
51
+ dataset = dataset.map(get_feature_map_new,
52
+ num_parallel_calls=tf.data.experimental.AUTOTUNE)
53
+ dataset = dataset.batch(32, drop_remainder=False)
54
+ dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
55
+
56
+ return dataset