Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,20 +1,12 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
import os
|
| 7 |
-
import mlflow
|
| 8 |
-
import shutil
|
| 9 |
-
|
| 10 |
-
# Disable MLflow tracking to avoid creating new runs in the deployed app
|
| 11 |
-
mlflow.set_tracking_uri("file:///dev/null")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
model_repo_id_app = "rakesh1248/random_forest_engine_condition_classifier" # Use the full repo_id
|
| 17 |
-
model_path_in_repo_app = "random_forest_model.joblib"
|
| 18 |
|
| 19 |
model_dir_app = "./model_cache"
|
| 20 |
os.makedirs(model_dir_app, exist_ok=True)
|
|
@@ -27,42 +19,30 @@ def load_model_app():
|
|
| 27 |
local_dir=model_dir_app
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
model_path = os.path.join(repo_path, random_forest_model.joblib)
|
| 32 |
|
| 33 |
-
# Load model directly using joblib
|
| 34 |
model = joblib.load(model_path)
|
| 35 |
-
|
| 36 |
-
# local_model_path = hf_hub_download(
|
| 37 |
-
# repo_id=model_repo_id_app,
|
| 38 |
-
# filename=model_path_in_repo_app,
|
| 39 |
-
# repo_type="model",
|
| 40 |
-
# local_dir=model_dir_app
|
| 41 |
-
# )
|
| 42 |
return model
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
st.error(f"Error loading model
|
| 45 |
st.stop()
|
| 46 |
|
| 47 |
loaded_model_app = load_model_app()
|
| 48 |
|
| 49 |
-
#
|
| 50 |
st.set_page_config(layout="wide")
|
| 51 |
st.title("Engine Predictive Maintenance App")
|
| 52 |
-
st.write("Predict whether an engine requires maintenance based on sensor readings.")
|
| 53 |
|
| 54 |
-
# Sidebar for user inputs
|
| 55 |
st.sidebar.header("Engine Sensor Readings")
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
coolant_temp = st.sidebar.slider("Coolant Temperature (°C)", min_value=60.0, max_value=200.0, value=80.0, step=0.1)
|
| 64 |
|
| 65 |
-
# Create a DataFrame from user inputs
|
| 66 |
input_data = pd.DataFrame([{
|
| 67 |
'Engine rpm': engine_rpm,
|
| 68 |
'Lub oil pressure': lub_oil_pressure,
|
|
@@ -72,22 +52,16 @@ input_data = pd.DataFrame([{
|
|
| 72 |
'Coolant temp': coolant_temp
|
| 73 |
}])
|
| 74 |
|
| 75 |
-
st.subheader("Current Input Parameters:")
|
| 76 |
st.write(input_data)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
prediction = loaded_model_app.predict(input_data)
|
| 82 |
-
prediction_proba = loaded_model_app.predict_proba(input_data)
|
| 83 |
-
|
| 84 |
-
st.subheader("Prediction Result:")
|
| 85 |
-
if prediction[0] == 1:
|
| 86 |
-
st.error("Engine Condition: Faulty (Maintenance Recommended)")
|
| 87 |
-
else:
|
| 88 |
-
st.success("Engine Condition: Normal")
|
| 89 |
|
| 90 |
-
|
| 91 |
-
st.
|
| 92 |
else:
|
| 93 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# --- Model Config --- #
|
| 8 |
+
model_repo_id_app = "rakesh1248/random_forest_engine_condition_classifier"
|
| 9 |
+
model_filename = "random_forest_model.joblib"
|
|
|
|
|
|
|
| 10 |
|
| 11 |
model_dir_app = "./model_cache"
|
| 12 |
os.makedirs(model_dir_app, exist_ok=True)
|
|
|
|
| 19 |
local_dir=model_dir_app
|
| 20 |
)
|
| 21 |
|
| 22 |
+
model_path = os.path.join(repo_path, model_filename)
|
|
|
|
| 23 |
|
|
|
|
| 24 |
model = joblib.load(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return model
|
| 26 |
+
|
| 27 |
except Exception as e:
|
| 28 |
+
st.error(f"Error loading model: {e}")
|
| 29 |
st.stop()
|
| 30 |
|
| 31 |
loaded_model_app = load_model_app()
|
| 32 |
|
| 33 |
+
# --- UI --- #
|
| 34 |
st.set_page_config(layout="wide")
|
| 35 |
st.title("Engine Predictive Maintenance App")
|
|
|
|
| 36 |
|
|
|
|
| 37 |
st.sidebar.header("Engine Sensor Readings")
|
| 38 |
|
| 39 |
+
engine_rpm = st.sidebar.slider("Engine RPM", 60, 2300, 750)
|
| 40 |
+
lub_oil_pressure = st.sidebar.slider("Lub Oil Pressure", 0.0, 8.0, 3.5, 0.1)
|
| 41 |
+
fuel_pressure = st.sidebar.slider("Fuel Pressure", 0.0, 22.0, 6.0, 0.1)
|
| 42 |
+
coolant_pressure = st.sidebar.slider("Coolant Pressure", 0.0, 8.0, 2.0, 0.1)
|
| 43 |
+
lub_oil_temp = st.sidebar.slider("Lub Oil Temperature", 70.0, 90.0, 78.0, 0.1)
|
| 44 |
+
coolant_temp = st.sidebar.slider("Coolant Temperature", 60.0, 200.0, 80.0, 0.1)
|
|
|
|
| 45 |
|
|
|
|
| 46 |
input_data = pd.DataFrame([{
|
| 47 |
'Engine rpm': engine_rpm,
|
| 48 |
'Lub oil pressure': lub_oil_pressure,
|
|
|
|
| 52 |
'Coolant temp': coolant_temp
|
| 53 |
}])
|
| 54 |
|
|
|
|
| 55 |
st.write(input_data)
|
| 56 |
|
| 57 |
+
if st.button("Predict"):
|
| 58 |
+
prediction = loaded_model_app.predict(input_data)
|
| 59 |
+
proba = loaded_model_app.predict_proba(input_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
if prediction[0] == 1:
|
| 62 |
+
st.error("Faulty Engine")
|
| 63 |
else:
|
| 64 |
+
st.success("Normal Engine")
|
| 65 |
+
|
| 66 |
+
st.write(f"Normal: {proba[0][0]:.2f}")
|
| 67 |
+
st.write(f"Faulty: {proba[0][1]:.2f}")
|