Spaces:
Runtime error
Runtime error
File size: 1,811 Bytes
b144cb7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | def generate_text_explanation(grouped_importance, prediction, confidence):
"""
grouped_importance : dict from grouping.py
Example:
{
"Semantic": 0.92,
"Stylometry": 0.21,
"Statistical": 0.08
}
prediction : "AI" or "Human"
confidence : float
"""
# Sort groups by importance
sorted_groups = sorted(grouped_importance.items(), key=lambda x: -x[1])
explanation_lines = []
explanation_lines.append(
f"The model predicts the code is {prediction} with confidence {round(confidence,4)}."
)
explanation_lines.append("Key contributing factors:")
for group, score in sorted_groups:
if group == "Semantic":
explanation_lines.append(
f"• Semantic patterns strongly influenced the decision, indicating structural similarity with {'AI‑generated' if prediction=='AI' else 'human‑written'} code."
)
elif group == "Stylometry":
explanation_lines.append(
f"• Stylometric characteristics such as identifier usage and coding style contributed to the prediction."
)
elif group == "Statistical":
explanation_lines.append(
f"• Token‑level statistical patterns (entropy, repetition, vocabulary richness) affected the classification."
)
elif group == "AST":
explanation_lines.append(
f"• Abstract Syntax Tree structure and control flow patterns influenced the decision."
)
elif group == "Language":
explanation_lines.append(
f"• Programming language encoding provided contextual information for the classifier."
)
return "\n".join(explanation_lines) |