Upload folder using huggingface_hub
Browse files- Dockerfile +13 -0
- app.py +118 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
matplotlib
|
| 6 |
+
joblib
|