Last active
August 16, 2025 14:51
-
-
Save HackingLZ/cf60a35850be773cec7fc203aaca0c98 to your computer and use it in GitHub Desktop.
Detect Python code written by AI easily with science
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import ast, re, sys | |
EMOJI_RE = re.compile( | |
r"[\U0001F300-\U0001F5FF\U0001F600-\U0001F64F\U0001F680-\U0001F6FF" | |
r"\U0001F700-\U0001F77F\U0001F780-\U0001F7FF\U0001F800-\U0001F8FF" | |
r"\U0001F900-\U0001F9FF\U0001FA00-\U0001FA6F\U0001FA70-\U0001FAFF" | |
r"\u2702-\u27B0\u24C2-\U0001F251\U00010000-\U0010FFFF]" | |
) | |
def lit_strings_from_print(call: ast.Call): | |
if not (isinstance(call.func, ast.Name) and call.func.id == "print"): | |
return () | |
for arg in call.args: | |
if isinstance(arg, ast.Constant) and isinstance(arg.value, str): | |
yield arg.value | |
elif isinstance(arg, ast.Str): # Py<3.8 | |
yield arg.s | |
elif isinstance(arg, ast.JoinedStr): # f-strings: only literal chunks | |
for v in arg.values: | |
if isinstance(v, ast.Constant) and isinstance(v.value, str): | |
yield v.value | |
elif isinstance(v, ast.Str): | |
yield v.s | |
def analyze(path): | |
code = open(path, "r", encoding="utf-8").read() | |
tree = ast.parse(code) | |
findings = [] | |
for node in ast.walk(tree): | |
if isinstance(node, ast.Call): | |
for s in lit_strings_from_print(node): | |
n = len(EMOJI_RE.findall(s)) | |
if n: | |
findings.append({"line": getattr(node, "lineno", "?"), "string": s, "emoji_count": n}) | |
total = sum(f["emoji_count"] for f in findings) | |
return total, findings | |
def main(): | |
if len(sys.argv) != 2: | |
print("Usage: python emoji_detector.py <python_file.py>"); sys.exit(1) | |
path = sys.argv[1] | |
total, items = analyze(path) | |
print(f"\nAnalyzing: {path}") | |
print(f"Total emojis in print statements: {total}") | |
if items: | |
print("\nPrint statements with emojis:") | |
for f in items: | |
preview = (f["string"][:50] + "...") if len(f["string"]) > 50 else f["string"] | |
print(f' Line {f["line"]}: {f["emoji_count"]} emoji(s) - "{preview}"') | |
print("\n⚠️ VERDICT: This code is likely AI-generated (>2 emojis in print statements)" | |
if total > 2 else "\n✓ VERDICT: This code appears to be human-written (≤2 emojis in print statements)") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment