Spaces:
Runtime error
Runtime error
Commit
·
09db170
1
Parent(s):
86bc19b
Upload 8 files
Browse files- .gitattributes +1 -0
- app.py +138 -0
- input_token_index.pkl +3 -0
- s2s/fingerprint.pb +3 -0
- s2s/keras_metadata.pb +3 -0
- s2s/saved_model.pb +3 -0
- s2s/variables/variables.data-00000-of-00001 +3 -0
- s2s/variables/variables.index +0 -0
- target_token_index.pkl +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
s2s/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from difflib import Differ
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
from keras.models import Model
|
| 6 |
+
from keras.layers import Input, LSTM, Dense, RNN
|
| 7 |
+
from tensorflow import keras
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pickle
|
| 10 |
+
|
| 11 |
+
model = keras.models.load_model("s2s")
|
| 12 |
+
|
| 13 |
+
with open("input_token_index.pkl", "rb") as file:
|
| 14 |
+
input_token_index = pickle.load(file)
|
| 15 |
+
|
| 16 |
+
with open("target_token_index.pkl", "rb") as file:
|
| 17 |
+
target_token_index = pickle.load(file)
|
| 18 |
+
|
| 19 |
+
num_encoder_tokens=len(input_token_index)
|
| 20 |
+
num_decoder_tokens=len(target_token_index)
|
| 21 |
+
latent_dim=256
|
| 22 |
+
max_encoder_seq_length=25
|
| 23 |
+
max_decoder_seq_length=24
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
encoder_inputs = model.input[0] # input_1
|
| 27 |
+
encoder_lstm_1 = model.layers[2]
|
| 28 |
+
encoder_outputs_1, h1, c1 = encoder_lstm_1(encoder_inputs)
|
| 29 |
+
encoder_lstm_2 = model.layers[4]
|
| 30 |
+
encoder_outputs, h2, c2 = encoder_lstm_2(encoder_outputs_1)
|
| 31 |
+
encoder_states = [h1, c1, h2, c2]
|
| 32 |
+
decoder_inputs=model.input[1]
|
| 33 |
+
|
| 34 |
+
out_layer1 = model.layers[3]
|
| 35 |
+
out_layer2 = model.layers[5]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
decoder_dense = model.layers[6]
|
| 39 |
+
|
| 40 |
+
# Reverse-lookup token index to decode sequences back to
|
| 41 |
+
# something readable.
|
| 42 |
+
reverse_input_char_index = dict(
|
| 43 |
+
(i, char) for char, i in input_token_index.items())
|
| 44 |
+
reverse_target_char_index = dict(
|
| 45 |
+
(i, char) for char, i in target_token_index.items())
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
encoder_model = Model(encoder_inputs, encoder_states)
|
| 49 |
+
|
| 50 |
+
decoder_state_input_h = Input(shape=(latent_dim,))
|
| 51 |
+
decoder_state_input_c = Input(shape=(latent_dim,))
|
| 52 |
+
decoder_state_input_h1 = Input(shape=(latent_dim,))
|
| 53 |
+
decoder_state_input_c1 = Input(shape=(latent_dim,))
|
| 54 |
+
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c,
|
| 55 |
+
decoder_state_input_h1, decoder_state_input_c1]
|
| 56 |
+
d_o, state_h, state_c = out_layer1(
|
| 57 |
+
decoder_inputs, initial_state=decoder_states_inputs[:2])
|
| 58 |
+
d_o, state_h1, state_c1 = out_layer2(
|
| 59 |
+
d_o, initial_state=decoder_states_inputs[-2:])
|
| 60 |
+
decoder_states = [state_h, state_c, state_h1, state_c1]
|
| 61 |
+
decoder_outputs = decoder_dense(d_o)
|
| 62 |
+
decoder_model = Model(
|
| 63 |
+
[decoder_inputs] + decoder_states_inputs,
|
| 64 |
+
[decoder_outputs] + decoder_states)
|
| 65 |
+
|
| 66 |
+
def decode_sequence(input_seq):
|
| 67 |
+
# Encode the input as state vectors.
|
| 68 |
+
states_value = encoder_model.predict(input_seq)
|
| 69 |
+
|
| 70 |
+
# Generate empty target sequence of length 1.
|
| 71 |
+
target_seq = np.zeros((1, 1, num_decoder_tokens))
|
| 72 |
+
# Populate the first character of target sequence with the start character.
|
| 73 |
+
target_seq[0, 0, target_token_index['\t']] = 1.
|
| 74 |
+
|
| 75 |
+
# Sampling loop for a batch of sequences
|
| 76 |
+
# (to simplify, here we assume a batch of size 1).
|
| 77 |
+
stop_condition = False
|
| 78 |
+
decoded_sentence = ''
|
| 79 |
+
while not stop_condition:
|
| 80 |
+
output_tokens, h, c, h1, c1 = decoder_model.predict(
|
| 81 |
+
[target_seq] + states_value) #######NOTICE THE ADDITIONAL HIDDEN STATES
|
| 82 |
+
|
| 83 |
+
# Sample a token
|
| 84 |
+
sampled_token_index = np.argmax(output_tokens[0, -1, :])
|
| 85 |
+
|
| 86 |
+
sampled_char = reverse_target_char_index[sampled_token_index]
|
| 87 |
+
decoded_sentence += sampled_char
|
| 88 |
+
|
| 89 |
+
# Exit condition: either hit max length
|
| 90 |
+
# or find stop character.
|
| 91 |
+
if (sampled_char == '\n' or
|
| 92 |
+
len(decoded_sentence) > max_decoder_seq_length):
|
| 93 |
+
stop_condition = True
|
| 94 |
+
|
| 95 |
+
# Update the target sequence (of length 1).
|
| 96 |
+
target_seq = np.zeros((1, 1, num_decoder_tokens))
|
| 97 |
+
target_seq[0, 0, sampled_token_index] = 1.
|
| 98 |
+
|
| 99 |
+
# Update states
|
| 100 |
+
states_value = [h, c, h1, c1]
|
| 101 |
+
|
| 102 |
+
return decoded_sentence
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def predict(s):
|
| 109 |
+
pattern = r'[^a-zA-Z\s]'
|
| 110 |
+
text_input = re.sub(pattern, '', s)
|
| 111 |
+
s = text_input.split()
|
| 112 |
+
encoder_input_data = np.zeros(
|
| 113 |
+
(len(s), max_encoder_seq_length, num_encoder_tokens), dtype="float32"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
for i, input_text in enumerate(s):
|
| 117 |
+
for t, char in enumerate(input_text):
|
| 118 |
+
encoder_input_data[i, t, input_token_index[char]] = 1.0
|
| 119 |
+
encoder_input_data[i, t + 1 :, input_token_index[" "]] = 1.0
|
| 120 |
+
|
| 121 |
+
decoded_sentences = []
|
| 122 |
+
for input_data in encoder_input_data:
|
| 123 |
+
decoded_sentence = decode_sequence(input_data[np.newaxis, :, :])
|
| 124 |
+
decoded_sentences.append(decoded_sentence)
|
| 125 |
+
return ' '.join(decoded_sentences).replace('\n', '')
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
demo = gr.Interface(
|
| 132 |
+
fn=predict,title='Transliteration System (English to Punjabi)',
|
| 133 |
+
inputs=gr.Textbox(label="Input", placeholder="Enter text here..."),
|
| 134 |
+
outputs=gr.Textbox(label="Output")
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
demo.launch(debug=True)
|
input_token_index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8ccb4e6b0609868f2eee9cf4f82bf3f9520b9d02d02ec7f50bd9123d945116c9
|
| 3 |
+
size 178
|
s2s/fingerprint.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6dfd89b9dc15828bf085ab4b1938a740cec1930ad6576b19c0e27ca96bdd5afa
|
| 3 |
+
size 56
|
s2s/keras_metadata.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:820154477994e01afe5d9f703fa5c2f2c22b3ece663846329725f4d983be9fa7
|
| 3 |
+
size 26543
|
s2s/saved_model.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:00965356e99d152df45359eefb4f8b0b2b280e9e119b104ee811926651c9bc39
|
| 3 |
+
size 2446864
|
s2s/variables/variables.data-00000-of-00001
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:525eb436a42139a445fe68102aa79476b738f6de3d13f3b16ba161c513ce7f41
|
| 3 |
+
size 13463227
|
s2s/variables/variables.index
ADDED
|
Binary file (2.16 kB). View file
|
|
|
target_token_index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:83c2b4330bb5bb0ecaf8955ee78ec7b9adce5536b2238e4dc1c0e8230f01e542
|
| 3 |
+
size 490
|