File size: 2,628 Bytes
8cd2b67
 
 
 
7704c0e
 
 
 
8cd2b67
7704c0e
8cd2b67
7704c0e
 
 
8cd2b67
 
7704c0e
 
8cd2b67
 
 
7704c0e
 
8cd2b67
7704c0e
 
 
 
 
 
 
 
 
 
 
 
 
8cd2b67
 
 
7704c0e
8cd2b67
 
7704c0e
8cd2b67
 
7704c0e
8cd2b67
 
7704c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b3da24
7704c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import streamlit as st
import pandas as pd
import joblib

# -----------------------------
# Page Config
# -----------------------------
st.set_page_config(page_title="Engine Condition App", layout="wide")

st.title("πŸš— Engine Condition Classification")

# -----------------------------
# Load Model
# -----------------------------
@st.cache_resource
def load_model():
    model = joblib.load("best_model.pkl")
    return model

model = load_model()

# βœ… IMPORTANT: use feature names from model training
feature_names = model.feature_names_in_

# -----------------------------
# SINGLE PREDICTION
# -----------------------------
st.header("πŸ”§ Manual Prediction")

input_values = []

cols = st.columns(3)

for i, feature in enumerate(feature_names):
    with cols[i % 3]:
        value = st.number_input(feature, value=0.0)
        input_values.append(value)

if st.button("Predict Engine Condition"):

    input_df = pd.DataFrame([input_values], columns=feature_names)

    prediction = model.predict(input_df)[0]
    probability = model.predict_proba(input_df)[0][1]

    if prediction == 1:
        st.error("⚠ Engine Needs Maintenance")
    else:
        st.success("βœ… Engine Operating Normally")

    st.write(f"**Failure Probability:** {probability:.3f}")

# -----------------------------
# BATCH PREDICTION
# -----------------------------
st.header("πŸ“‚ Batch Prediction")

uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])

if uploaded_file is not None:
    try:
        df = pd.read_csv(uploaded_file)

        # Safety row limit (prevents Space crash)
        if len(df) > 20000:
            st.warning("⚠ File too large. Max 10,000 rows allowed.")
        else:
            # Check missing columns
            missing_cols = [col for col in feature_names if col not in df.columns]

            if missing_cols:
                st.error(f"Missing required columns: {missing_cols}")
            else:
                df = df[feature_names]

                probabilities = model.predict_proba(df)[:, 1]
                df["Failure_Probability"] = probabilities
                df["Prediction"] = (probabilities >= 0.5).astype(int)

                st.success("βœ… Predictions completed!")

                st.dataframe(df.head())

                csv = df.to_csv(index=False).encode("utf-8")

                st.download_button(
                    label="Download Results",
                    data=csv,
                    file_name="engine_predictions.csv",
                    mime="text/csv",
                )

    except Exception as e:
        st.error(f"Error processing file: {e}")