sun-tana commited on
Commit
74ed80e
β€’
1 Parent(s): 3dfbd2f

add model weight

Browse files
Files changed (1) hide show
  1. app.py +110 -3
app.py CHANGED
@@ -1,11 +1,118 @@
1
  import gradio as gr
2
 
3
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- pipe = pipeline("translation", model="sun-tana/residual_text_t1_m1")
6
 
7
  def predict(text):
8
- return pipe(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  iface = gr.Interface(
11
  fn=predict,
 
1
  import gradio as gr
2
 
3
+ from pythainlp import word_tokenize
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from tensorflow import keras
7
+ from tensorflow.keras.preprocessing.text import Tokenizer
8
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
9
+ from tensorflow.keras.models import Model
10
+ from tensorflow.keras.layers import Input, Embedding, Conv1D, MaxPooling1D, Dense, Flatten, Concatenate, Dropout, Dot, Activation, Reshape, Permute, Multiply
11
+ from keras import backend as K
12
+ import pandas as pd
13
+ from transformers import TFAutoModel, AutoTokenizer
14
+ from sklearn.model_selection import train_test_split
15
+
16
+ # load the tokenizer and transformer model
17
+ tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base",max_length=60) #xlm-roberta-base bert-base-multilingual-cased
18
+ transformer_model = TFAutoModel.from_pretrained("xlm-roberta-base") #philschmid/tiny-bert-sst2-distilled
19
+ max_seq_length = 32
20
+
21
+ def create_model():
22
+ # input_layer = Input(shape=(input_ids.shape[1],))
23
+ # embedding_layer = Embedding(max_seq_length+ 1, 200, input_length=max_seq_length)(input_layer)
24
+
25
+ inputs = tf.keras.layers.Input(shape=(input_ids.shape[1],), dtype=tf.int32)
26
+ embedding_layer = transformer_model(inputs)[0]
27
+
28
+ flatten_layer = Flatten()(embedding_layer)
29
+ x1 = Dense(64, activation='relu')(flatten_layer)
30
+ x1 = Dense(32, activation='relu')(x1)
31
+ x1 = Dense(16, activation='relu')(x1)
32
+
33
+
34
+ x2 = Dense(64, activation='relu')(flatten_layer)
35
+ x2 = Dense(32, activation='relu')(x2)
36
+ x2 = Dense(16, activation='relu')(x2)
37
+
38
+ x3 = Dense(64, activation='relu')(flatten_layer)
39
+ x3 = Dense(32, activation='relu')(x3)
40
+ x3 = Dense(16, activation='relu')(x3)
41
+
42
+ x4 = Dense(64, activation='relu')(flatten_layer)
43
+ x4 = Dense(32, activation='relu')(x4)
44
+ x4 = Dense(16, activation='relu')(x4)
45
+
46
+
47
+ x5 = Dense(64, activation='relu')(flatten_layer)
48
+ x5 = Dense(32, activation='relu')(x5)
49
+ x5 = Dense(16, activation='relu')(x5)
50
+
51
+ x6 = Dense(512, activation='relu')(flatten_layer)
52
+ x6 = Dense(256, activation='relu')(x6)
53
+ x6 = Dense(128, activation='relu')(x6)
54
+
55
+ x7 = Dense(128, activation='relu')(flatten_layer)
56
+ x7 = Dense(64, activation='relu')(x7)
57
+ x7 = Dense(32, activation='relu')(x7)
58
+
59
+ x8 = Dense(256, activation='relu')(flatten_layer)
60
+ x8 = Dense(128, activation='relu')(x8)
61
+ x8 = Dense(64, activation='relu')(x8)
62
+
63
+
64
+ output_layer1 = Dense(1, activation='sigmoid', name='output1')(x1)
65
+ output_layer2 = Dense(1, activation='sigmoid', name='output2')(x2)
66
+ output_layer3 = Dense(1, activation='sigmoid', name='output3')(x3)
67
+ output_layer4 = Dense(1, activation='sigmoid', name='output4')(x4)
68
+ output_layer5 = Dense(1, activation='sigmoid', name='output5')(x5)
69
+ output_layer6 = Dense(num_class_label_6, activation='softmax', name='output6')(x6)
70
+ output_layer7 = Dense(num_class_label_7, activation='softmax', name='output7')(x7)
71
+ output_layer8 = Dense(num_class_label_8, activation='softmax', name='output8')(x8)
72
+
73
+ model = Model(inputs=inputs , outputs=[output_layer1, output_layer2, output_layer3,output_layer4,output_layer5,output_layer6,output_layer7,output_layer8])
74
+ model.load_weights("t1_m1.h5")
75
+ return model
76
+
77
+
78
+ model =create_model()
79
+
80
 
 
81
 
82
  def predict(text):
83
+ test_texts = [text]
84
+ spilt_thai_text = [word_tokenize(x) for x in test_texts]
85
+ new_input_ids = tokenizer(spilt_thai_text, padding=True, truncation=True, return_tensors="tf",is_split_into_words=True)["input_ids"]
86
+ test_padded_sequences = pad_sequences(new_input_ids, maxlen=max_seq_length,padding='post',truncating='post',value=1) #post pre
87
+ print(test_padded_sequences.shape)
88
+ predicted_labels = model.predict(test_padded_sequences)
89
+
90
+ for i in range(len(test_texts)):
91
+ print(test_texts[i])
92
+ valid = 1 if predicted_labels[0][i] > 0.5 else 0
93
+ is_scene = 1 if predicted_labels[1][i] > 0.5 else 0
94
+ has_num = 1 if predicted_labels[2][i] > 0.5 else 0
95
+ print(f'is_valid : {valid}')
96
+ print(f'is_scene : {is_scene}')
97
+ print(f'has_num : {has_num}')
98
+
99
+ turn = 1 if predicted_labels[3][i] > 0.5 else 0
100
+ print(f'turn_on_off : {turn}')
101
+ print(f'device : ΰΉ„ΰΈŸ')
102
+
103
+ env_id = np.argmax(predicted_labels[5][i])
104
+ env_label = env_decode[env_id]
105
+
106
+ hour_id = np.argmax(predicted_labels[6][i])
107
+ hour_label = hour_decode[hour_id]
108
+
109
+ minute_id = np.argmax(predicted_labels[7][i])
110
+ minute_label = minute_decode[minute_id]
111
+ print(f'env : {env_label}')
112
+ print(f'hour : {hour_label}')
113
+ print(f'minute : {minute_label}')
114
+ print('----')
115
+ return 'hello'
116
 
117
  iface = gr.Interface(
118
  fn=predict,