Phani1008 commited on
Commit
d1bbf06
Β·
verified Β·
1 Parent(s): 09c969b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -120,32 +120,43 @@ if uploaded_py is not None:
120
  st.markdown("### πŸ“„ Code Preview")
121
  st.code(code_text, language="python")
122
 
123
- # Extract approximate metrics, predict
124
  metrics_vector = extract_simple_metrics_from_code(code_text)
125
  metrics_array = np.array(metrics_vector).reshape(1, -1)
126
 
127
- # Scale and predict
128
  scaled = scaler.transform(metrics_array)
129
- pred = model.predict(scaled)[0]
130
 
131
  if hasattr(model, "predict_proba"):
132
- proba = model.predict_proba(scaled)[0][1]
133
  else:
134
- proba = None
 
 
 
 
 
 
 
135
 
136
  st.markdown("### πŸ” File-level Defect Prediction")
137
- if pred == 1:
138
  st.error("⚠️ Defect Likely")
139
  else:
140
  st.success("βœ… No Defect Predicted")
141
 
142
- if proba is not None:
143
- st.write(f"Estimated probability of defect: **{proba:.2f}**")
144
 
145
  # Suspicious lines section
146
  st.markdown("### 🧷 Suspicious Lines (Rule-Based Heuristics)")
147
- suspicious = find_suspicious_lines(code_text)
148
-
 
 
 
 
 
149
  if not suspicious:
150
  st.info("No suspicious patterns found by the simple rules.")
151
  else:
 
120
  st.markdown("### πŸ“„ Code Preview")
121
  st.code(code_text, language="python")
122
 
123
+ # Extract approximate metrics from code and get ML prediction
124
  metrics_vector = extract_simple_metrics_from_code(code_text)
125
  metrics_array = np.array(metrics_vector).reshape(1, -1)
126
 
 
127
  scaled = scaler.transform(metrics_array)
128
+ ml_pred = model.predict(scaled)[0]
129
 
130
  if hasattr(model, "predict_proba"):
131
+ ml_proba = model.predict_proba(scaled)[0][1]
132
  else:
133
+ ml_proba = None
134
+
135
+ # Rule-based suspicious lines
136
+ suspicious = find_suspicious_lines(code_text)
137
+
138
+ # πŸ”΄ HYBRID DECISION:
139
+ # If ML says defect OR we found suspicious lines β†’ treat as defect
140
+ is_defect = (ml_pred == 1) or (len(suspicious) > 0)
141
 
142
  st.markdown("### πŸ” File-level Defect Prediction")
143
+ if is_defect:
144
  st.error("⚠️ Defect Likely")
145
  else:
146
  st.success("βœ… No Defect Predicted")
147
 
148
+ if ml_proba is not None:
149
+ st.write(f"Estimated probability from ML model: **{ml_proba:.2f}**")
150
 
151
  # Suspicious lines section
152
  st.markdown("### 🧷 Suspicious Lines (Rule-Based Heuristics)")
153
+ if not suspicious:
154
+ st.info("No suspicious patterns found by the simple rules.")
155
+ else:
156
+ for line_no, line_text, reason in suspicious:
157
+ st.write(f"**Line {line_no}** β€” {reason}")
158
+ st.code(line_text, language="python")
159
+
160
  if not suspicious:
161
  st.info("No suspicious patterns found by the simple rules.")
162
  else: