Spaces:
No application file
No application file
main app
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from tensorflow.keras.utils import pad_sequences
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
encoder_model = load_model("encoder_model.keras")
|
| 11 |
+
decoder_model = load_model("decoder_model.keras")
|
| 12 |
+
|
| 13 |
+
with open("encoder_tokenizer.pkl","rb") as f:
|
| 14 |
+
encoder_tokenizer = pickle.load(f)
|
| 15 |
+
|
| 16 |
+
with open("decoder_tokenizer.pkl","rb") as f:
|
| 17 |
+
decoder_tokenizer = pickle.load(f)
|
| 18 |
+
|
| 19 |
+
with open("config.pkl","rb") as f:
|
| 20 |
+
config = pickle.load(f)
|
| 21 |
+
|
| 22 |
+
input_maxln = config["input_mxlen"]
|
| 23 |
+
output_maxln = config["output_mxlen"]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def translate(sentence):
|
| 27 |
+
|
| 28 |
+
sequence = encoder_tokenizer.texts_to_sequences([sentence])
|
| 29 |
+
|
| 30 |
+
sequence = pad_sequences(sequence,
|
| 31 |
+
maxlen=input_maxln,
|
| 32 |
+
padding="post")
|
| 33 |
+
|
| 34 |
+
states = encoder_model.predict(sequence, verbose=0)
|
| 35 |
+
|
| 36 |
+
start = decoder_tokenizer.word_index["<start>"]
|
| 37 |
+
end = decoder_tokenizer.word_index["<end>"]
|
| 38 |
+
|
| 39 |
+
target_seq = np.array([[start]])
|
| 40 |
+
|
| 41 |
+
translated = []
|
| 42 |
+
|
| 43 |
+
while True:
|
| 44 |
+
|
| 45 |
+
output, h, c = decoder_model.predict(
|
| 46 |
+
[target_seq] + states,
|
| 47 |
+
verbose=0
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
token = np.argmax(output[0, -1, :])
|
| 51 |
+
|
| 52 |
+
if token == end:
|
| 53 |
+
break
|
| 54 |
+
|
| 55 |
+
word = decoder_tokenizer.index_word.get(token, "")
|
| 56 |
+
|
| 57 |
+
if word:
|
| 58 |
+
translated.append(word)
|
| 59 |
+
|
| 60 |
+
target_seq = np.array([[token]])
|
| 61 |
+
states = [h, c]
|
| 62 |
+
|
| 63 |
+
if len(translated) > output_maxln:
|
| 64 |
+
break
|
| 65 |
+
|
| 66 |
+
return " ".join(translated)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@app.route("/")
|
| 70 |
+
def home():
|
| 71 |
+
return render_template("index.html")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@app.route("/translate", methods=["POST"])
|
| 75 |
+
def translate_api():
|
| 76 |
+
|
| 77 |
+
text = request.json["text"]
|
| 78 |
+
|
| 79 |
+
prediction = translate(text)
|
| 80 |
+
|
| 81 |
+
return jsonify({
|
| 82 |
+
"translation": prediction
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
app.run(debug=True)
|