AttributeError: ‘str’ object has no attribute ‘text_input’ Can anybody please help me with this error. I am unable to clear it

AttributeError: ‘str’ object has no attribute ‘text_input’

Traceback:

File "c:\users\misbah\anaconda3\envs\diabetes\lib\site-packages\streamlit\ScriptRunner.py", line 319, in _run_script
    exec(code, module.__dict__)File "C:\Users\Misbah\Jupyter Notebooks\Projects\diabetes\app.py", line 71, in <module>
    main()File "C:\Users\Misbah\Jupyter Notebooks\Projects\diabetes\app.py", line 44, in main
    ins = st.text_input("Insulin Level (IU/mL)", "Type Here")

This is the code i am running:

import pandas as pd

import numpy as np

import pickle

clf = pickle.load(open("diabetes_model.pkl", "rb"))

def predict_diabetes(Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, bmi, DiabetesPedigreeFunction, Age):

    p = int(Pregnancies)

    g = int(Glucose)

    bp = int(BloodPressure)

    st = int(SkinThickness)

    ins = int(Insulin)

    bmi = float(bmi)

    dpf = float(DiabetesPedigreeFunction)

    age = int(Age)

    data = np.array([[p, g, bp, st, ins, bmi, dpf, age]])

    prediction = clf.predict(data)[0]

    return int(prediction)

def main():

    import streamlit as st

    st.write("""

    # My First ML Web App!

    #### Mohammed Misbahullah Sheriff""")

    head_html = """

    <div style="background-color:#3498DB; padding:10px">

    <h2 style="color:white; text-align:center;">DIABETES PREDICTOR</h2>

    </div>

    """

    st.markdown(head_html, unsafe_allow_html=True)

    p = st.text_input("No. of Pregnancies", "Type Here")

    g = st.text_input("Glucose Level (mg/dL)", "Type Here")

    bp = st.text_input("Blood Pressure (mmHg)", "Type Here")

    st = st.text_input("Skin Thickness (mm)", "Type Here")

    ins = st.text_input("Insulin Level (IU/mL)", "Type Here")

    bmi = st.text_input("Body Mass Index", "Type Here")

    dpf = st.text_input("Diabetes Pedigree Function (eg. 0.55)", "Type Here")

    age = st.text_input("Age (years)", "Type Here")

    safe_html = """

    <div style="background-color:#1D8348; padding:10px">

    <h2 style="color:white; text-align:center;">You don't have diabetes! &#128512;</h2>

    </div>

    """

    unsafe_html = """

    <div style="background-color:#CB4335 padding:10px">

    <h2 style="color:white; text-align:center;">You have diabetes. &128577;</h2>

    </div>

    """

    if st.button("PREDICT"):

        output = predict_diabetes(p, g, bp, st, ins, bmi, dpf, age)

        if output == 1:

            st.markdown(unsafe_html, unsafe_allow_html=True)

        else:

            st.markdown(safe_html, unsafe_allow_html=True)

if __name__ == "__main__":

    main()

Hello @Mohammed_Misbahullah,

st = st.text_input("Skin Thickness (mm)", "Type Here")

On that line you’re redefining st to a string. That’s why you’re having that AttributeError exception.

2 Likes

Thank you!! :slightly_smiling_face: