首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FastAPI返回BERT模型结果和指标

FastAPI返回BERT模型结果和指标
EN

Stack Overflow用户
提问于 2021-01-25 21:36:12
回答 1查看 115关注 0票数 0

我有一个使用BERT的情感分析模型,我想通过FastAPI预测文本得到结果,但总是给出否定的答案(我认为这是因为预测没有给出预测结果)。

这是我的代码:

代码语言:javascript
复制
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分数等。

我正在使用这个模型

代码语言:javascript
复制
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])
EN

回答 1

Stack Overflow用户

发布于 2021-01-26 16:14:50

您正在使用Path parameter构造。这意味着要调用你的API端点,你需要做这样一个调用:http://localhost:8000/predict/some_text。问题是some_text在大小写中包含空格。除了像%20这样的显式HTML (我甚至不确定这是否有效),它将无法注册空格,并且您将只有第一个单词。

相反,您最好使用Request body构造。所以是POST而不是GET。如下所示:

代码语言:javascript
复制
from pydantic import BaseModel


class Text(BaseModel):
    text: str


app = FastAPI()


@app.post("/predict/")
async def predict(text: Text):
    text = text.text
    ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65885841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档