-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
44 lines (37 loc) · 1.68 KB
/
app.py
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
43
44
"""
Iris Classification Streamlit App
This module loads the pre-trained Iris classifier pipeline from disk,
defines iris class mappings, and sets up a Streamlit interface that allows
users to input iris flower features and obtain predictions for the iris species.
"""
import streamlit as st
import pickle
import numpy as np
# Load the saved pipeline (generated by train_model.py)
try:
with open("iris_pipeline.pkl", "rb") as f:
pipeline = pickle.load(f)
except Exception as e:
st.error(f"Error loading the model pipeline: {e}")
st.stop()
# Mapping of iris class indices to human-readable species names
iris_classes = {0: "setosa", 1: "versicolor", 2: "virginica"}
# Application title and description
st.title("Iris Flower Classification")
st.write("Enter the iris features to predict the species:")
# Input fields for iris features
sepal_length = st.number_input("Sepal Length", min_value=0.0, max_value=10.0, value=5.1)
sepal_width = st.number_input("Sepal Width", min_value=0.0, max_value=10.0, value=3.5)
petal_length = st.number_input("Petal Length", min_value=0.0, max_value=10.0, value=1.4)
petal_width = st.number_input("Petal Width", min_value=0.0, max_value=10.0, value=0.2)
# When the Predict button is clicked, perform the prediction
if st.button("Predict"):
# Prepare the feature vector as a 2D array
features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
try:
# Perform prediction using the loaded pipeline
prediction = pipeline.predict(features)
species = iris_classes[prediction[0]]
st.write("Predicted Iris Species:", species)
except Exception as e:
st.error(f"Error during prediction: {e}")