diff --git a/api/api_functions.py b/api/api_functions.py index 44f42eb504eb55239b2e957c53eced978949522e..8f2984856e0e2f6e10aa04656112e1025ac95a9c 100644 --- a/api/api_functions.py +++ b/api/api_functions.py @@ -2,6 +2,7 @@ from flask import make_response, jsonify from func.ner.ner import * from func.sentiment.sentiment import * +from func.translation.translation import run_translation_on_text from func.usas.usas import * @@ -16,6 +17,18 @@ def get_ner_for_data(page): return make_response(jsonify(result), 400) +# Perform translation on a file +# TAKES XML text page +# Returns english translation results +def get_translation_for_data(page): + + result = run_translation_on_text(page) + + if result["code"] == "SUCCESS": + return make_response(jsonify(result), 201) + + return make_response(jsonify(result), 400) + # Perform USAS analysis on a file # TAKES XML text page diff --git a/func/translation/translation.py b/func/translation/translation.py new file mode 100644 index 0000000000000000000000000000000000000000..6a03d67214766264752c4eef2682c200bc87ee09 --- /dev/null +++ b/func/translation/translation.py @@ -0,0 +1,26 @@ +import torch +from ckip_transformers.nlp import CkipWordSegmenter, CkipPosTagger, CkipNerChunker +from transformers import pipeline +import pandas as pd + +from shared.translate import translate + +#page = '尼罗河 是一æ¡æµç¶“éžæ´²æ±éƒ¨èˆ‡åŒ—部的河æµï¼Œèˆ‡ä¸éžåœ°å€çš„剛果河ã€éžæ´²å—部的赞比西河以åŠè¥¿éžåœ°åŒºçš„尼日尔河並列éžæ´²æœ€å¤§çš„四個河æµç³»çµ±ã€‚' +# Perform NER on Text +def run_translation_on_text(page): + + translation = '<p>Translation</p>' + translation = translation + '<span>' + translation = translation + translate(page).text + translation = translation +'</span>' + + result = {'output': translation,'message': 'Done', 'code': 'SUCCESS'} + + return result + + + + + + + diff --git a/main.py b/main.py index d6027af832e34d77c80d471ad823a7464a1f770c..4a14e71ad676ae89f5f0b84675b9ae2d26e3f5cf 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ app.config['CORS_HEADERS'] = 'Content-Type' @app.route("/") def home(): - return "Hello World!" + return "Success!" @app.route("/system-check") @@ -23,17 +23,23 @@ def test(): def ner(): request_data = request.get_json() - #print(request_data) page = request_data['page'] result = get_ner_for_data(page) return result +@app.route("/translate", methods=['POST']) + +def translate(): + request_data = request.get_json() + page = request_data['page'] + result = get_translation_for_data(page) + return result + + @app.route("/usas", methods=['POST']) def usas(): - print('hello') request_data = request.get_json() - print(request_data) page = request_data['page'] result = get_usas_for_data(page) @@ -43,7 +49,6 @@ def usas(): def sentiment(): request_data = request.get_json() - print(request_data) page = request_data['page'] result = get_sentiment_for_data(page)