JSONDecodeError:期望值:第 1 行第 1 列(字符 0)streamlit API

时间:2021-05-24 21:46:08

标签: python json api streamlit

我正在尝试使用 Flask/streamlit 构建基于机器学习模型的 API。但我收到错误“JSONDecodeError:预期值:第 1 行第 1 列(字符 0)”。我知道这是一个错误,即响应的形式不是 Json 类型的数据并且需要解码。我不怎么使用 Json,我尝试了很多东西,但仍然遇到同样的错误。


import streamlit as st
import requests
import datetime
import shap
import json
import pickle
import pandas as pd
import streamlit.components.v1 as components
import matplotlib.pyplot as plt
import sys
from json import JSONEncoder, JSONDecoder, JSONDecodeError
from bson import ObjectId
from tornado.escape import json_decode


st.title('title')
url = 'http://127.0.0.1:5000/'
endpoint = '/'

# description and instructions
st.write('''title''')

st.sidebar.header('User Input features')

def user_input_features():
    input_features = {}
    input_features ["age"] = st.sidebar.slider('Age', 18, 120)
    input_features["sex"] = st.sidebar.selectbox('Sex', ['0', '1']) # 0 femal, 1 male
    input_features["exang"]= st.sidebar.selectbox('Is Excersize Inducing Angina?', ['0', '1']) # 1 yes
    input_features["caa"] = st.sidebar.selectbox('Number of major vessels', ['1', '2', '3']) # Range (1-3 vessels)
    input_features["cp"] = st.sidebar.selectbox('What type of chest pain do ypu have?', ['1', '2', '3', '4'])
    input_features["trtbps"] = st.sidebar.slider('Systolic blood pressure (Hg mm)', 60, 250)
    input_features["chol"] = st.sidebar.slider('Colesterol level (mg/dl)', 100, 600)
    input_features["fbs"] = st.sidebar.selectbox('Fasting blood sugar >120 ?', ['1', '0']) # 1 means yes
    input_features["restecg"] = st.sidebar.selectbox('Type of rest ECG?', ['1', '2', '3']) #(if Normal type (0), if ST-T wave abnormality type (1), propable left ventricular hypertrophy type (2)
    input_features["thalachh"] = st.sidebar.slider('Max heart rate acheived?', 60, 250)
    input_features["oldpeak"] = st.sidebar.slider('Previous peak', 0, 7)
    input_features["slp"] = st.sidebar.slider('slope', 0, 2)
    input_features["thall"] = st.sidebar.slider('β-Thalassemia cardiomyopathy', 0, 3)
    return [input_features]

json_data = user_input_features()


def st_shap(plot, height=None):
    shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
    components.html(shap_html, height=height)
    
# read pickle files
with open('score_objects.pkl', 'rb') as handle:
    features_selected, clf, explainer = pickle.load(handle)

# explain model prediction results
def explain_model_prediction(data):
    # Calculate Shap values
    shap_values = explainer.shap_values(data)
    p = shap.force_plot(explainer.expected_value[1], shap_values[1], data)
    return p, shap_values
# submit values
submit = st.sidebar.button('Get predictions')
if submit:
    results = requests.get(url+endpoint, json=json_data)
    results = json.loads(results.text)
    results = pd.DataFrame([results])
    
    st.header('Final Result')
    prediction = results["prediction"]
    probability = results["probability"]
    
    st.write("Prediction: ", int(prediction))
    st.write("Probability: ", round(float(probability),3))

整个错误文本是:

File "C:\Users\mas082\AppData\Roaming\Python\Python36\site-packages\streamlit\script_runner.py", line 333, in _run_script
    exec(code, module.__dict__)
File "C:\Users\mas082\OneDrive - UiT Office 365\Desktop\VS_Code\flask_end\streamlit\webapp.py", line 63, in <module>
    results = json.loads(results.text)
File "c:\users\mas082\anaconda3\envs\tensor\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
File "c:\users\mas082\anaconda3\envs\tensor\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\users\mas082\anaconda3\envs\tensor\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

0 个答案:

没有答案