【发布时间】:2021-06-10 01:11:14
【问题描述】:
import uvicorn
from fastapi import FastAPI
# 2. Create the app object
app = FastAPI()
# 3. Index route, opens automatically on http://127.0.0.1:8000
class RunModel():
@app.get('/')
def index(self):
return {'message': 'Hello'}
@app.get('/predict')
def get_res(self, feat1: float, feat2:float):
res = feat1 + feat2
return {'result': f'{res:.4f}'}
run_model = RunModel()
# 5. Run the API with uvicorn
# Will run on http://127.0.0.1:8000
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)
当我首先运行它时,我得到了错误(在终端而不是浏览器中)422 Unprocessable Entity。接下来是当我访问 http://localhost:8000/docs 时,它似乎希望我为 /predict 路由输入 3 个值,这两个功能符合预期和 self。所以问题是我怎样才能使用这个类结构并且仍然使用fastapi(即忽略self)。
【问题讨论】: