Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465import jsonimport joblibimport pandas as pdimport gradio as grfrom huggingface_hub import hf_hub_downloadMODEL_REPO = "Swetha1929/predictive-maintenance-engine-model"# Download files from the HF model repositorymodel_path = hf_hub_download(repo_id=MODEL_REPO,filename="best_model.pkl")feature_path = hf_hub_download(repo_id=MODEL_REPO,filename="feature_names.txt")info_path = hf_hub_download(repo_id=MODEL_REPO,filename="model_info.json")# Load modelmodel = joblib.load(model_path)# Load featureswith open(feature_path, "r") as f:feature_names = [line.strip() for line in f]# Load metadatawith open(info_path) as f:model_info = json.load(f)def predict(*values):df = pd.DataFrame([dict(zip(feature_names, values))])pred = model.predict(df)[0]return str(pred)with gr.Blocks() as demo:gr.Markdown("# Engine Condition Prediction")inputs = []for feature in feature_names:inputs.append(gr.Number(label=feature))output = gr.Textbox(label="Prediction")btn = gr.Button("Predict")btn.click(predict,inputs=inputs,outputs=output)demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import osimport jsonimport joblibimport pandas as pdimport streamlit as stst.set_page_config(page_title="Engine Condition Prediction", layout="wide")st.title("Engine Condition Prediction App")st.write("Enter the feature values below and click Predict.")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():if not os.path.exists(MODEL_FILE):raise FileNotFoundError(f"Missing model file: {MODEL_FILE}")if not os.path.exists(FEATURE_FILE):raise FileNotFoundError(f"Missing feature file: {FEATURE_FILE}")model = joblib.load(MODEL_FILE)with open(FEATURE_FILE, "r", encoding="utf-8") as f:feature_names = [line.strip() for line in f if line.strip()]model_info = {}if os.path.exists(INFO_FILE):with open(INFO_FILE, "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(str(e))st.stop()if model_info:st.caption(f"Best model: {model_info.get('best_model_name', 'Unknown')}")st.caption(f"Test F1: {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"):proba = model.predict_proba(input_df)st.write("Prediction probabilities:")st.dataframe(pd.DataFrame(proba))
app.py+45-48