Skip to content

Model Server API

Lee Dong Ho edited this page Jul 27, 2019 · 17 revisions

Using AlpacaServer

Installation (You can skip this part if you already installed it.)

AlpacaTag:
  - alpaca_client
  + alpaca_server:
     - alpaca_model
     - alpaca_serving
  - annotation

move into alpaca_server directory and enter the command:

python -m pip install .

start AlpacaServer

CUDA_VISIBLE_DEVICES=" " alpaca-serving-start

Currently, GPU does not support.

Using AlpacaClient

Installation (You can skip this part if you already installed it.)

AlpacaTag:
  + alpaca_client:
      - __init__.py
      - setup.py
  - alpaca_server
  - annotation

move into alpaca_client directory and enter the command:

python -m pip install .

Methods

Method Description
.initiate(project_id) Model Initiate: If the model of same project id already exists in the directory, this method loads the model. If not, initiates the model
.online_initiate(sentences, predefined_label) If the model is firstly initialized, it needs to make mappings of words, chars and labels to feature indices. (word_vocab, char_vocab, label_vocab)
.online_learning(sentences, labels, epoch, batch) feeds annotated sentences into the model with specified epoch and batch size.
.active_learning(sentences, acquire) Feeds the sentences into the model and get the most ambiguous instances by size of acquire
.predict(sentences) Feeds the sentences and get the prediction

Tutorial

1. Connect to server

from alpaca_serving.client import *
ac = AlpacaClient()

2. Initiate the model

ac.initiate(1)
# 'MODEL INITIATED' if there is no existing model with same project_id = 1
# 'MODEL LOADED' if there is existing model with same project_id = 1

If 'MODEL INITIATED' appears, we should build word, label mappings before we use the model.

ac.online_initiate(sent,[['B-PER', 'I-PER', 'B-LOC', 'I-LOC', 'B-ORG', 'I-ORG', 'B-MISC', 'I-MISC', 'O']])

3. Get indices of the most ambiguous instances by active learning method.

active_indices = ac.active_learning(sent, acquire=5)

4. Train the model with those instances.

active_sent = [sent[a_i] for a_i in active_indices]
active_label = [label[a_i] for a_i in active_indices]
ac.online_learning(active_sent,active_label, epoch=5, batch=5)
# 'Online learning completed'

5. Get prediction

ac.predict(‘New York and Paris’)
# {'words': ['Paris', 'and', 'New', 'York'], 'entities': [], 'tags': [['B-LOC', 'O', 'B-LOC', 'I-LOC']]}

What if 'error' message comes out?

It's because of resource overloaded in the server-side. Wait 2~3 seconds and Try request again.