Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +20 -35
src/main.py
CHANGED
@@ -1,41 +1,26 @@
|
|
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 |
-
|
6 |
-
import torch
|
7 |
-
import os
|
8 |
|
9 |
-
app = Flask(__name__)
|
10 |
-
app.config['TITLE'] = '
|
11 |
-
|
12 |
-
# 캐시 디렉토리 설정
|
13 |
-
cache_dir = "/tmp/huggingface"
|
14 |
-
if not os.path.exists(cache_dir):
|
15 |
-
os.makedirs(cache_dir, exist_ok=True)
|
16 |
-
os.environ['TRANSFORMERS_CACHE'] = cache_dir
|
17 |
-
os.environ['HF_HOME'] = cache_dir
|
18 |
-
|
19 |
-
# CPU 설정
|
20 |
-
device = torch.device('cpu')
|
21 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
22 |
-
|
23 |
-
# 번역 모델 초기화
|
24 |
-
model_name = "Helsinki-NLP/opus-mt-ko-en"
|
25 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
|
26 |
-
model = AutoModelForSeq2SeqTranslation.from_pretrained(model_name, cache_dir=cache_dir)
|
27 |
-
model = model.to(device)
|
28 |
|
29 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
30 |
dataset, list_2000_tokens = dg.load_data()
|
31 |
|
32 |
def translate_korean_to_english(text):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
@app.route('/')
|
41 |
def index():
|
@@ -44,10 +29,10 @@ def index():
|
|
44 |
@app.route('/translate/', methods=['POST'])
|
45 |
def result():
|
46 |
if request.method == 'POST':
|
47 |
-
|
48 |
try:
|
49 |
-
|
50 |
-
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=
|
51 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
52 |
|
53 |
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()]
|
@@ -59,8 +44,8 @@ def result():
|
|
59 |
|
60 |
return render_template('result.html',
|
61 |
title=app.config['TITLE'],
|
62 |
-
original_sentence=
|
63 |
-
english_translation=
|
64 |
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
65 |
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
66 |
except Exception as e:
|
@@ -74,4 +59,4 @@ def video_feed():
|
|
74 |
mimetype='multipart/x-mixed-replace; boundary=frame')
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
-
app.run(host="0.0.0.0", port=
|
|
|
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():
|
|
|
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()]
|
|
|
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:
|
|
|
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)
|