Spaces:
Runtime error
Runtime error
| 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) |