Upload folder using huggingface_hub
Browse files- Dockerfile +33 -0
- README.md +4 -7
- app.py +91 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.10 installed
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
#Install neccesary libraries:
|
| 5 |
+
RUN apt-get update && apt-get install -y build-essential && rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
# Set the working directory inside the container to /app
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
| 11 |
+
COPY . .
|
| 12 |
+
|
| 13 |
+
# Install Python dependencies listed in requirements.txt
|
| 14 |
+
RUN pip3 install -r requirements.txt
|
| 15 |
+
|
| 16 |
+
RUN useradd -m -u 1000 user
|
| 17 |
+
USER user
|
| 18 |
+
ENV HOME=/home/user \
|
| 19 |
+
PATH=/home/user/.local/bin:$PATH
|
| 20 |
+
|
| 21 |
+
#Below ones don't work in huggingface streamlit spaces:
|
| 22 |
+
#ENV HUGGINGFACE_USER_NAME=${HUGGINGFACE_USER_NAME}
|
| 23 |
+
#ENV HUGGINGFACE_MODEL_NAME=${HUGGINGFACE_MODEL_NAME}
|
| 24 |
+
|
| 25 |
+
WORKDIR $HOME/app
|
| 26 |
+
|
| 27 |
+
COPY --chown=user . $HOME/app
|
| 28 |
+
|
| 29 |
+
# Expose container port for Streamlit:
|
| 30 |
+
EXPOSE 7860
|
| 31 |
+
|
| 32 |
+
# Define the command to run the Streamlit app on port "7860" and make it accessible externally
|
| 33 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false", "--gatherUsageStats=false"]
|
README.md
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 🐢
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: docker
|
|
|
|
|
|
|
|
|
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
-
short_description: Engine Failure Prediction Streamlit Space
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Engine Failure Predictor
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: docker
|
| 4 |
+
sdk_version: latest
|
| 5 |
+
app_port: 7860
|
| 6 |
+
app_file: app.py
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
|
|
|
| 9 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import joblib
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# To ensure app starts loading quickly we set the title first:
|
| 8 |
+
st.set_page_config(page_title="Engine Failure Prediction", layout="centered")
|
| 9 |
+
|
| 10 |
+
# Common constants
|
| 11 |
+
HUGGINGFACE_USER_NAME = os.getenv('HUGGINGFACE_USER_NAME')
|
| 12 |
+
HUGGINGFACE_MODEL_NAME = os.getenv('HUGGINGFACE_MODEL_NAME')
|
| 13 |
+
|
| 14 |
+
# Download the model from the Model Hub
|
| 15 |
+
@st.cache_resource # Use caching to avoid re-downloading on every slider move
|
| 16 |
+
def load_remote_model():
|
| 17 |
+
try:
|
| 18 |
+
repo_id = f"{HUGGINGFACE_USER_NAME}/{HUGGINGFACE_MODEL_NAME}"
|
| 19 |
+
model_path = hf_hub_download(
|
| 20 |
+
repo_id=repo_id,
|
| 21 |
+
filename="model.joblib"
|
| 22 |
+
)
|
| 23 |
+
return joblib.load(model_path)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Error loading model: {e}")
|
| 26 |
+
return e
|
| 27 |
+
|
| 28 |
+
# Streamlit UI Setup
|
| 29 |
+
st.title("Engine Failure Prediction")
|
| 30 |
+
st.write("""
|
| 31 |
+
This tool predicts engine health based on real-time telemetry.
|
| 32 |
+
Adjust the sliders below to simulate engine sensor data.
|
| 33 |
+
""")
|
| 34 |
+
|
| 35 |
+
st.divider()
|
| 36 |
+
|
| 37 |
+
# Create UI Layout
|
| 38 |
+
col1, col2 = st.columns(2)
|
| 39 |
+
|
| 40 |
+
with col1:
|
| 41 |
+
engine_rpm = st.number_input("Engine RPM", min_value=20, max_value=2500, value=791, step=1, help="Rotations per minute")
|
| 42 |
+
lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, max_value=8.0, value=3.3, step=0.1)
|
| 43 |
+
fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, max_value=25.0, value=6.6, step=0.1)
|
| 44 |
+
|
| 45 |
+
with col2:
|
| 46 |
+
coolant_pressure = st.number_input("Coolant Pressure (bar)", min_value=0.0, max_value=8.0, value=2.3, step=0.1)
|
| 47 |
+
lub_oil_temp = st.number_input("Lub Oil Temp (°C)", min_value=30.0, max_value=100.0, value=77.6, step=0.1)
|
| 48 |
+
coolant_temp = st.number_input("Coolant Temp (°C)", min_value=30.0, max_value=200.0, value=78.4, step=0.1)
|
| 49 |
+
|
| 50 |
+
# Prepare input data matching the exact training schema
|
| 51 |
+
# these keys match the 'numeric_scaling' list in our model training script
|
| 52 |
+
input_dict = {
|
| 53 |
+
"engine_rpm": engine_rpm,
|
| 54 |
+
"lub_oil_pressure": lub_oil_pressure,
|
| 55 |
+
"fuel_pressure": fuel_pressure,
|
| 56 |
+
"coolant_pressure": coolant_pressure,
|
| 57 |
+
"lub_oil_temp": lub_oil_temp,
|
| 58 |
+
"coolant_temp": coolant_temp
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
input_data = pd.DataFrame([input_dict])
|
| 62 |
+
|
| 63 |
+
# Prediction Logic
|
| 64 |
+
# We use 0.5 but a lower value could be slightly more sensitive to failures (maximizing Recall)
|
| 65 |
+
classification_threshold = 0.5
|
| 66 |
+
|
| 67 |
+
st.divider()
|
| 68 |
+
|
| 69 |
+
if st.button("Generate Prediction", type="primary"):
|
| 70 |
+
# loading the model(it will be cached so only first time it will actually download)
|
| 71 |
+
model = load_remote_model()
|
| 72 |
+
|
| 73 |
+
# The 'model' here is the Scikit-Learn Pipeline
|
| 74 |
+
# It automatically runs the StandardScaler on input_data before passing to XGBoost
|
| 75 |
+
prediction_proba = model.predict_proba(input_data)[0, 1]
|
| 76 |
+
|
| 77 |
+
# Apply custom threshold
|
| 78 |
+
prediction = 1 if prediction_proba >= classification_threshold else 0
|
| 79 |
+
|
| 80 |
+
if prediction == 1:
|
| 81 |
+
st.error(f"### ⚠️ CRITICAL: Engine Failure Likely\n**Probability of Failure:** {prediction_proba:.2%}")
|
| 82 |
+
st.write("Immediate maintenance inspection recommended to avoid service disruption.")
|
| 83 |
+
else:
|
| 84 |
+
st.success(f"### ✅ NORMAL: Engine Healthy\n**Probability of Failure:** {prediction_proba:.2%}")
|
| 85 |
+
st.write("Engine parameters are within safe operating margins.")
|
| 86 |
+
|
| 87 |
+
# Add technical metadata for your portfolio
|
| 88 |
+
with st.expander("View Model & System Details"):
|
| 89 |
+
st.write(f"**Model Source:** Hugging Face Hub ({HUGGINGFACE_MODEL_NAME})")
|
| 90 |
+
st.write(f"**Threshold Applied:** {classification_threshold}")
|
| 91 |
+
st.write("**Architecture:** Pipeline(StandardScaler -> XGBoost)")
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
huggingface_hub==0.32.6
|
| 3 |
+
streamlit==1.43.2
|
| 4 |
+
joblib==1.5.1
|
| 5 |
+
scikit-learn==1.6.0
|
| 6 |
+
xgboost==2.1.4
|