forked from ShannonAI/service-streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_example.py
36 lines (25 loc) · 888 Bytes
/
flask_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# coding=utf-8
# Created by Meteorix at 2019/7/30
from bert_model import TextInfillingModel as Model
from flask import Flask, request, jsonify
from service_streamer import ThreadedStreamer
app = Flask(__name__)
model = None
streamer = None
@app.route("/naive", methods=["POST"])
def naive_predict():
inputs = request.form.getlist("s")
outputs = model.predict(inputs)
return jsonify(outputs)
@app.route("/stream", methods=["POST"])
def stream_predict():
inputs = request.form.getlist("s")
outputs = streamer.predict(inputs)
return jsonify(outputs)
if __name__ == "__main__":
model = Model()
# start child thread as worker
streamer = ThreadedStreamer(model.predict, batch_size=64, max_latency=0.1)
# spawn child process as worker
# streamer = Streamer(model.predict, batch_size=64, max_latency=0.1)
app.run(port=5005, debug=False)