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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…