处理特定的个性化例外 /条件(FastApi,Pydantic模型,预测模型部署)
我正在使用 Pydantic模型用于使用 fastapi 来部署预测的机器学习模型,所以我想处理以下例外/条件:
- 给出许多输入,如果其中一个不匹配功能要求(类型,长度...)给出了该特定无效输入的例外,但是显示了其他有效输入的输出
我想实现的目标
输入:
[
{
"name":"John",
"age": 20,
"salary": 15000
},
{
"name":"Emma",
"age": 25,
"salary": 28000
},
{
"name":"David",
"age": "test",
"salary": 50000
},
{
"name":"Liza",
"age": 5000,
"salary": 30000
}
]
输出:
[
{
"prediction":"Class1",
"probability": 0.88
},
{
"prediction":"Class0",
"probability": 0.79
},
{
"ËRROR: Expected type int but got str instead"
},
{
"ËRROR: invalid age number"
}
]
我对基本模型类别的内容:
from pydantic import BaseModel, validator
from typing import List
n_inputs = 3
n_outputs = 2
class Inputs(BaseModel):
name: str
age: int
salary: float
class InputsList(BaseModel):
inputs: List[Inputs]
@validator("inputs", pre=True)
def check_dimension(cls, v):
for point in v:
if len(point) != n_inputs:
raise ValueError(f"Input data must have a length of {n_inputs} features")
return v
class Outputs(BaseModel):
prediction: str
probability: float
class OutputsList(BaseModel):
output: List[Outputs]
@validator("output", pre=True)
def check_dimension(cls, v):
for point in v:
if len(point) != n_outputs:
raise ValueError(f"Output data must a length of {n_outputs}")
return v
我的问题是: - >如何通过上面的代码实现这种例外或条件处理?
I'm using Pydantic models for data validation with FastAPI to deploy a Machine Learning model for predictions, so I want to handle the following exceptions / conditions :
- Giving many inputs if one of them doesn't match the features requirements (types, length...) throw an exception for that specific invalid input but show outputs of the other valid inputs
What I want to achieve
Inputs :
[
{
"name":"John",
"age": 20,
"salary": 15000
},
{
"name":"Emma",
"age": 25,
"salary": 28000
},
{
"name":"David",
"age": "test",
"salary": 50000
},
{
"name":"Liza",
"age": 5000,
"salary": 30000
}
]
Outputs :
[
{
"prediction":"Class1",
"probability": 0.88
},
{
"prediction":"Class0",
"probability": 0.79
},
{
"ËRROR: Expected type int but got str instead"
},
{
"ËRROR: invalid age number"
}
]
What I have with my base model classes :
from pydantic import BaseModel, validator
from typing import List
n_inputs = 3
n_outputs = 2
class Inputs(BaseModel):
name: str
age: int
salary: float
class InputsList(BaseModel):
inputs: List[Inputs]
@validator("inputs", pre=True)
def check_dimension(cls, v):
for point in v:
if len(point) != n_inputs:
raise ValueError(f"Input data must have a length of {n_inputs} features")
return v
class Outputs(BaseModel):
prediction: str
probability: float
class OutputsList(BaseModel):
output: List[Outputs]
@validator("output", pre=True)
def check_dimension(cls, v):
for point in v:
if len(point) != n_outputs:
raise ValueError(f"Output data must a length of {n_outputs}")
return v
My question is :
-> How can I achieve this kind of exception or condition handling with my code above ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过解码提交的JSON并自己处理清单来做到这一点。然后,当预期数据类型和已提交的数据类型之间存在不匹配时,您可以捕获
verionationError
pydantic升起。将您的列表提交到
/foo
endpoint生成(在这种情况下)已处理的值列表或错误:You can do this by decoding the submitted JSON and handling the list yourself. You can then catch the
ValidationError
raise by Pydantic when there's a mismatch between expected and submitted data types.Submitting your list to the
/foo
endpoint generates (in this case) a list of processed values or an error: