Spaces:
Build error
Build error
Create ai_monitor.py
Browse files- ai_monitor.py +21 -0
ai_monitor.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# New ai_monitor.py
|
| 2 |
+
from sklearn.ensemble import IsolationForest
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
class AIActivityMonitor:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.model = IsolationForest(contamination=0.1)
|
| 8 |
+
|
| 9 |
+
def detect_unusual_activity(self, activity_data):
|
| 10 |
+
# Convert activity data to features
|
| 11 |
+
features = self._extract_features(activity_data)
|
| 12 |
+
predictions = self.model.fit_predict(features)
|
| 13 |
+
return predictions == -1 # True for anomalies
|
| 14 |
+
|
| 15 |
+
def _extract_features(self, activity_data):
|
| 16 |
+
# Extract numerical features from activity data
|
| 17 |
+
return np.array([[
|
| 18 |
+
data.get('login_count', 0),
|
| 19 |
+
data.get('post_count', 0),
|
| 20 |
+
data.get('interaction_count', 0)
|
| 21 |
+
] for data in activity_data])
|