我有一個使用 FastAPI 部署的機器學習模型,但問題是我需要該模型采用二體參數app = FastAPI()class Inputs(BaseModel): industry: str = None file: UploadFile = File(...)@app.post("/predict")async def predict(inputs: Inputs): # params industry = inputs.industry file = inputs.file ### some code ### return predicted value當我嘗試發送輸入參數時,我在郵遞員中收到錯誤,請參見下圖,
1 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
如果您正在接收 JSON 數據,
application/json
請使用普通的 Pydantic 模型。這將是與 API 通信的最常見方式。
如果您收到原始文件(例如圖片或 PDF 文件)并將其存儲在服務器中,則使用
UploadFile
,它將作為表單數據 (?multipart/form-data
) 發送。如果您需要接收某種類型的非 JSON 結構化內容,但希望以某種方式進行驗證(例如 Excel 文件),您仍然需要使用上傳它并在代碼中執行所有必要的驗證
UploadFile
。您可以在自己的代碼中使用 Pydantic 進行驗證,但在這種情況下 FastAPI 無法為您執行此操作。
因此,就您而言,路由器應該是,
from fastapi import FastAPI, File, UploadFile, Form
app = FastAPI()
@app.post("/predict")
async def predict(
? ? ? ? industry: str = Form(...),
? ? ? ? file: UploadFile = File(...)
):
? ? # rest of your logic
? ? return {"industry": industry, "filename": file.filename}
添加回答
舉報
0/150
提交
取消