Phani1008 commited on
Commit
62b36ef
ยท
verified ยท
1 Parent(s): 12c25bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -20
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import streamlit as st
2
  import joblib
3
  import numpy as np
 
4
 
5
- # Load model and scaler
6
  @st.cache_resource
7
  def load_artifacts():
8
  model = joblib.load("bug_predictor_model.pkl")
@@ -12,7 +13,10 @@ def load_artifacts():
12
  model, scaler = load_artifacts()
13
 
14
  st.title("๐Ÿ” Software Bug Prediction System")
15
- st.write("Predict whether a software module is likely to be **defective** based on its metrics (NASA KC1 dataset).")
 
 
 
16
 
17
  # List of feature names in the same order as training
18
  feature_names = [
@@ -21,26 +25,106 @@ feature_names = [
21
  'uniq_Opnd', 'total_Op', 'total_Opnd', 'branchCount'
22
  ]
23
 
24
- st.subheader("๐Ÿ“ฅ Enter Module Metrics")
25
 
26
- inputs = []
27
- cols = st.columns(3)
 
 
 
28
 
29
- for idx, name in enumerate(feature_names):
30
- with cols[idx % 3]:
31
- val = st.number_input(name, value=0.0)
32
- inputs.append(val)
33
 
34
- if st.button("Predict Defect Risk"):
35
- input_array = np.array(inputs).reshape(1, -1)
36
- scaled = scaler.transform(input_array)
37
- pred = model.predict(scaled)[0]
38
- proba = getattr(model, "predict_proba", lambda x: None)(scaled)
39
 
40
- if pred == 1:
41
- st.error("โš ๏ธ Defect Likely")
42
- else:
43
- st.success("โœ… No Defect Predicted")
44
 
45
- if proba is not None:
46
- st.write(f"Probability of Defect: **{proba[0][1]:.2f}**")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import joblib
3
  import numpy as np
4
+ import pandas as pd
5
 
6
+ # Load model and scaler once, cached for performance
7
  @st.cache_resource
8
  def load_artifacts():
9
  model = joblib.load("bug_predictor_model.pkl")
 
13
  model, scaler = load_artifacts()
14
 
15
  st.title("๐Ÿ” Software Bug Prediction System")
16
+ st.write(
17
+ "Predict whether a software module is likely to be **defective** based on metrics "
18
+ "from the NASA KC1 dataset."
19
+ )
20
 
21
  # List of feature names in the same order as training
22
  feature_names = [
 
25
  'uniq_Opnd', 'total_Op', 'total_Opnd', 'branchCount'
26
  ]
27
 
28
+ tab_single, tab_bulk = st.tabs(["๐Ÿงฎ Single module input", "๐Ÿ“‚ Bulk prediction via CSV"])
29
 
30
+ # =========================
31
+ # TAB 1: SINGLE ROW INPUT
32
+ # =========================
33
+ with tab_single:
34
+ st.subheader("๐Ÿ“ฅ Enter Module Metrics Manually")
35
 
36
+ inputs = []
37
+ cols = st.columns(3) # 3-column layout for nicer UI
 
 
38
 
39
+ for idx, name in enumerate(feature_names):
40
+ with cols[idx % 3]:
41
+ val = st.number_input(name, value=0.0)
42
+ inputs.append(val)
 
43
 
44
+ if st.button("Predict Defect Risk", key="single_predict"):
45
+ # Convert inputs to 2D array
46
+ input_array = np.array(inputs).reshape(1, -1)
 
47
 
48
+ # Scale using same scaler from training
49
+ scaled = scaler.transform(input_array)
50
+
51
+ # Predict with loaded model
52
+ pred = model.predict(scaled)[0]
53
+
54
+ # Probability of defect (if supported)
55
+ proba = model.predict_proba(scaled)[0][1] if hasattr(model, "predict_proba") else None
56
+
57
+ if pred == 1:
58
+ st.error("โš ๏ธ Defect Likely")
59
+ else:
60
+ st.success("โœ… No Defect Predicted")
61
+
62
+ if proba is not None:
63
+ st.write(f"Probability of Defect: **{proba:.2f}**")
64
+
65
+
66
+ # =========================
67
+ # TAB 2: BULK CSV PREDICTION
68
+ # =========================
69
+ with tab_bulk:
70
+ st.subheader("๐Ÿ“‚ Upload CSV for Bulk Prediction")
71
+ st.write(
72
+ "Upload a CSV file containing the following columns (no target column needed):"
73
+ )
74
+ st.code(", ".join(feature_names), language="text")
75
+
76
+ uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
77
+
78
+ if uploaded_file is not None:
79
+ try:
80
+ df = pd.read_csv(uploaded_file)
81
+
82
+ st.write("๐Ÿ“„ Preview of uploaded data:")
83
+ st.dataframe(df.head())
84
+
85
+ # Check if all required columns exist
86
+ missing_cols = [col for col in feature_names if col not in df.columns]
87
+ if missing_cols:
88
+ st.error(
89
+ "The following required columns are missing from the uploaded file:\n"
90
+ + ", ".join(missing_cols)
91
+ )
92
+ else:
93
+ # Keep only the required columns in correct order
94
+ X = df[feature_names].copy()
95
+
96
+ # Scale features
97
+ X_scaled = scaler.transform(X)
98
+
99
+ # Predict
100
+ preds = model.predict(X_scaled)
101
+
102
+ # Probabilities (if available)
103
+ if hasattr(model, "predict_proba"):
104
+ probas = model.predict_proba(X_scaled)[:, 1]
105
+ else:
106
+ probas = None
107
+
108
+ # Add predictions to dataframe
109
+ df["Defect_Prediction"] = np.where(
110
+ preds == 1, "Defect Likely", "No Defect Predicted"
111
+ )
112
+
113
+ if probas is not None:
114
+ df["Defect_Probability"] = probas
115
+
116
+ st.success("โœ… Predictions generated!")
117
+ st.write("๐Ÿ“Š Results:")
118
+ st.dataframe(df.head())
119
+
120
+ # Allow user to download results
121
+ csv_data = df.to_csv(index=False).encode("utf-8")
122
+ st.download_button(
123
+ label="โฌ‡๏ธ Download Predictions as CSV",
124
+ data=csv_data,
125
+ file_name="bug_predictions.csv",
126
+ mime="text/csv",
127
+ )
128
+
129
+ except Exception as e:
130
+ st.error(f"โŒ Error reading file: {e}")