thalaivanan87 commited on
Commit
4fd6e54
·
1 Parent(s): 98d67fd

Add Gradio app with batch prediction

Browse files
Files changed (3) hide show
  1. README.md +2 -13
  2. app.py +54 -0
  3. requirements.txt +8 -0
README.md CHANGED
@@ -1,13 +1,2 @@
1
- ---
2
- title: Predictive Maintenance
3
- emoji: 📈
4
- colorFrom: indigo
5
- colorTo: blue
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
- short_description: Predictive Maintenance App
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # Predictive Maintenance App
2
+ This app predicts engine condition (Faulty or Active) based on sensor readings using a Decision Tree model.
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import numpy as np
4
+ import pandas as pd
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ # Download model & scaler from Hugging Face Hub
8
+ model_path = hf_hub_download(repo_id="thalaivanan/pm-decision-tree", filename="model.joblib")
9
+ scaler_path = hf_hub_download(repo_id="thalaivanan/pm-decision-tree", filename="scaler.joblib")
10
+
11
+ model = joblib.load(model_path)
12
+ scaler = joblib.load(scaler_path)
13
+
14
+ feature_names = ["engine_rpm", "lub_oil_pressure", "fuel_pressure", "coolant_pressure", "lub_oil_temp", "coolant_temp"]
15
+
16
+ # Single prediction function
17
+ def predict_single(engine_rpm, lub_oil_pressure, fuel_pressure, coolant_pressure, lub_oil_temp, coolant_temp):
18
+ features = np.array([[engine_rpm, lub_oil_pressure, fuel_pressure, coolant_pressure, lub_oil_temp, coolant_temp]])
19
+ scaled_features = scaler.transform(features)
20
+ prediction = model.predict(scaled_features)[0]
21
+ return "Faulty" if prediction == 1 else "Active"
22
+
23
+ # Batch prediction function
24
+ def predict_batch(file):
25
+ df = pd.read_csv(file)
26
+ if not all(col in df.columns for col in feature_names):
27
+ return "Error: CSV must contain columns: " + ", ".join(feature_names)
28
+ scaled_features = scaler.transform(df[feature_names])
29
+ predictions = model.predict(scaled_features)
30
+ df["prediction"] = ["Faulty" if p == 1 else "Active" for p in predictions]
31
+ output_file = "predictions.csv"
32
+ df.to_csv(output_file, index=False)
33
+ return output_file
34
+
35
+ # Gradio UI
36
+ single_inputs = [
37
+ gr.Number(label="Engine RPM"),
38
+ gr.Number(label="Lub Oil Pressure"),
39
+ gr.Number(label="Fuel Pressure"),
40
+ gr.Number(label="Coolant Pressure"),
41
+ gr.Number(label="Lub Oil Temp"),
42
+ gr.Number(label="Coolant Temp")
43
+ ]
44
+
45
+ single_output = gr.Textbox(label="Engine Condition")
46
+
47
+ batch_input = gr.File(label="Upload CSV", file_types=[".csv"])
48
+ batch_output = gr.File(label="Download Predictions")
49
+
50
+ tab1 = gr.Interface(fn=predict_single, inputs=single_inputs, outputs=single_output, title="Single Prediction")
51
+ tab2 = gr.Interface(fn=predict_batch, inputs=batch_input, outputs=batch_output, title="Batch Prediction")
52
+
53
+ demo = gr.TabbedInterface([tab1, tab2], ["Single Prediction", "Batch Prediction"])
54
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ scikit-learn
3
+ imbalanced-learn
4
+ xgboost
5
+ joblib
6
+ huggingface_hub
7
+ numpy
8
+ matplotlib