Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

python - FastAPI return BERT model result and metrics

I have sentiment analysis model using BERT and I want to get the result from predicting text via FastAPI but it always give negative answer (I think it is because the prediction didn't give prediction result).

This is my code:

import uvicorn
from fastapi import FastAPI
import joblib

# models
sentiment_model = open("sentiment-analysis-model.pkl", "rb")
sentiment_clf = joblib.load(sentiment_model)

# init app
app = FastAPI()

# Routes
@app.get('/')
async def index():
    return {"text": "Hello World! huehue"}

@app.get('/predict/{text}')
async def predict(text):
    prediction, raw_outputs = sentiment_clf.predict(text)
    if prediction == 0:
        result = "neutral"
    elif prediction == 1:
        result = "positive"
    else:
        result = "negative"
    return{"text": text, "prediction":result}

if __name__ == '__main__':
    uvicorn.run(app, host="127.0.0.1", port=8000)

Also I want to print accuracy, F1 Score etc.

I'm using this model

from simpletransformers.classification import ClassificationModel

model = ClassificationModel('bert', 'bert-base-multilingual-uncased', num_labels=3, use_cuda=False, 
                            args={'reprocess_input_data': True, 'overwrite_output_dir': True, 'num_train_epochs': 1},
                            weight=[3, 0.5, 1])
question from:https://stackoverflow.com/questions/65885841/fastapi-return-bert-model-result-and-metrics

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using a Path parameter construction. Meaning that to call your API endpoint, you need to do such a call: http://localhost:8000/predict/some_text. The issue is that some_text contains spaces in your case. Appart from putting explicit HTML espace things like %20 (I am not sure this would even work), this will fail to register the space and you will just have the first word.

Instead you would be better of using a Request body construction. So a POST instead of a GET. Something like this:

from pydantic import BaseModel


class Text(BaseModel):
    text: str


app = FastAPI()


@app.post("/predict/")
async def predict(text: Text):
    text = text.text
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...