File size: 10,143 Bytes
2e82911
 
 
 
 
 
 
 
 
 
0c747f1
 
2e82911
 
0c747f1
920ab65
2e82911
0c747f1
 
2e82911
c8a2c74
2e82911
0c747f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e82911
0c747f1
 
 
 
 
 
 
2e82911
 
0c747f1
 
 
 
2e82911
0c747f1
 
 
 
 
2e82911
0c747f1
2e82911
0c747f1
 
2e82911
 
0c747f1
 
2e82911
0c747f1
 
 
 
 
 
 
2e82911
0c747f1
 
 
 
 
 
 
 
2e82911
 
0c747f1
2e82911
0c747f1
2e82911
0c747f1
 
2e82911
0c747f1
 
2e82911
 
 
0c747f1
2e82911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb50e4f
 
2e82911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np


from sklearn.feature_extraction.text import CountVectorizer
from tensorflow.keras.models import Model
from tensorflow.keras import models
from tensorflow.keras.layers import Input,LSTM,Dense
#translation
input_texts_translation=['again.', 'arrive.', 'bathroom.', 'believe.', 'can.', 'deaf.', 'fine.', 'go.', 'hello.', 'help.', 'home.', 'how.', 'hungury.', 'sorry.', 'call.', 'later.', 'learn.', 'like.', 'live.', 'meet.', 'my.', 'name.', 'nice.', 'no.', 'please.', 'see.', 'share.', 'sign.', 'slow.', 'takecare.', 'talk.', 'thank you.', 'time.', 'understand.', 'we.', 'what.', 'when.', 'where.', 'who.', 'yes.', 'you.', 'you.', 'you.', 'you.', 'name.', 'good.', 'everning.', 'night.', 'how you.', 'you name what.', 'my name.', 'you live where.', 'i.', 'live.', 'you help.', 'yes help me.', 'you understand.', 'i hungry.', 'good everning.', 'good night.', 'pleased.', 'nice meet you.', 'i fine.', 'home arrive when.', 'where bathroom.', 'believe my.', 'call.', 'call.','deaf.', 'i call later.', 'i deaf.', 'what time.', 'i sorry.', 'on my.', 'my.', 'believe.', 'learn.', 'learn.', 'what are you learning.', 'you learn sign where.', 'i go home.', 'i.', 'i.', 'see you later.', 'meet.', 'meet.', 'we meet.', 'like.', 'i like.', 'talk later.', 'later.', 'later.']
input_characters=[' ', '.', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y']
target_characters=['\t', '\n', ' ', '.', 'ं', 'आ', 'उ', 'क', 'ख', 'ग', 'घ', 'च', 'ज', 'झ', 'ट', 'ड', 'ण', 'त', 'द', 'न', 'प', 'फ', 'ब', 'भ', 'म', 'य', 'र', 'ल', 'ळ', 'व', 'श', 'स', 'ह', 'ा', 'ि', 'ी', 'ु', 'ू', 'े', 'ो', '्']


num_en_chars = 24
num_dec_chars_translation = 40

max_input_length = 22
max_target_length = 36

cv=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char') 

#Inference model
#load the model
model = models.load_model("model_translation")
#construct encoder model from the output of second layer
#discard the encoder output and store only states.
enc_outputs, state_h_enc, state_c_enc = model.layers[2].output 
#add input object and state from the layer.
en_model = Model(model.input[0], [state_h_enc, state_c_enc])
#create Input object for hidden and cell state for decoder
#shape of layer with hidden or latent dimension
dec_state_input_h = Input(shape=(256,),name="input_4")
dec_state_input_c = Input(shape=(256,),name="input_5")
dec_states_inputs = [dec_state_input_h, dec_state_input_c]
#add input from the encoder output and initialize with states.
dec_lstm = model.layers[3]
dec_outputs, state_h_dec, state_c_dec = dec_lstm(
    model.input[1], initial_state=dec_states_inputs
)
dec_states = [state_h_dec, state_c_dec]
dec_dense = model.layers[4]
dec_outputs = dec_dense(dec_outputs)
#create Model with the input of decoder state input and encoder input
#and decoder output with the decoder states.
dec_model = Model(
    [model.input[1]] + dec_states_inputs, [dec_outputs] + dec_states
)
def decode_sequence_translation(input_seq):
        #create a dictionary with a key as index and value as characters.
        reverse_target_char_index = dict(enumerate(target_characters))
        #get the states from the user input sequence
        states_value = en_model.predict(input_seq)

        #fit target characters and 
        #initialize every first character to be 1 which is '\t'.
        #Generate empty target sequence of length 1.
        co=cv.fit(target_characters) 
        target_seq=np.array([co.transform(list("\t")).toarray().tolist()],dtype="float32")

        #if the iteration reaches the end of text than it will be stop the it
        stop_condition = False
        #append every predicted character in decoded sentence
        decoded_sentence = ""

        while not stop_condition:
            #get predicted output and discard hidden and cell state.
            output_chars, h, c = dec_model.predict([target_seq] + states_value)

            #get the index and from the dictionary get the character.
            char_index = np.argmax(output_chars[0, -1, :])
            text_char = reverse_target_char_index[char_index]
            decoded_sentence += text_char
                # Exit condition: either hit max length
            # or find a stop character.
            if text_char == "\n" or len(decoded_sentence) > max_target_length:
                stop_condition = True
            #update target sequence to the current character index.
            target_seq = np.zeros((1, 1, num_dec_chars))
            target_seq[0, 0, char_index] = 1.0
            states_value = [h, c]
        #return the decoded sentence
        return decoded_sentence

    

def bagofcharacter_translation(input_t):
        cv=CountVectorizer(binary=True,tokenizer=lambda txt:
        txt.split(),stop_words=None,analyzer='char') 
        en_in_data=[] ; pad_en=[1]+[0]*(len(input_characters)-1)
    
        cv_inp= cv.fit(input_characters)
        en_in_data.append(cv_inp.transform(list(input_t)).toarray().tolist())
    
        if len(input_t)< max_input_length:
          for _ in range(max_input_length-len(input_t)):
            en_in_data[0].append(pad_en)
    
        return np.array(en_in_data,dtype="float32")
    
#transliteration

input_characters_transliteration=[' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u','v', 'w', 'x', 'y', 'z']
target_characters_transliteration=['\t', '\n', 'ँ', 'ं', 'ः', 'अ', 'आ', 'इ', 'ई', 'उ', 'ऊ', 'ए', 'ऐ', 'ऑ', 'ओ', 'औ', 'क', 'ख', 'ग', 'घ', 'च', 'छ', 'ज', 'झ', 'ट', 'ठ', 'ड', 'ढ', 'ण', 'त', 'थ', 'द', 'ध', 'न', 'प', 'फ', 'ब', 'भ', 'म', 'य', 'र', 'ल', 'ळ', 'व', 'श', 'ष', 'स', 'ह', '़', 'ा', 'ि', 'ी', 'ु', 'ू', 'ृ', 'े','ै', 'ॉ', 'ो', 'ौ', '्', 'क़', 'ख़', 'ग़', 'ज़', 'ड़', 'ढ़', 'फ़']
num_en_chars_transliteration = 27
num_dec_chars_transliteration = 68
max_input_length_transliteration = 21
max_target_length_transliteration = 20
cv_transliteration=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char') 
model_transliteration = models.load_model("s2s_transliteration")

enc_outputs_transliteration, state_h_enc_transliteration, state_c_enc_transliteration = model_transliteration.layers[2].output 

en_model_transliteration = Model(model_transliteration.input[0], [state_h_enc_transliteration, state_c_enc_transliteration])

dec_state_input_h_transliteration = Input(shape=(256,), name="input_6")
dec_state_input_c_transliteration = Input(shape=(256,), name="input_7")
dec_states_inputs_transliteration = [dec_state_input_h_transliteration, dec_state_input_c_transliteration]
dec_lstm_transliteration = model_transliteration.layers[3]
dec_outputs_transliteration, state_h_dec_transliteration, state_c_dec_transliteration = dec_lstm_transliteration(
    model_transliteration.input[1], initial_state=dec_states_inputs_transliteration
)
dec_states_transliteration = [state_h_dec_transliteration, state_c_dec_transliteration]
dec_dense_transliteration = model_transliteration.layers[4]
dec_outputs_transliteration = dec_dense_transliteration(dec_outputs_transliteration)
dec_model_transliteration = Model(
    [model_transliteration.input[1]] + dec_states_inputs_transliteration, [dec_outputs_transliteration] + dec_states_transliteration
)
def decode_sequence_transliteration(input_seq):
       
        reverse_target_char_index_transliteration = dict(enumerate(target_characters_transliteration))
       
        states_value_transliteration = en_model_transliteration.predict(input_seq)

        co=cv_transliteration.fit(target_characters_transliteration) 
        target_seq_transliteration=np.array([co.transform(list("\t")).toarray().tolist()],dtype="float32")

        stop_condition = False  
        decoded_sentence = ""
        while not stop_condition:            
            output_chars, h, c = dec_model_transliteration.predict([target_seq_transliteration] + states_value_transliteration)
            char_index = np.argmax(output_chars[0, -1, :])
            text_char = reverse_target_char_index_transliteration[char_index]
            decoded_sentence += text_char

            if text_char == "\n" or len(decoded_sentence) > max_target_length_transliteration:
                stop_condition = True
           
            target_seq_transliteration = np.zeros((1, 1, num_dec_chars_transliteration))
            target_seq_transliteration[0, 0, char_index] = 1.0
            states_value_transliteration = [h, c]
      
        return decoded_sentence
def bagofcharacter_transliteration(input_t):
        cv_transliteration=CountVectorizer(binary=True,tokenizer=lambda txt:
        txt.split(),stop_words=None,analyzer='char') 
        en_in_data=[] ; pad_en=[1]+[0]*(len(input_characters_transliteration)-1)
        print("hi3")
         
        cv_inp= cv_transliteration.fit(input_characters_transliteration)
        en_in_data.append(cv_inp.transform(list(input_t)).toarray().tolist())
    
        if len(input_t)< max_input_length_transliteration:
          for _ in range(max_input_length_transliteration-len(input_t)):
            en_in_data[0].append(pad_en)
    
        return np.array(en_in_data,dtype="float32")


def translate_to_Konkani(sent):  
    
    input_text = sent.split(',') 
    output_texts=""
    for x in input_text:
        term=x+"."
        k=term.split(' ')
        
        if term in input_texts_translation :
            
            en_in_data = bagofcharacter_translation( x.lower()+".")    
            x=decode_sequence_translation(en_in_data)
            output_texts+=" "+ x 
    
        else:
        
            
            en_in_data = bagofcharacter_transliteration( x.lower()+".")    
            x=decode_sequence_transliteration(en_in_data)
            output_texts+=" "+ x   
         
    return output_texts

iface = gr.Interface(fn=translate_to_Konkani, inputs="text", outputs="text")
iface.launch()