Spaces:
Building
Building
Update src/main-backup.py
Browse files- src/main-backup.py +161 -45
src/main-backup.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
import display_gloss as dg
|
3 |
import synonyms_preprocess as sp
|
4 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
@@ -10,6 +9,7 @@ import os
|
|
10 |
import requests
|
11 |
from urllib.parse import quote, unquote
|
12 |
import tempfile
|
|
|
13 |
|
14 |
app = Flask(__name__, static_folder='static')
|
15 |
app.config['TITLE'] = 'Sign Language Translate'
|
@@ -17,58 +17,102 @@ app.config['TITLE'] = 'Sign Language Translate'
|
|
17 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
18 |
dataset, list_2000_tokens = dg.load_data()
|
19 |
|
20 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
url = "https://translate.googleapis.com/translate_a/single"
|
23 |
params = {
|
24 |
-
"client": "gtx",
|
25 |
"sl": "ko",
|
26 |
"tl": "en",
|
27 |
"dt": "t",
|
28 |
-
"q": text.
|
29 |
}
|
30 |
response = requests.get(url, params=params)
|
31 |
-
if response.status_code
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
-
print(f"
|
38 |
return text
|
39 |
|
40 |
-
def
|
|
|
41 |
try:
|
42 |
-
|
43 |
-
for frame in dg.generate_video(gloss_list, dataset, list_2000_tokens):
|
44 |
-
frame_data = frame.split(b'\r\n\r\n')[1]
|
45 |
-
nparr = np.frombuffer(frame_data, np.uint8)
|
46 |
-
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
47 |
-
frames.append(img)
|
48 |
|
49 |
-
if
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
temp_path = temp_file.name
|
57 |
-
|
58 |
-
out = cv2.VideoWriter(temp_path, fourcc, 25, (width, height))
|
59 |
-
|
60 |
-
for frame in frames:
|
61 |
-
out.write(frame)
|
62 |
-
out.release()
|
63 |
-
|
64 |
-
with open(temp_path, 'rb') as f:
|
65 |
-
video_bytes = f.read()
|
66 |
-
|
67 |
-
os.remove(temp_path)
|
68 |
-
return video_bytes
|
69 |
except Exception as e:
|
70 |
-
print(f"
|
71 |
-
|
72 |
|
73 |
@app.route('/')
|
74 |
def index():
|
@@ -82,19 +126,48 @@ def result():
|
|
82 |
return render_template('error.html', error="Please enter text to translate")
|
83 |
|
84 |
try:
|
|
|
85 |
english_text = translate_korean_to_english(input_text)
|
86 |
if not english_text:
|
87 |
raise Exception("Translation failed")
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
90 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
91 |
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
for gloss in gloss_list_lower]
|
97 |
-
gloss_sentence_after_synonym = " ".join(gloss_list)
|
98 |
|
99 |
return render_template('result.html',
|
100 |
title=app.config['TITLE'],
|
@@ -105,6 +178,49 @@ def result():
|
|
105 |
except Exception as e:
|
106 |
return render_template('error.html', error=f"Translation error: {str(e)}")
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
@app.route('/video_feed')
|
109 |
def video_feed():
|
110 |
sentence = request.args.get('gloss_sentence_to_display', '')
|
@@ -137,4 +253,4 @@ def download_video(gloss_sentence):
|
|
137 |
return f"Error downloading video: {str(e)}", 500
|
138 |
|
139 |
if __name__ == "__main__":
|
140 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
|
1 |
import display_gloss as dg
|
2 |
import synonyms_preprocess as sp
|
3 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
|
|
9 |
import requests
|
10 |
from urllib.parse import quote, unquote
|
11 |
import tempfile
|
12 |
+
import re
|
13 |
|
14 |
app = Flask(__name__, static_folder='static')
|
15 |
app.config['TITLE'] = 'Sign Language Translate'
|
|
|
17 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
18 |
dataset, list_2000_tokens = dg.load_data()
|
19 |
|
20 |
+
def clean_quotes(text):
|
21 |
+
"""따옴표 정리 함수"""
|
22 |
+
text = re.sub(r"'+", "'", text)
|
23 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
24 |
+
return text
|
25 |
+
|
26 |
+
def is_korean(text):
|
27 |
+
"""한글이 포함되어 있는지 확인"""
|
28 |
+
return bool(re.search('[가-힣]', text))
|
29 |
+
|
30 |
+
def is_english(text):
|
31 |
+
"""텍스트가 영어인지 확인하는 함수"""
|
32 |
+
text_without_quotes = re.sub(r"'[^']*'|\s", "", text)
|
33 |
+
return bool(re.match(r'^[A-Za-z.,!?-]*$', text_without_quotes))
|
34 |
+
|
35 |
+
def normalize_quotes(text):
|
36 |
+
"""따옴표 형식을 정규화하는 함수"""
|
37 |
+
text = re.sub(r"'+", "'", text)
|
38 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
39 |
+
|
40 |
+
# 이미 따옴표로 묶인 단어가 있으면 그대로 반환
|
41 |
+
if re.search(r"'[^']*'", text):
|
42 |
+
return text
|
43 |
+
|
44 |
+
return text
|
45 |
+
|
46 |
+
def find_quoted_words(text):
|
47 |
+
"""작은따옴표로 묶인 단어들을 찾는 함수"""
|
48 |
+
return re.findall(r"'([^']*)'", text)
|
49 |
+
|
50 |
+
def spell_out_word(word):
|
51 |
+
"""단어를 개별 알파벳으로 분리하는 함수"""
|
52 |
+
return ' '.join(list(word.lower()))
|
53 |
+
|
54 |
+
def translate_korean_text(text):
|
55 |
+
"""한글 전용 번역 함수"""
|
56 |
try:
|
57 |
+
quoted_match = re.search(r"'([^']*)'", text)
|
58 |
+
if not quoted_match:
|
59 |
+
return text
|
60 |
+
|
61 |
+
quoted_word = quoted_match.group(1)
|
62 |
+
|
63 |
url = "https://translate.googleapis.com/translate_a/single"
|
64 |
params = {
|
65 |
+
"client": "gtx",
|
66 |
"sl": "ko",
|
67 |
"tl": "en",
|
68 |
"dt": "t",
|
69 |
+
"q": text.replace(f"'{quoted_word}'", "XXXXX")
|
70 |
}
|
71 |
response = requests.get(url, params=params)
|
72 |
+
if response.status_code != 200:
|
73 |
+
return text
|
74 |
+
|
75 |
+
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
76 |
+
|
77 |
+
if re.match(r'^[A-Za-z]+$', quoted_word):
|
78 |
+
proper_noun = quoted_word.upper()
|
79 |
else:
|
80 |
+
params["q"] = quoted_word
|
81 |
+
response = requests.get(url, params=params)
|
82 |
+
if response.status_code == 200:
|
83 |
+
proper_noun = response.json()[0][0][0].upper()
|
84 |
+
else:
|
85 |
+
proper_noun = quoted_word.upper()
|
86 |
+
|
87 |
+
final_text = translated_text.replace("XXXXX", f"'{proper_noun}'")
|
88 |
+
final_text = re.sub(r'\bNAME\b', 'name', final_text)
|
89 |
+
final_text = final_text.replace(" .", ".")
|
90 |
+
|
91 |
+
return final_text
|
92 |
+
|
93 |
except Exception as e:
|
94 |
+
print(f"Korean translation error: {e}")
|
95 |
return text
|
96 |
|
97 |
+
def translate_korean_to_english(text):
|
98 |
+
"""전체 텍스트 번역 함수"""
|
99 |
try:
|
100 |
+
text = normalize_quotes(text)
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
if is_english(text):
|
103 |
+
quoted_match = re.search(r"'([^']*)'", text)
|
104 |
+
if quoted_match:
|
105 |
+
quoted_word = quoted_match.group(1).upper()
|
106 |
+
text = re.sub(r"'[^']*'", f"'{quoted_word}'", text, 1)
|
107 |
+
return text
|
108 |
|
109 |
+
if is_korean(text):
|
110 |
+
return translate_korean_text(text)
|
111 |
+
|
112 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
except Exception as e:
|
114 |
+
print(f"Translation error: {e}")
|
115 |
+
return text
|
116 |
|
117 |
@app.route('/')
|
118 |
def index():
|
|
|
126 |
return render_template('error.html', error="Please enter text to translate")
|
127 |
|
128 |
try:
|
129 |
+
input_text = normalize_quotes(input_text)
|
130 |
english_text = translate_korean_to_english(input_text)
|
131 |
if not english_text:
|
132 |
raise Exception("Translation failed")
|
133 |
+
|
134 |
+
quoted_words = find_quoted_words(english_text)
|
135 |
+
|
136 |
+
clean_english = re.sub(r"'([^']*)'", r"\1", english_text)
|
137 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=clean_english)
|
138 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
139 |
|
140 |
+
processed_gloss = []
|
141 |
+
words = generated_gloss.split()
|
142 |
+
|
143 |
+
for word in words:
|
144 |
+
word_upper = word.upper()
|
145 |
+
if quoted_words and word_upper in [w.upper() for w in quoted_words]:
|
146 |
+
spelled_word = spell_out_word(word)
|
147 |
+
processed_gloss.extend(['FINGERSPELL-START'] + spelled_word.split() + ['FINGERSPELL-END'])
|
148 |
+
else:
|
149 |
+
processed_gloss.append(word.lower())
|
150 |
+
|
151 |
+
gloss_sentence_before_synonym = " ".join(processed_gloss)
|
152 |
+
|
153 |
+
final_gloss = []
|
154 |
+
i = 0
|
155 |
+
while i < len(processed_gloss):
|
156 |
+
if processed_gloss[i] == 'FINGERSPELL-START':
|
157 |
+
final_gloss.extend(processed_gloss[i:i+2])
|
158 |
+
i += 2
|
159 |
+
while i < len(processed_gloss) and processed_gloss[i] != 'FINGERSPELL-END':
|
160 |
+
final_gloss.append(processed_gloss[i])
|
161 |
+
i += 1
|
162 |
+
if i < len(processed_gloss):
|
163 |
+
final_gloss.append(processed_gloss[i])
|
164 |
+
i += 1
|
165 |
+
else:
|
166 |
+
word = processed_gloss[i]
|
167 |
+
final_gloss.append(sp.find_synonyms(word, nlp, dict_docs_spacy, list_2000_tokens))
|
168 |
+
i += 1
|
169 |
|
170 |
+
gloss_sentence_after_synonym = " ".join(final_gloss)
|
|
|
|
|
171 |
|
172 |
return render_template('result.html',
|
173 |
title=app.config['TITLE'],
|
|
|
178 |
except Exception as e:
|
179 |
return render_template('error.html', error=f"Translation error: {str(e)}")
|
180 |
|
181 |
+
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
182 |
+
try:
|
183 |
+
frames = []
|
184 |
+
is_spelling = False
|
185 |
+
|
186 |
+
for gloss in gloss_list:
|
187 |
+
if gloss == 'FINGERSPELL-START':
|
188 |
+
is_spelling = True
|
189 |
+
continue
|
190 |
+
elif gloss == 'FINGERSPELL-END':
|
191 |
+
is_spelling = False
|
192 |
+
continue
|
193 |
+
|
194 |
+
for frame in dg.generate_video([gloss], dataset, list_2000_tokens):
|
195 |
+
frame_data = frame.split(b'\r\n\r\n')[1]
|
196 |
+
nparr = np.frombuffer(frame_data, np.uint8)
|
197 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
198 |
+
frames.append(img)
|
199 |
+
|
200 |
+
if not frames:
|
201 |
+
raise Exception("No frames generated")
|
202 |
+
|
203 |
+
height, width = frames[0].shape[:2]
|
204 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
205 |
+
|
206 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
|
207 |
+
temp_path = temp_file.name
|
208 |
+
|
209 |
+
out = cv2.VideoWriter(temp_path, fourcc, 25, (width, height))
|
210 |
+
|
211 |
+
for frame in frames:
|
212 |
+
out.write(frame)
|
213 |
+
out.release()
|
214 |
+
|
215 |
+
with open(temp_path, 'rb') as f:
|
216 |
+
video_bytes = f.read()
|
217 |
+
|
218 |
+
os.remove(temp_path)
|
219 |
+
return video_bytes
|
220 |
+
except Exception as e:
|
221 |
+
print(f"Error generating video: {str(e)}")
|
222 |
+
raise
|
223 |
+
|
224 |
@app.route('/video_feed')
|
225 |
def video_feed():
|
226 |
sentence = request.args.get('gloss_sentence_to_display', '')
|
|
|
253 |
return f"Error downloading video: {str(e)}", 500
|
254 |
|
255 |
if __name__ == "__main__":
|
256 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|