NEW_WEB_SERVICE.md 2.2 KB
Newer Older
B
barrierye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# How to develop a new Web service?

([简体中文](NEW_WEB_SERVICE_CN.md)|English)

This document will take the image classification service based on the Imagenet data set as an example to introduce how to develop a new web service. The complete code can be visited at [here](https://github.com/PaddlePaddle/Serving/blob/develop/python/examples/imagenet/image_classification_service.py).

## WebService base class

Paddle Serving implements the [WebService](https://github.com/PaddlePaddle/Serving/blob/develop/python/paddle_serving_server/web_service.py#L23) base class. You need to override its `preprocess` and `postprocess` method. The default implementation is as follows:

```python
class WebService(object):
  
B
barrierye 已提交
14
    def preprocess(self, feed={}, fetch=[]):
B
barrierye 已提交
15 16 17 18 19 20 21 22 23
        return feed, fetch
    def postprocess(self, feed={}, fetch=[], fetch_map=None):
        return fetch_map
```

### preprocess

The preprocess method has two input parameters, `feed` and `fetch`. For an HTTP request `request`:

B
barrierye 已提交
24
- The value of `feed` is the feed part `request.json["feed"]` in the request data 
B
barrierye 已提交
25 26 27 28 29 30 31 32
- The value of `fetch` is the fetch part `request.json["fetch"]` in the request data

The return values are the feed and fetch values used in the prediction.

### postprocess

The postprocess method has three input parameters, `feed`, `fetch` and `fetch_map`:

B
barrierye 已提交
33
- The value of `feed` is the feed part `request.json["feed"]` in the request data 
B
barrierye 已提交
34 35 36 37 38 39 40 41 42
- The value of `fetch` is the fetch part `request.json["fetch"]` in the request data
- The value of `fetch_map` is the model output value.

The return value will be processed as `{"reslut": fetch_map}` as the return of the HTTP request.

## Develop ImageService class

```python
class ImageService(WebService):
B
barrierye 已提交
43

B
barrierye 已提交
44 45
    def preprocess(self, feed={}, fetch=[]):
        reader = ImageReader()
B
barrierye 已提交
46 47 48 49 50
        feed_batch = []
        for ins in feed:
            if "image" not in ins:
                raise ("feed data error!")
            sample = base64.b64decode(ins["image"])
B
barrierye 已提交
51
            img = reader.process_image(sample)
B
barrierye 已提交
52 53
            feed_batch.append({"image": img})
        return feed_batch, fetch
B
barrierye 已提交
54 55 56
```

For the above `ImageService`, only the `preprocess` method is rewritten to process the image data in Base64 format into the data format required by prediction.