Streamlit上缺少值



我试图将后端(FastAPI)与我使用Streamlit来预测值的前端连接起来。

我遵循这个教程:https://medium.com/codex/streamlit-fastapi-%EF%B8%8F-the-ingredients-you-need-for-your-next-data-science-recipe-ffbeb5f76a92

但是无论我做什么,我总是在Streamlit上得到相同的错误:

API的响应={"detail":[{"loc"; ["body","name_contract_type"],"msg";field required","type";children_count"],"msg";field required","type";value_error.missing"},{"loc"; ["body"," fam_member& quot;],"msg";字段required" type";value_error.missing"},{"loc&;; ["body","amt_credit_sum"],"msg&;;field required","type";DAYS_INSTALMENT_delay"],"msg&;;field required","type";value_error.missing"; {"loc&;; ["body","bureau_year"],"msg&required","type"value_error.missing"}]}

下面是我的API代码:
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
import json
app = FastAPI()
class User_input(BaseModel):
name_contract_type: int
children_count: int
fam_members: int
amt_credit_sum: float
DAYS_INSTALMENT_delay: int
amt_income_total: float
credit_active: int
bureau_year: int


with open(PATH + "lr.pkl", "rb") as f:
model = pickle.load(f)
@app.post('/Loan')
def loan_pred(input_parameters: User_input):
input_data= input_parameters.json()
input_dictionary = json.loads(input_data)
#input features
contract = input_dictionary['name_contract_type']
children = input_dictionary['children_count']
members = input_dictionary['fam_members']
credit_amt =input_dictionary['amt_credit_sum']
delay = input_dictionary['DAYS_INSTALMENT_delay']
amt_income_total =input_dictionary['amt_income_total']
credit_active =input_dictionary['credit_active']
bureau =input_dictionary['bureau_year']
input_list = [contract, children, members, credit_amt, delay, credit_active,
amt_income_total,bureau]
prediction = model.predict([input_list])
if  (prediction[0]== 0):
return'The customer will refund his loan'
else:
return'The customer will not refund his loan'


# Here's my code for streamlit :
#input features
contract = st.sidebar.slider("X",0,100,20)
children = st.sidebar.slider("a",0,100,20)
credit_amnt =st.sidebar.slider("b",0,100,20)
members = st.sidebar.slider("c",0,100,20)
credit_active =st.sidebar.slider("d",0,100,20)
amt_income_total =st.sidebar.slider("e",0,100,20)
bureau =st.sidebar.slider("f",0,100,20)
delay =st.sidebar.slider("g",0,100,20)
user_input_dict={"contract": contract, "children":children, "credit_amnt":credit_amnt,       "members":members, "credit_active":credit_active,

"amt_income_total"delay" amt_income_total:延迟,"bureau":局}

btn_predict = st.sidebar.button("Predict")
if btn_predict:
res = requests.post(url='https://66c4-34-73-148-78.ngrok.io/Loan',data=json.dumps(user_input_dict))
st.subheader(f"Response from API ={res.text}")

谢谢你的帮助

我试了所有办法,但还是没办法解决

好吧,让我重述一下整个背景:

我做了一个项目,包括预测客户是否能够退还贷款,0:客户退还,1:客户不退还

然后我用FastAPI作为后端,Streamlit作为前端部署我的模型,我的目标是连接Streamlit和FastAPI。为此,我遵循了我在问题中提到的教程。

我的FastAPI代码工作并返回一个预测。我接下来所做的是使用Streamlit作为模型输入,并使用请求帖子通过后端(FastAPI)获得预测。但是当我这样做的时候,我得到了我上面提到的错误(dfloc错误缺失值),我希望现在它不那么令人困惑,更清楚。