Spaces:
Building
Building
Update src/main-backup.py
Browse files- src/main-backup.py +96 -18
src/main-backup.py
CHANGED
@@ -1,8 +1,15 @@
|
|
|
|
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__, static_folder='static')
|
8 |
app.config['TITLE'] = 'Sign Language Translate'
|
@@ -11,16 +18,57 @@ 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 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
@app.route('/')
|
26 |
def index():
|
@@ -29,10 +77,16 @@ def index():
|
|
29 |
@app.route('/translate/', methods=['POST'])
|
30 |
def result():
|
31 |
if request.method == 'POST':
|
32 |
-
|
|
|
|
|
|
|
33 |
try:
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
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,12 +98,12 @@ def result():
|
|
44 |
|
45 |
return render_template('result.html',
|
46 |
title=app.config['TITLE'],
|
47 |
-
original_sentence=
|
48 |
-
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:
|
52 |
-
return render_template('error.html', error=str(e))
|
53 |
|
54 |
@app.route('/video_feed')
|
55 |
def video_feed():
|
@@ -58,5 +112,29 @@ def video_feed():
|
|
58 |
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
59 |
mimetype='multipart/x-mixed-replace; boundary=frame')
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if __name__ == "__main__":
|
62 |
-
app.run(host="0.0.0.0", port=
|
|
|
1 |
+
# app.py
|
2 |
import display_gloss as dg
|
3 |
import synonyms_preprocess as sp
|
4 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
5 |
+
from flask import Flask, render_template, Response, request, send_file
|
6 |
+
import io
|
7 |
+
import cv2
|
8 |
+
import numpy as np
|
9 |
+
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'
|
|
|
18 |
dataset, list_2000_tokens = dg.load_data()
|
19 |
|
20 |
def translate_korean_to_english(text):
|
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.strip()
|
29 |
+
}
|
30 |
+
response = requests.get(url, params=params)
|
31 |
+
if response.status_code == 200:
|
32 |
+
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
33 |
+
return translated_text
|
34 |
+
else:
|
35 |
+
raise Exception(f"Translation API returned status code: {response.status_code}")
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Translation error: {e}")
|
38 |
+
return text
|
39 |
+
|
40 |
+
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
41 |
+
try:
|
42 |
+
frames = []
|
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 not frames:
|
50 |
+
raise Exception("No frames generated")
|
51 |
+
|
52 |
+
height, width = frames[0].shape[:2]
|
53 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
54 |
+
|
55 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
|
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"Error generating video: {str(e)}")
|
71 |
+
raise
|
72 |
|
73 |
@app.route('/')
|
74 |
def index():
|
|
|
77 |
@app.route('/translate/', methods=['POST'])
|
78 |
def result():
|
79 |
if request.method == 'POST':
|
80 |
+
input_text = request.form['inputSentence'].strip()
|
81 |
+
if not input_text:
|
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 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
90 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
91 |
|
92 |
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()]
|
|
|
98 |
|
99 |
return render_template('result.html',
|
100 |
title=app.config['TITLE'],
|
101 |
+
original_sentence=input_text,
|
102 |
+
english_translation=english_text,
|
103 |
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
104 |
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
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():
|
|
|
112 |
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
113 |
mimetype='multipart/x-mixed-replace; boundary=frame')
|
114 |
|
115 |
+
@app.route('/download_video/<path:gloss_sentence>')
|
116 |
+
def download_video(gloss_sentence):
|
117 |
+
try:
|
118 |
+
decoded_sentence = unquote(gloss_sentence)
|
119 |
+
gloss_list = decoded_sentence.split()
|
120 |
+
|
121 |
+
if not gloss_list:
|
122 |
+
return "No gloss provided", 400
|
123 |
+
|
124 |
+
video_bytes = generate_complete_video(gloss_list, dataset, list_2000_tokens)
|
125 |
+
|
126 |
+
if not video_bytes:
|
127 |
+
return "Failed to generate video", 500
|
128 |
+
|
129 |
+
return send_file(
|
130 |
+
io.BytesIO(video_bytes),
|
131 |
+
mimetype='video/mp4',
|
132 |
+
as_attachment=True,
|
133 |
+
download_name='sign_language.mp4'
|
134 |
+
)
|
135 |
+
except Exception as e:
|
136 |
+
print(f"Download error: {str(e)}")
|
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)
|