Download app.py from Garg06/Engine-Predictive-Maintenance: direct link, hf CLI and curl.
- Browser
- Download file 4.85 kB
-
https://huggingface.co/spaces/Garg06/Engine-Predictive-Maintenance/resolve/main/app.py
- Command line
-
hf download hf://spaces/Garg06/Engine-Predictive-Maintenance/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Garg06/Engine-Predictive-Maintenance/resolve/main/app.py
4.85 kB
| import streamlit as st | |
| import pandas as pd | |
| from huggingface_hub import hf_hub_download | |
| import joblib | |
| # Download and load the best engine model from Hugging Face Model Hub. | |
| # The model is a scikit-learn compatible estimator saved with joblib. | |
| model_path = hf_hub_download( | |
| repo_id='Garg06/Predictive-Maintenance-Model', | |
| filename='best_engine_model.joblib' | |
| ) | |
| model = joblib.load(model_path) | |
| # ββ App header ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| st.title('Engine Predictive Maintenance App') | |
| st.write(""" | |
| This application predicts whether an engine is **Faulty** or **Normal** based on | |
| real-time sensor readings. Enter the current sensor values below to get a prediction. | |
| """) | |
| # ββ Sensor input fields βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| st.header('Engine Sensor Readings') | |
| # Engine RPM: observed range 61β2239, typical idle around 750β800 RPM. | |
| engine_rpm = st.number_input( | |
| 'Engine RPM', | |
| min_value=0.0, max_value=2500.0, value=800.0, step=1.0, | |
| help='Engine revolutions per minute. Typical idle: ~750 RPM.' | |
| ) | |
| # Lub oil pressure: observed range 0β7.27 bar. | |
| lub_oil_pressure = st.number_input( | |
| 'Lub Oil Pressure (bar)', | |
| min_value=0.0, max_value=10.0, value=3.3, step=0.01, | |
| help='Lubrication oil pressure in bar. Healthy range: 2.5β4.5 bar.' | |
| ) | |
| # Fuel pressure: observed range 0β21.14 bar. | |
| fuel_pressure = st.number_input( | |
| 'Fuel Pressure (bar)', | |
| min_value=0.0, max_value=25.0, value=6.7, step=0.01, | |
| help='Fuel system pressure in bar. Healthy range: 4.9β7.7 bar.' | |
| ) | |
| # Coolant pressure: observed range 0β7.48 bar. | |
| coolant_pressure = st.number_input( | |
| 'Coolant Pressure (bar)', | |
| min_value=0.0, max_value=10.0, value=2.3, step=0.01, | |
| help='Engine coolant system pressure in bar. Healthy range: 1.6β2.8 bar.' | |
| ) | |
| # Lub oil temperature: observed range 71β90 Β°C. | |
| lub_oil_temp = st.number_input( | |
| 'Lub Oil Temperature (Β°C)', | |
| min_value=60.0, max_value=100.0, value=77.6, step=0.1, | |
| help='Lubrication oil temperature in Β°C. Normal operating range: 75β80 Β°C.' | |
| ) | |
| # Coolant temperature: observed range 62β196 Β°C. | |
| coolant_temp = st.number_input( | |
| 'Coolant Temperature (Β°C)', | |
| min_value=60.0, max_value=200.0, value=78.4, step=0.1, | |
| help='Engine coolant temperature in Β°C. Normal range: 74β83 Β°C.' | |
| ) | |
| # ββ Assemble raw sensor input βββββββββββββββββββββββββββββββββββββββββββββ | |
| # Column names must exactly match those used during model training. | |
| input_data = pd.DataFrame([{ | |
| 'Engine rpm': engine_rpm, | |
| 'Lub oil pressure': lub_oil_pressure, | |
| 'Fuel pressure': fuel_pressure, | |
| 'Coolant pressure': coolant_pressure, | |
| 'lub oil temp': lub_oil_temp, | |
| 'Coolant temp': coolant_temp, | |
| }]) | |
| # ββ Feature engineering β must match prep.py exactly βββββββββββββββββββββ | |
| # The model was trained on 12 features (6 raw + 6 engineered). | |
| # The saved Pipeline handles StandardScaling internally. | |
| eps = 1e-6 | |
| input_data['pressure_ratio_fuel_coolant'] = ( | |
| input_data['Fuel pressure'] / (input_data['Coolant pressure'] + eps) | |
| ) | |
| input_data['temp_delta_oil_coolant'] = ( | |
| input_data['lub oil temp'] - input_data['Coolant temp'] | |
| ) | |
| input_data['lub_pressure_per_rpm'] = ( | |
| (input_data['Lub oil pressure'] / (input_data['Engine rpm'] + eps)) * 1000 | |
| ) | |
| input_data['coolant_pressure_per_rpm'] = ( | |
| (input_data['Coolant pressure'] / (input_data['Engine rpm'] + eps)) * 1000 | |
| ) | |
| input_data['thermal_stress_index'] = ( | |
| (input_data['lub oil temp'] + input_data['Coolant temp']) / 2 | |
| ) | |
| input_data['pressure_diff_fuel_lub'] = ( | |
| input_data['Fuel pressure'] - input_data['Lub oil pressure'] | |
| ) | |
| # ββ Prediction ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if st.button('Predict Engine Condition'): | |
| prediction = model.predict(input_data)[0] | |
| probability = model.predict_proba(input_data)[0] | |
| if prediction == 1: | |
| st.error( | |
| f'β οΈ **Faulty Engine Detected!** ' | |
| f'Confidence: {probability[1]:.1%} ' | |
| f'\n\nImmediate inspection is recommended to prevent breakdown.' | |
| ) | |
| else: | |
| st.success( | |
| f'β **Engine is Normal.** ' | |
| f'Confidence: {probability[0]:.1%} ' | |
| f'\n\nAll sensor readings are within acceptable limits.' | |
| ) | |
| st.subheader('Input Summary') | |
| st.dataframe(input_data) | |