SunnyShaurya commited on
Commit
fc4b800
Β·
verified Β·
1 Parent(s): 572584e

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +65 -105
  2. requirements.txt +3 -4
app.py CHANGED
@@ -1,118 +1,78 @@
1
- import streamlit as st
2
  import pandas as pd
3
  import joblib
4
  import matplotlib.pyplot as plt
5
- from huggingface_hub import hf_hub_download
6
 
7
- # -----------------------------------------
8
- # Load Model from Hugging Face Model Hub
9
- # -----------------------------------------
10
-
11
- REPO_ID = "SunnyShaurya/engine-condition-classifier"
12
-
13
- model_path = hf_hub_download(
14
- repo_id=REPO_ID,
15
- filename="engine_condition_rf_production.joblib"
16
- )
17
-
18
- threshold_path = hf_hub_download(
19
- repo_id=REPO_ID,
20
- filename="decision_threshold.joblib"
21
- )
22
-
23
- model = joblib.load(model_path)
24
- saved_threshold = joblib.load(threshold_path)
25
 
26
  feature_names = model.feature_names_in_
27
 
28
- # -----------------------------------------
29
- # Streamlit UI
30
- # -----------------------------------------
31
-
32
- st.title("Engine Condition Classification System")
33
-
34
- st.markdown("### Adjust Decision Threshold")
35
- user_threshold = st.slider(
36
- "Decision Threshold",
37
- min_value=0.1,
38
- max_value=0.9,
39
- value=float(saved_threshold),
40
- step=0.01
41
- )
42
-
43
- # -----------------------------------------
44
- # Single Prediction Section
45
- # -----------------------------------------
46
-
47
- st.markdown("## Manual Engine Input")
48
-
49
- input_data = []
50
-
51
- for feature in feature_names:
52
- value = st.number_input(f"{feature}", value=0.0)
53
- input_data.append(value)
54
-
55
- if st.button("Predict Engine Condition"):
56
-
57
- input_df = pd.DataFrame([input_data], columns=feature_names)
58
  probability = model.predict_proba(input_df)[0][1]
59
- prediction = 1 if probability >= user_threshold else 0
60
-
61
- st.write("### Probability of Failure:", round(probability, 4))
62
- st.write(f"Model Confidence: {round(probability*100,2)}%")
63
-
64
- # Explanation Logic
65
- if probability > 0.75:
66
- st.info("High risk detected. Immediate inspection recommended.")
67
- elif probability > 0.55:
68
- st.warning("Moderate risk. Preventive check advised.")
69
- else:
70
- st.success("Low risk. Engine likely operating normally.")
71
-
72
  if prediction == 1:
73
- st.error("⚠ Engine Likely Faulty")
74
  else:
75
- st.success("βœ… Engine Operating Normally")
76
-
77
- # -----------------------------------------
78
- # Confidence Visualization
79
- # -----------------------------------------
80
-
81
- fig, ax = plt.subplots()
82
- ax.bar(["Normal Probability", "Failure Probability"],
83
- [1 - probability, probability])
84
- ax.set_ylim(0, 1)
85
- ax.set_ylabel("Probability")
86
- st.pyplot(fig)
87
-
88
- # -----------------------------------------
89
- # Batch Prediction (CSV Upload)
90
- # -----------------------------------------
91
-
92
- st.markdown("## Batch Prediction (CSV Upload)")
93
-
94
- uploaded_file = st.file_uploader("Upload CSV File", type=["csv"])
95
-
96
- if uploaded_file is not None:
97
-
98
- df = pd.read_csv(uploaded_file)
99
-
100
- # Ensure columns match training features
101
  df = df[feature_names]
102
-
103
  probabilities = model.predict_proba(df)[:, 1]
104
-
105
  df["Probability_of_Failure"] = probabilities
106
- df["Prediction"] = (probabilities >= user_threshold).astype(int)
107
-
108
- st.write("### Prediction Results")
109
- st.write(df.head())
110
-
111
- csv = df.to_csv(index=False).encode("utf-8")
112
-
113
- st.download_button(
114
- "Download Predictions",
115
- csv,
116
- "engine_predictions.csv",
117
- "text/csv"
118
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import pandas as pd
3
  import joblib
4
  import matplotlib.pyplot as plt
5
+ import numpy as np
6
 
7
+ # ----------------------------
8
+ # Load Model
9
+ # ----------------------------
10
+ model = joblib.load("engine_condition_rf_production.joblib")
11
+ saved_threshold = joblib.load("decision_threshold.joblib")
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  feature_names = model.feature_names_in_
14
 
15
+ # ----------------------------
16
+ # Single Prediction Function
17
+ # ----------------------------
18
+ def predict_engine(*inputs):
19
+ input_df = pd.DataFrame([inputs], columns=feature_names)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  probability = model.predict_proba(input_df)[0][1]
21
+ prediction = 1 if probability >= saved_threshold else 0
22
+
 
 
 
 
 
 
 
 
 
 
 
23
  if prediction == 1:
24
+ result = "⚠ Engine Likely Faulty"
25
  else:
26
+ result = "βœ… Engine Operating Normally"
27
+
28
+ return result, round(probability, 4)
29
+
30
+ # ----------------------------
31
+ # Batch Prediction Function
32
+ # ----------------------------
33
+ def batch_predict(file):
34
+ df = pd.read_csv(file.name)
35
+
36
+ missing_cols = [col for col in feature_names if col not in df.columns]
37
+
38
+ if missing_cols:
39
+ return f"Missing required columns: {missing_cols}"
40
+
 
 
 
 
 
 
 
 
 
 
 
41
  df = df[feature_names]
 
42
  probabilities = model.predict_proba(df)[:, 1]
43
+
44
  df["Probability_of_Failure"] = probabilities
45
+ df["Prediction"] = (probabilities >= saved_threshold).astype(int)
46
+
47
+ output_file = "engine_predictions.csv"
48
+ df.to_csv(output_file, index=False)
49
+
50
+ return output_file
51
+
52
+ # ----------------------------
53
+ # Build UI
54
+ # ----------------------------
55
+ with gr.Blocks() as demo:
56
+
57
+ gr.Markdown("# πŸš— Engine Condition Classification System")
58
+
59
+ gr.Markdown("## πŸ”§ Manual Prediction")
60
+
61
+ inputs = []
62
+ for feature in feature_names:
63
+ inputs.append(gr.Number(label=feature))
64
+
65
+ output_text = gr.Textbox(label="Prediction Result")
66
+ output_prob = gr.Number(label="Failure Probability")
67
+
68
+ btn = gr.Button("Predict Engine Condition")
69
+ btn.click(predict_engine, inputs, [output_text, output_prob])
70
+
71
+ gr.Markdown("## πŸ“‚ Batch Prediction (CSV Upload)")
72
+
73
+ file_input = gr.File(label="Upload CSV File")
74
+ file_output = gr.File(label="Download Predictions")
75
+
76
+ file_input.change(batch_predict, file_input, file_output)
77
+
78
+ demo.launch()
requirements.txt CHANGED
@@ -1,7 +1,6 @@
1
- streamlit
2
- scikit-learn==1.6.1
3
  pandas
4
  numpy
5
- joblib
6
- huggingface_hub
7
  matplotlib
 
 
1
+ gradio
 
2
  pandas
3
  numpy
4
+ scikit-learn
 
5
  matplotlib
6
+ joblib