From cc1be73c0f9a6b51ed9681ff826fdc39a239a0f1 Mon Sep 17 00:00:00 2001 From: Tom Edwards <edwardstj1@cardiff.ac.uk> Date: Mon, 23 Sep 2024 20:01:09 +0100 Subject: [PATCH] Work towards USAS + ner + translation + sentiment analysis --- api/api_functions.py | 13 +++++++++++++ func/translation/translation.py | 26 ++++++++++++++++++++++++++ main.py | 15 ++++++++++----- 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 func/translation/translation.py diff --git a/api/api_functions.py b/api/api_functions.py index 44f42eb..8f29848 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 0000000..6a03d67 --- /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 d6027af..4a14e71 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) -- GitLab