Spaces:
Runtime error
Runtime error
Create ethical_filter.py
Browse files- ethical_filter.py +28 -0
ethical_filter.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class EthicalFilter:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.blocked_keywords = {
|
| 4 |
+
"violence", "harm", "explosive", "attack", "hate", "suicide", "kill", "destroy",
|
| 5 |
+
"malware", "exploit", "virus", "ddos", "overthrow", "abuse"
|
| 6 |
+
}
|
| 7 |
+
self.flagged_keywords = {
|
| 8 |
+
"sad", "alone", "self-harm", "worthless", "die", "suffer", "broken"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def analyze_query(self, query: str) -> dict:
|
| 12 |
+
query_lower = query.lower()
|
| 13 |
+
blocked_hits = [word for word in self.blocked_keywords if word in query_lower]
|
| 14 |
+
flagged_hits = [word for word in self.flagged_keywords if word in query_lower]
|
| 15 |
+
|
| 16 |
+
if blocked_hits:
|
| 17 |
+
return {
|
| 18 |
+
"status": "blocked",
|
| 19 |
+
"reason": f"Detected unsafe keywords: {', '.join(blocked_hits)}"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
if flagged_hits:
|
| 23 |
+
return {
|
| 24 |
+
"status": "flagged",
|
| 25 |
+
"warning": f"Sensitive content detected: {', '.join(flagged_hits)}"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
return {"status": "safe"}
|