nahiar commited on
Commit
2e8c34c
·
verified ·
1 Parent(s): 69791da

Upload tiktok-bot-detection model

Browse files
Files changed (4) hide show
  1. .gitattributes +0 -34
  2. README.md +236 -0
  3. inference_example.py +195 -0
  4. requirements.txt +4 -0
.gitattributes CHANGED
@@ -1,35 +1 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
  *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  *.pkl filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ license: mit
4
+ tags:
5
+ - bot-detection
6
+ - tiktok
7
+ - random-forest
8
+ - sklearn
9
+ - social-media
10
+ - classification
11
+ metrics:
12
+ - accuracy
13
+ - precision
14
+ - recall
15
+ - f1
16
+ - roc-auc
17
+ library_name: scikit-learn
18
+ ---
19
+
20
+ # TikTok Bot Detection Model
21
+
22
+ ## Model Description
23
+
24
+ This Random Forest classifier is designed to detect bot accounts on TikTok based on profile features and behavioral patterns. The model analyzes various account characteristics to determine whether an account is likely automated (bot) or genuine (human).
25
+
26
+ ## Model Details
27
+
28
+ - **Model Type**: Random Forest Classifier
29
+ - **Framework**: scikit-learn
30
+ - **Task**: Binary Classification (Bot vs Human)
31
+ - **Language**: Python
32
+ - **License**: MIT
33
+
34
+ ## Performance Metrics
35
+
36
+ The model achieves excellent performance on the test dataset:
37
+
38
+ - **ROC-AUC Score**: 0.9747
39
+ - **Accuracy**: High accuracy in distinguishing bots from legitimate accounts
40
+
41
+ The ROC curve demonstrates strong discriminative ability with an AUC close to 1.0, indicating excellent model performance.
42
+
43
+ ## Features Used
44
+
45
+ The model uses the following 12 features for prediction:
46
+
47
+ 1. **IsPrivate** - Whether the account is set to private
48
+ 2. **IsVerified** - Whether the account has a verification badge
49
+ 3. **HasProfilePic** - Whether the account has a profile picture
50
+ 4. **FollowingCount** - Number of accounts being followed
51
+ 5. **FollowerCount** - Number of followers
52
+ 6. **HasInstagram** - Whether Instagram link is present in bio
53
+ 7. **HasYoutube** - Whether YouTube link is present in bio
54
+ 8. **HasBio** - Whether the account has a bio description
55
+ 9. **HasLinkInBio** - Whether there's a link in the bio
56
+ 10. **HasPosts** - Whether the account has made any posts
57
+ 11. **PostsCount** - Total number of posts
58
+ 12. **FollowToFollowerRatio** - Ratio of following to followers
59
+
60
+ ## Intended Use
61
+
62
+ ### Primary Uses
63
+
64
+ - Identifying potential bot accounts on TikTok
65
+ - Content moderation and platform integrity
66
+ - Research on social media bot behavior
67
+ - Automated account screening
68
+
69
+ ### Out-of-Scope Uses
70
+
71
+ - This model is specifically trained for TikTok and should not be used for other platforms without retraining
72
+ - Should not be the sole basis for account suspension decisions
73
+ - Not designed for real-time detection without proper infrastructure
74
+
75
+ ## How to Use
76
+
77
+ ### Installation
78
+
79
+ ```bash
80
+ pip install scikit-learn pandas numpy joblib
81
+ ```
82
+
83
+ ### Loading the Model
84
+
85
+ ```python
86
+ import joblib
87
+ import pandas as pd
88
+ import numpy as np
89
+ from sklearn.preprocessing import MinMaxScaler
90
+
91
+ # Load the model
92
+ model = joblib.load('TIKTOK_BOT_Detection_Model_v1.pkl')
93
+
94
+ # Prepare your data
95
+ features = ['IsPrivate', 'IsVerified', 'HasProfilePic', 'FollowingCount',
96
+ 'FollowerCount', 'HasInstagram', 'HasYoutube',
97
+ 'HasBio', 'HasLinkInBio', 'HasPosts', 'PostsCount',
98
+ 'FollowToFollowerRatio']
99
+
100
+ # Example account data
101
+ account_data = {
102
+ 'IsPrivate': 0,
103
+ 'IsVerified': 0,
104
+ 'HasProfilePic': 1,
105
+ 'FollowingCount': 5000,
106
+ 'FollowerCount': 100,
107
+ 'HasInstagram': 0,
108
+ 'HasYoutube': 0,
109
+ 'HasBio': 0,
110
+ 'HasLinkInBio': 1,
111
+ 'HasPosts': 1,
112
+ 'PostsCount': 50,
113
+ 'FollowToFollowerRatio': 50.0
114
+ }
115
+
116
+ # Create DataFrame
117
+ df = pd.DataFrame([account_data])
118
+
119
+ # Scale features (use the same scaler as training)
120
+ scaler = MinMaxScaler()
121
+ # Note: In production, you should save and load the scaler from training
122
+ df_scaled = scaler.fit_transform(df[features])
123
+
124
+ # Make prediction
125
+ prediction = model.predict(df_scaled)
126
+ probability = model.predict_proba(df_scaled)
127
+
128
+ print(f"Prediction: {'Bot' if prediction[0] == 1 else 'Human'}")
129
+ print(f"Confidence - Human: {probability[0][0]:.2%}, Bot: {probability[0][1]:.2%}")
130
+ ```
131
+
132
+ ### Batch Prediction
133
+
134
+ ```python
135
+ # For multiple accounts
136
+ accounts_df = pd.read_csv('accounts_to_check.csv')
137
+ accounts_scaled = scaler.transform(accounts_df[features])
138
+
139
+ predictions = model.predict(accounts_scaled)
140
+ probabilities = model.predict_proba(accounts_scaled)
141
+
142
+ # Add results to DataFrame
143
+ accounts_df['is_bot'] = predictions
144
+ accounts_df['bot_probability'] = probabilities[:, 1]
145
+ ```
146
+
147
+ ## Training Data
148
+
149
+ The model was trained on a dataset of TikTok accounts with labeled bot/human classifications. The dataset includes:
150
+
151
+ - Balanced distribution of bot and human accounts
152
+ - Various account types and behavioral patterns
153
+ - Features extracted from public profile information
154
+
155
+ **Note**: The training data is proprietary and not included in this repository.
156
+
157
+ ## Training Procedure
158
+
159
+ ### Preprocessing
160
+
161
+ 1. Feature extraction from TikTok account profiles
162
+ 2. Calculation of derived features (e.g., FollowToFollowerRatio)
163
+ 3. MinMax normalization of all features to [0, 1] range
164
+ 4. Train-test split with stratification
165
+
166
+ ### Hyperparameters
167
+
168
+ - **Algorithm**: Random Forest Classifier
169
+ - **Normalization**: MinMaxScaler
170
+ - **Cross-validation**: Stratified K-Fold
171
+
172
+ The model was trained using scikit-learn's RandomForestClassifier with optimized hyperparameters selected through cross-validation.
173
+
174
+ ## Limitations and Bias
175
+
176
+ ### Limitations
177
+
178
+ - Model performance depends on the quality of input features
179
+ - May not generalize to new bot patterns not seen during training
180
+ - Requires accurate feature extraction from TikTok profiles
181
+ - Performance may degrade over time as bot behaviors evolve
182
+
183
+ ### Potential Biases
184
+
185
+ - May be biased toward bot patterns present in the training data
186
+ - Could have regional or cultural biases depending on training data composition
187
+ - May misclassify legitimate accounts with unusual behavior patterns
188
+
189
+ ### Recommendations
190
+
191
+ - Regularly retrain the model with new data
192
+ - Use as part of a multi-layered detection system
193
+ - Implement human review for high-stakes decisions
194
+ - Monitor for false positives and adjust thresholds accordingly
195
+
196
+ ## Ethical Considerations
197
+
198
+ - This model should be used responsibly and not for harassment
199
+ - Consider privacy implications when analyzing user accounts
200
+ - Ensure compliance with TikTok's terms of service and relevant laws
201
+ - Implement appropriate safeguards against misuse
202
+
203
+ ## Model Card Authors
204
+
205
+ This model card was created as part of the Bot Detection project for social media platforms.
206
+
207
+ ## Citation
208
+
209
+ If you use this model in your research, please cite:
210
+
211
+ ```bibtex
212
+ @misc{tiktok_bot_detection_2024,
213
+ title={TikTok Bot Detection Model},
214
+ author={Your Name/Organization},
215
+ year={2024},
216
+ publisher={Hugging Face},
217
+ howpublished={\url{https://huggingface.co/your-username/tiktok-bot-detection}}
218
+ }
219
+ ```
220
+
221
+ ## Contact
222
+
223
+ For questions or feedback about this model, please open an issue in the repository or contact the maintainers.
224
+
225
+ ## Updates and Maintenance
226
+
227
+ - **Version**: 1.0
228
+ - **Last Updated**: November 2024
229
+ - **Status**: Active
230
+
231
+ Future updates may include:
232
+
233
+ - Improved feature engineering
234
+ - Additional training data
235
+ - Hyperparameter optimization
236
+ - Support for new TikTok features
inference_example.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Example inference script for TikTok Bot Detection Model
3
+ """
4
+
5
+ import joblib
6
+ import pandas as pd
7
+ from sklearn.preprocessing import MinMaxScaler
8
+
9
+
10
+ def load_model(model_path="TIKTOK_BOT_Detection_Model_v1.pkl"):
11
+ """Load the trained bot detection model"""
12
+ return joblib.load(model_path)
13
+
14
+
15
+ def prepare_features(account_data):
16
+ """
17
+ Prepare account features for prediction
18
+
19
+ Args:
20
+ account_data (dict): Dictionary containing account features
21
+
22
+ Returns:
23
+ numpy.ndarray: Scaled features ready for prediction
24
+ """
25
+ features = [
26
+ "IsPrivate",
27
+ "IsVerified",
28
+ "HasProfilePic",
29
+ "FollowingCount",
30
+ "FollowerCount",
31
+ "HasInstagram",
32
+ "HasYoutube",
33
+ "HasBio",
34
+ "HasLinkInBio",
35
+ "HasPosts",
36
+ "PostsCount",
37
+ "FollowToFollowerRatio",
38
+ ]
39
+
40
+ df = pd.DataFrame([account_data])
41
+
42
+ # Scale features
43
+ scaler = MinMaxScaler()
44
+ df_scaled = scaler.fit_transform(df[features])
45
+
46
+ return df_scaled
47
+
48
+
49
+ def predict_single_account(model, account_data):
50
+ """
51
+ Predict if a single account is a bot
52
+
53
+ Args:
54
+ model: Trained sklearn model
55
+ account_data (dict): Account features
56
+
57
+ Returns:
58
+ dict: Prediction results with probabilities
59
+ """
60
+ features_scaled = prepare_features(account_data)
61
+
62
+ prediction = model.predict(features_scaled)[0]
63
+ probability = model.predict_proba(features_scaled)[0]
64
+
65
+ return {
66
+ "is_bot": bool(prediction),
67
+ "bot_probability": float(probability[1]),
68
+ "human_probability": float(probability[0]),
69
+ "confidence": float(max(probability)),
70
+ }
71
+
72
+
73
+ def predict_batch(model, accounts_df):
74
+ """
75
+ Predict for multiple accounts at once
76
+
77
+ Args:
78
+ model: Trained sklearn model
79
+ accounts_df (pd.DataFrame): DataFrame with account features
80
+
81
+ Returns:
82
+ pd.DataFrame: Original data with predictions added
83
+ """
84
+ features = [
85
+ "IsPrivate",
86
+ "IsVerified",
87
+ "HasProfilePic",
88
+ "FollowingCount",
89
+ "FollowerCount",
90
+ "HasInstagram",
91
+ "HasYoutube",
92
+ "HasBio",
93
+ "HasLinkInBio",
94
+ "HasPosts",
95
+ "PostsCount",
96
+ "FollowToFollowerRatio",
97
+ ]
98
+
99
+ scaler = MinMaxScaler()
100
+ features_scaled = scaler.fit_transform(accounts_df[features])
101
+
102
+ predictions = model.predict(features_scaled)
103
+ probabilities = model.predict_proba(features_scaled)
104
+
105
+ accounts_df["is_bot"] = predictions
106
+ accounts_df["bot_probability"] = probabilities[:, 1]
107
+ accounts_df["human_probability"] = probabilities[:, 0]
108
+
109
+ return accounts_df
110
+
111
+
112
+ # Example usage
113
+ if __name__ == "__main__":
114
+ # Load model
115
+ print("Loading TikTok bot detection model...")
116
+ model = load_model()
117
+ print("✓ Model loaded successfully!\n")
118
+
119
+ # Example 1: Single account prediction
120
+ print("=" * 60)
121
+ print("Example 1: Single Account Prediction")
122
+ print("=" * 60)
123
+
124
+ suspicious_account = {
125
+ "IsPrivate": 0,
126
+ "IsVerified": 0,
127
+ "HasProfilePic": 1,
128
+ "FollowingCount": 5000,
129
+ "FollowerCount": 100,
130
+ "HasInstagram": 0,
131
+ "HasYoutube": 0,
132
+ "HasBio": 0,
133
+ "HasLinkInBio": 1,
134
+ "HasPosts": 1,
135
+ "PostsCount": 50,
136
+ "FollowToFollowerRatio": 50.0,
137
+ }
138
+
139
+ result = predict_single_account(model, suspicious_account)
140
+
141
+ print(f"Account Analysis:")
142
+ print(f" Following: {suspicious_account['FollowingCount']}")
143
+ print(f" Followers: {suspicious_account['FollowerCount']}")
144
+ print(f" Posts: {suspicious_account['PostsCount']}")
145
+ print(f"\nPrediction:")
146
+ print(f" Is Bot: {result['is_bot']}")
147
+ print(f" Bot Probability: {result['bot_probability']:.2%}")
148
+ print(f" Confidence: {result['confidence']:.2%}")
149
+
150
+ # Example 2: Batch prediction
151
+ print(f"\n{'='*60}")
152
+ print("Example 2: Batch Prediction")
153
+ print("=" * 60)
154
+
155
+ accounts = pd.DataFrame(
156
+ [
157
+ {
158
+ "IsPrivate": 0,
159
+ "IsVerified": 1,
160
+ "HasProfilePic": 1,
161
+ "FollowingCount": 500,
162
+ "FollowerCount": 10000,
163
+ "HasInstagram": 1,
164
+ "HasYoutube": 1,
165
+ "HasBio": 1,
166
+ "HasLinkInBio": 1,
167
+ "HasPosts": 1,
168
+ "PostsCount": 200,
169
+ "FollowToFollowerRatio": 0.05,
170
+ },
171
+ {
172
+ "IsPrivate": 0,
173
+ "IsVerified": 0,
174
+ "HasProfilePic": 0,
175
+ "FollowingCount": 8000,
176
+ "FollowerCount": 50,
177
+ "HasInstagram": 0,
178
+ "HasYoutube": 0,
179
+ "HasBio": 0,
180
+ "HasLinkInBio": 1,
181
+ "HasPosts": 1,
182
+ "PostsCount": 10,
183
+ "FollowToFollowerRatio": 160.0,
184
+ },
185
+ ]
186
+ )
187
+
188
+ results = predict_batch(model, accounts.copy())
189
+
190
+ print("\nResults:")
191
+ for idx, row in results.iterrows():
192
+ print(f"\nAccount {idx + 1}:")
193
+ print(f" Followers: {row['FollowerCount']}")
194
+ print(f" Is Bot: {bool(row['is_bot'])}")
195
+ print(f" Bot Probability: {row['bot_probability']:.2%}")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ scikit-learn>=1.3.0
2
+ pandas>=2.0.0
3
+ numpy>=1.24.0
4
+ joblib>=1.3.0