From 6b91b754a02c6e004074d4f91d1cf3845836fdf4 Mon Sep 17 00:00:00 2001 From: JiaLei123 Date: Mon, 27 Nov 2017 11:13:51 +0800 Subject: [PATCH] modify apis.md document, and add a web to api test (#23) * add the counter to make the index page can work * 1. add web-util.thml so the user can use the web to test the provided api service. 2. modify the apis document add the api description, --- chi_annotator/webui/apis/ca.py | 21 ++- chi_annotator/webui/static/index.html | 3 +- chi_annotator/webui/static/js/core.js | 46 +++++++ .../webui/static/text_classification.html | 122 ++++++++++-------- chi_annotator/webui/static/web_util.html | 86 ++++++++++++ docs/apis.md | 26 ++++ 6 files changed, 244 insertions(+), 60 deletions(-) create mode 100644 chi_annotator/webui/static/web_util.html diff --git a/chi_annotator/webui/apis/ca.py b/chi_annotator/webui/apis/ca.py index 12e38fc..0afb944 100644 --- a/chi_annotator/webui/apis/ca.py +++ b/chi_annotator/webui/apis/ca.py @@ -1,6 +1,7 @@ import json from flask import Flask, request, redirect, url_for, jsonify, send_from_directory +from flask_cors import CORS from werkzeug.utils import secure_filename import os from utils.mongoUtil import get_mongo_client @@ -10,6 +11,7 @@ app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +CORS(app) @app.route("/") @@ -70,13 +72,19 @@ def load_local_dataset(): load local unlabeled dataset :return: """ - filepath = request.args.get("filepath") + if request.method == 'POST': + filepath = request.data.get("filepath") + else: + filepath = request.args.get("filepath") + print(filepath) # read file - ca = get_mongo_client() - with open(filepath) as f: + ca = get_mongo_client(uri='mongodb://localhost:27017/') + with open(filepath, 'r', encoding='utf-8') as f: for line in f: # label, txt = line.split(" ", 1) - ca["test"].insert_one({"txt": line}) + print("get string %s" % line) + label, txt = line.split(" ", 1) + ca["test"].insert_one({"txt": txt, "label": label}) return jsonify(data={"status": "success"}, code=200, message="load success") @@ -159,4 +167,7 @@ def check_offline_progress(): ca = get_mongo_client() text = ca["test"].insert_one({"label": label, "text": text}) - return jsonify(data={"progress": 50}, code=200, message="annotate success") \ No newline at end of file + return jsonify(data={"progress": 50}, code=200, message="annotate success") + +if __name__=="__main__": + app.run() \ No newline at end of file diff --git a/chi_annotator/webui/static/index.html b/chi_annotator/webui/static/index.html index 4c09473..4f0b1d9 100755 --- a/chi_annotator/webui/static/index.html +++ b/chi_annotator/webui/static/index.html @@ -41,7 +41,8 @@

Welcome to canno chinese annotator!!

  • Text Classification
  • Named Entity Reconisition
  • Information Extraction
  • -
  • Help
  • +
  • web api util
  • +
  • Help
  • diff --git a/chi_annotator/webui/static/js/core.js b/chi_annotator/webui/static/js/core.js index 9566ef5..363b915 100644 --- a/chi_annotator/webui/static/js/core.js +++ b/chi_annotator/webui/static/js/core.js @@ -31,3 +31,49 @@ var example2 = new Vue({ }) + +axios.defaults.baseURL = 'http://localhost:5000'; +// axios.interceptors.request.use( +// config =>{ +// config.data = JSON.stringify(config.data) +// config.headers = { +// 'Content-Type':'application/json' +// } +// return config; +// } +// ); + +var load_local_data = new Vue({ + el: '#load_local_data', + data: { + message: "load local test data", + file_path: 'D:\\Github\\Chinese-Annotator\\chi_annotator\\data\\files\\spam_classify_chi_shuf.txt', + }, + // define methods under the `methods` object + methods: { + load_local_data_post: function (event) { + // Make a request for a user with a given ID + axios.post('/load_local_dataset',{ + filepath: this.file_path, + }) + .then(function (response) { + this.message = response.data.message + console.log(response); + }) + .catch(function (error) { + console.log(error); + }); + }, + load_local_data_get: function (event) { + // Make a request for a user with a given ID + axios.get('/load_local_dataset?filepath=' + this.file_path) + .then(function (response) { + this.message = response.data.message + console.log(response); + }) + .catch(function (error) { + console.log(error); + }); + } + } +}) \ No newline at end of file diff --git a/chi_annotator/webui/static/text_classification.html b/chi_annotator/webui/static/text_classification.html index 588bfbc..d7008ff 100755 --- a/chi_annotator/webui/static/text_classification.html +++ b/chi_annotator/webui/static/text_classification.html @@ -1,74 +1,88 @@ + - -anatine - a free css template - - - - + - -
    - +
    + +
    + +

    Text Classification

    + + + + + + + + +
    +

    Labeled Text

    + +

    Morbi fermentum condimentum felis, commodo vestibulum sem mattis sed. + , mollis vitae tincidunt in, malesuada vitae turpis. Sed aliquam libero ut velit bibendum + consectetur. Quisque sagittis, est in laoreet semper, enim dui consequat felis, faucibus ornare urna velit nec leo. + Maecenas condimentum velit vitae est lobortis fermentum. In tristique sem vitae metus ornare luctus tempus nisl volutpat. + Integer et est id nisi tempus pharetra sagittis et libero.

    + + + right + wrong +    + undo + cancel +
    +
    +

    History

    @@ -95,13 +109,12 @@

    History

    ID

     

    -
    - + -
    @@ -109,7 +122,8 @@

    History

    - + - + + \ No newline at end of file diff --git a/chi_annotator/webui/static/web_util.html b/chi_annotator/webui/static/web_util.html new file mode 100644 index 0000000..d371598 --- /dev/null +++ b/chi_annotator/webui/static/web_util.html @@ -0,0 +1,86 @@ + + + + + + anatine - a free css template + + + + + +
    + +
    + +
    +

    API Testing

    + +
    +

    this web is for API testing, it list all API that provided by the web service backend. user can use this web site + to access the web api send GET or POST request

    +
    + + +

    Load Local Data

    + +
    + + + + +

    {{ message }}

    +
    + + +
    + + + + +
    + +
    + +
    + + + + + + + + \ No newline at end of file diff --git a/docs/apis.md b/docs/apis.md index e69de29..f7cddd6 100644 --- a/docs/apis.md +++ b/docs/apis.md @@ -0,0 +1,26 @@ + +#API 文档 + +## 如何启动web和Reset API 服务 +本框架使用前后端分离,所以要分别启动前端的页面和后端的web服务 + +### 启动前端页面 +前端使用纯前端技术,不需要后端之前就可以运行 + +运行方式: +1. 直接使用浏览器打开*.html +2. 使用Apache http service,使用这种方式只需要修改apache服务器的配置文件./conf/httpd.conf中的DocumentRoot和Directory,将其修改为Chinese-Annotator\chi_annotator\webui\static。然后重启httpd服务器,直接访问localhost即可 + +### 后端服务的启动 +后端使用Python的flask,所以需要安装必要的包,所需要的依赖包参见项目目录中的requirements.txt +1. 将/webui export到PYTHONPATH +2. 运行 python ca.py + + +## API 接口说明 +/load_local_dataset + +支持post和get请求 +参数:filepath 本地文件的绝对路径 + +功能: 将本地的文件load到Mongo数据库 \ No newline at end of file