简体   繁体   English

Streamlit 上的缺失值

[英]Missing value on Streamlit

I tried to connect the backend(FastAPI) with the frontend that I do with Streamlit to predict a value.我尝试将后端 (FastAPI) 与我使用 Streamlit 所做的前端连接起来以预测一个值。

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

But whatever I do I keep getting the same error on Streamlit:但无论我做什么,我都会在 Streamlit 上遇到同样的错误:

Response from API ={"detail":[{"loc":["body","name_contract_type"],"msg":"field required","type":"value_error.missing"},{"loc":["body","children_count"],"msg":"field required","type":"value_error.missing"},{"loc":["body","fam_members"],"msg":"field required","type":"value_error.missing"},{"loc":["body","amt_credit_sum"],"msg":"field required","type":"value_error.missing"},{"loc":["body","DAYS_INSTALMENT_delay"],"msg":"field required","type":"value_error.missing"},{"loc":["body","bureau_year"],"msg":"field required","type":"value_error.missing"}]}来自 API 的响应 ={"detail":[{"loc":["body","name_contract_type"],"msg":"field required","type":"value_error.missing"},{"loc": ["body","children_count"],"msg":"必填字段","type":"value_error.missing"},{"loc":["body","fam_members"],"msg":"必填字段","type":"value_error.missing"},{"loc":["body","amt_credit_sum"],"msg":"必填字段","type":"value_error.missing"}, {"loc":["body","DAYS_INSTALMENT_delay"],"msg":"必填字段","type":"value_error.missing"},{"loc":["body","bureau_year"], “消息”:“必填字段”,“类型”:“value_error.missing”}]}

Here's my API code:这是我的 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":amt_income_total,"delay":delay,"bureau":bureau} "amt_income_total":amt_income_total,"delay":延迟,"bureau":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}")

Thanks for your help谢谢你的帮助

I tried everything I could but still could not figure it out我尽我所能但仍然无法弄清楚

Well, let me recap the whole context:好吧,让我回顾一下整个上下文:

I did a project that consisted of predicting if a customer would be able to refund a loan or not, 0: the customer refunds it,1: the customer doesn't我做了一个项目,包括预测客户是否能够偿还贷款,0:客户退款,1:客户不能

Then I deployed my model with FastAPI as a backend and Streamlit as the frontend, my goal is to connect Streamlit and FastAPI.然后我部署了我的 model,FastAPI 作为后端,Streamlit 作为前端,我的目标是连接 Streamlit 和 FastAPI。 For this, I followed the tutorial that I mentionned in my question.为此,我遵循了我在问题中提到的教程。

My FastAPI code works and returns a prediction.我的 FastAPI 代码有效并返回预测。 What I did next was using Streamlit for the model inputs and use a request post to get a prediction by the backend(FastAPI).我接下来做的是对 model 输入使用 Streamlit,并使用请求发布来获得后端 (FastAPI) 的预测。 But when I did that I got the error that I mentionned above (dfloc error missing value ) I hope now it's less confusing and more clear.但是当我这样做时,我得到了上面提到的错误(dfloc error missing value)我希望现在它不那么混乱并且更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM