Upload folder using huggingface_hub
Browse filesFiles changed (4)
- DockerfileAdded+33
- README.md+4-7
- app.pyAdded+91
- requirements.txtAdded+6
123456789101112131415161718192021222324252627282930313233# Use a minimal base image with Python 3.10 installedFROM python:3.10-slim
#Install neccesary libraries:RUN apt-get update && apt-get install -y build-essential && rm -rf /var/lib/apt/lists/*
# Set the working directory inside the container to /appWORKDIR /app
# Copy all files from the current directory on the host to the container's /app directoryCOPY . .
# Install Python dependencies listed in requirements.txtRUN pip3 install -r requirements.txt
RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH
#Below ones don't work in huggingface streamlit spaces:#ENV HUGGINGFACE_USER_NAME=${HUGGINGFACE_USER_NAME}#ENV HUGGINGFACE_MODEL_NAME=${HUGGINGFACE_MODEL_NAME}
WORKDIR $HOME/app
COPY --chown=user . $HOME/app
# Expose container port for Streamlit:EXPOSE 7860
# Define the command to run the Streamlit app on port "7860" and make it accessible externallyCMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false", "--gatherUsageStats=false"]123456789101112---title: EngineFailurePredictionSpaceemoji: 🐢colorFrom: yellowcolorTo: graysdk: dockerpinned: falselicense: mitshort_description: Engine Failure Prediction Streamlit Space---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789---title: Engine Failure Predictorsdk: dockersdk_version: latestapp_port: 7860app_file: app.pypinned: falselicense: mit---
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import streamlit as stimport pandas as pdfrom huggingface_hub import hf_hub_downloadimport joblibimport os
# To ensure app starts loading quickly we set the title first:st.set_page_config(page_title="Engine Failure Prediction", layout="centered")
# Common constantsHUGGINGFACE_USER_NAME = os.getenv('HUGGINGFACE_USER_NAME')HUGGINGFACE_MODEL_NAME = os.getenv('HUGGINGFACE_MODEL_NAME')
# Download the model from the Model Hub@st.cache_resource # Use caching to avoid re-downloading on every slider movedef load_remote_model(): try: repo_id = f"{HUGGINGFACE_USER_NAME}/{HUGGINGFACE_MODEL_NAME}" model_path = hf_hub_download( repo_id=repo_id, filename="model.joblib" ) return joblib.load(model_path) except Exception as e: print(f"Error loading model: {e}") return e
# Streamlit UI Setupst.title("Engine Failure Prediction")st.write("""This tool predicts engine health based on real-time telemetry.Adjust the sliders below to simulate engine sensor data.""")
st.divider()
# Create UI Layoutcol1, col2 = st.columns(2)
with col1: engine_rpm = st.number_input("Engine RPM", min_value=20, max_value=2500, value=791, step=1, help="Rotations per minute") lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, max_value=8.0, value=3.3, step=0.1) fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, max_value=25.0, value=6.6, step=0.1)
with col2: coolant_pressure = st.number_input("Coolant Pressure (bar)", min_value=0.0, max_value=8.0, value=2.3, step=0.1) lub_oil_temp = st.number_input("Lub Oil Temp (°C)", min_value=30.0, max_value=100.0, value=77.6, step=0.1) coolant_temp = st.number_input("Coolant Temp (°C)", min_value=30.0, max_value=200.0, value=78.4, step=0.1)
# Prepare input data matching the exact training schema# these keys match the 'numeric_scaling' list in our model training scriptinput_dict = { "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}
input_data = pd.DataFrame([input_dict])
# Prediction Logic# We use 0.5 but a lower value could be slightly more sensitive to failures (maximizing Recall)classification_threshold = 0.5
st.divider()
if st.button("Generate Prediction", type="primary"): # loading the model(it will be cached so only first time it will actually download) model = load_remote_model()
# The 'model' here is the Scikit-Learn Pipeline # It automatically runs the StandardScaler on input_data before passing to XGBoost prediction_proba = model.predict_proba(input_data)[0, 1]
# Apply custom threshold prediction = 1 if prediction_proba >= classification_threshold else 0
if prediction == 1: st.error(f"### ⚠️ CRITICAL: Engine Failure Likely\n**Probability of Failure:** {prediction_proba:.2%}") st.write("Immediate maintenance inspection recommended to avoid service disruption.") else: st.success(f"### ✅ NORMAL: Engine Healthy\n**Probability of Failure:** {prediction_proba:.2%}") st.write("Engine parameters are within safe operating margins.")
# Add technical metadata for your portfoliowith st.expander("View Model & System Details"): st.write(f"**Model Source:** Hugging Face Hub ({HUGGINGFACE_MODEL_NAME})") st.write(f"**Threshold Applied:** {classification_threshold}") st.write("**Architecture:** Pipeline(StandardScaler -> XGBoost)")123456pandas==2.2.2huggingface_hub==0.32.6streamlit==1.43.2joblib==1.5.1scikit-learn==1.6.0xgboost==2.1.4DockerfileAdded+33
README.md+4-7
app.pyAdded+91
requirements.txtAdded+6