# POST 즉 predict
json = {
'text' = ['review1', 'review2' ... ],
'do_fast' : true # or false
}
# response
{"idx0" : {
"text" : 'review1',
'label' : 'positive' or 'negative',
'confidence' : float
},
...
"idxn" : {
"text" : 'reviewn',
'label' : 'positive' or 'negative',
'confidence' : float
}
...
}
import torch
def initialize(self, ):
from transformers import AutoTokenizer, AutoModelForSequenceClassification
self.model_name_or_path = 'sackoh/bert-base-multilingual-cased-nsmc'
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path)
self.model = AutoModelForSequenceClassification.from_pretrained(self.model_name_or_path)
self.model.to('cpu') # 모델이 CPU에서만 돌아가도록 설정
def preprocess(self, text):
# preprocess raw text
model_input = self._clean_text(text)
# vectorize cleaned text
model_input = self.tokenizer(text, return_tensors = 'pt', padding = True)
return model_input
def inference(self, model_input):
with torch.no_grad(): # 가중치 조정 X
model_output = self.model(**model_input)[0].cpu()
# 밑에 코드는 나중에 배움. 지금은 몰라도 됨.
model_output = 1.0 / (1.0 + torch.exp(-model_output))
model_output = model_output.numpy().astype('float')
return model_output
def postprocess(self, model_output):
# process predictions to predicted label and output format
predicted_probabilities = model_output.max(axis = 1)
predicted_ids = model_output.argmax(axis = 1)
predicted_labels = [self.id2label[id_] for id_ in predicted_ids]
return predicted_labels, predicted_probabilities
def handle(self, data):
# do above processes
model_input = self.preprocess(data)
model_output = self.inference(model_input)
return self.postprocess(model_output)
python -m unittest -v test_model_handler.py
from flask import Flask, request, json
from model import MLModelHandler, DLModelHandler
app = Flask(__name__)
# assign model handler as global variable [2 LINES]
ml_handler = MLModelHandler()
dl_handler = DLModelHandler()
@app.route("/predict", methods=["POST"])
def predict():
# handle request and body
body = request.get_json()
text = body.get('text', '')
text = [text] if isinstance(text, str) else text
do_fast = body.get('do_fast', True)
# model inference [2 LINES]
if do_fast: # 머신러닝 모델 사용
predictions = ml_handler.handle(text)
else:
predictions = dl_handler.handle(text)
# response
result = json.dumps({str(i): {'text': t, 'label': l, 'confidence': c}
for i, (t, l, c) in enumerate(zip(text, predictions[0], predictions[1]))}) # predictions[0] : label, predictions[1] : label에 대한 confidence값
return result
if __name__ == "__main__":
app.run(host='0.0.0.0', port='5000', debug=True)
curl -d '{"text" : ["영화 오랜만에 봤는데 괜찮은 영화였어", "정말 지루했어"], "use_fast" : False}'\
-H "Content-Type: application/json"\
-X POST\
http://43.201.30.132:5000/predict
import requests
url = 'http://43.201.30.132:5000/predict'
response = requests.post(url, json = data)
--> 파이썬 방식으로 해봤지만 다음과 같은 오류 발생...