Spaces:
Paused
Paused
Create main-backup.py
Browse files- src/main-backup.py +62 -0
src/main-backup.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, jsonify
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__, static_folder='static')
|
| 8 |
+
app.config['TITLE'] = 'Sign Language Translate'
|
| 9 |
+
|
| 10 |
+
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 11 |
+
dataset, list_2000_tokens = dg.load_data()
|
| 12 |
+
|
| 13 |
+
def translate_korean_to_english(text):
|
| 14 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
| 15 |
+
params = {
|
| 16 |
+
"client": "gtx",
|
| 17 |
+
"sl": "ko",
|
| 18 |
+
"tl": "en",
|
| 19 |
+
"dt": "t",
|
| 20 |
+
"q": text
|
| 21 |
+
}
|
| 22 |
+
response = requests.get(url, params=params)
|
| 23 |
+
return response.json()[0][0][0]
|
| 24 |
+
|
| 25 |
+
@app.route('/')
|
| 26 |
+
def index():
|
| 27 |
+
return render_template('index.html', title=app.config['TITLE'])
|
| 28 |
+
|
| 29 |
+
@app.route('/translate/', methods=['POST'])
|
| 30 |
+
def result():
|
| 31 |
+
if request.method == 'POST':
|
| 32 |
+
korean_sentence = request.form['inputSentence']
|
| 33 |
+
try:
|
| 34 |
+
english_translation = translate_korean_to_english(korean_sentence)
|
| 35 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_translation)
|
| 36 |
+
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
| 37 |
+
|
| 38 |
+
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()]
|
| 39 |
+
gloss_sentence_before_synonym = " ".join(gloss_list_lower)
|
| 40 |
+
|
| 41 |
+
gloss_list = [sp.find_synonyms(gloss, nlp, dict_docs_spacy, list_2000_tokens)
|
| 42 |
+
for gloss in gloss_list_lower]
|
| 43 |
+
gloss_sentence_after_synonym = " ".join(gloss_list)
|
| 44 |
+
|
| 45 |
+
return render_template('result.html',
|
| 46 |
+
title=app.config['TITLE'],
|
| 47 |
+
original_sentence=korean_sentence,
|
| 48 |
+
english_translation=english_translation,
|
| 49 |
+
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
| 50 |
+
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return render_template('error.html', error=str(e))
|
| 53 |
+
|
| 54 |
+
@app.route('/video_feed')
|
| 55 |
+
def video_feed():
|
| 56 |
+
sentence = request.args.get('gloss_sentence_to_display', '')
|
| 57 |
+
gloss_list = sentence.split()
|
| 58 |
+
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
| 59 |
+
mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|