Upload inference.py with huggingface_hub
Browse files- inference.py +121 -0
inference.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Hugging Face compatible inference for content moderation
|
| 4 |
+
"""
|
| 5 |
+
import pickle
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from enum import Enum
|
| 8 |
+
|
| 9 |
+
class AgeMode(Enum):
|
| 10 |
+
UNDER_13 = "under_13"
|
| 11 |
+
TEEN_PLUS = "teen_plus"
|
| 12 |
+
|
| 13 |
+
class ContentLabel(Enum):
|
| 14 |
+
SAFE = 0
|
| 15 |
+
HARASSMENT = 1
|
| 16 |
+
SWEARING_REACTION = 2
|
| 17 |
+
SWEARING_AGGRESSIVE = 3
|
| 18 |
+
HATE_SPEECH = 4
|
| 19 |
+
SPAM = 5
|
| 20 |
+
|
| 21 |
+
class DualModeFilter:
|
| 22 |
+
"""
|
| 23 |
+
Dual-mode content filter for Hugging Face
|
| 24 |
+
|
| 25 |
+
Usage:
|
| 26 |
+
filter = DualModeFilter("Naymmm/content-moderation-dual-mode")
|
| 27 |
+
result = filter.check("text here", age=15)
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
def __init__(self, repo_id="darwinkernelpanic/moderat", token=None):
|
| 31 |
+
# Download model from HF
|
| 32 |
+
model_path = hf_hub_download(
|
| 33 |
+
repo_id=repo_id,
|
| 34 |
+
filename="moderation_model.pkl",
|
| 35 |
+
token=token
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Load model
|
| 39 |
+
with open(model_path, 'rb') as f:
|
| 40 |
+
self.pipeline = pickle.load(f)
|
| 41 |
+
|
| 42 |
+
self.under_13_blocked = [1, 2, 3, 4, 5]
|
| 43 |
+
self.teen_plus_blocked = [1, 3, 4, 5]
|
| 44 |
+
self.label_names = [l.name for l in ContentLabel]
|
| 45 |
+
|
| 46 |
+
def predict(self, text):
|
| 47 |
+
"""Predict label for text"""
|
| 48 |
+
prediction = self.pipeline.predict([text])[0]
|
| 49 |
+
probs = self.pipeline.predict_proba([text])[0]
|
| 50 |
+
confidence = max(probs)
|
| 51 |
+
return ContentLabel(prediction), confidence
|
| 52 |
+
|
| 53 |
+
def check(self, text, age):
|
| 54 |
+
"""
|
| 55 |
+
Check content against age-appropriate filters
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
text: Text to check
|
| 59 |
+
age: User age (determines strict vs laxed mode)
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
dict with 'allowed', 'label', 'confidence', 'mode', 'reason'
|
| 63 |
+
"""
|
| 64 |
+
label, confidence = self.predict(text)
|
| 65 |
+
mode = AgeMode.TEEN_PLUS if age >= 13 else AgeMode.UNDER_13
|
| 66 |
+
|
| 67 |
+
# Low confidence check
|
| 68 |
+
if confidence < 0.5:
|
| 69 |
+
return {
|
| 70 |
+
"allowed": True,
|
| 71 |
+
"label": "UNCERTAIN",
|
| 72 |
+
"confidence": confidence,
|
| 73 |
+
"mode": mode.value,
|
| 74 |
+
"reason": "Low confidence - manual review recommended"
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
# Check if blocked for this age
|
| 78 |
+
if age >= 13:
|
| 79 |
+
allowed = label.value not in self.teen_plus_blocked
|
| 80 |
+
else:
|
| 81 |
+
allowed = label.value not in self.under_13_blocked
|
| 82 |
+
|
| 83 |
+
reason = "Safe"
|
| 84 |
+
if not allowed:
|
| 85 |
+
if label == ContentLabel.SWEARING_REACTION and age >= 13:
|
| 86 |
+
reason = "Swearing permitted as reaction (13+)"
|
| 87 |
+
allowed = True
|
| 88 |
+
else:
|
| 89 |
+
reason = f"{label.name} detected"
|
| 90 |
+
|
| 91 |
+
return {
|
| 92 |
+
"allowed": allowed,
|
| 93 |
+
"label": label.name,
|
| 94 |
+
"confidence": confidence,
|
| 95 |
+
"mode": mode.value,
|
| 96 |
+
"reason": reason
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
# Example usage
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
print("Testing Dual-Mode Content Filter")
|
| 102 |
+
print("="*50)
|
| 103 |
+
|
| 104 |
+
# Initialize (downloads model from HF)
|
| 105 |
+
filter_sys = DualModeFilter()
|
| 106 |
+
|
| 107 |
+
tests = [
|
| 108 |
+
("that was a great game", 10),
|
| 109 |
+
("that was a great game", 15),
|
| 110 |
+
("shit that sucks", 10),
|
| 111 |
+
("shit that sucks", 15),
|
| 112 |
+
("you're a piece of shit", 15),
|
| 113 |
+
("kill yourself", 15),
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
for text, age in tests:
|
| 117 |
+
result = filter_sys.check(text, age)
|
| 118 |
+
status = "✅ ALLOWED" if result["allowed"] else "❌ BLOCKED"
|
| 119 |
+
print(f"\nAge {age}: '{text}'")
|
| 120 |
+
print(f" {status} - {result['reason']}")
|
| 121 |
+
print(f" Confidence: {result['confidence']:.2f}")
|