github-actions commited on
Commit
7f06bf5
·
1 Parent(s): 9a6bc35

Deploy from GitHub Actions

Browse files
Files changed (1) hide show
  1. app.py +105 -33
app.py CHANGED
@@ -1,49 +1,121 @@
 
1
  import json
2
  import joblib
3
  import pandas as pd
4
- import gradio as gr
5
- import spaces
6
  from huggingface_hub import hf_hub_download
7
 
 
8
  REPO_ID = "Swetha1929/predictive-maintenance-engine-model"
9
 
10
- model_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pkl")
11
- feature_path = hf_hub_download(repo_id=REPO_ID, filename="feature_names.txt")
 
 
12
 
13
- model = joblib.load(model_path)
 
14
 
15
- with open(feature_path, "r", encoding="utf-8") as f:
16
- feature_names = [line.strip() for line in f if line.strip()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- try:
19
- info_path = hf_hub_download(repo_id=REPO_ID, filename="model_info.json")
20
- with open(info_path, "r", encoding="utf-8") as f:
21
- model_info = json.load(f)
22
- except Exception:
23
  model_info = {}
24
 
25
- @spaces.GPU
26
- def predict(*values):
27
- input_df = pd.DataFrame([list(values)], columns=feature_names)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  prediction = model.predict(input_df)[0]
29
 
 
 
 
 
30
  if hasattr(model, "predict_proba"):
31
- probs = model.predict_proba(input_df)[0]
32
- return str(prediction), {f"Class {i}": float(p) for i, p in enumerate(probs)}
33
-
34
- return str(prediction), {}
35
-
36
- inputs = [gr.Number(label=feature) for feature in feature_names]
37
-
38
- demo = gr.Interface(
39
- fn=predict,
40
- inputs=inputs,
41
- outputs=[
42
- gr.Textbox(label="Predicted Engine Condition"),
43
- gr.Label(label="Prediction Probabilities")
44
- ],
45
- title="Engine Condition Prediction",
46
- description="Enter engine parameters to predict the engine condition."
47
- )
48
 
49
- demo.launch()
 
 
 
 
 
 
 
1
+ import os
2
  import json
3
  import joblib
4
  import pandas as pd
5
+ import streamlit as st
 
6
  from huggingface_hub import hf_hub_download
7
 
8
+ # Hugging Face Model Repository
9
  REPO_ID = "Swetha1929/predictive-maintenance-engine-model"
10
 
11
+ st.set_page_config(
12
+ page_title="Engine Condition Prediction",
13
+ layout="wide"
14
+ )
15
 
16
+ st.title("Engine Condition Prediction App")
17
+ st.write("Enter the engine feature values below and click Predict.")
18
 
19
+ # Local backup (used only if HF download fails)
20
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
21
+ MODELS_DIR = os.path.join(BASE_DIR, "models")
22
+
23
+ MODEL_FILE = os.path.join(MODELS_DIR, "best_model.pkl")
24
+ FEATURE_FILE = os.path.join(MODELS_DIR, "feature_names.txt")
25
+ INFO_FILE = os.path.join(MODELS_DIR, "model_info.json")
26
+
27
+
28
+ @st.cache_resource
29
+ def load_artifacts():
30
+ try:
31
+ model_path = hf_hub_download(
32
+ repo_id=REPO_ID,
33
+ filename="best_model.pkl"
34
+ )
35
+
36
+ feature_path = hf_hub_download(
37
+ repo_id=REPO_ID,
38
+ filename="feature_names.txt"
39
+ )
40
+
41
+ info_path = hf_hub_download(
42
+ repo_id=REPO_ID,
43
+ filename="model_info.json"
44
+ )
45
+
46
+ except Exception:
47
+ model_path = MODEL_FILE
48
+ feature_path = FEATURE_FILE
49
+ info_path = INFO_FILE
50
+
51
+ model = joblib.load(model_path)
52
+
53
+ with open(feature_path, "r", encoding="utf-8") as f:
54
+ feature_names = [
55
+ line.strip()
56
+ for line in f
57
+ if line.strip()
58
+ ]
59
 
 
 
 
 
 
60
  model_info = {}
61
 
62
+ if os.path.exists(info_path):
63
+ with open(info_path, "r", encoding="utf-8") as f:
64
+ model_info = json.load(f)
65
+
66
+ return model, feature_names, model_info
67
+
68
+
69
+ try:
70
+ model, feature_names, model_info = load_artifacts()
71
+
72
+ except Exception as e:
73
+ st.error(f"Error loading model: {e}")
74
+ st.stop()
75
+
76
+
77
+ if model_info:
78
+ st.caption(
79
+ f"Best Model : {model_info.get('best_model_name','Unknown')}"
80
+ )
81
+
82
+ st.caption(
83
+ f"Test F1 Score : {model_info.get('test_f1','N/A')}"
84
+ )
85
+
86
+
87
+ inputs = {}
88
+
89
+ cols = st.columns(2)
90
+
91
+ for i, feature in enumerate(feature_names):
92
+
93
+ with cols[i % 2]:
94
+
95
+ inputs[feature] = st.number_input(
96
+ feature,
97
+ value=0.0,
98
+ step=1.0,
99
+ format="%.4f"
100
+ )
101
+
102
+
103
+ if st.button("Predict"):
104
+
105
+ input_df = pd.DataFrame([inputs])
106
+
107
  prediction = model.predict(input_df)[0]
108
 
109
+ st.success(
110
+ f"Predicted Engine Condition : {prediction}"
111
+ )
112
+
113
  if hasattr(model, "predict_proba"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
+ probabilities = model.predict_proba(input_df)
116
+
117
+ st.subheader("Prediction Probabilities")
118
+
119
+ st.dataframe(
120
+ pd.DataFrame(probabilities)
121
+ )