Ahm*_*nis 20 python scikit-learn fastapi
我正在为我的 ML 模型使用 FAST API。
我有一个管道。
lr_tfidf = Pipeline([('vect', tfidf),
('clf', LogisticRegression(penalty='l2'))])
Run Code Online (Sandbox Code Playgroud)
现在在 Fast API 中,当我想要预测并将结果显示为 API 时,我的代码是
app = FastAPI()
@app.post('/predict')
def predict_species(data: str):
data = np.array([data])
prob = lr_tfidf.predict_proba(data).max()
pred = lr_tfidf.predict(data)
return {'Probability': f'{prob}',
'Predictions':f'{pred}'}
Run Code Online (Sandbox Code Playgroud)
我是从教程里复制过来的。当我通过 FASTAPI 在 GUI 上测试它时,它工作得很好,如图所示,即它显示了概率和预测。
当我转到 GUI 提供的请求 URL 时http://127.0.0.1:8000/predict?data=hello(测试数据是 hello),它给了我错误。
{"detail":"Method Not Allowed"}
Run Code Online (Sandbox Code Playgroud)
在我的终端上,错误消息是
INFO: 127.0.0.1:42568 - "GET /predict?data=hello HTTP/1.1" 405 Method Not Allowed
Run Code Online (Sandbox Code Playgroud)
fin*_*mer 27
端点的方法定义为POST( @app.post('/predict'))。当您从浏览器调用 URL 时,HTTP 方法为GET。
一个简单的解决方案是将端点方法更改为GETvia @app.get。
但这很可能违反 REST-API 端点的命名方式以及何时使用 HTTP 方法。一个很好的起点是https://restfulapi.net/resource-naming/。
或者也许您正在实现 RPC(远程过程调用)?它也可以有所不同。
| 归档时间: |
|
| 查看次数: |
59736 次 |
| 最近记录: |