Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
import joblib
|
| 6 |
+
|
| 7 |
+
# -------- Application Model Settings -------- #
|
| 8 |
+
MODEL_REPOSITORY = "deepakietb/random_forest_engine_condition_classifier"
|
| 9 |
+
MODEL_FILE = "random_forest_model.joblib"
|
| 10 |
+
|
| 11 |
+
CACHE_DIRECTORY = "./model_cache"
|
| 12 |
+
os.makedirs(CACHE_DIRECTORY, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
# -------- Model Loader with Caching -------- #
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def initialize_model():
|
| 17 |
+
"""
|
| 18 |
+
Downloads the model artifact (if not already present)
|
| 19 |
+
and loads it into memory for inference.
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
downloaded_path = snapshot_download(
|
| 23 |
+
repo_id=MODEL_REPOSITORY,
|
| 24 |
+
local_dir=CACHE_DIRECTORY
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
complete_model_path = os.path.join(downloaded_path, MODEL_FILE)
|
| 28 |
+
|
| 29 |
+
return joblib.load(complete_model_path)
|
| 30 |
+
|
| 31 |
+
except Exception as err:
|
| 32 |
+
# Display failure message and halt execution
|
| 33 |
+
st.error(f"Model initialization failed: {err}")
|
| 34 |
+
st.stop()
|
| 35 |
+
|
| 36 |
+
model_instance = initialize_model()
|
| 37 |
+
|
| 38 |
+
# -------- Streamlit Page Configuration -------- #
|
| 39 |
+
st.set_page_config(layout="wide")
|
| 40 |
+
st.title("Engine Predictive Maintenance App")
|
| 41 |
+
|
| 42 |
+
# -------- Sidebar: Input Controls -------- #
|
| 43 |
+
st.sidebar.header("Engine Sensor Readings")
|
| 44 |
+
|
| 45 |
+
engine_rpm = st.sidebar.slider("Engine RPM", min_value=60, max_value=2300, value=750)
|
| 46 |
+
lub_oil_pressure = st.sidebar.slider("Lub Oil Pressure", 0.0, 8.0, 3.5, 0.1)
|
| 47 |
+
fuel_pressure = st.sidebar.slider("Fuel Pressure", 0.0, 22.0, 6.0, 0.1)
|
| 48 |
+
coolant_pressure = st.sidebar.slider("Coolant Pressure", 0.0, 8.0, 2.0, 0.1)
|
| 49 |
+
lub_oil_temp = st.sidebar.slider("Lub Oil Temperature", 70.0, 90.0, 78.0, 0.1)
|
| 50 |
+
coolant_temp = st.sidebar.slider("Coolant Temperature", 60.0, 200.0, 80.0, 0.1)
|
| 51 |
+
|
| 52 |
+
# -------- Prepare Input Payload -------- #
|
| 53 |
+
features_payload = {
|
| 54 |
+
'Engine rpm': engine_rpm,
|
| 55 |
+
'Lub oil pressure': lub_oil_pressure,
|
| 56 |
+
'Fuel pressure': fuel_pressure,
|
| 57 |
+
'Coolant pressure': coolant_pressure,
|
| 58 |
+
'lub oil temp': lub_oil_temp,
|
| 59 |
+
'Coolant temp': coolant_temp
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
input_frame = pd.DataFrame([features_payload])
|
| 63 |
+
|
| 64 |
+
# Display input values for verification
|
| 65 |
+
st.write(input_frame)
|
| 66 |
+
|
| 67 |
+
# -------- Prediction Execution -------- #
|
| 68 |
+
if st.button("Predict"):
|
| 69 |
+
predicted_class = model_instance.predict(input_frame)
|
| 70 |
+
probability_scores = model_instance.predict_proba(input_frame)
|
| 71 |
+
|
| 72 |
+
# Interpret prediction outcome
|
| 73 |
+
if predicted_class[0] == 1:
|
| 74 |
+
st.error("Faulty Engine")
|
| 75 |
+
else:
|
| 76 |
+
st.success("Normal Engine")
|
| 77 |
+
|
| 78 |
+
# Display probability distribution
|
| 79 |
+
st.write(f"Normal: {probability_scores[0][0]:.2f}")
|
| 80 |
+
st.write(f"Faulty: {probability_scores[0][1]:.2f}")
|