From 46fca61cc24535636acf3b67c9b0fb7f85d86770 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 10 Feb 2021 10:51:17 -0500 Subject: [PATCH 1/3] Add /detect API --- app/app.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 44 ++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 main.py diff --git a/app/app.py b/app/app.py index c7b7b5c..70bda51 100644 --- a/app/app.py +++ b/app/app.py @@ -258,6 +258,86 @@ def create_app(char_limit=-1, req_limit=-1, batch_limit=-1, ga_id=None, debug=Fa except Exception as e: abort(500, description="Cannot translate text: %s" % str(e)) + @app.route("/detect", methods=['POST']) + def detect(): + """ + Detect the language of a single text + --- + tags: + - translate + parameters: + - in: formData + name: q + schema: + type: string + example: Hello world! + required: true + description: Text to detect + responses: + 200: + description: Detections + schema: + id: detections + type: array + items: + type: object + properties: + confidence: + type: number + format: float + minimum: 0 + maximum: 1 + description: Confidence value + example: 0.6 + language: + type: string + description: Language code + example: en + 400: + description: Invalid request + schema: + id: error-response + type: object + properties: + error: + type: string + description: Error message + 500: + description: Detection error + schema: + id: error-response + type: object + properties: + error: + type: string + description: Error message + 429: + description: Slow down + schema: + id: error-slow-down + type: object + properties: + error: + type: string + description: Reason for slow down + """ + if request.is_json: + json = request.get_json() + q = json.get('q') + else: + q = request.values.get("q") + + if not q: + abort(400, description="Invalid request: missing q parameter") + + candidate_langs = list(filter(lambda l: l.lang in language_map, detect_langs(q))) + candidate_langs.sort(key=lambda l: l.prob, reverse=True) + return jsonify([{ + 'confidence': l.prob, + 'language': l.lang + } for l in candidate_langs]) + + @app.route("/frontend/settings") def frontend_settings(): """ diff --git a/main.py b/main.py new file mode 100644 index 0000000..348900d --- /dev/null +++ b/main.py @@ -0,0 +1,44 @@ +import argparse +from app.app import create_app + +parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API') +parser.add_argument('--host', type=str, + help='Hostname (%(default)s)', default="127.0.0.1") +parser.add_argument('--port', type=int, + help='Port (%(default)s)', default=5000) +parser.add_argument('--char-limit', default=-1, type=int, metavar="", + help='Set character limit (%(default)s)') +parser.add_argument('--req-limit', default=-1, type=int, metavar="", + help='Set maximum number of requests per minute per client (%(default)s)') +parser.add_argument('--batch-limit', default=-1, type=int, metavar="", + help='Set maximum number of texts to translate in a batch request (%(default)s)') +parser.add_argument('--ga-id', type=str, default=None, metavar="", + help='Enable Google Analytics on the API client page by providing an ID (%(default)s)') +parser.add_argument('--debug', default=False, action="store_true", + help="Enable debug environment") +parser.add_argument('--ssl', default=None, action="store_true", + help="Whether to enable SSL") +parser.add_argument('--frontend-language-source', type=str, default="en", metavar="", + help='Set frontend default language - source (%(default)s)') +parser.add_argument('--frontend-language-target', type=str, default="es", metavar="", + help='Set frontend default language - target (%(default)s)') +parser.add_argument('--frontend-timeout', type=int, default=500, metavar="", + help='Set frontend translation timeout (%(default)s)') + +args = parser.parse_args() + + +if __name__ == "__main__": + app = create_app(char_limit=args.char_limit, + req_limit=args.req_limit, + batch_limit=args.batch_limit, + ga_id=args.ga_id, + debug=args.debug, + frontend_language_source=args.frontend_language_source, + frontend_language_target=args.frontend_language_target, + frontend_timeout=args.frontend_timeout) + if args.debug: + app.run(host=args.host, port=args.port) + else: + from waitress import serve + serve(app, host=args.host, port=args.port, url_scheme='https' if args.ssl else 'http') From 2be93e45a298394916f94f79de40e585fddd2fb5 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 10 Feb 2021 11:04:02 -0500 Subject: [PATCH 2/3] Update main.py --- app/__init__.py | 1 + main.py | 44 ++------------------------------------------ 2 files changed, 3 insertions(+), 42 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index e69de29..c28a133 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -0,0 +1 @@ +from .main import main diff --git a/main.py b/main.py index 348900d..bf80680 100644 --- a/main.py +++ b/main.py @@ -1,44 +1,4 @@ -import argparse -from app.app import create_app - -parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API') -parser.add_argument('--host', type=str, - help='Hostname (%(default)s)', default="127.0.0.1") -parser.add_argument('--port', type=int, - help='Port (%(default)s)', default=5000) -parser.add_argument('--char-limit', default=-1, type=int, metavar="", - help='Set character limit (%(default)s)') -parser.add_argument('--req-limit', default=-1, type=int, metavar="", - help='Set maximum number of requests per minute per client (%(default)s)') -parser.add_argument('--batch-limit', default=-1, type=int, metavar="", - help='Set maximum number of texts to translate in a batch request (%(default)s)') -parser.add_argument('--ga-id', type=str, default=None, metavar="", - help='Enable Google Analytics on the API client page by providing an ID (%(default)s)') -parser.add_argument('--debug', default=False, action="store_true", - help="Enable debug environment") -parser.add_argument('--ssl', default=None, action="store_true", - help="Whether to enable SSL") -parser.add_argument('--frontend-language-source', type=str, default="en", metavar="", - help='Set frontend default language - source (%(default)s)') -parser.add_argument('--frontend-language-target', type=str, default="es", metavar="", - help='Set frontend default language - target (%(default)s)') -parser.add_argument('--frontend-timeout', type=int, default=500, metavar="", - help='Set frontend translation timeout (%(default)s)') - -args = parser.parse_args() - +from app import main if __name__ == "__main__": - app = create_app(char_limit=args.char_limit, - req_limit=args.req_limit, - batch_limit=args.batch_limit, - ga_id=args.ga_id, - debug=args.debug, - frontend_language_source=args.frontend_language_source, - frontend_language_target=args.frontend_language_target, - frontend_timeout=args.frontend_timeout) - if args.debug: - app.run(host=args.host, port=args.port) - else: - from waitress import serve - serve(app, host=args.host, port=args.port, url_scheme='https' if args.ssl else 'http') + main() From 53cf88f78581807c76052b730ee74b77e71e80d7 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 10 Feb 2021 11:05:23 -0500 Subject: [PATCH 3/3] update readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 914ca66..01a0eea 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,9 @@ git clone https://github.com/uav4geo/LibreTranslate cd LibreTranslate pip install -e . libretranslate [args] + +# Or +python main.py [args] ``` Then open a web browser to http://localhost:5000