【问题标题】:API to serve roberta ClassificationModel with FastAPI使用 FastAPI 为 roberta ClassificationModel 提供服务的 API
【发布时间】:2020-12-04 20:59:51
【问题描述】:

我在 colab 上使用 simpletransformers 模型训练了转换器,下载了序列化模型,我在使用它进行推理方面几乎没有问题。 在 jupyter 上的模型上加载模型可以工作,但是在使用 fastapi 时会出错 这就是我在 jupyter 上使用它的方式:

from scipy.special import softmax
label_cols = ['art', 'politics', 'health', 'tourism']
model = torch.load("model.bin")
pred = model.predict(['i love politics'])[1]
preds = softmax(pred,axis=1)
preds

它给出以下结果:array([[0.00230123, 0.97465035, 0.00475409, 0.01829433]])

我尝试过如下使用fastapi,但一直报错:

from pydantic import BaseModel
class Message(BaseModel):
    text : str
model = torch.load("model.bin")
@app.post("/predict")
def predict_health(data: Message):
    prediction = model.predict(data.text)[1]
    preds = softmax(prediction, axis=1)
    return {"results": preds}

【问题讨论】:

    标签: nlp fastapi bert-language-model roberta-language-model simpletransformers


    【解决方案1】:

    你能具体说明你得到的错误吗,否则很难看到调试

    此外,jupyter 代码中的 model.predict 函数似乎将数组作为输入,而在您的 fastapi 代码中,您将字符串直接传递给该函数。

    所以不妨试试

    ...
    prediction = model.predict([data.text])[1]
    ...
    

    【讨论】:

      【解决方案2】:

      很难说没有错误。

      如果对您有帮助,可以查看这篇文章,该文章展示了如何使用 Hugging Face 转换器(Bart Large MNLI 模型)和 FastAPI 构建分类:https://nlpcloud.io/nlp-machine-learning-classification-api-production-fastapi-transformers-nlpcloud.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-15
        • 1970-01-01
        • 2015-05-13
        • 2017-08-04
        • 2017-06-08
        • 2015-09-14
        • 2018-02-23
        • 2018-09-11
        相关资源
        最近更新 更多