Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +48 -32
src/main.py
CHANGED
@@ -1,47 +1,57 @@
|
|
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
|
|
|
7 |
import os
|
8 |
|
9 |
app = Flask(__name__, static_folder='static')
|
10 |
app.config['TITLE'] = 'Sign Language Translate'
|
11 |
|
12 |
-
# Set cache directory
|
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 |
-
# Force CPU usage
|
20 |
-
device = torch.device('cpu')
|
21 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
22 |
-
|
23 |
-
# Load pre-trained Korean-English translation model
|
24 |
-
model_name = "Helsinki-NLP/opus-mt-ko-en"
|
25 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
|
26 |
-
model = MarianMTModel.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 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
print(f"Translation error: {e}")
|
43 |
return text
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
@app.route('/')
|
46 |
def index():
|
47 |
return render_template('index.html', title=app.config['TITLE'])
|
@@ -52,10 +62,6 @@ def result():
|
|
52 |
input_text = request.form['inputSentence']
|
53 |
try:
|
54 |
english_text = translate_korean_to_english(input_text)
|
55 |
-
|
56 |
-
if english_text == input_text and any('\u3131' <= char <= '\u318F' or '\uAC00' <= char <= '\uD7A3' for char in input_text):
|
57 |
-
raise Exception("Translation failed")
|
58 |
-
|
59 |
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
60 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
61 |
|
@@ -73,7 +79,6 @@ def result():
|
|
73 |
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
74 |
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
75 |
except Exception as e:
|
76 |
-
print(f"Error in translation process: {e}")
|
77 |
return render_template('error.html', error=str(e))
|
78 |
|
79 |
@app.route('/video_feed')
|
@@ -83,5 +88,16 @@ def video_feed():
|
|
83 |
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
84 |
mimetype='multipart/x-mixed-replace; boundary=frame')
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
if __name__ == "__main__":
|
87 |
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
|
4 |
+
from flask import Flask, render_template, Response, request, send_file
|
5 |
+
import io
|
6 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
import os
|
9 |
|
10 |
app = Flask(__name__, static_folder='static')
|
11 |
app.config['TITLE'] = 'Sign Language Translate'
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
14 |
dataset, list_2000_tokens = dg.load_data()
|
15 |
|
16 |
def translate_korean_to_english(text):
|
17 |
try:
|
18 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
19 |
+
params = {
|
20 |
+
"client": "gtx",
|
21 |
+
"sl": "ko",
|
22 |
+
"tl": "en",
|
23 |
+
"dt": "t",
|
24 |
+
"q": text
|
25 |
+
}
|
26 |
+
response = requests.get(url, params=params)
|
27 |
+
return response.json()[0][0][0]
|
28 |
except Exception as e:
|
29 |
print(f"Translation error: {e}")
|
30 |
return text
|
31 |
|
32 |
+
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
33 |
+
frames = []
|
34 |
+
for frame in dg.generate_video(gloss_list, dataset, list_2000_tokens):
|
35 |
+
frame_data = frame.split(b'\r\n\r\n')[1]
|
36 |
+
nparr = np.frombuffer(frame_data, np.uint8)
|
37 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
38 |
+
frames.append(img)
|
39 |
+
|
40 |
+
height, width = frames[0].shape[:2]
|
41 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
42 |
+
temp_path = os.path.join('/tmp', 'temp.mp4')
|
43 |
+
out = cv2.VideoWriter(temp_path, fourcc, 25, (width, height))
|
44 |
+
|
45 |
+
for frame in frames:
|
46 |
+
out.write(frame)
|
47 |
+
out.release()
|
48 |
+
|
49 |
+
with open(temp_path, 'rb') as f:
|
50 |
+
video_bytes = f.read()
|
51 |
+
|
52 |
+
os.remove(temp_path)
|
53 |
+
return video_bytes
|
54 |
+
|
55 |
@app.route('/')
|
56 |
def index():
|
57 |
return render_template('index.html', title=app.config['TITLE'])
|
|
|
62 |
input_text = request.form['inputSentence']
|
63 |
try:
|
64 |
english_text = translate_korean_to_english(input_text)
|
|
|
|
|
|
|
|
|
65 |
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
66 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
67 |
|
|
|
79 |
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
80 |
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
81 |
except Exception as e:
|
|
|
82 |
return render_template('error.html', error=str(e))
|
83 |
|
84 |
@app.route('/video_feed')
|
|
|
88 |
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
89 |
mimetype='multipart/x-mixed-replace; boundary=frame')
|
90 |
|
91 |
+
@app.route('/download_video/<gloss_sentence>')
|
92 |
+
def download_video(gloss_sentence):
|
93 |
+
gloss_list = gloss_sentence.split()
|
94 |
+
video_bytes = generate_complete_video(gloss_list, dataset, list_2000_tokens)
|
95 |
+
return send_file(
|
96 |
+
io.BytesIO(video_bytes),
|
97 |
+
mimetype='video/mp4',
|
98 |
+
as_attachment=True,
|
99 |
+
download_name='sign_language.mp4'
|
100 |
+
)
|
101 |
+
|
102 |
if __name__ == "__main__":
|
103 |
app.run(host="0.0.0.0", port=7860, debug=True)
|