stylometric-ai-detector 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dinis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.4
2
+ Name: stylometric-ai-detector
3
+ Version: 0.2.1
4
+ Summary: AI vs Human text detection using stylometric features and Random Forest
5
+ Author: Dinis
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Dinis
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/dinis-a/test-stylometry
29
+ Project-URL: Repository, https://github.com/dinis-a/test-stylometry
30
+ Classifier: Development Status :: 4 - Beta
31
+ Classifier: Intended Audience :: Developers
32
+ Classifier: Intended Audience :: Science/Research
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: Programming Language :: Python :: 3.9
35
+ Classifier: Programming Language :: Python :: 3.10
36
+ Classifier: Programming Language :: Python :: 3.11
37
+ Classifier: Programming Language :: Python :: 3.12
38
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
39
+ Classifier: Topic :: Text Processing :: Linguistic
40
+ Requires-Python: >=3.9
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: scikit-learn>=1.0
44
+ Requires-Dist: joblib>=1.0
45
+ Requires-Dist: huggingface_hub>=0.20
46
+ Provides-Extra: dev
47
+ Requires-Dist: pytest>=7.0; extra == "dev"
48
+ Requires-Dist: isort>=5.0; extra == "dev"
49
+ Requires-Dist: black>=23.0; extra == "dev"
50
+ Dynamic: license-file
51
+
52
+ # stylometric-ai-detector
53
+
54
+ AI vs Human text detection using stylometric features and a Random Forest classifier.
55
+
56
+ ## Overview
57
+
58
+ This library provides two functions:
59
+ - **`extract_stylometric_features(text)`** — Extract 8 stylometric features from any text.
60
+ - **`predict(text)`** — Classify text as AI-generated or human-written, with a confidence score.
61
+
62
+ The model is a Random Forest classifier achieving **96% accuracy**, trained on the [AI vs Human Text](https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text) dataset. On first use, the model is automatically downloaded from Hugging Face and cached locally.
63
+
64
+ ## Installation
65
+
66
+ ```bash
67
+ pip install stylometric-ai-detector
68
+ ```
69
+
70
+ For the latest dev version from Test PyPI:
71
+
72
+ ```bash
73
+ pip install -i https://test.pypi.org/simple/ stylometric-ai-detector
74
+ ```
75
+
76
+ ## Quick Start
77
+
78
+ ```python
79
+ from stylometric_ai_detector import extract_stylometric_features, predict
80
+
81
+ # Extract stylometric features
82
+ features = extract_stylometric_features("The quick brown fox jumps over the lazy dog.")
83
+ print(features)
84
+ # {
85
+ # 'char_count': 44,
86
+ # 'word_count': 9,
87
+ # 'avg_word_len': 3.777...,
88
+ # 'punct_count': 1,
89
+ # 'sentence_count': 1,
90
+ # 'avg_sentence_len': 9.0,
91
+ # 'upper_case_count': 0,
92
+ # 'title_case_count': 1,
93
+ # }
94
+
95
+ # Predict AI vs Human (model auto-downloaded from Hugging Face on first call)
96
+ result = predict(text="Artificial intelligence is transforming our world.")
97
+ print(result)
98
+ # {'label': 'AI', 'probability': 0.87}
99
+
100
+ # Or pass pre-computed features
101
+ result = predict(features=features)
102
+ print(result)
103
+ # {'label': 'Human', 'probability': 0.92}
104
+ ```
105
+
106
+ ## Stylometric Features
107
+
108
+ | Feature | Description |
109
+ |--------------------|-----------------------------------------------|
110
+ | `char_count` | Total number of characters |
111
+ | `word_count` | Total number of words |
112
+ | `avg_word_len` | Average word length |
113
+ | `punct_count` | Number of punctuation characters |
114
+ | `sentence_count` | Number of sentences |
115
+ | `avg_sentence_len` | Average sentence length (in words) |
116
+ | `upper_case_count` | Number of fully uppercase alphabetic words |
117
+ | `title_case_count` | Number of title-case words |
118
+
119
+ ## Model
120
+
121
+ The trained Random Forest model is hosted on Hugging Face at [`dinisds/stylometric-ai-detector`](https://huggingface.co/dinisds/stylometric-ai-detector). It is downloaded and cached to `~/.cache/stylometric-ai-detector/` on first use — no extra setup needed.
122
+
123
+ ## Dataset
124
+
125
+ Trained on [Shanegerami's AI vs Human Text](https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text) dataset from Kaggle. The dataset contains ~487k text samples labeled as human-written (0) or AI-generated (1).
126
+
127
+ ## License
128
+
129
+ MIT
@@ -0,0 +1,78 @@
1
+ # stylometric-ai-detector
2
+
3
+ AI vs Human text detection using stylometric features and a Random Forest classifier.
4
+
5
+ ## Overview
6
+
7
+ This library provides two functions:
8
+ - **`extract_stylometric_features(text)`** — Extract 8 stylometric features from any text.
9
+ - **`predict(text)`** — Classify text as AI-generated or human-written, with a confidence score.
10
+
11
+ The model is a Random Forest classifier achieving **96% accuracy**, trained on the [AI vs Human Text](https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text) dataset. On first use, the model is automatically downloaded from Hugging Face and cached locally.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pip install stylometric-ai-detector
17
+ ```
18
+
19
+ For the latest dev version from Test PyPI:
20
+
21
+ ```bash
22
+ pip install -i https://test.pypi.org/simple/ stylometric-ai-detector
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ```python
28
+ from stylometric_ai_detector import extract_stylometric_features, predict
29
+
30
+ # Extract stylometric features
31
+ features = extract_stylometric_features("The quick brown fox jumps over the lazy dog.")
32
+ print(features)
33
+ # {
34
+ # 'char_count': 44,
35
+ # 'word_count': 9,
36
+ # 'avg_word_len': 3.777...,
37
+ # 'punct_count': 1,
38
+ # 'sentence_count': 1,
39
+ # 'avg_sentence_len': 9.0,
40
+ # 'upper_case_count': 0,
41
+ # 'title_case_count': 1,
42
+ # }
43
+
44
+ # Predict AI vs Human (model auto-downloaded from Hugging Face on first call)
45
+ result = predict(text="Artificial intelligence is transforming our world.")
46
+ print(result)
47
+ # {'label': 'AI', 'probability': 0.87}
48
+
49
+ # Or pass pre-computed features
50
+ result = predict(features=features)
51
+ print(result)
52
+ # {'label': 'Human', 'probability': 0.92}
53
+ ```
54
+
55
+ ## Stylometric Features
56
+
57
+ | Feature | Description |
58
+ |--------------------|-----------------------------------------------|
59
+ | `char_count` | Total number of characters |
60
+ | `word_count` | Total number of words |
61
+ | `avg_word_len` | Average word length |
62
+ | `punct_count` | Number of punctuation characters |
63
+ | `sentence_count` | Number of sentences |
64
+ | `avg_sentence_len` | Average sentence length (in words) |
65
+ | `upper_case_count` | Number of fully uppercase alphabetic words |
66
+ | `title_case_count` | Number of title-case words |
67
+
68
+ ## Model
69
+
70
+ The trained Random Forest model is hosted on Hugging Face at [`dinisds/stylometric-ai-detector`](https://huggingface.co/dinisds/stylometric-ai-detector). It is downloaded and cached to `~/.cache/stylometric-ai-detector/` on first use — no extra setup needed.
71
+
72
+ ## Dataset
73
+
74
+ Trained on [Shanegerami's AI vs Human Text](https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text) dataset from Kaggle. The dataset contains ~487k text samples labeled as human-written (0) or AI-generated (1).
75
+
76
+ ## License
77
+
78
+ MIT
@@ -0,0 +1,56 @@
1
+ [build-system]
2
+ requires = ["setuptools>=64", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "stylometric-ai-detector"
7
+ version = "0.2.1"
8
+ description = "AI vs Human text detection using stylometric features and Random Forest"
9
+ readme = "README.md"
10
+ license = {file = "LICENSE"}
11
+ requires-python = ">=3.9"
12
+ authors = [
13
+ {name = "Dinis"},
14
+ ]
15
+ classifiers = [
16
+ "Development Status :: 4 - Beta",
17
+ "Intended Audience :: Developers",
18
+ "Intended Audience :: Science/Research",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.9",
21
+ "Programming Language :: Python :: 3.10",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
25
+ "Topic :: Text Processing :: Linguistic",
26
+ ]
27
+ dependencies = [
28
+ "scikit-learn>=1.0",
29
+ "joblib>=1.0",
30
+ "huggingface_hub>=0.20",
31
+ ]
32
+
33
+ [project.optional-dependencies]
34
+ dev = [
35
+ "pytest>=7.0",
36
+ "isort>=5.0",
37
+ "black>=23.0",
38
+ ]
39
+
40
+ [project.urls]
41
+ Homepage = "https://github.com/dinis-a/test-stylometry"
42
+ Repository = "https://github.com/dinis-a/test-stylometry"
43
+
44
+ [tool.setuptools.packages.find]
45
+ include = ["stylometric_ai_detector*"]
46
+
47
+ [tool.isort]
48
+ profile = "black"
49
+ line_length = 100
50
+
51
+ [tool.black]
52
+ line-length = 100
53
+ target-version = ["py39"]
54
+
55
+ [tool.pytest.ini_options]
56
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,15 @@
1
+ """stylometric-ai-detector: AI vs Human text detection using stylometric features and Random Forest.
2
+
3
+ Trained on the "AI vs Human Text" dataset from Kaggle:
4
+ https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text
5
+
6
+ Provides two main functions:
7
+ - extract_stylometric_features(text) -> dict
8
+ - predict(text=None, features=None) -> dict
9
+ """
10
+
11
+ from stylometric_ai_detector.features import extract_stylometric_features
12
+ from stylometric_ai_detector.predict import predict
13
+
14
+ __all__ = ["extract_stylometric_features", "predict"]
15
+ __version__ = "0.2.1"
@@ -0,0 +1,63 @@
1
+ """Stylometric feature extraction for text analysis."""
2
+
3
+ import re
4
+ import string
5
+
6
+
7
+ def extract_stylometric_features(text):
8
+ """Extract 8 stylometric features from a text string.
9
+
10
+ Features extracted:
11
+ - char_count: Total number of characters
12
+ - word_count: Total number of words
13
+ - avg_word_len: Average word length
14
+ - punct_count: Number of punctuation characters
15
+ - sentence_count: Number of sentences
16
+ - avg_sentence_len: Average sentence length in words
17
+ - upper_case_count: Number of fully uppercase alphabetic words
18
+ - title_case_count: Number of title-case words
19
+
20
+ Args:
21
+ text: Input text string.
22
+
23
+ Returns:
24
+ dict with keys: char_count, word_count, avg_word_len, punct_count,
25
+ sentence_count, avg_sentence_len, upper_case_count,
26
+ title_case_count.
27
+ """
28
+ if not isinstance(text, str) or not text.strip():
29
+ return {
30
+ "char_count": 0,
31
+ "word_count": 0,
32
+ "avg_word_len": 0.0,
33
+ "punct_count": 0,
34
+ "sentence_count": 0,
35
+ "avg_sentence_len": 0.0,
36
+ "upper_case_count": 0,
37
+ "title_case_count": 0,
38
+ }
39
+
40
+ char_count = len(text)
41
+ words = text.split()
42
+ word_count = len(words)
43
+ avg_word_len = sum(len(w) for w in words) / word_count if word_count > 0 else 0.0
44
+ punct_count = sum(1 for c in text if c in string.punctuation)
45
+
46
+ sentences = re.split(r"[.!?]+", text)
47
+ sentence_count = len([s for s in sentences if s.strip()])
48
+ sentence_count = sentence_count if sentence_count > 0 else 1 # avoid division by zero
49
+ avg_sentence_len = word_count / sentence_count
50
+
51
+ upper_case_count = sum(1 for w in words if w.isupper() and w.isalpha())
52
+ title_case_count = sum(1 for w in words if w.istitle())
53
+
54
+ return {
55
+ "char_count": char_count,
56
+ "word_count": word_count,
57
+ "avg_word_len": avg_word_len,
58
+ "punct_count": punct_count,
59
+ "sentence_count": sentence_count,
60
+ "avg_sentence_len": avg_sentence_len,
61
+ "upper_case_count": upper_case_count,
62
+ "title_case_count": title_case_count,
63
+ }
@@ -0,0 +1,122 @@
1
+ """AI vs Human text prediction using stylometric features and a trained Random Forest model.
2
+
3
+ The model is hosted on Hugging Face and downloaded/cached on first use.
4
+ """
5
+
6
+ import logging
7
+ import os
8
+ import warnings
9
+
10
+ import joblib
11
+ from huggingface_hub import hf_hub_download
12
+
13
+ from stylometric_ai_detector.features import extract_stylometric_features
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ # Feature order matching the trained model
18
+ _FEATURE_ORDER = [
19
+ "char_count",
20
+ "word_count",
21
+ "avg_word_len",
22
+ "punct_count",
23
+ "sentence_count",
24
+ "avg_sentence_len",
25
+ "upper_case_count",
26
+ "title_case_count",
27
+ ]
28
+
29
+ # Hugging Face model repo configuration
30
+ _HF_REPO_ID = os.environ.get("HF_MODEL_REPO", "dinisds/stylometric-ai-detector")
31
+ _HF_FILENAME = "random_forest_stylometric_model.joblib"
32
+ _CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "stylometric-ai-detector")
33
+
34
+ _model = None
35
+
36
+
37
+ def _load_model():
38
+ """Lazy-load the model, downloading from Hugging Face on first use.
39
+
40
+ Lookup order:
41
+ 1. Package data directory (bundled in wheel / local dev)
42
+ 2. User cache directory (~/.cache/stylometric-ai-detector/)
43
+ 3. Download from Hugging Face
44
+ """
45
+ global _model
46
+ if _model is None:
47
+ os.makedirs(_CACHE_DIR, exist_ok=True)
48
+
49
+ local_path = None
50
+
51
+ # 1. Check package data directory (bundled model or local dev)
52
+ pkg_path = os.path.join(os.path.dirname(__file__), "data", _HF_FILENAME)
53
+ if os.path.exists(pkg_path):
54
+ local_path = pkg_path
55
+
56
+ # 2. Check user cache
57
+ if local_path is None:
58
+ cache_path = os.path.join(_CACHE_DIR, _HF_FILENAME)
59
+ if os.path.exists(cache_path):
60
+ local_path = cache_path
61
+
62
+ # 3. Download from Hugging Face
63
+ if local_path is None:
64
+ logger.info("Downloading model from Hugging Face: %s", _HF_REPO_ID)
65
+ local_path = hf_hub_download(
66
+ repo_id=_HF_REPO_ID,
67
+ filename=_HF_FILENAME,
68
+ cache_dir=_CACHE_DIR,
69
+ )
70
+
71
+ _model = joblib.load(local_path)
72
+ return _model
73
+
74
+
75
+ def _features_to_array(features_dict):
76
+ """Convert a feature dict to the ordered array expected by the model."""
77
+ return [[features_dict[k] for k in _FEATURE_ORDER]]
78
+
79
+
80
+ def predict(text=None, features=None):
81
+ """Predict whether a text was written by AI or a human.
82
+
83
+ Provide either `text` (raw string) or `features` (pre-computed feature dict).
84
+ If both are provided, `text` takes precedence and features are extracted from it.
85
+
86
+ The model is downloaded from Hugging Face on first use and cached locally.
87
+
88
+ Args:
89
+ text: Raw text string to classify.
90
+ features: Pre-computed stylometric feature dict (from extract_stylometric_features).
91
+
92
+ Returns:
93
+ dict with:
94
+ - label: "AI" or "Human"
95
+ - probability: Confidence score for the predicted class (float between 0 and 1).
96
+
97
+ Raises:
98
+ ValueError: If neither text nor features is provided.
99
+ OSError: If the model cannot be downloaded from Hugging Face.
100
+ """
101
+ if text is not None:
102
+ features = extract_stylometric_features(text)
103
+ elif features is None:
104
+ raise ValueError("Either 'text' or 'features' must be provided.")
105
+
106
+ model = _load_model()
107
+ X = _features_to_array(features)
108
+ with warnings.catch_warnings():
109
+ warnings.filterwarnings(
110
+ "ignore",
111
+ message="X does not have valid feature names",
112
+ category=UserWarning,
113
+ )
114
+ prediction = model.predict(X)[0]
115
+ probabilities = model.predict_proba(X)[0]
116
+
117
+ label = "AI" if prediction == 1.0 else "Human"
118
+
119
+ return {
120
+ "label": label,
121
+ "probability": float(max(probabilities)),
122
+ }
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.4
2
+ Name: stylometric-ai-detector
3
+ Version: 0.2.1
4
+ Summary: AI vs Human text detection using stylometric features and Random Forest
5
+ Author: Dinis
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Dinis
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/dinis-a/test-stylometry
29
+ Project-URL: Repository, https://github.com/dinis-a/test-stylometry
30
+ Classifier: Development Status :: 4 - Beta
31
+ Classifier: Intended Audience :: Developers
32
+ Classifier: Intended Audience :: Science/Research
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: Programming Language :: Python :: 3.9
35
+ Classifier: Programming Language :: Python :: 3.10
36
+ Classifier: Programming Language :: Python :: 3.11
37
+ Classifier: Programming Language :: Python :: 3.12
38
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
39
+ Classifier: Topic :: Text Processing :: Linguistic
40
+ Requires-Python: >=3.9
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: scikit-learn>=1.0
44
+ Requires-Dist: joblib>=1.0
45
+ Requires-Dist: huggingface_hub>=0.20
46
+ Provides-Extra: dev
47
+ Requires-Dist: pytest>=7.0; extra == "dev"
48
+ Requires-Dist: isort>=5.0; extra == "dev"
49
+ Requires-Dist: black>=23.0; extra == "dev"
50
+ Dynamic: license-file
51
+
52
+ # stylometric-ai-detector
53
+
54
+ AI vs Human text detection using stylometric features and a Random Forest classifier.
55
+
56
+ ## Overview
57
+
58
+ This library provides two functions:
59
+ - **`extract_stylometric_features(text)`** — Extract 8 stylometric features from any text.
60
+ - **`predict(text)`** — Classify text as AI-generated or human-written, with a confidence score.
61
+
62
+ The model is a Random Forest classifier achieving **96% accuracy**, trained on the [AI vs Human Text](https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text) dataset. On first use, the model is automatically downloaded from Hugging Face and cached locally.
63
+
64
+ ## Installation
65
+
66
+ ```bash
67
+ pip install stylometric-ai-detector
68
+ ```
69
+
70
+ For the latest dev version from Test PyPI:
71
+
72
+ ```bash
73
+ pip install -i https://test.pypi.org/simple/ stylometric-ai-detector
74
+ ```
75
+
76
+ ## Quick Start
77
+
78
+ ```python
79
+ from stylometric_ai_detector import extract_stylometric_features, predict
80
+
81
+ # Extract stylometric features
82
+ features = extract_stylometric_features("The quick brown fox jumps over the lazy dog.")
83
+ print(features)
84
+ # {
85
+ # 'char_count': 44,
86
+ # 'word_count': 9,
87
+ # 'avg_word_len': 3.777...,
88
+ # 'punct_count': 1,
89
+ # 'sentence_count': 1,
90
+ # 'avg_sentence_len': 9.0,
91
+ # 'upper_case_count': 0,
92
+ # 'title_case_count': 1,
93
+ # }
94
+
95
+ # Predict AI vs Human (model auto-downloaded from Hugging Face on first call)
96
+ result = predict(text="Artificial intelligence is transforming our world.")
97
+ print(result)
98
+ # {'label': 'AI', 'probability': 0.87}
99
+
100
+ # Or pass pre-computed features
101
+ result = predict(features=features)
102
+ print(result)
103
+ # {'label': 'Human', 'probability': 0.92}
104
+ ```
105
+
106
+ ## Stylometric Features
107
+
108
+ | Feature | Description |
109
+ |--------------------|-----------------------------------------------|
110
+ | `char_count` | Total number of characters |
111
+ | `word_count` | Total number of words |
112
+ | `avg_word_len` | Average word length |
113
+ | `punct_count` | Number of punctuation characters |
114
+ | `sentence_count` | Number of sentences |
115
+ | `avg_sentence_len` | Average sentence length (in words) |
116
+ | `upper_case_count` | Number of fully uppercase alphabetic words |
117
+ | `title_case_count` | Number of title-case words |
118
+
119
+ ## Model
120
+
121
+ The trained Random Forest model is hosted on Hugging Face at [`dinisds/stylometric-ai-detector`](https://huggingface.co/dinisds/stylometric-ai-detector). It is downloaded and cached to `~/.cache/stylometric-ai-detector/` on first use — no extra setup needed.
122
+
123
+ ## Dataset
124
+
125
+ Trained on [Shanegerami's AI vs Human Text](https://www.kaggle.com/datasets/shanegerami/ai-vs-human-text) dataset from Kaggle. The dataset contains ~487k text samples labeled as human-written (0) or AI-generated (1).
126
+
127
+ ## License
128
+
129
+ MIT
@@ -0,0 +1,13 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ stylometric_ai_detector/__init__.py
5
+ stylometric_ai_detector/features.py
6
+ stylometric_ai_detector/predict.py
7
+ stylometric_ai_detector.egg-info/PKG-INFO
8
+ stylometric_ai_detector.egg-info/SOURCES.txt
9
+ stylometric_ai_detector.egg-info/dependency_links.txt
10
+ stylometric_ai_detector.egg-info/requires.txt
11
+ stylometric_ai_detector.egg-info/top_level.txt
12
+ tests/test_features.py
13
+ tests/test_predict.py
@@ -0,0 +1,8 @@
1
+ scikit-learn>=1.0
2
+ joblib>=1.0
3
+ huggingface_hub>=0.20
4
+
5
+ [dev]
6
+ pytest>=7.0
7
+ isort>=5.0
8
+ black>=23.0
@@ -0,0 +1 @@
1
+ stylometric_ai_detector
@@ -0,0 +1,68 @@
1
+ """Tests for stylometric feature extraction."""
2
+
3
+ from stylometric_ai_detector import extract_stylometric_features
4
+
5
+
6
+ def test_empty_string():
7
+ """Empty or whitespace-only strings should return all zeros."""
8
+ result = extract_stylometric_features("")
9
+ assert result["char_count"] == 0
10
+ assert result["word_count"] == 0
11
+ assert result["avg_word_len"] == 0.0
12
+
13
+
14
+ def test_non_string_input():
15
+ """Non-string input should return all zeros."""
16
+ result = extract_stylometric_features(None)
17
+ assert result["char_count"] == 0
18
+ assert result["word_count"] == 0
19
+
20
+
21
+ def test_simple_sentence():
22
+ """Basic feature extraction on a simple sentence."""
23
+ text = "Hello world. This is a test."
24
+ result = extract_stylometric_features(text)
25
+
26
+ assert result["char_count"] == len(text)
27
+ # "Hello", "world.", "This", "is", "a", "test."
28
+ assert result["word_count"] == 6
29
+ assert result["sentence_count"] == 2
30
+ # Punctuation: '.' (after world) and '.' (after test) = 2
31
+ assert result["punct_count"] == 2
32
+ # No fully uppercase words
33
+ assert result["upper_case_count"] == 0
34
+ # "Hello" and "This" are title-case
35
+ assert result["title_case_count"] == 2
36
+
37
+
38
+ def test_all_feature_keys():
39
+ """Verify all 8 expected feature keys are present."""
40
+ result = extract_stylometric_features("Some sample text.")
41
+ expected_keys = {
42
+ "char_count",
43
+ "word_count",
44
+ "avg_word_len",
45
+ "punct_count",
46
+ "sentence_count",
47
+ "avg_sentence_len",
48
+ "upper_case_count",
49
+ "title_case_count",
50
+ }
51
+ assert set(result.keys()) == expected_keys
52
+
53
+
54
+ def test_longer_text():
55
+ """Feature extraction on a multi-sentence paragraph."""
56
+ text = (
57
+ "Artificial intelligence is transforming the world. "
58
+ "Many people are excited about the possibilities. "
59
+ "However, some are concerned about the risks."
60
+ )
61
+ result = extract_stylometric_features(text)
62
+
63
+ assert result["char_count"] > 0
64
+ assert result["word_count"] > 10
65
+ assert result["sentence_count"] == 3
66
+ assert result["avg_word_len"] > 0
67
+ assert result["avg_sentence_len"] > 0
68
+ assert isinstance(result["avg_word_len"], float)
@@ -0,0 +1,81 @@
1
+ """Tests for the predict function."""
2
+
3
+ import os
4
+
5
+ import pytest
6
+
7
+ # Skip predict tests if model is not cached locally (neither in package nor HF cache)
8
+ _PKG_DIR = os.path.join(os.path.dirname(__file__), "..", "stylometric_ai_detector", "data")
9
+ _CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "stylometric-ai-detector")
10
+ _MODEL_FILENAME = "random_forest_stylometric_model.joblib"
11
+ _MODEL_AVAILABLE = os.path.exists(os.path.join(_PKG_DIR, _MODEL_FILENAME)) or os.path.exists(
12
+ os.path.join(_CACHE_DIR, _MODEL_FILENAME)
13
+ )
14
+
15
+ pytestmark = pytest.mark.skipif(
16
+ not _MODEL_AVAILABLE,
17
+ reason="Model not cached locally — run predict() once to download from Hugging Face",
18
+ )
19
+
20
+
21
+ def test_predict_with_text():
22
+ """Prediction should work with raw text input."""
23
+ from stylometric_ai_detector import predict
24
+
25
+ result = predict(text="This is a sample sentence for testing purposes.")
26
+ assert "label" in result
27
+ assert "probability" in result
28
+ assert result["label"] in ("AI", "Human")
29
+ assert 0.0 <= result["probability"] <= 1.0
30
+
31
+
32
+ def test_predict_with_features():
33
+ """Prediction should work with pre-computed feature dict."""
34
+ from stylometric_ai_detector import extract_stylometric_features, predict
35
+
36
+ features = extract_stylometric_features(
37
+ "The quick brown fox jumps over the lazy dog. It was a sunny day."
38
+ )
39
+ result = predict(features=features)
40
+ assert "label" in result
41
+ assert "probability" in result
42
+ assert result["label"] in ("AI", "Human")
43
+ assert 0.0 <= result["probability"] <= 1.0
44
+
45
+
46
+ def test_predict_text_takes_precedence():
47
+ """When both text and features are given, text should be used."""
48
+ from stylometric_ai_detector import predict
49
+
50
+ result = predict(
51
+ text="Hello world.",
52
+ features={
53
+ "char_count": 999,
54
+ "word_count": 999,
55
+ "avg_word_len": 99.0,
56
+ "punct_count": 99,
57
+ "sentence_count": 99,
58
+ "avg_sentence_len": 99.0,
59
+ "upper_case_count": 99,
60
+ "title_case_count": 99,
61
+ },
62
+ )
63
+ assert result["label"] in ("AI", "Human")
64
+
65
+
66
+ def test_predict_no_input_raises():
67
+ """Calling predict with no arguments should raise ValueError."""
68
+ from stylometric_ai_detector import predict
69
+
70
+ with pytest.raises(ValueError, match="Either 'text' or 'features' must be provided"):
71
+ predict()
72
+
73
+
74
+ def test_predict_consistency():
75
+ """Same input should produce the same output."""
76
+ from stylometric_ai_detector import predict
77
+
78
+ text = "The theory of relativity explains the relationship between space and time."
79
+ result1 = predict(text=text)
80
+ result2 = predict(text=text)
81
+ assert result1 == result2