Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +31 -63
src/main.py
CHANGED
@@ -2,86 +2,54 @@ 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
|
|
|
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 |
|
13 |
def translate_korean_to_english(text):
|
14 |
-
|
15 |
-
|
16 |
-
|
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 |
-
|
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 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
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():
|
|
|
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 transformers import pipeline
|
6 |
+
import torch
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
+
app.config['TITLE'] = 'ASL Translator'
|
10 |
+
|
11 |
+
# Force CPU usage
|
12 |
+
device = torch.device('cpu')
|
13 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device=device)
|
14 |
|
|
|
15 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
16 |
dataset, list_2000_tokens = dg.load_data()
|
17 |
|
18 |
def translate_korean_to_english(text):
|
19 |
+
# Check if input is Korean using Unicode range
|
20 |
+
if any('\u3131' <= char <= '\u318F' or '\uAC00' <= char <= '\uD7A3' for char in text):
|
21 |
+
translation = translator(text)[0]['translation_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return translation
|
23 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
input_text = request.form['inputSentence']
|
33 |
+
try:
|
34 |
+
english_text = translate_korean_to_english(input_text)
|
35 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
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=input_text,
|
48 |
+
english_translation=english_text,
|
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():
|