Spaces:
Paused
Paused
Update src/main.py
Browse files- src/main.py +25 -33
src/main.py
CHANGED
|
@@ -1,59 +1,51 @@
|
|
| 1 |
import display_gloss as dg
|
| 2 |
import synonyms_preprocess as sp
|
| 3 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
| 4 |
-
from flask import Flask,
|
|
|
|
| 5 |
|
| 6 |
-
# ---- Initialise Flask App
|
| 7 |
-
#
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 13 |
dataset, list_2000_tokens = dg.load_data()
|
|
|
|
| 14 |
|
| 15 |
-
# ---- Render the homepage template
|
| 16 |
-
#
|
| 17 |
@app.route('/')
|
| 18 |
def index():
|
| 19 |
-
|
| 20 |
return render_template('index.html')
|
| 21 |
|
| 22 |
-
# ---- Translate english input sentence into gloss sentence
|
| 23 |
-
#
|
| 24 |
@app.route('/translate/', methods=['POST'])
|
| 25 |
def result():
|
| 26 |
-
|
| 27 |
if request.method == 'POST':
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
| 33 |
-
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()
|
| 34 |
gloss_sentence_before_synonym = " ".join(gloss_list_lower)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# ---- Substitute gloss tokens with synonyms if not in the common token list
|
| 37 |
-
#
|
| 38 |
-
gloss_list = [sp.find_synonyms(gloss, nlp, dict_docs_spacy, list_2000_tokens) for gloss in gloss_list_lower]
|
| 39 |
-
gloss_sentence_after_synonym = " ".join(gloss_list)
|
| 40 |
-
|
| 41 |
-
# ---- Render the result template with both versions of the gloss sentence
|
| 42 |
-
#
|
| 43 |
-
return render_template('translate.html',\
|
| 44 |
-
sentence=sentence,\
|
| 45 |
-
gloss_sentence_before_synonym=gloss_sentence_before_synonym,\
|
| 46 |
-
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
| 47 |
-
|
| 48 |
-
# ---- Generate video streaming from gloss_sentence
|
| 49 |
-
#
|
| 50 |
@app.route('/video_feed')
|
| 51 |
def video_feed():
|
| 52 |
-
|
| 53 |
sentence = request.args.get('gloss_sentence_to_display', '')
|
| 54 |
gloss_list = sentence.split()
|
| 55 |
-
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
app.debug = True
|
| 59 |
-
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
|
|
| 1 |
import display_gloss as dg
|
| 2 |
import synonyms_preprocess as sp
|
| 3 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
| 4 |
+
from flask import Flask, render_template, Response, request
|
| 5 |
+
from googletrans import Translator
|
| 6 |
|
|
|
|
|
|
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Initialize translators and data
|
|
|
|
| 10 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 11 |
dataset, list_2000_tokens = dg.load_data()
|
| 12 |
+
ko_to_en_translator = Translator()
|
| 13 |
|
|
|
|
|
|
|
| 14 |
@app.route('/')
|
| 15 |
def index():
|
|
|
|
| 16 |
return render_template('index.html')
|
| 17 |
|
|
|
|
|
|
|
| 18 |
@app.route('/translate/', methods=['POST'])
|
| 19 |
def result():
|
|
|
|
| 20 |
if request.method == 'POST':
|
| 21 |
+
# Get Korean input and translate to English
|
| 22 |
+
korean_sentence = request.form['inputSentence']
|
| 23 |
+
english_translation = ko_to_en_translator.translate(korean_sentence, src='ko', dest='en').text
|
| 24 |
+
|
| 25 |
+
# Translate English to ASL gloss
|
| 26 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_translation)
|
| 27 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
| 28 |
+
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()]
|
| 29 |
gloss_sentence_before_synonym = " ".join(gloss_list_lower)
|
| 30 |
+
|
| 31 |
+
# Process synonyms
|
| 32 |
+
gloss_list = [sp.find_synonyms(gloss, nlp, dict_docs_spacy, list_2000_tokens)
|
| 33 |
+
for gloss in gloss_list_lower]
|
| 34 |
+
gloss_sentence_after_synonym = " ".join(gloss_list)
|
| 35 |
+
|
| 36 |
+
return render_template('translate.html',
|
| 37 |
+
original_sentence=korean_sentence,
|
| 38 |
+
english_translation=english_translation,
|
| 39 |
+
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
| 40 |
+
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
@app.route('/video_feed')
|
| 43 |
def video_feed():
|
|
|
|
| 44 |
sentence = request.args.get('gloss_sentence_to_display', '')
|
| 45 |
gloss_list = sentence.split()
|
| 46 |
+
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
| 47 |
+
mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
app.debug = True
|
| 51 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|