File size: 1,610 Bytes
26d859f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
import joblib
import numpy as np
import pandas as pd

# Load the pre-trained model
model = joblib.load("iris_model.pkl")

# Define the mapping for iris species
species = {0: "setosa", 1: "versicolor", 2: "virginica"}

# App Title
st.title("Iris Species Classifier")
st.write("Enter the measurements of an Iris flower to predict its species.")

# Input widgets for user to enter measurements
sepal_length = st.number_input("Sepal Length (cm)", min_value=0.0, value=5.1)
sepal_width  = st.number_input("Sepal Width (cm)", min_value=0.0, value=3.5)
petal_length = st.number_input("Petal Length (cm)", min_value=0.0, value=1.4)
petal_width  = st.number_input("Petal Width (cm)", min_value=0.0, value=0.2)

if st.button("Predict"):
    # Prepare the input as a 2D array for prediction
    input_features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
    prediction = model.predict(input_features)
    st.success(f"The predicted Iris species is **{species[prediction[0]]}**.")


### TO-DO: ADD A BAR CHART WITH THE MEASUREMENTS ENTERED.
### Create a dataframe first with the input values. Then use streamlit's bar_chart function: https://docs.streamlit.io/develop/api-reference/charts/st.bar_chart
    df = pd.DataFrame({
            "Measurement": ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"],
            "Value (cm)": [sepal_length, sepal_width, petal_length, petal_width]
        })

    # Use the measurement as index for better display
    df.set_index("Measurement", inplace=True)

    st.subheader("Entered Measurements")
    st.bar_chart(df)