Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 130 |
|
| 131 |
if hasattr(model, "predict_proba"):
|
| 132 |
-
|
| 133 |
else:
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
st.markdown("### π File-level Defect Prediction")
|
| 137 |
-
if
|
| 138 |
st.error("β οΈ Defect Likely")
|
| 139 |
else:
|
| 140 |
st.success("β
No Defect Predicted")
|
| 141 |
|
| 142 |
-
if
|
| 143 |
-
st.write(f"Estimated probability
|
| 144 |
|
| 145 |
# Suspicious lines section
|
| 146 |
st.markdown("### π§· Suspicious Lines (Rule-Based Heuristics)")
|
| 147 |
-
|
| 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:
|