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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": [],
|
| 7 |
+
"name": "moderat-speed-test.ipynb"
|
| 8 |
+
},
|
| 9 |
+
"kernelspec": {
|
| 10 |
+
"name": "python3",
|
| 11 |
+
"display_name": "Python 3"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"cells": [
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "markdown",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"source": [
|
| 19 |
+
"# π‘οΈ moderat - Speed Test & Benchmark\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"Test inference speeds for the dual-mode content moderation model.\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"**Model:** [darwinkernelpanic/moderat](https://huggingface.co/darwinkernelpanic/moderat)"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"cell_type": "code",
|
| 28 |
+
"execution_count": null,
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [],
|
| 31 |
+
"source": [
|
| 32 |
+
"# @title 1. Install dependencies\n",
|
| 33 |
+
"!pip install -q scikit-learn huggingface-hub"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"cell_type": "code",
|
| 38 |
+
"execution_count": null,
|
| 39 |
+
"metadata": {},
|
| 40 |
+
"outputs": [],
|
| 41 |
+
"source": [
|
| 42 |
+
"# @title 2. Download model from Hugging Face\n",
|
| 43 |
+
"from huggingface_hub import hf_hub_download\n",
|
| 44 |
+
"import pickle\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"MODEL_REPO = \"darwinkernelpanic/moderat\"\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"# Download model\n",
|
| 49 |
+
"model_path = hf_hub_download(\n",
|
| 50 |
+
" repo_id=MODEL_REPO,\n",
|
| 51 |
+
" filename=\"moderation_model.pkl\"\n",
|
| 52 |
+
")\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"# Load model\n",
|
| 55 |
+
"with open(model_path, 'rb') as f:\n",
|
| 56 |
+
" pipeline = pickle.load(f)\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"print(f\"β
Model loaded from {MODEL_REPO}\")"
|
| 59 |
+
]
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"cell_type": "code",
|
| 63 |
+
"execution_count": null,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"outputs": [],
|
| 66 |
+
"source": [
|
| 67 |
+
"# @title 3. Define inference functions\n",
|
| 68 |
+
"from enum import Enum\n",
|
| 69 |
+
"import time\n",
|
| 70 |
+
"\n",
|
| 71 |
+
"class ContentLabel(Enum):\n",
|
| 72 |
+
" SAFE = 0\n",
|
| 73 |
+
" HARASSMENT = 1\n",
|
| 74 |
+
" SWEARING_REACTION = 2\n",
|
| 75 |
+
" SWEARING_AGGRESSIVE = 3\n",
|
| 76 |
+
" HATE_SPEECH = 4\n",
|
| 77 |
+
" SPAM = 5\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"def predict(text):\n",
|
| 80 |
+
" \"\"\"Run inference and return label + confidence\"\"\"\n",
|
| 81 |
+
" prediction = pipeline.predict([text])[0]\n",
|
| 82 |
+
" probs = pipeline.predict_proba([text])[0]\n",
|
| 83 |
+
" confidence = max(probs)\n",
|
| 84 |
+
" return ContentLabel(prediction), confidence\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"def check_content(text, age):\n",
|
| 87 |
+
" \"\"\"Dual-mode filter\"\"\"\n",
|
| 88 |
+
" label, confidence = predict(text)\n",
|
| 89 |
+
" \n",
|
| 90 |
+
" under_13_blocked = [1, 2, 3, 4, 5]\n",
|
| 91 |
+
" teen_plus_blocked = [1, 3, 4, 5]\n",
|
| 92 |
+
" \n",
|
| 93 |
+
" if age >= 13:\n",
|
| 94 |
+
" allowed = label.value not in teen_plus_blocked\n",
|
| 95 |
+
" else:\n",
|
| 96 |
+
" allowed = label.value not in under_13_blocked\n",
|
| 97 |
+
" \n",
|
| 98 |
+
" # Allow reaction swearing for 13+\n",
|
| 99 |
+
" if not allowed and label == ContentLabel.SWEARING_REACTION and age >= 13:\n",
|
| 100 |
+
" allowed = True\n",
|
| 101 |
+
" \n",
|
| 102 |
+
" return {\n",
|
| 103 |
+
" \"allowed\": allowed,\n",
|
| 104 |
+
" \"label\": label.name,\n",
|
| 105 |
+
" \"confidence\": confidence\n",
|
| 106 |
+
" }"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"cell_type": "code",
|
| 111 |
+
"execution_count": null,
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [],
|
| 114 |
+
"source": [
|
| 115 |
+
"# @title 4. Single inference speed test\n",
|
| 116 |
+
"test_text = \"damn that's crazy\"\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"# Warm up\n",
|
| 119 |
+
"_ = predict(test_text)\n",
|
| 120 |
+
"\n",
|
| 121 |
+
"# Time single inference\n",
|
| 122 |
+
"times = []\n",
|
| 123 |
+
"for _ in range(100):\n",
|
| 124 |
+
" start = time.perf_counter()\n",
|
| 125 |
+
" result = predict(test_text)\n",
|
| 126 |
+
" end = time.perf_counter()\n",
|
| 127 |
+
" times.append((end - start) * 1000) # Convert to ms\n",
|
| 128 |
+
"\n",
|
| 129 |
+
"avg_time = sum(times) / len(times)\n",
|
| 130 |
+
"min_time = min(times)\n",
|
| 131 |
+
"max_time = max(times)\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"print(f\"π Single Inference Speed (100 runs)\")\n",
|
| 134 |
+
"print(f\" Average: {avg_time:.3f} ms\")\n",
|
| 135 |
+
"print(f\" Min: {min_time:.3f} ms\")\n",
|
| 136 |
+
"print(f\" Max: {max_time:.3f} ms\")\n",
|
| 137 |
+
"print(f\" Throughput: {1000/avg_time:.1f} inferences/second\")"
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"cell_type": "code",
|
| 142 |
+
"execution_count": null,
|
| 143 |
+
"metadata": {},
|
| 144 |
+
"outputs": [],
|
| 145 |
+
"source": [
|
| 146 |
+
"# @title 5. Batch inference speed test\n",
|
| 147 |
+
"test_texts = [\n",
|
| 148 |
+
" \"that was a great game\",\n",
|
| 149 |
+
" \"shit that sucks\",\n",
|
| 150 |
+
" \"you're a piece of shit\",\n",
|
| 151 |
+
" \"kill yourself\",\n",
|
| 152 |
+
" \"i love this song\",\n",
|
| 153 |
+
" \"damn that's crazy\",\n",
|
| 154 |
+
" \"click here for free robux\",\n",
|
| 155 |
+
" \"congratulations\",\n",
|
| 156 |
+
"] * 100 # 800 total texts\n",
|
| 157 |
+
"\n",
|
| 158 |
+
"print(f\"Testing batch of {len(test_texts)} texts...\")\n",
|
| 159 |
+
"\n",
|
| 160 |
+
"start = time.perf_counter()\n",
|
| 161 |
+
"results = [predict(t) for t in test_texts]\n",
|
| 162 |
+
"end = time.perf_counter()\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"total_time = (end - start) * 1000\n",
|
| 165 |
+
"avg_per_text = total_time / len(test_texts)\n",
|
| 166 |
+
"\n",
|
| 167 |
+
"print(f\"\\nπ Batch Inference Results\")\n",
|
| 168 |
+
"print(f\" Total time: {total_time:.1f} ms\")\n",
|
| 169 |
+
"print(f\" Average per text: {avg_per_text:.3f} ms\")\n",
|
| 170 |
+
"print(f\" Throughput: {len(test_texts)/(total_time/1000):.1f} texts/second\")"
|
| 171 |
+
]
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
"cell_type": "code",
|
| 175 |
+
"execution_count": null,
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"outputs": [],
|
| 178 |
+
"source": [
|
| 179 |
+
"# @title 6. Dual-mode comparison test\n",
|
| 180 |
+
"test_cases = [\n",
|
| 181 |
+
" (\"that was a great game\", 10),\n",
|
| 182 |
+
" (\"that was a great game\", 15),\n",
|
| 183 |
+
" (\"shit that sucks\", 10),\n",
|
| 184 |
+
" (\"shit that sucks\", 15),\n",
|
| 185 |
+
" (\"you're a piece of shit\", 10),\n",
|
| 186 |
+
" (\"you're a piece of shit\", 15),\n",
|
| 187 |
+
" (\"kill yourself\", 10),\n",
|
| 188 |
+
" (\"kill yourself\", 15),\n",
|
| 189 |
+
"]\n",
|
| 190 |
+
"\n",
|
| 191 |
+
"print(\"π Dual-Mode Filter Results\\n\")\n",
|
| 192 |
+
"print(f\"{'Text':<30} {'Age':<6} {'Status':<10} {'Label':<20} {'Conf':<6}\")\n",
|
| 193 |
+
"print(\"-\" * 75)\n",
|
| 194 |
+
"\n",
|
| 195 |
+
"for text, age in test_cases:\n",
|
| 196 |
+
" result = check_content(text, age)\n",
|
| 197 |
+
" status = \"β
ALLOW\" if result[\"allowed\"] else \"β BLOCK\"\n",
|
| 198 |
+
" print(f\"{text:<30} {age:<6} {status:<10} {result['label']:<20} {result['confidence']:.2f}\")"
|
| 199 |
+
]
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"cell_type": "code",
|
| 203 |
+
"execution_count": null,
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"outputs": [],
|
| 206 |
+
"source": [
|
| 207 |
+
"# @title 7. Memory usage check\n",
|
| 208 |
+
"import sys\n",
|
| 209 |
+
"\n",
|
| 210 |
+
"# Estimate model size in memory\n",
|
| 211 |
+
"model_size = sys.getsizeof(pipeline) / 1024 / 1024\n",
|
| 212 |
+
"print(f\"πΎ Model memory usage: ~{model_size:.2f} MB\")\n",
|
| 213 |
+
"\n",
|
| 214 |
+
"# Check if GPU available (Colab usually has CPU only for sklearn)\n",
|
| 215 |
+
"import os\n",
|
| 216 |
+
"gpu_available = 'COLAB_GPU' in os.environ\n",
|
| 217 |
+
"print(f\"π₯ GPU available: {gpu_available}\")\n",
|
| 218 |
+
"print(f\"β‘ Running on: CPU (sklearn uses CPU)\")"
|
| 219 |
+
]
|
| 220 |
+
},
|
| 221 |
+
{
|
| 222 |
+
"cell_type": "markdown",
|
| 223 |
+
"metadata": {},
|
| 224 |
+
"source": [
|
| 225 |
+
"## π Expected Results\n",
|
| 226 |
+
"\n",
|
| 227 |
+
"On Google Colab (CPU):\n",
|
| 228 |
+
"- **Single inference:** ~0.5-2ms\n",
|
| 229 |
+
"- **Throughput:** ~500-2000 inferences/second\n",
|
| 230 |
+
"- **Memory:** ~5-15MB\n",
|
| 231 |
+
"\n",
|
| 232 |
+
"## π Links\n",
|
| 233 |
+
"\n",
|
| 234 |
+
"- Model: https://huggingface.co/darwinkernelpanic/moderat\n",
|
| 235 |
+
"- GitHub: Add your repo here"
|
| 236 |
+
]
|
| 237 |
+
}
|
| 238 |
+
]
|
| 239 |
+
}
|