praveenchugh commited on
Commit
4e7aaa3
·
verified ·
1 Parent(s): 7f86de4

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +34 -40
app.py CHANGED
@@ -1,32 +1,24 @@
1
  # ============================================================
2
  # Imports
3
  # ============================================================
4
- import pandas as pd
5
  import streamlit as st
 
6
  import joblib
7
  from huggingface_hub import hf_hub_download
8
 
 
9
  # ============================================================
10
  # Model Loading
11
  # ============================================================
12
  @st.cache_resource
13
  def load_model():
14
- """
15
- Download and load trained model from Hugging Face Hub.
16
-
17
- Caching ensures the model is loaded only once
18
- and reused across user interactions.
19
- """
20
  model_path = hf_hub_download(
21
  repo_id="praveenchugh/engine-condition-gbm-model",
22
  filename="gbm_model.joblib",
23
  )
24
-
25
- # Load model from downloaded file
26
  return joblib.load(model_path)
27
 
28
 
29
- # Initialize model
30
  model = load_model()
31
 
32
 
@@ -36,21 +28,15 @@ model = load_model()
36
  st.title("Engine Condition Predictor")
37
 
38
  st.write(
39
- "Enter engine sensor values to determine whether "
40
- "the engine condition is normal or anomalous."
41
  )
42
 
43
 
44
  # ============================================================
45
- # Input Collection Function
46
  # ============================================================
47
  def get_user_inputs():
48
- """
49
- Collect user inputs from Streamlit UI and
50
- return them as a single-row pandas DataFrame.
51
- """
52
-
53
- # Engine operational inputs
54
  engine_rpm = st.number_input("Engine RPM", value=800.0)
55
  lub_oil_pressure = st.number_input("Lub Oil Pressure", value=3.0)
56
  fuel_pressure = st.number_input("Fuel Pressure", value=2.0)
@@ -58,17 +44,15 @@ def get_user_inputs():
58
  lub_oil_temp = st.number_input("Lub Oil Temp", value=75.0)
59
  coolant_temp = st.number_input("Coolant Temp", value=85.0)
60
 
61
- # Convert inputs into DataFrame (model expects this format)
62
- input_df = pd.DataFrame(
63
- [{
64
- "Engine rpm": engine_rpm,
65
- "Lub oil pressure": lub_oil_pressure,
66
- "Fuel pressure": fuel_pressure,
67
- "Coolant pressure": coolant_pressure,
68
- "Lub oil temp": lub_oil_temp,
69
- "Coolant temp": coolant_temp
70
- }]
71
- )
72
 
73
  return input_df
74
 
@@ -76,19 +60,29 @@ def get_user_inputs():
76
  # ============================================================
77
  # Prediction Logic
78
  # ============================================================
79
- # Capture user inputs
80
  input_df = get_user_inputs()
81
 
82
- # Run prediction when button is clicked
83
  if st.button("Predict"):
84
 
85
- # Generate prediction (0 = normal, 1 = anomaly)
86
- prediction = model.predict(input_df)[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- # Display results
89
- st.subheader("Prediction Result")
90
 
91
- if prediction == 1:
92
- st.error("Engine Condition: Anomalous")
93
- else:
94
- st.success("Engine Condition: Normal")
 
1
  # ============================================================
2
  # Imports
3
  # ============================================================
 
4
  import streamlit as st
5
+ import pandas as pd
6
  import joblib
7
  from huggingface_hub import hf_hub_download
8
 
9
+
10
  # ============================================================
11
  # Model Loading
12
  # ============================================================
13
  @st.cache_resource
14
  def load_model():
 
 
 
 
 
 
15
  model_path = hf_hub_download(
16
  repo_id="praveenchugh/engine-condition-gbm-model",
17
  filename="gbm_model.joblib",
18
  )
 
 
19
  return joblib.load(model_path)
20
 
21
 
 
22
  model = load_model()
23
 
24
 
 
28
  st.title("Engine Condition Predictor")
29
 
30
  st.write(
31
+ "Provide engine sensor values to predict whether the engine condition "
32
+ "is normal or anomalous."
33
  )
34
 
35
 
36
  # ============================================================
37
+ # Input Collection
38
  # ============================================================
39
  def get_user_inputs():
 
 
 
 
 
 
40
  engine_rpm = st.number_input("Engine RPM", value=800.0)
41
  lub_oil_pressure = st.number_input("Lub Oil Pressure", value=3.0)
42
  fuel_pressure = st.number_input("Fuel Pressure", value=2.0)
 
44
  lub_oil_temp = st.number_input("Lub Oil Temp", value=75.0)
45
  coolant_temp = st.number_input("Coolant Temp", value=85.0)
46
 
47
+ # IMPORTANT: Match EXACT training column names
48
+ input_df = pd.DataFrame([{
49
+ "Engine rpm": engine_rpm,
50
+ "Lub oil pressure": lub_oil_pressure,
51
+ "Fuel pressure": fuel_pressure,
52
+ "Coolant pressure": coolant_pressure,
53
+ "lub oil temp": lub_oil_temp,
54
+ "Coolant temp": coolant_temp
55
+ }])
 
 
56
 
57
  return input_df
58
 
 
60
  # ============================================================
61
  # Prediction Logic
62
  # ============================================================
 
63
  input_df = get_user_inputs()
64
 
 
65
  if st.button("Predict"):
66
 
67
+ try:
68
+ # Ensure column order matches training
69
+ if hasattr(model, "feature_names_in_"):
70
+ input_df = input_df[model.feature_names_in_]
71
+
72
+ prediction = model.predict(input_df)[0]
73
+
74
+ st.subheader("Prediction Result")
75
+
76
+ if prediction == 1:
77
+ st.error("Engine Condition: Anomalous")
78
+ else:
79
+ st.success("Engine Condition: Normal")
80
+
81
+ except Exception as e:
82
+ st.error(f"Prediction failed: {str(e)}")
83
 
84
+ # Debug info (very useful)
85
+ st.write("Input columns:", input_df.columns.tolist())
86
 
87
+ if hasattr(model, "feature_names_in_"):
88
+ st.write("Model expects:", list(model.feature_names_in_))