File size: 5,190 Bytes
2e8c34c |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
"""
Example inference script for TikTok Bot Detection Model
"""
import joblib
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
def load_model(model_path="TIKTOK_BOT_Detection_Model_v1.pkl"):
"""Load the trained bot detection model"""
return joblib.load(model_path)
def prepare_features(account_data):
"""
Prepare account features for prediction
Args:
account_data (dict): Dictionary containing account features
Returns:
numpy.ndarray: Scaled features ready for prediction
"""
features = [
"IsPrivate",
"IsVerified",
"HasProfilePic",
"FollowingCount",
"FollowerCount",
"HasInstagram",
"HasYoutube",
"HasBio",
"HasLinkInBio",
"HasPosts",
"PostsCount",
"FollowToFollowerRatio",
]
df = pd.DataFrame([account_data])
# Scale features
scaler = MinMaxScaler()
df_scaled = scaler.fit_transform(df[features])
return df_scaled
def predict_single_account(model, account_data):
"""
Predict if a single account is a bot
Args:
model: Trained sklearn model
account_data (dict): Account features
Returns:
dict: Prediction results with probabilities
"""
features_scaled = prepare_features(account_data)
prediction = model.predict(features_scaled)[0]
probability = model.predict_proba(features_scaled)[0]
return {
"is_bot": bool(prediction),
"bot_probability": float(probability[1]),
"human_probability": float(probability[0]),
"confidence": float(max(probability)),
}
def predict_batch(model, accounts_df):
"""
Predict for multiple accounts at once
Args:
model: Trained sklearn model
accounts_df (pd.DataFrame): DataFrame with account features
Returns:
pd.DataFrame: Original data with predictions added
"""
features = [
"IsPrivate",
"IsVerified",
"HasProfilePic",
"FollowingCount",
"FollowerCount",
"HasInstagram",
"HasYoutube",
"HasBio",
"HasLinkInBio",
"HasPosts",
"PostsCount",
"FollowToFollowerRatio",
]
scaler = MinMaxScaler()
features_scaled = scaler.fit_transform(accounts_df[features])
predictions = model.predict(features_scaled)
probabilities = model.predict_proba(features_scaled)
accounts_df["is_bot"] = predictions
accounts_df["bot_probability"] = probabilities[:, 1]
accounts_df["human_probability"] = probabilities[:, 0]
return accounts_df
# Example usage
if __name__ == "__main__":
# Load model
print("Loading TikTok bot detection model...")
model = load_model()
print("✓ Model loaded successfully!\n")
# Example 1: Single account prediction
print("=" * 60)
print("Example 1: Single Account Prediction")
print("=" * 60)
suspicious_account = {
"IsPrivate": 0,
"IsVerified": 0,
"HasProfilePic": 1,
"FollowingCount": 5000,
"FollowerCount": 100,
"HasInstagram": 0,
"HasYoutube": 0,
"HasBio": 0,
"HasLinkInBio": 1,
"HasPosts": 1,
"PostsCount": 50,
"FollowToFollowerRatio": 50.0,
}
result = predict_single_account(model, suspicious_account)
print(f"Account Analysis:")
print(f" Following: {suspicious_account['FollowingCount']}")
print(f" Followers: {suspicious_account['FollowerCount']}")
print(f" Posts: {suspicious_account['PostsCount']}")
print(f"\nPrediction:")
print(f" Is Bot: {result['is_bot']}")
print(f" Bot Probability: {result['bot_probability']:.2%}")
print(f" Confidence: {result['confidence']:.2%}")
# Example 2: Batch prediction
print(f"\n{'='*60}")
print("Example 2: Batch Prediction")
print("=" * 60)
accounts = pd.DataFrame(
[
{
"IsPrivate": 0,
"IsVerified": 1,
"HasProfilePic": 1,
"FollowingCount": 500,
"FollowerCount": 10000,
"HasInstagram": 1,
"HasYoutube": 1,
"HasBio": 1,
"HasLinkInBio": 1,
"HasPosts": 1,
"PostsCount": 200,
"FollowToFollowerRatio": 0.05,
},
{
"IsPrivate": 0,
"IsVerified": 0,
"HasProfilePic": 0,
"FollowingCount": 8000,
"FollowerCount": 50,
"HasInstagram": 0,
"HasYoutube": 0,
"HasBio": 0,
"HasLinkInBio": 1,
"HasPosts": 1,
"PostsCount": 10,
"FollowToFollowerRatio": 160.0,
},
]
)
results = predict_batch(model, accounts.copy())
print("\nResults:")
for idx, row in results.iterrows():
print(f"\nAccount {idx + 1}:")
print(f" Followers: {row['FollowerCount']}")
print(f" Is Bot: {bool(row['is_bot'])}")
print(f" Bot Probability: {row['bot_probability']:.2%}")
|