Spaces:
Runtime error
Runtime error
Commit ·
368e931
1
Parent(s): 0c2f71a
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,32 +7,16 @@ from tensorflow.keras.models import Model
|
|
| 7 |
from tensorflow.keras import models
|
| 8 |
from tensorflow.keras.layers import Input,LSTM,Dense
|
| 9 |
#translation
|
| 10 |
-
input_texts_translation=[]
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
with open('word.txt','r',encoding='utf-8') as f:
|
| 16 |
-
rows=f.read().split('\n')
|
| 17 |
-
count=0
|
| 18 |
-
for row in rows[:92]:
|
| 19 |
-
|
| 20 |
-
input_text,target_text = row.split('\t')
|
| 21 |
-
|
| 22 |
-
target_text='\t' + target_text + '\n'
|
| 23 |
-
input_texts_translation.append(input_text.lower())
|
| 24 |
-
target_texts_translation.append(target_text.lower())
|
| 25 |
-
|
| 26 |
-
input_characters_translation.update(list(input_text.lower()))
|
| 27 |
-
target_characters_translation.update(list(target_text.lower()))
|
| 28 |
-
input_characters_translation = sorted(list(input_characters_translation))
|
| 29 |
-
target_characters_translation = sorted(list(target_characters_translation))
|
| 30 |
|
| 31 |
-
num_en_chars_translation =
|
| 32 |
-
num_dec_chars_translation =
|
| 33 |
|
| 34 |
-
max_input_length_translation =
|
| 35 |
-
max_target_length_translation =
|
| 36 |
|
| 37 |
cv_translation=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char')
|
| 38 |
|
|
@@ -104,41 +88,25 @@ def bagofcharacter_translation(input_t):
|
|
| 104 |
|
| 105 |
return np.array(en_in_data,dtype="float32")
|
| 106 |
#transliteration
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
input_text_transliteration,target_text_transliteration = row.split('\t')
|
| 115 |
-
target_text_transliteration='\t' + target_text_transliteration + '\n'
|
| 116 |
-
input_texts_transliteration.append(input_text_transliteration.lower())
|
| 117 |
-
target_texts_transliteration.append(target_text_transliteration.lower())
|
| 118 |
-
input_characters_transliteration.update(list(input_text_transliteration.lower()))
|
| 119 |
-
target_characters_transliteration.update(list(target_text_transliteration.lower()))
|
| 120 |
-
input_characters_transliteration = sorted(list(input_characters_transliteration))
|
| 121 |
-
target_characters_transliteration = sorted(list(target_characters_transliteration))
|
| 122 |
-
|
| 123 |
-
num_en_chars_transliteration = len(input_characters_transliteration)
|
| 124 |
-
num_dec_chars_transliteration = len(target_characters_transliteration)
|
| 125 |
-
max_input_length_transliteration = max([len(i) for i in input_texts_transliteration])
|
| 126 |
-
max_target_length_transliteration = max([len(i) for i in target_texts_transliteration])
|
| 127 |
cv_transliteration=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char')
|
| 128 |
-
|
| 129 |
-
#load the model
|
| 130 |
model_transliteration = models.load_model("s2s_transliteration")
|
| 131 |
-
|
| 132 |
-
#discard the encoder output and store only states.
|
| 133 |
enc_outputs_transliteration, state_h_enc_transliteration, state_c_enc_transliteration = model_transliteration.layers[2].output
|
| 134 |
-
|
| 135 |
en_model_transliteration = Model(model_transliteration.input[0], [state_h_enc_transliteration, state_c_enc_transliteration])
|
| 136 |
-
|
| 137 |
-
#shape of layer with hidden or latent dimension
|
| 138 |
dec_state_input_h_transliteration = Input(shape=(256,), name="input_6")
|
| 139 |
dec_state_input_c_transliteration = Input(shape=(256,), name="input_7")
|
| 140 |
dec_states_inputs_transliteration = [dec_state_input_h_transliteration, dec_state_input_c_transliteration]
|
| 141 |
-
|
| 142 |
dec_lstm_transliteration = model_transliteration.layers[3]
|
| 143 |
dec_outputs_transliteration, state_h_dec_transliteration, state_c_dec_transliteration = dec_lstm_transliteration(
|
| 144 |
model_transliteration.input[1], initial_state=dec_states_inputs_transliteration
|
|
@@ -146,51 +114,41 @@ dec_outputs_transliteration, state_h_dec_transliteration, state_c_dec_transliter
|
|
| 146 |
dec_states_transliteration = [state_h_dec_transliteration, state_c_dec_transliteration]
|
| 147 |
dec_dense_transliteration = model_transliteration.layers[4]
|
| 148 |
dec_outputs_transliteration = dec_dense_transliteration(dec_outputs_transliteration)
|
| 149 |
-
|
| 150 |
-
#and decoder output with the decoder states.
|
| 151 |
dec_model_transliteration = Model(
|
| 152 |
[model_transliteration.input[1]] + dec_states_inputs_transliteration, [dec_outputs_transliteration] + dec_states_transliteration
|
| 153 |
)
|
| 154 |
def decode_sequence_transliteration(input_seq):
|
| 155 |
-
|
| 156 |
reverse_target_char_index_transliteration = dict(enumerate(target_characters_transliteration))
|
| 157 |
-
|
| 158 |
states_value_transliteration = en_model_transliteration.predict(input_seq)
|
| 159 |
|
| 160 |
-
#fit target characters and
|
| 161 |
-
#initialize every first character to be 1 which is '\t'.
|
| 162 |
-
#Generate empty target sequence of length 1.
|
| 163 |
co=cv_transliteration.fit(target_characters_transliteration)
|
| 164 |
target_seq_transliteration=np.array([co.transform(list("\t")).toarray().tolist()],dtype="float32")
|
| 165 |
|
| 166 |
-
|
| 167 |
-
stop_condition = False
|
| 168 |
-
#append every predicted character in decoded sentence
|
| 169 |
decoded_sentence = ""
|
| 170 |
-
|
| 171 |
-
while not stop_condition:
|
| 172 |
-
#get predicted output and discard hidden and cell state.
|
| 173 |
output_chars, h, c = dec_model_transliteration.predict([target_seq_transliteration] + states_value_transliteration)
|
| 174 |
-
|
| 175 |
-
#get the index and from the dictionary get the character.
|
| 176 |
char_index = np.argmax(output_chars[0, -1, :])
|
| 177 |
text_char = reverse_target_char_index_transliteration[char_index]
|
| 178 |
decoded_sentence += text_char
|
| 179 |
-
|
| 180 |
-
# or find a stop character.
|
| 181 |
if text_char == "\n" or len(decoded_sentence) > max_target_length_transliteration:
|
| 182 |
stop_condition = True
|
| 183 |
-
|
| 184 |
target_seq_transliteration = np.zeros((1, 1, num_dec_chars_transliteration))
|
| 185 |
target_seq_transliteration[0, 0, char_index] = 1.0
|
| 186 |
states_value_transliteration = [h, c]
|
| 187 |
-
|
| 188 |
return decoded_sentence
|
| 189 |
def bagofcharacter_transliteration(input_t):
|
| 190 |
cv_transliteration=CountVectorizer(binary=True,tokenizer=lambda txt:
|
| 191 |
txt.split(),stop_words=None,analyzer='char')
|
| 192 |
en_in_data=[] ; pad_en=[1]+[0]*(len(input_characters_transliteration)-1)
|
| 193 |
-
|
|
|
|
| 194 |
cv_inp= cv_transliteration.fit(input_characters_transliteration)
|
| 195 |
en_in_data.append(cv_inp.transform(list(input_t)).toarray().tolist())
|
| 196 |
|
|
@@ -208,10 +166,12 @@ def translate_to_Konkani(sent):
|
|
| 208 |
for x in input_text:
|
| 209 |
term=x+"."
|
| 210 |
if term in input_texts_translation:
|
|
|
|
| 211 |
en_in_data = bagofcharacter_translation( x.lower()+".")
|
| 212 |
x=decode_sequence_translation(en_in_data)
|
| 213 |
output_texts+=" "+ x
|
| 214 |
else:
|
|
|
|
| 215 |
en_in_data = bagofcharacter_transliteration( x.lower()+".")
|
| 216 |
x=decode_sequence_transliteration(en_in_data)
|
| 217 |
output_texts+=" "+ x
|
|
|
|
| 7 |
from tensorflow.keras import models
|
| 8 |
from tensorflow.keras.layers import Input,LSTM,Dense
|
| 9 |
#translation
|
| 10 |
+
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.']
|
| 11 |
+
input_characters_translation=[' ', '.', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y']
|
| 12 |
+
target_characters_translation=['\t', '\n', ' ', '.', 'ं', 'आ', 'उ', 'क', 'ख', 'ग', 'घ', 'च', 'ज', 'झ', 'ट', 'ड', 'ण', 'त', 'द', 'न', 'प', 'फ', 'ब', 'भ', 'म', 'य', 'र', 'ल', 'ळ', 'व', 'श', 'स', 'ह', 'ा', 'ि', 'ी', 'ु', 'ू', 'े', 'ो', '्']
|
| 13 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
num_en_chars_translation = 24
|
| 16 |
+
num_dec_chars_translation = 41
|
| 17 |
|
| 18 |
+
max_input_length_translation = 22
|
| 19 |
+
max_target_length_translation = 37
|
| 20 |
|
| 21 |
cv_translation=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char')
|
| 22 |
|
|
|
|
| 88 |
|
| 89 |
return np.array(en_in_data,dtype="float32")
|
| 90 |
#transliteration
|
| 91 |
+
|
| 92 |
+
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']
|
| 93 |
+
target_characters_transliteration=['\t', '\n', 'ँ', 'ं', 'ः', 'अ', 'आ', 'इ', 'ई', 'उ', 'ऊ', 'ए', 'ऐ', 'ऑ', 'ओ', 'औ', 'क', 'ख', 'ग', 'घ', 'च', 'छ', 'ज', 'झ', 'ट', 'ठ', 'ड', 'ढ', 'ण', 'त', 'थ', 'द', 'ध', 'न', 'प', 'फ', 'ब', 'भ', 'म', 'य', 'र', 'ल', 'ळ', 'व', 'श', 'ष', 'स', 'ह', '़', 'ा', 'ि', 'ी', 'ु', 'ू', 'ृ', 'े','ै', 'ॉ', 'ो', 'ौ', '्', 'क़', 'ख़', 'ग़', 'ज़', 'ड़', 'ढ़', 'फ़']
|
| 94 |
+
num_en_chars_transliteration = 27
|
| 95 |
+
num_dec_chars_transliteration = 68
|
| 96 |
+
max_input_length_transliteration = 21
|
| 97 |
+
max_target_length_transliteration = 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
cv_transliteration=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char')
|
| 99 |
+
|
|
|
|
| 100 |
model_transliteration = models.load_model("s2s_transliteration")
|
| 101 |
+
|
|
|
|
| 102 |
enc_outputs_transliteration, state_h_enc_transliteration, state_c_enc_transliteration = model_transliteration.layers[2].output
|
| 103 |
+
|
| 104 |
en_model_transliteration = Model(model_transliteration.input[0], [state_h_enc_transliteration, state_c_enc_transliteration])
|
| 105 |
+
|
|
|
|
| 106 |
dec_state_input_h_transliteration = Input(shape=(256,), name="input_6")
|
| 107 |
dec_state_input_c_transliteration = Input(shape=(256,), name="input_7")
|
| 108 |
dec_states_inputs_transliteration = [dec_state_input_h_transliteration, dec_state_input_c_transliteration]
|
| 109 |
+
|
| 110 |
dec_lstm_transliteration = model_transliteration.layers[3]
|
| 111 |
dec_outputs_transliteration, state_h_dec_transliteration, state_c_dec_transliteration = dec_lstm_transliteration(
|
| 112 |
model_transliteration.input[1], initial_state=dec_states_inputs_transliteration
|
|
|
|
| 114 |
dec_states_transliteration = [state_h_dec_transliteration, state_c_dec_transliteration]
|
| 115 |
dec_dense_transliteration = model_transliteration.layers[4]
|
| 116 |
dec_outputs_transliteration = dec_dense_transliteration(dec_outputs_transliteration)
|
| 117 |
+
|
|
|
|
| 118 |
dec_model_transliteration = Model(
|
| 119 |
[model_transliteration.input[1]] + dec_states_inputs_transliteration, [dec_outputs_transliteration] + dec_states_transliteration
|
| 120 |
)
|
| 121 |
def decode_sequence_transliteration(input_seq):
|
| 122 |
+
|
| 123 |
reverse_target_char_index_transliteration = dict(enumerate(target_characters_transliteration))
|
| 124 |
+
|
| 125 |
states_value_transliteration = en_model_transliteration.predict(input_seq)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
| 127 |
co=cv_transliteration.fit(target_characters_transliteration)
|
| 128 |
target_seq_transliteration=np.array([co.transform(list("\t")).toarray().tolist()],dtype="float32")
|
| 129 |
|
| 130 |
+
stop_condition = False
|
|
|
|
|
|
|
| 131 |
decoded_sentence = ""
|
| 132 |
+
while not stop_condition:
|
|
|
|
|
|
|
| 133 |
output_chars, h, c = dec_model_transliteration.predict([target_seq_transliteration] + states_value_transliteration)
|
|
|
|
|
|
|
| 134 |
char_index = np.argmax(output_chars[0, -1, :])
|
| 135 |
text_char = reverse_target_char_index_transliteration[char_index]
|
| 136 |
decoded_sentence += text_char
|
| 137 |
+
|
|
|
|
| 138 |
if text_char == "\n" or len(decoded_sentence) > max_target_length_transliteration:
|
| 139 |
stop_condition = True
|
| 140 |
+
|
| 141 |
target_seq_transliteration = np.zeros((1, 1, num_dec_chars_transliteration))
|
| 142 |
target_seq_transliteration[0, 0, char_index] = 1.0
|
| 143 |
states_value_transliteration = [h, c]
|
| 144 |
+
|
| 145 |
return decoded_sentence
|
| 146 |
def bagofcharacter_transliteration(input_t):
|
| 147 |
cv_transliteration=CountVectorizer(binary=True,tokenizer=lambda txt:
|
| 148 |
txt.split(),stop_words=None,analyzer='char')
|
| 149 |
en_in_data=[] ; pad_en=[1]+[0]*(len(input_characters_transliteration)-1)
|
| 150 |
+
print("hi3")
|
| 151 |
+
|
| 152 |
cv_inp= cv_transliteration.fit(input_characters_transliteration)
|
| 153 |
en_in_data.append(cv_inp.transform(list(input_t)).toarray().tolist())
|
| 154 |
|
|
|
|
| 166 |
for x in input_text:
|
| 167 |
term=x+"."
|
| 168 |
if term in input_texts_translation:
|
| 169 |
+
print("hi1")
|
| 170 |
en_in_data = bagofcharacter_translation( x.lower()+".")
|
| 171 |
x=decode_sequence_translation(en_in_data)
|
| 172 |
output_texts+=" "+ x
|
| 173 |
else:
|
| 174 |
+
print("hi2")
|
| 175 |
en_in_data = bagofcharacter_transliteration( x.lower()+".")
|
| 176 |
x=decode_sequence_transliteration(en_in_data)
|
| 177 |
output_texts+=" "+ x
|