Spaces:
Sleeping
Sleeping
github-actions commited on
Commit ·
7f06bf5
1
Parent(s): 9a6bc35
Deploy from GitHub Actions
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import jsonimport joblibimport pandas as pdimport gradio as grimport spacesfrom huggingface_hub import hf_hub_downloadREPO_ID = "Swetha1929/predictive-maintenance-engine-model"model_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pkl")feature_path = hf_hub_download(repo_id=REPO_ID, filename="feature_names.txt")model = joblib.load(model_path)with open(feature_path, "r", encoding="utf-8") as f:feature_names = [line.strip() for line in f if line.strip()]try:info_path = hf_hub_download(repo_id=REPO_ID, filename="model_info.json")with open(info_path, "r", encoding="utf-8") as f:model_info = json.load(f)except Exception:model_info = {}@spaces.GPUdef predict(*values):input_df = pd.DataFrame([list(values)], columns=feature_names)prediction = model.predict(input_df)[0]if hasattr(model, "predict_proba"):probs = model.predict_proba(input_df)[0]return str(prediction), {f"Class {i}": float(p) for i, p in enumerate(probs)}return str(prediction), {}inputs = [gr.Number(label=feature) for feature in feature_names]demo = gr.Interface(fn=predict,inputs=inputs,outputs=[gr.Textbox(label="Predicted Engine Condition"),gr.Label(label="Prediction Probabilities")],title="Engine Condition Prediction",description="Enter engine parameters to predict the engine condition.")demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121import osimport jsonimport joblibimport pandas as pdimport streamlit as stfrom huggingface_hub import hf_hub_download# Hugging Face Model RepositoryREPO_ID = "Swetha1929/predictive-maintenance-engine-model"st.set_page_config(page_title="Engine Condition Prediction",layout="wide")st.title("Engine Condition Prediction App")st.write("Enter the engine feature values below and click Predict.")# Local backup (used only if HF download fails)BASE_DIR = os.path.dirname(os.path.abspath(__file__))MODELS_DIR = os.path.join(BASE_DIR, "models")MODEL_FILE = os.path.join(MODELS_DIR, "best_model.pkl")FEATURE_FILE = os.path.join(MODELS_DIR, "feature_names.txt")INFO_FILE = os.path.join(MODELS_DIR, "model_info.json")@st.cache_resourcedef load_artifacts():try:model_path = hf_hub_download(repo_id=REPO_ID,filename="best_model.pkl")feature_path = hf_hub_download(repo_id=REPO_ID,filename="feature_names.txt")info_path = hf_hub_download(repo_id=REPO_ID,filename="model_info.json")except Exception:model_path = MODEL_FILEfeature_path = FEATURE_FILEinfo_path = INFO_FILEmodel = joblib.load(model_path)with open(feature_path, "r", encoding="utf-8") as f:feature_names = [line.strip()for line in fif line.strip()]model_info = {}if os.path.exists(info_path):with open(info_path, "r", encoding="utf-8") as f:model_info = json.load(f)return model, feature_names, model_infotry:model, feature_names, model_info = load_artifacts()except Exception as e:st.error(f"Error loading model: {e}")st.stop()if model_info:st.caption(f"Best Model : {model_info.get('best_model_name','Unknown')}")st.caption(f"Test F1 Score : {model_info.get('test_f1','N/A')}")inputs = {}cols = st.columns(2)for i, feature in enumerate(feature_names):with cols[i % 2]:inputs[feature] = st.number_input(feature,value=0.0,step=1.0,format="%.4f")if st.button("Predict"):input_df = pd.DataFrame([inputs])prediction = model.predict(input_df)[0]st.success(f"Predicted Engine Condition : {prediction}")if hasattr(model, "predict_proba"):probabilities = model.predict_proba(input_df)st.subheader("Prediction Probabilities")st.dataframe(pd.DataFrame(probabilities))
app.py+105-33