我有一个使用BERT的情感分析模型,我想通过FastAPI预测文本得到结果,但总是给出否定的答案(我认为这是因为预测没有给出预测结果)。
这是我的代码:
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)我还想打印精度,F1分数等。
我正在使用这个模型
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])发布于 2021-01-26 16:14:50
您正在使用Path parameter构造。这意味着要调用你的API端点,你需要做这样一个调用:http://localhost:8000/predict/some_text。问题是some_text在大小写中包含空格。除了像%20这样的显式HTML (我甚至不确定这是否有效),它将无法注册空格,并且您将只有第一个单词。
相反,您最好使用Request body构造。所以是POST而不是GET。如下所示:
from pydantic import BaseModel
class Text(BaseModel):
text: str
app = FastAPI()
@app.post("/predict/")
async def predict(text: Text):
text = text.text
...https://stackoverflow.com/questions/65885841
复制相似问题