MLOps / app.py
tiffieszn's picture
Fix app.py location and update Dockerfile CMD for HF Space
b375645
Raw
History Blame Contribute Delete
1.32 kB
import streamlit as st
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# --- Train model on startup ---
@st.cache_resource
def train_model():
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
return model, iris
model, iris = train_model()
# --- Streamlit UI ---
st.title("🌸 Iris Flower Classifier (MLOps Demo)")
st.write("This app trains a RandomForestClassifier on the Iris dataset and predicts the flower type.")
# User input
sepal_length = st.slider("Sepal length (cm)", 4.0, 8.0, 5.5)
sepal_width = st.slider("Sepal width (cm)", 2.0, 4.5, 3.0)
petal_length = st.slider("Petal length (cm)", 1.0, 7.0, 4.0)
petal_width = st.slider("Petal width (cm)", 0.1, 2.5, 1.0)
input_data = [[sepal_length, sepal_width, petal_length, petal_width]]
# Prediction
prediction = model.predict(input_data)[0]
predicted_class = iris.target_names[prediction]
st.subheader("Prediction")
st.write(f"🌼 The predicted Iris species is: **{predicted_class}**")