Initial
Browse files- .idea/.gitignore +3 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/language-translator-ml-codes.iml +8 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- LangTransGui.py +266 -0
- demo.py +45 -0
- eng-french.txt +0 -0
- langTraining.py +125 -0
- s2s/keras_metadata.pb +3 -0
- s2s/saved_model.pb +3 -0
- s2s/variables/variables.data-00000-of-00001 +0 -0
- s2s/variables/variables.index +0 -0
- training_data.pkl +0 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/language-translator-ml-codes.iml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="inheritedJdk" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
</module>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/language-translator-ml-codes.iml" filepath="$PROJECT_DIR$/.idea/language-translator-ml-codes.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
LangTransGui.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tkinter
|
| 2 |
+
from tkinter import messagebox
|
| 3 |
+
from tkinter import *
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 7 |
+
from tensorflow.keras.models import Model
|
| 8 |
+
from tensorflow.keras import models
|
| 9 |
+
from tensorflow.keras.layers import Input, LSTM, Dense
|
| 10 |
+
import speech_recognition as sr
|
| 11 |
+
import pyttsx3
|
| 12 |
+
|
| 13 |
+
BG_GRAY = "#ABB2B9"
|
| 14 |
+
BG_COLOR = "#000"
|
| 15 |
+
TEXT_COLOR = "#000"
|
| 16 |
+
FONT = "Melvetica 14"
|
| 17 |
+
FONT_BOLD = "Melvetica 13 bold"
|
| 18 |
+
|
| 19 |
+
cv = CountVectorizer(binary=True, tokenizer=lambda txt: txt.split(), stop_words=None, analyzer='char')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class LangTRans:
|
| 23 |
+
def __init__(self):
|
| 24 |
+
# initialize tkinter window and load the file
|
| 25 |
+
self.window = Tk()
|
| 26 |
+
self.demo_window()
|
| 27 |
+
self.datafile()
|
| 28 |
+
|
| 29 |
+
def datafile(self):
|
| 30 |
+
# get all datas from datafile and load the model.
|
| 31 |
+
datafile = pickle.load(open("training_data.pkl", "rb"))
|
| 32 |
+
self.input_characters = datafile['input_characters']
|
| 33 |
+
self.target_characters = datafile['target_characters']
|
| 34 |
+
self.max_input_length = datafile['max_input_length']
|
| 35 |
+
self.max_target_length = datafile['max_target_length']
|
| 36 |
+
self.num_en_chars = datafile['num_en_chars']
|
| 37 |
+
self.num_dec_chars = datafile['num_dec_chars']
|
| 38 |
+
self.loadmodel()
|
| 39 |
+
|
| 40 |
+
# runwindow
|
| 41 |
+
def run(self):
|
| 42 |
+
self.window.mainloop()
|
| 43 |
+
|
| 44 |
+
def run2(self):
|
| 45 |
+
self.window.mainloop()
|
| 46 |
+
|
| 47 |
+
def demo_window(self):
|
| 48 |
+
self.window.title("Language Translator")
|
| 49 |
+
self.window.resizable(width=False, height=False)
|
| 50 |
+
self.window.configure(width=800, height=300)
|
| 51 |
+
|
| 52 |
+
# head_label.place(relwidth=1)
|
| 53 |
+
# line = Label(self.window,width=450,bg=BG_COLOR)
|
| 54 |
+
# line.place(relwidth=1,rely=0.07,relheight=0.012)
|
| 55 |
+
self.myText = StringVar()
|
| 56 |
+
head_label = Label(self.window, text="Translate to french Language!", font=FONT_BOLD, pady=10)
|
| 57 |
+
head_label.grid(row=0, column=3, padx=20, pady=20, columnspan=5)
|
| 58 |
+
head_label.grid_rowconfigure(1, weight=1)
|
| 59 |
+
head_label.grid_columnconfigure(1, weight=1)
|
| 60 |
+
|
| 61 |
+
Label(self.window, text="Input Text:").grid(row=1, padx=10, pady=10)
|
| 62 |
+
self.e1 = Entry(self.window)
|
| 63 |
+
self.e1.grid(row=2, column=1)
|
| 64 |
+
send_button2 = Button(self.window, text="Voice", font=FONT_BOLD, width=2, bg="Red",
|
| 65 |
+
command=lambda: self.voice_input(None))
|
| 66 |
+
send_button2.grid(row=3, column=1, sticky=W + E + N + S, padx=20, pady=20)
|
| 67 |
+
Label(self.window, text="Translated text in French:").grid(row=1, column=4,sticky="W")
|
| 68 |
+
self.output_box = Label(self.window, width=20, text="", textvariable=self.myText)
|
| 69 |
+
self.output_box.grid(row=2, column=5)
|
| 70 |
+
self.e1 = Entry(self.window)
|
| 71 |
+
self.e1.grid(row=1, column=1)
|
| 72 |
+
send_button = Button(self.window, text="Translate", font=FONT_BOLD, width=2, bg="blue", command=lambda: self.on_enter(None))
|
| 73 |
+
send_button.grid(row=3, column=3, sticky=W + E + N + S, padx=20, pady=20)
|
| 74 |
+
|
| 75 |
+
send_button1 = Button(self.window, text="Voice Output", font=FONT_BOLD, bg="Red",
|
| 76 |
+
command=lambda: self.on_enter_voice(None))
|
| 77 |
+
send_button1.grid(row=3, column=4, sticky=W + E + N + S, padx=20, pady=20)
|
| 78 |
+
|
| 79 |
+
def main_window(self):
|
| 80 |
+
# add title to window and configure it
|
| 81 |
+
self.window.title("Language Translator")
|
| 82 |
+
self.window.resizable(width=False, height=False)
|
| 83 |
+
self.window.configure(width=520, height=520, bg=BG_COLOR)
|
| 84 |
+
|
| 85 |
+
head_label = Label(self.window, bg=BG_COLOR, fg=TEXT_COLOR, text="Translate to french Language!",
|
| 86 |
+
font=FONT_BOLD, pady=10)
|
| 87 |
+
head_label.place(relwidth=1)
|
| 88 |
+
line = Label(self.window, width=450, bg=BG_COLOR)
|
| 89 |
+
line.place(relwidth=1, rely=0.07, relheight=0.012)
|
| 90 |
+
|
| 91 |
+
# create text widget where input and output will be displayed
|
| 92 |
+
self.text_widget = Text(self.window, width=20, height=2, bg="#fff", fg="#000", font=FONT, padx=5, pady=5)
|
| 93 |
+
self.text_widget.place(relheight=0.745, relwidth=1, rely=0.08)
|
| 94 |
+
self.text_widget.configure(cursor="arrow", state=DISABLED)
|
| 95 |
+
|
| 96 |
+
# create scrollbar
|
| 97 |
+
scrollbar = Scrollbar(self.text_widget)
|
| 98 |
+
scrollbar.place(relheight=1, relx=0.974)
|
| 99 |
+
scrollbar.configure(command=self.text_widget.yview)
|
| 100 |
+
|
| 101 |
+
# create bottom label where text widget will placed
|
| 102 |
+
bottom_label = Label(self.window, bg=BG_GRAY, height=80)
|
| 103 |
+
bottom_label.place(relwidth=1, rely=0.825)
|
| 104 |
+
# this is for user to put english text
|
| 105 |
+
self.msg_entry = Entry(bottom_label, bg="#2C3E50", fg=TEXT_COLOR, font=FONT)
|
| 106 |
+
self.msg_entry.place(relwidth=0.65, relheight=0.06, rely=0.008, relx=0.008)
|
| 107 |
+
self.msg_entry.focus()
|
| 108 |
+
self.msg_entry.bind("<Return>", self.on_enter)
|
| 109 |
+
# send button which will call on_enter function to send the text
|
| 110 |
+
send_button2 = Button(bottom_label, text="Voice\n Input", font=FONT_BOLD, width=2, bg="Red",
|
| 111 |
+
command=lambda: self.voice_input(None))
|
| 112 |
+
send_button2.place(relx=0.66, rely=0.008, relheight=0.06, relwidth=0.1325)
|
| 113 |
+
|
| 114 |
+
# send button which will call on_enter function to send the text
|
| 115 |
+
send_button = Button(bottom_label, text="Only Text", font=FONT_BOLD, width=8, bg="Red",
|
| 116 |
+
command=lambda: self.on_enter(None))
|
| 117 |
+
send_button.place(relx=0.80, rely=0.008, relheight=0.03, relwidth=0.20)
|
| 118 |
+
# send button which will call on_enter function to send the text
|
| 119 |
+
send_button1 = Button(bottom_label, text="Voice", font=FONT_BOLD, width=2, bg="Red",
|
| 120 |
+
command=lambda: self.on_enter_voice(None))
|
| 121 |
+
send_button1.place(relx=0.80, rely=0.04, relheight=0.027, relwidth=0.20)
|
| 122 |
+
|
| 123 |
+
def loadmodel(self):
|
| 124 |
+
# Inference model
|
| 125 |
+
# load the model
|
| 126 |
+
model = models.load_model("s2s")
|
| 127 |
+
# construct encoder model from the output of second layer
|
| 128 |
+
# discard the encoder output and store only states.
|
| 129 |
+
enc_outputs, state_h_enc, state_c_enc = model.layers[2].output # lstm_1
|
| 130 |
+
# add input object and state from the layer.
|
| 131 |
+
self.en_model = Model(model.input[0], [state_h_enc, state_c_enc])
|
| 132 |
+
|
| 133 |
+
# create Input object for hidden and cell state for decoder
|
| 134 |
+
# shape of layer with hidden or latent dimension
|
| 135 |
+
dec_state_input_h = Input(shape=(256,), name="input_3")
|
| 136 |
+
dec_state_input_c = Input(shape=(256,), name="input_4")
|
| 137 |
+
dec_states_inputs = [dec_state_input_h, dec_state_input_c]
|
| 138 |
+
|
| 139 |
+
# add input from the encoder output and initialize with
|
| 140 |
+
# states.
|
| 141 |
+
dec_lstm = model.layers[3]
|
| 142 |
+
dec_outputs, state_h_dec, state_c_dec = dec_lstm(
|
| 143 |
+
model.input[1], initial_state=dec_states_inputs
|
| 144 |
+
)
|
| 145 |
+
dec_states = [state_h_dec, state_c_dec]
|
| 146 |
+
dec_dense = model.layers[4]
|
| 147 |
+
dec_outputs = dec_dense(dec_outputs)
|
| 148 |
+
# create Model with the input of decoder state input and encoder input
|
| 149 |
+
# and decoder output with the decoder states.
|
| 150 |
+
self.dec_model = Model(
|
| 151 |
+
[model.input[1]] + dec_states_inputs, [dec_outputs] + dec_states
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
def decode_sequence(self, input_seq):
|
| 155 |
+
# create dict object to get character from the index.
|
| 156 |
+
reverse_target_char_index = dict(enumerate(self.target_characters))
|
| 157 |
+
# get the states from the user input sequence
|
| 158 |
+
states_value = self.en_model.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.fit(self.target_characters)
|
| 164 |
+
target_seq = np.array([co.transform(list("\t")).toarray().tolist()], dtype="float32")
|
| 165 |
+
|
| 166 |
+
# if the iteration reaches the end of text than it will be stop the it
|
| 167 |
+
stop_condition = False
|
| 168 |
+
# append every predicted character in decoded sentence
|
| 169 |
+
decoded_sentence = ""
|
| 170 |
+
while not stop_condition:
|
| 171 |
+
# get predicted output and discard hidden and cell state.
|
| 172 |
+
output_chars, h, c = self.dec_model.predict([target_seq] + states_value)
|
| 173 |
+
|
| 174 |
+
# get the index and from dictionary get character from it.
|
| 175 |
+
char_index = np.argmax(output_chars[0, -1, :])
|
| 176 |
+
text_char = reverse_target_char_index[char_index]
|
| 177 |
+
decoded_sentence += text_char
|
| 178 |
+
|
| 179 |
+
# Exit condition: either hit max length
|
| 180 |
+
# or find stop character.
|
| 181 |
+
if text_char == "\n" or len(decoded_sentence) > self.max_target_length:
|
| 182 |
+
stop_condition = True
|
| 183 |
+
# update target sequence to the current character index.
|
| 184 |
+
target_seq = np.zeros((1, 1, self.num_dec_chars))
|
| 185 |
+
target_seq[0, 0, char_index] = 1.0
|
| 186 |
+
states_value = [h, c]
|
| 187 |
+
# return the decoded sentence
|
| 188 |
+
return decoded_sentence
|
| 189 |
+
|
| 190 |
+
def on_enter(self, event):
|
| 191 |
+
# get user query and bot response
|
| 192 |
+
msg = self.e1.get()
|
| 193 |
+
# self.my_msg(msg, "English")
|
| 194 |
+
self.deocded_output(msg)
|
| 195 |
+
|
| 196 |
+
def on_enter_voice(self, event):
|
| 197 |
+
# get user query and bot response
|
| 198 |
+
msg = self.output_box["text"]
|
| 199 |
+
# self.my_msg(msg, "English")
|
| 200 |
+
self.deocded_output_voice(msg)
|
| 201 |
+
|
| 202 |
+
def voice_input(self, event):
|
| 203 |
+
r = sr.Recognizer()
|
| 204 |
+
with sr.Microphone() as source:
|
| 205 |
+
audio = r.listen(source,phrase_time_limit=5)
|
| 206 |
+
|
| 207 |
+
try:
|
| 208 |
+
msg = r.recognize_google(audio)
|
| 209 |
+
# print(msg)
|
| 210 |
+
self.e1.insert(0, msg)
|
| 211 |
+
# self.msg_entry.insert(0,msg)
|
| 212 |
+
|
| 213 |
+
except:
|
| 214 |
+
print("Not working")
|
| 215 |
+
# tkinter.messagebox.showerror(title="Error", message="Sorry could not recognize what you said.")
|
| 216 |
+
|
| 217 |
+
def bagofcharacters(self, input_t):
|
| 218 |
+
cv = CountVectorizer(binary=True, tokenizer=lambda txt: txt.split(), stop_words=None, analyzer='char')
|
| 219 |
+
en_in_data = [];
|
| 220 |
+
pad_en = [1] + [0] * (len(self.input_characters) - 1)
|
| 221 |
+
|
| 222 |
+
cv_inp = cv.fit(self.input_characters)
|
| 223 |
+
en_in_data.append(cv_inp.transform(list(input_t)).toarray().tolist())
|
| 224 |
+
|
| 225 |
+
if len(input_t) < self.max_input_length:
|
| 226 |
+
for _ in range(self.max_input_length - len(input_t)):
|
| 227 |
+
en_in_data[0].append(pad_en)
|
| 228 |
+
|
| 229 |
+
return np.array(en_in_data, dtype="float32")
|
| 230 |
+
|
| 231 |
+
def deocded_output(self, msg):
|
| 232 |
+
# self.text_widget.configure(state=NORMAL)
|
| 233 |
+
# en_in_data = self.bagofcharacters(msg.lower() + ".")
|
| 234 |
+
# self.text_widget.insert(END, str(sender) + " : " + self.decode_sequence(en_in_data)
|
| 235 |
+
# + "\n\n")
|
| 236 |
+
# self.text_widget.configure(state=DISABLED)
|
| 237 |
+
# self.text_widget.see(END)
|
| 238 |
+
en_in_data = self.bagofcharacters(msg.lower())
|
| 239 |
+
# print(self.decode_sequence(en_in_data))
|
| 240 |
+
self.myText.set(self.decode_sequence(en_in_data))
|
| 241 |
+
|
| 242 |
+
def deocded_output_voice(self, msg):
|
| 243 |
+
# self.text_widget.configure(state=NORMAL)
|
| 244 |
+
# en_in_data = self.bagofcharacters(msg.lower() + ".")
|
| 245 |
+
# self.text_widget.insert(END, str(sender) + " : " + self.decode_sequence(en_in_data)
|
| 246 |
+
# + "\n\n")
|
| 247 |
+
engine = pyttsx3.init()
|
| 248 |
+
engine.setProperty("rate", 135)
|
| 249 |
+
engine.say(msg)
|
| 250 |
+
engine.runAndWait()
|
| 251 |
+
# self.text_widget.configure(state=DISABLED)
|
| 252 |
+
# self.text_widget.see(END)
|
| 253 |
+
|
| 254 |
+
def my_msg(self, msg, sender):
|
| 255 |
+
if not msg:
|
| 256 |
+
return
|
| 257 |
+
self.msg_entry.delete(0, END)
|
| 258 |
+
self.text_widget.configure(state=NORMAL)
|
| 259 |
+
self.text_widget.insert(END, str(sender) + " : " + str(msg) + "\n")
|
| 260 |
+
self.text_widget.configure(state=DISABLED)
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
# run the file
|
| 264 |
+
if __name__ == "__main__":
|
| 265 |
+
LT = LangTRans()
|
| 266 |
+
LT.run2()
|
demo.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from tkinter import *
|
| 2 |
+
# root = Tk()
|
| 3 |
+
# root.geometry("500x500+0+0")
|
| 4 |
+
# frmMain = Frame(root,bg="blue")
|
| 5 |
+
#
|
| 6 |
+
# startbutton = Button(frmMain, text="Start",height=1,width=4)
|
| 7 |
+
# startbutton.grid()
|
| 8 |
+
#
|
| 9 |
+
# #Configure the row/col of our frame and root window to be resizable and fill all available space
|
| 10 |
+
# frmMain.grid(row=0, column=0, sticky="NESW")
|
| 11 |
+
# frmMain.grid_rowconfigure(0, weight=1)
|
| 12 |
+
# frmMain.grid_columnconfigure(0, weight=1)
|
| 13 |
+
# root.grid_rowconfigure(0, weight=1)
|
| 14 |
+
# root.grid_columnconfigure(0, weight=1)
|
| 15 |
+
#
|
| 16 |
+
# root.mainloop()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# import speech_recognition as sr
|
| 20 |
+
# r = sr.Recognizer()
|
| 21 |
+
# with sr.Microphone() as source:
|
| 22 |
+
# print("Speak Anything")
|
| 23 |
+
# audio = r.listen(source,phrase_time_limit=5)
|
| 24 |
+
# try:
|
| 25 |
+
# text = r.recognize_google(audio)
|
| 26 |
+
# print("You said : {}".format(text))
|
| 27 |
+
# except:
|
| 28 |
+
# print("Sorry could not recognize what you said")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
#Import tkinter library
|
| 32 |
+
from tkinter import *
|
| 33 |
+
#Create an instance of Tkinter frame or window
|
| 34 |
+
win= Tk()
|
| 35 |
+
#Set the geometry of tkinter frame
|
| 36 |
+
win.geometry("750x250")
|
| 37 |
+
#Make the window sticky for every case
|
| 38 |
+
win.grid_rowconfigure(0, weight=1)
|
| 39 |
+
win.grid_columnconfigure(0, weight=1)
|
| 40 |
+
#Create a Label
|
| 41 |
+
label=Label(win, text="This is a Centered Text",font=('Aerial 15 bold'))
|
| 42 |
+
label.grid(row=2, column=0)
|
| 43 |
+
label.grid_rowconfigure(1, weight=1)
|
| 44 |
+
label.grid_columnconfigure(1, weight=1)
|
| 45 |
+
win.mainloop()
|
eng-french.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
langTraining.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Load all the required modules.
|
| 2 |
+
from tensorflow.keras.models import Model
|
| 3 |
+
from tensorflow.keras import models
|
| 4 |
+
from tensorflow.keras.utils import plot_model
|
| 5 |
+
from tensorflow.keras.layers import Input,LSTM,Dense
|
| 6 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pickle
|
| 9 |
+
|
| 10 |
+
#initialize all variables
|
| 11 |
+
input_texts=[]
|
| 12 |
+
target_texts=[]
|
| 13 |
+
input_characters=set()
|
| 14 |
+
target_characters=set()
|
| 15 |
+
|
| 16 |
+
#read dataset file
|
| 17 |
+
with open('eng-french.txt','r',encoding='utf-8') as f:
|
| 18 |
+
rows=f.read().split('\n')
|
| 19 |
+
#read first 10,000 rows from dataset
|
| 20 |
+
for row in rows[:10000]:
|
| 21 |
+
#split input and target by '\t'=tab
|
| 22 |
+
input_text,target_text = row.split('\t')
|
| 23 |
+
#add '\t' at start and '\n' at end of text.
|
| 24 |
+
target_text='\t' + target_text + '\n'
|
| 25 |
+
input_texts.append(input_text.lower())
|
| 26 |
+
target_texts.append(target_text.lower())
|
| 27 |
+
#split character from text and add in respective sets
|
| 28 |
+
input_characters.update(list(input_text.lower()))
|
| 29 |
+
target_characters.update(list(target_text.lower()))
|
| 30 |
+
|
| 31 |
+
#sort input and target characters
|
| 32 |
+
input_characters = sorted(list(input_characters))
|
| 33 |
+
target_characters = sorted(list(target_characters))
|
| 34 |
+
#get the total length of input and target characters
|
| 35 |
+
num_en_chars = len(input_characters)
|
| 36 |
+
num_dec_chars = len(target_characters)
|
| 37 |
+
#get the maximum length of input and target text.
|
| 38 |
+
max_input_length = max([len(i) for i in input_texts])
|
| 39 |
+
max_target_length = max([len(i) for i in target_texts])
|
| 40 |
+
|
| 41 |
+
def bagofcharacters(input_texts,target_texts):
|
| 42 |
+
#inintialize encoder , decoder input and target data.
|
| 43 |
+
en_in_data=[] ; dec_in_data=[] ; dec_tr_data=[]
|
| 44 |
+
#padding variable with first character as 1 as rest all 0.
|
| 45 |
+
pad_en=[1]+[0]*(len(input_characters)-1)
|
| 46 |
+
pad_dec=[0]*(len(target_characters)) ; pad_dec[2]=1
|
| 47 |
+
#countvectorizer for one hot encoding as we want to tokenize character so
|
| 48 |
+
#anlyzer is true and None the stopwords action.
|
| 49 |
+
cv=CountVectorizer(binary=True,tokenizer=lambda txt: txt.split(),stop_words=None,analyzer='char')
|
| 50 |
+
for i,(input_t,target_t) in enumerate(zip(input_texts,target_texts)):
|
| 51 |
+
#fit the input characters into the CountVectorizer function
|
| 52 |
+
cv_inp= cv.fit(input_characters)
|
| 53 |
+
|
| 54 |
+
#transform the input text from the help of CountVectorizer fit.
|
| 55 |
+
#it character present than put 1 and 0 otherwise.
|
| 56 |
+
en_in_data.append(cv_inp.transform(list(input_t)).toarray().tolist())
|
| 57 |
+
cv_tar= cv.fit(target_characters)
|
| 58 |
+
dec_in_data.append(cv_tar.transform(list(target_t)).toarray().tolist())
|
| 59 |
+
#decoder target will be one timestep ahead because it will not consider
|
| 60 |
+
#the first character i.e. '\t'.
|
| 61 |
+
dec_tr_data.append(cv_tar.transform(list(target_t)[1:]).toarray().tolist())
|
| 62 |
+
|
| 63 |
+
#add padding variable if the length of the input or target text is smaller
|
| 64 |
+
#than their respective maximum input or target length.
|
| 65 |
+
if len(input_t) < max_input_length:
|
| 66 |
+
for _ in range(max_input_length-len(input_t)):
|
| 67 |
+
en_in_data[i].append(pad_en)
|
| 68 |
+
if len(target_t) < max_target_length:
|
| 69 |
+
for _ in range(max_target_length-len(target_t)):
|
| 70 |
+
dec_in_data[i].append(pad_dec)
|
| 71 |
+
if (len(target_t)-1) < max_target_length:
|
| 72 |
+
for _ in range(max_target_length-len(target_t)+1):
|
| 73 |
+
dec_tr_data[i].append(pad_dec)
|
| 74 |
+
|
| 75 |
+
#convert list to numpy array with data type float32
|
| 76 |
+
en_in_data=np.array(en_in_data,dtype="float32")
|
| 77 |
+
dec_in_data=np.array(dec_in_data,dtype="float32")
|
| 78 |
+
dec_tr_data=np.array(dec_tr_data,dtype="float32")
|
| 79 |
+
|
| 80 |
+
return en_in_data,dec_in_data,dec_tr_data
|
| 81 |
+
|
| 82 |
+
#create input object of total number of encoder characters
|
| 83 |
+
en_inputs = Input(shape=(None, num_en_chars))
|
| 84 |
+
#create LSTM with the hidden dimension of 256
|
| 85 |
+
#return state=True as we don't want output sequence.
|
| 86 |
+
encoder = LSTM(256, return_state=True)
|
| 87 |
+
#discard encoder output and store hidden and cell state.
|
| 88 |
+
en_outputs, state_h, state_c = encoder(en_inputs)
|
| 89 |
+
en_states = [state_h, state_c]
|
| 90 |
+
|
| 91 |
+
#create input object of total number of decoder characters
|
| 92 |
+
dec_inputs = Input(shape=(None, num_dec_chars))
|
| 93 |
+
#create LSTM with the hidden dimension of 256
|
| 94 |
+
#return state and return sequences as we want output sequence.
|
| 95 |
+
dec_lstm = LSTM(256, return_sequences=True, return_state=True)
|
| 96 |
+
#initialize the decoder model with the states on encoder.
|
| 97 |
+
dec_outputs, _, _ = dec_lstm(dec_inputs, initial_state=en_states)
|
| 98 |
+
#Output layer with shape of total number of decoder characters
|
| 99 |
+
dec_dense = Dense(num_dec_chars, activation="softmax")
|
| 100 |
+
dec_outputs = dec_dense(dec_outputs)
|
| 101 |
+
|
| 102 |
+
#create Model and store all variables
|
| 103 |
+
model = Model([en_inputs, dec_inputs], dec_outputs)
|
| 104 |
+
pickle.dump({'input_characters':input_characters,'target_characters':target_characters,
|
| 105 |
+
'max_input_length':max_input_length,'max_target_length':max_target_length,
|
| 106 |
+
'num_en_chars':num_en_chars,'num_dec_chars':num_dec_chars},open("training_data.pkl","wb"))
|
| 107 |
+
|
| 108 |
+
#load the data and train the model
|
| 109 |
+
# en_in_data,dec_in_data,dec_tr_data = bagofcharacters(input_texts,target_texts)
|
| 110 |
+
# model.compile(
|
| 111 |
+
# optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]
|
| 112 |
+
# )
|
| 113 |
+
# model.fit(
|
| 114 |
+
# [en_in_data, dec_in_data],
|
| 115 |
+
# dec_tr_data,
|
| 116 |
+
# batch_size=64,
|
| 117 |
+
# epochs=200,
|
| 118 |
+
# validation_split=0.2,
|
| 119 |
+
# )
|
| 120 |
+
# Save model
|
| 121 |
+
model.save("s2s")cd
|
| 122 |
+
#summary and model plot
|
| 123 |
+
model.summary()
|
| 124 |
+
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
|
| 125 |
+
|
s2s/keras_metadata.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:be4652d088201a57835ce518342d56905ac75859198c0b00d2d0ef37cd6b8ead
|
| 3 |
+
size 14765
|
s2s/saved_model.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5f3c8c32dad3dec377098cd2a83875af88ecb781adf80a40fd83c34285a375ba
|
| 3 |
+
size 1426649
|
s2s/variables/variables.data-00000-of-00001
ADDED
|
Binary file (2.64 MB). View file
|
|
|
s2s/variables/variables.index
ADDED
|
Binary file (629 Bytes). View file
|
|
|
training_data.pkl
ADDED
|
Binary file (537 Bytes). View file
|
|
|