Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +59 -27
src/main.py
CHANGED
@@ -1,12 +1,12 @@
|
|
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 |
import requests
|
6 |
|
7 |
-
app = Flask(__name__
|
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 |
|
@@ -16,40 +16,72 @@ def translate_korean_to_english(text):
|
|
16 |
"client": "gtx",
|
17 |
"sl": "ko",
|
18 |
"tl": "en",
|
19 |
-
"dt": "t",
|
20 |
"q": text
|
21 |
}
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
@app.route('/')
|
26 |
def index():
|
27 |
-
return render_template('index.html'
|
28 |
|
29 |
@app.route('/translate/', methods=['POST'])
|
30 |
def result():
|
31 |
if request.method == 'POST':
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
@app.route('/video_feed')
|
55 |
def video_feed():
|
|
|
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 |
import requests
|
6 |
|
7 |
+
app = Flask(__name__)
|
|
|
8 |
|
9 |
+
# Initialize data
|
10 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
11 |
dataset, list_2000_tokens = dg.load_data()
|
12 |
|
|
|
16 |
"client": "gtx",
|
17 |
"sl": "ko",
|
18 |
"tl": "en",
|
19 |
+
"dt": ["t", "bd"], # Added "bd" for better translation
|
20 |
"q": text
|
21 |
}
|
22 |
+
try:
|
23 |
+
response = requests.get(url, params=params)
|
24 |
+
translation = response.json()[0][0][0]
|
25 |
+
# Basic post-processing
|
26 |
+
translation = translation.replace("It is", "").replace("There is", "").strip()
|
27 |
+
return translation
|
28 |
+
except Exception as e:
|
29 |
+
print(f"Translation error: {e}")
|
30 |
+
return text
|
31 |
+
|
32 |
+
def process_gloss_conversion(english_text):
|
33 |
+
# Custom mapping for common Korean-specific terms
|
34 |
+
term_mapping = {
|
35 |
+
"Korea": "KOREA",
|
36 |
+
"Seoul": "SEOUL",
|
37 |
+
"four seasons": "FOUR SEASON",
|
38 |
+
"beautiful": "BEAUTIFUL",
|
39 |
+
"country": "COUNTRY"
|
40 |
+
}
|
41 |
+
|
42 |
+
# Apply mappings before ASL conversion
|
43 |
+
for term, replacement in term_mapping.items():
|
44 |
+
english_text = english_text.replace(term.lower(), replacement)
|
45 |
+
|
46 |
+
return english_text
|
47 |
|
48 |
@app.route('/')
|
49 |
def index():
|
50 |
+
return render_template('index.html')
|
51 |
|
52 |
@app.route('/translate/', methods=['POST'])
|
53 |
def result():
|
54 |
if request.method == 'POST':
|
55 |
+
input_text = request.form['inputSentence']
|
56 |
+
|
57 |
+
# Check if input is Korean (simplified check)
|
58 |
+
is_korean = any(ord('가') <= ord(char) <= ord('힣') for char in input_text)
|
59 |
+
|
60 |
+
if is_korean:
|
61 |
+
english_translation = translate_korean_to_english(input_text)
|
62 |
+
else:
|
63 |
+
english_translation = input_text
|
|
|
|
|
|
|
64 |
|
65 |
+
# Pre-process for better ASL conversion
|
66 |
+
processed_english = process_gloss_conversion(english_translation)
|
67 |
+
|
68 |
+
# Convert to ASL
|
69 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=processed_english)
|
70 |
+
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
71 |
+
|
72 |
+
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()]
|
73 |
+
gloss_sentence_before_synonym = " ".join(gloss_list_lower)
|
74 |
+
|
75 |
+
# Apply custom synonym rules
|
76 |
+
gloss_list = [sp.find_synonyms(gloss, nlp, dict_docs_spacy, list_2000_tokens)
|
77 |
+
for gloss in gloss_list_lower]
|
78 |
+
gloss_sentence_after_synonym = " ".join(gloss_list)
|
79 |
+
|
80 |
+
return render_template('result.html',
|
81 |
+
original_sentence=input_text,
|
82 |
+
english_translation=english_translation,
|
83 |
+
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
84 |
+
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
85 |
|
86 |
@app.route('/video_feed')
|
87 |
def video_feed():
|