Skip to content

Model Server API

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

Using AlpacaClient

Installation

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

The best way to install the client is via setup.py install. move into alpaca_client directory and enter the command:

python setup.py 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.

Clone this wiki locally