Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- Dockerfile +21 -0
- app.py +46 -0
- requirements.txt +9 -0
- sign2img.py +15 -0
- text2sign.py +77 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.8
|
| 2 |
+
|
| 3 |
+
RUN apt update -y
|
| 4 |
+
RUN apt install -y wget zip unzip
|
| 5 |
+
|
| 6 |
+
COPY . .
|
| 7 |
+
RUN pip install -r requirements.txt
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Spoken 2 Sign
|
| 11 |
+
RUN pip install --pre -f https://dist.mxnet.io/python 'mxnet==2.0.0b20220206'
|
| 12 |
+
|
| 13 |
+
RUN wget https://github.com/SignBridgeApp/models/releases/download/v0.0/spoken2symbol.zip
|
| 14 |
+
RUN unzip spoken2symbol.zip
|
| 15 |
+
RUN rm spoken2symbol.zip
|
| 16 |
+
RUN apt install -y sentencepiece
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Start
|
| 20 |
+
EXPOSE 7860
|
| 21 |
+
CMD ["python3", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify, request, send_file
|
| 2 |
+
import sign2img
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
TEXT_SIGN = True if os.environ.get("TEXT_SIGN", False) else False
|
| 6 |
+
if TEXT_SIGN:
|
| 7 |
+
import text2sign
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@app.route("/text2sign")
|
| 13 |
+
def translate_text():
|
| 14 |
+
if not TEXT_SIGN:
|
| 15 |
+
return jsonify({"error": f"TEXT_SIGN is not trie"}), 400
|
| 16 |
+
|
| 17 |
+
text = request.args.get("text", None)
|
| 18 |
+
if not text:
|
| 19 |
+
return jsonify({"error": "No text provided"}), 400
|
| 20 |
+
|
| 21 |
+
symbol = text2sign.translate(text)
|
| 22 |
+
return jsonify({"translation": symbol})
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@app.route("/sign2img")
|
| 26 |
+
def covert_text():
|
| 27 |
+
sign = request.args.get("sign", None)
|
| 28 |
+
if not sign:
|
| 29 |
+
return jsonify({"error": "No sign provided"}), 400
|
| 30 |
+
|
| 31 |
+
img = sign2img.convert(sign)
|
| 32 |
+
return send_file(img, mimetype='image/png', download_name="sign.png")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@app.route("/")
|
| 36 |
+
def index():
|
| 37 |
+
return jsonify({"Server": "OK"})
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@app.errorhandler(404)
|
| 41 |
+
def notfound(e):
|
| 42 |
+
return jsonify({"error": "Not a path"}), 404
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
|
| 3 |
+
# Spoken 2 Sign
|
| 4 |
+
torch==1.10.0 -f https://download.pytorch.org/whl/cpu/
|
| 5 |
+
sockeye==3.0.13
|
| 6 |
+
numpy==1.23
|
| 7 |
+
git+https://github.com/sign-language-processing/signwriting.git
|
| 8 |
+
sentencepiece
|
| 9 |
+
# --pre -f https://dist.mxnet.io/python 'mxnet==2.0.0b20220206'
|
sign2img.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
from signwriting.visualizer.visualize import signwriting_to_image
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def to_bytes_io(image: Image) -> BytesIO:
|
| 7 |
+
imgByte = BytesIO()
|
| 8 |
+
image.save(imgByte, "png")
|
| 9 |
+
imgByte.seek(0)
|
| 10 |
+
return imgByte
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def convert(fsw):
|
| 14 |
+
img: Image = signwriting_to_image(fsw)
|
| 15 |
+
return to_bytes_io(img)
|
text2sign.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import mxnet as mx
|
| 2 |
+
from sockeye import inference, model
|
| 3 |
+
import sentencepiece as spm
|
| 4 |
+
|
| 5 |
+
import warnings
|
| 6 |
+
warnings.filterwarnings("ignore")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
DEVICE = mx.cpu()
|
| 10 |
+
MODEL_FOLDER = "spoken2symbol"
|
| 11 |
+
SPM_PATH = MODEL_FOLDER + "/spm.model"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
MODELS, SRC_VOCABS, TARG_VOCABS = model.load_models(
|
| 15 |
+
context=DEVICE, dtype=None, model_folders=[MODEL_FOLDER], inference_only=True)
|
| 16 |
+
SPM_MODEL = spm.SentencePieceProcessor(model_file=SPM_PATH)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
LANG_CODE = "en"
|
| 20 |
+
CONT_CODE = "ase" # "us"
|
| 21 |
+
TRANS_TYPE = "sent"
|
| 22 |
+
N_BEST = 3
|
| 23 |
+
BEAM_SIZE = N_BEST
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def translate(text):
|
| 27 |
+
tag_str = f"<2{LANG_CODE}> <4{CONT_CODE}> <{TRANS_TYPE}>"
|
| 28 |
+
formatted = f"{tag_str} {text}"
|
| 29 |
+
encoded = " ".join(SPM_MODEL.encode(formatted, out_type=str))
|
| 30 |
+
|
| 31 |
+
translator = inference.Translator(
|
| 32 |
+
context=DEVICE,
|
| 33 |
+
ensemble_mode="linear",
|
| 34 |
+
scorer=inference.CandidateScorer(),
|
| 35 |
+
output_scores=True,
|
| 36 |
+
batch_size=1,
|
| 37 |
+
BEAM_SIZE=BEAM_SIZE,
|
| 38 |
+
beam_search_stop="all",
|
| 39 |
+
nbest_size=N_BEST,
|
| 40 |
+
models=MODELS,
|
| 41 |
+
source_vocabs=SRC_VOCABS,
|
| 42 |
+
target_vocabs=TARG_VOCABS,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
encoded = inference.make_input_from_plain_string(0, encoded)
|
| 46 |
+
output = translator.translate([encoded])[0]
|
| 47 |
+
|
| 48 |
+
# translations = []
|
| 49 |
+
symbols_candidates = output.nbest_translations
|
| 50 |
+
factors_candidates = output.nbest_factor_translations
|
| 51 |
+
for symbols, factors in zip(symbols_candidates, factors_candidates):
|
| 52 |
+
symbols = symbols.split(" ")
|
| 53 |
+
xs = factors[0].split(" ")
|
| 54 |
+
ys = factors[1].split(" ")
|
| 55 |
+
fsw = ""
|
| 56 |
+
|
| 57 |
+
for i, (symbol, x, y) in enumerate(zip(symbols, xs, ys)):
|
| 58 |
+
if symbol != "P":
|
| 59 |
+
if i != 0:
|
| 60 |
+
if (
|
| 61 |
+
not symbol.startswith("S")
|
| 62 |
+
or symbol.startswith("S387")
|
| 63 |
+
or symbol.startswith("S388")
|
| 64 |
+
or symbol.startswith("S389")
|
| 65 |
+
or symbol.startswith("S38a")
|
| 66 |
+
or symbol.startswith("S38b")
|
| 67 |
+
):
|
| 68 |
+
fsw += " "
|
| 69 |
+
fsw += symbol
|
| 70 |
+
fsw += x
|
| 71 |
+
fsw += "x"
|
| 72 |
+
fsw += y
|
| 73 |
+
|
| 74 |
+
# translations.append(fsw)
|
| 75 |
+
return fsw
|
| 76 |
+
|
| 77 |
+
# return translations
|