我在本地使用FastApi运行LogicRegression模型(127.0.0.1(。然后它发生ValueError,如下所示:ValueError: [TypeError("'numpy.int32' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
服务器响应为:500 Undocumented Error:Internal Server Error
但是,当我在没有FastApi的情况下在本地运行代码时,一切都很顺利。
@app.put("/senti/{comment}")
def getSentiment(comment: str):
comment = comment.lower().strip()
X = getD2Vfeature(comment)
y = getSentiPolarity(X)
print(y)
return {"senti polarity":y}
if __name__ == "__main__":
# goes well when running here
comment = """
great... lets work to get this in soon before the codebase changes again...
"""
getSentiment(comment)
getSentiPolity((如下:
def getSentiPolarity(X):
model = joblib.load('source/logicregression_liwc123_d2v.pkl')
y = model.predict([X])
return y[0]
y=model.predict([X])
有什么问题吗?
Fast API不支持numpy.*
数据类型。您可以通过调用NumPyval.item()
函数转换为本机python类型。因此,对于上面的问题,你本可以做return y[0].item()
,它就会起作用。