Upload folder using huggingface_hub
Browse files- Dockerfile +2 -1
- app.py +78 -58
Dockerfile
CHANGED
|
@@ -33,9 +33,10 @@ USER user
|
|
| 33 |
EXPOSE 7860
|
| 34 |
|
| 35 |
# Health check for container monitoring
|
| 36 |
-
HEALTHCHECK --interval=30s --timeout=10s --start-period=
|
| 37 |
CMD curl --fail http://localhost:7860/_stcore/health || exit 1
|
| 38 |
|
|
|
|
| 39 |
# Run Streamlit on port 7860 with production settings
|
| 40 |
CMD ["streamlit", "run", "app.py", \
|
| 41 |
"--server.port=7860", \
|
|
|
|
| 33 |
EXPOSE 7860
|
| 34 |
|
| 35 |
# Health check for container monitoring
|
| 36 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
| 37 |
CMD curl --fail http://localhost:7860/_stcore/health || exit 1
|
| 38 |
|
| 39 |
+
|
| 40 |
# Run Streamlit on port 7860 with production settings
|
| 41 |
CMD ["streamlit", "run", "app.py", \
|
| 42 |
"--server.port=7860", \
|
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
Streamlit Application for Engine Predictive Maintenance
|
| 3 |
-
With detailed logging for debugging
|
| 4 |
"""
|
| 5 |
|
| 6 |
import streamlit as st
|
|
@@ -34,7 +33,6 @@ except Exception as e:
|
|
| 34 |
st.stop()
|
| 35 |
|
| 36 |
# CRITICAL: Feature columns must EXACTLY match model training
|
| 37 |
-
# These are from model_prep.py FEATURE_COLUMNS
|
| 38 |
FEATURE_COLUMNS = [
|
| 39 |
"Engine rpm",
|
| 40 |
"Lub oil pressure",
|
|
@@ -90,56 +88,69 @@ st.markdown("""
|
|
| 90 |
|
| 91 |
@st.cache_resource
|
| 92 |
def load_model():
|
| 93 |
-
"""Load model from Hugging Face with detailed logging"""
|
| 94 |
|
| 95 |
print("\n" + "=" * 70, file=sys.stderr)
|
| 96 |
print("LOADING MODEL FROM HUGGING FACE", file=sys.stderr)
|
| 97 |
print("=" * 70, file=sys.stderr)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
def main():
|
|
@@ -157,8 +168,9 @@ def main():
|
|
| 157 |
unsafe_allow_html=True
|
| 158 |
)
|
| 159 |
|
| 160 |
-
# Load model
|
| 161 |
-
model
|
|
|
|
| 162 |
|
| 163 |
if model is None:
|
| 164 |
st.error(f"β Failed to load prediction model")
|
|
@@ -175,6 +187,16 @@ def main():
|
|
| 175 |
st.write(f"- HF_TOKEN set: {os.environ.get('HF_TOKEN') is not None}")
|
| 176 |
st.write("- Expected repo: Quantum9999/xgb-predictive-maintenance")
|
| 177 |
st.write("- Expected file: xgb_tuned_model.joblib")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
st.stop()
|
| 180 |
|
|
@@ -288,20 +310,18 @@ def main():
|
|
| 288 |
st.markdown("---")
|
| 289 |
|
| 290 |
if st.button("π Predict Engine Condition", use_container_width=True, type="primary"):
|
| 291 |
-
#
|
| 292 |
input_df = pd.DataFrame([{
|
| 293 |
-
"Engine rpm": engine_rpm,
|
| 294 |
-
"Lub oil pressure": lub_oil_pressure,
|
| 295 |
-
"Fuel pressure": fuel_pressure,
|
| 296 |
-
"Coolant pressure": coolant_pressure,
|
| 297 |
-
"lub oil temp": lub_oil_temp,
|
| 298 |
-
"Coolant temp": coolant_temp
|
| 299 |
}])
|
| 300 |
|
| 301 |
try:
|
| 302 |
print(f"Making prediction with input: {input_df.to_dict()}", file=sys.stderr)
|
| 303 |
-
print(f"Input columns: {input_df.columns.tolist()}", file=sys.stderr)
|
| 304 |
-
print(f"Expected columns: {FEATURE_COLUMNS}", file=sys.stderr)
|
| 305 |
|
| 306 |
# Make prediction
|
| 307 |
prediction = model.predict(input_df)[0]
|
|
|
|
| 1 |
"""
|
| 2 |
Streamlit Application for Engine Predictive Maintenance
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import streamlit as st
|
|
|
|
| 33 |
st.stop()
|
| 34 |
|
| 35 |
# CRITICAL: Feature columns must EXACTLY match model training
|
|
|
|
| 36 |
FEATURE_COLUMNS = [
|
| 37 |
"Engine rpm",
|
| 38 |
"Lub oil pressure",
|
|
|
|
| 88 |
|
| 89 |
@st.cache_resource
|
| 90 |
def load_model():
|
| 91 |
+
"""Load model from Hugging Face with detailed logging and retries"""
|
| 92 |
|
| 93 |
print("\n" + "=" * 70, file=sys.stderr)
|
| 94 |
print("LOADING MODEL FROM HUGGING FACE", file=sys.stderr)
|
| 95 |
print("=" * 70, file=sys.stderr)
|
| 96 |
|
| 97 |
+
max_retries = 3
|
| 98 |
+
retry_count = 0
|
| 99 |
+
|
| 100 |
+
while retry_count < max_retries:
|
| 101 |
+
try:
|
| 102 |
+
# CORRECT: Use HF_TOKEN (as configured in your HF Space secrets)
|
| 103 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 104 |
+
print(f"HF_TOKEN found: {hf_token is not None}", file=sys.stderr)
|
| 105 |
+
|
| 106 |
+
if hf_token:
|
| 107 |
+
print("Authenticating with Hugging Face...", file=sys.stderr)
|
| 108 |
+
login(token=hf_token)
|
| 109 |
+
print("β Authentication successful", file=sys.stderr)
|
| 110 |
+
else:
|
| 111 |
+
print("β No HF_TOKEN - attempting public access", file=sys.stderr)
|
| 112 |
+
|
| 113 |
+
# Download model
|
| 114 |
+
print("\nDownloading model...", file=sys.stderr)
|
| 115 |
+
print(" Repo: Quantum9999/xgb-predictive-maintenance", file=sys.stderr)
|
| 116 |
+
print(" File: xgb_tuned_model.joblib", file=sys.stderr)
|
| 117 |
+
|
| 118 |
+
model_path = hf_hub_download(
|
| 119 |
+
repo_id="Quantum9999/xgb-predictive-maintenance",
|
| 120 |
+
filename="xgb_tuned_model.joblib",
|
| 121 |
+
token=hf_token,
|
| 122 |
+
cache_dir="/tmp/hf_cache" # Use tmp for faster access
|
| 123 |
+
)
|
| 124 |
+
print(f"β Model downloaded: {model_path}", file=sys.stderr)
|
| 125 |
+
|
| 126 |
+
# Load model
|
| 127 |
+
print("Loading model into memory...", file=sys.stderr)
|
| 128 |
+
model = joblib.load(model_path)
|
| 129 |
+
print("β Model loaded successfully", file=sys.stderr)
|
| 130 |
+
|
| 131 |
+
# Verify model features
|
| 132 |
+
if hasattr(model, 'feature_names_in_'):
|
| 133 |
+
print(f"Model expects features: {model.feature_names_in_}", file=sys.stderr)
|
| 134 |
+
|
| 135 |
+
print("=" * 70 + "\n", file=sys.stderr)
|
| 136 |
+
|
| 137 |
+
return model, None
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
retry_count += 1
|
| 141 |
+
error_msg = f"Model loading attempt {retry_count}/{max_retries} failed: {str(e)}"
|
| 142 |
+
print(f"β {error_msg}", file=sys.stderr)
|
| 143 |
+
|
| 144 |
+
if retry_count < max_retries:
|
| 145 |
+
import time
|
| 146 |
+
wait_time = 2 * retry_count
|
| 147 |
+
print(f"Retrying in {wait_time} seconds...", file=sys.stderr)
|
| 148 |
+
time.sleep(wait_time)
|
| 149 |
+
else:
|
| 150 |
+
import traceback
|
| 151 |
+
print(f"Final traceback:\n{traceback.format_exc()}", file=sys.stderr)
|
| 152 |
+
print("=" * 70 + "\n", file=sys.stderr)
|
| 153 |
+
return None, error_msg
|
| 154 |
|
| 155 |
|
| 156 |
def main():
|
|
|
|
| 168 |
unsafe_allow_html=True
|
| 169 |
)
|
| 170 |
|
| 171 |
+
# Load model with progress indicator
|
| 172 |
+
with st.spinner("Loading AI model... This may take a moment."):
|
| 173 |
+
model, error = load_model()
|
| 174 |
|
| 175 |
if model is None:
|
| 176 |
st.error(f"β Failed to load prediction model")
|
|
|
|
| 187 |
st.write(f"- HF_TOKEN set: {os.environ.get('HF_TOKEN') is not None}")
|
| 188 |
st.write("- Expected repo: Quantum9999/xgb-predictive-maintenance")
|
| 189 |
st.write("- Expected file: xgb_tuned_model.joblib")
|
| 190 |
+
|
| 191 |
+
st.write("\n**Your Setup (from screenshots):**")
|
| 192 |
+
st.write("β
HF Space has HF_TOKEN secret (Image 1)")
|
| 193 |
+
st.write("β
GitHub has HF_EN_TOKEN secret (Image 2)")
|
| 194 |
+
st.write("β
GitHub token for pushing code (Image 3)")
|
| 195 |
+
|
| 196 |
+
st.write("\n**Next Steps:**")
|
| 197 |
+
st.write("1. Verify HF_TOKEN secret exists in Space settings")
|
| 198 |
+
st.write("2. Check Space logs for detailed error messages")
|
| 199 |
+
st.write("3. Ensure model repo is accessible")
|
| 200 |
|
| 201 |
st.stop()
|
| 202 |
|
|
|
|
| 310 |
st.markdown("---")
|
| 311 |
|
| 312 |
if st.button("π Predict Engine Condition", use_container_width=True, type="primary"):
|
| 313 |
+
# Create input DataFrame with exact column names
|
| 314 |
input_df = pd.DataFrame([{
|
| 315 |
+
"Engine rpm": engine_rpm,
|
| 316 |
+
"Lub oil pressure": lub_oil_pressure,
|
| 317 |
+
"Fuel pressure": fuel_pressure,
|
| 318 |
+
"Coolant pressure": coolant_pressure,
|
| 319 |
+
"lub oil temp": lub_oil_temp,
|
| 320 |
+
"Coolant temp": coolant_temp
|
| 321 |
}])
|
| 322 |
|
| 323 |
try:
|
| 324 |
print(f"Making prediction with input: {input_df.to_dict()}", file=sys.stderr)
|
|
|
|
|
|
|
| 325 |
|
| 326 |
# Make prediction
|
| 327 |
prediction = model.predict(input_df)[0]
|