Create agents/nsfw_agent.py
Browse files- agents/nsfw_agent.py +47 -0
agents/nsfw_agent.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
from semantic_index import FaissSemanticIndex
|
| 3 |
+
from embedding_cache import EmbeddingCache
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class NSFWMatchingAgent:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.model_name = "all-MiniLM-L6-v2"
|
| 10 |
+
self.model = SentenceTransformer(self.model_name)
|
| 11 |
+
self.cache = EmbeddingCache(self.model_name)
|
| 12 |
+
self.index = FaissSemanticIndex(dim=384) # MiniLM output size
|
| 13 |
+
self.texts = []
|
| 14 |
+
|
| 15 |
+
self.sources = [
|
| 16 |
+
"aifeifei798/DPO_Pairs-Roleplay-NSFW",
|
| 17 |
+
"Maxx0/sexting-nsfw-adultconten",
|
| 18 |
+
"QuietImpostor/Claude-3-Opus-Claude-3.5-Sonnnet-9k",
|
| 19 |
+
"HuggingFaceTB/everyday-conversations-llama3.1-2k",
|
| 20 |
+
"Chadgpt-fam/sexting_dataset"
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
self._load_and_index()
|
| 24 |
+
|
| 25 |
+
def _load_and_index(self):
|
| 26 |
+
for source in self.sources:
|
| 27 |
+
try:
|
| 28 |
+
dataset = load_dataset(source)
|
| 29 |
+
texts = []
|
| 30 |
+
for split in dataset:
|
| 31 |
+
if 'text' in dataset[split].features:
|
| 32 |
+
texts.extend(dataset[split]['text'])
|
| 33 |
+
elif 'content' in dataset[split].features:
|
| 34 |
+
texts.extend(dataset[split]['content'])
|
| 35 |
+
|
| 36 |
+
embeddings = self.cache.compute_or_load_embeddings(
|
| 37 |
+
texts, dataset_key=source.replace("/", "_")
|
| 38 |
+
)
|
| 39 |
+
self.index.add(embeddings, texts)
|
| 40 |
+
self.texts.extend(texts)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"[WARN] Failed to load {source}: {e}")
|
| 43 |
+
|
| 44 |
+
def match(self, query: str) -> str:
|
| 45 |
+
query_vec = self.model.encode([query], convert_to_tensor=True)
|
| 46 |
+
result = self.index.search(query_vec, k=1)
|
| 47 |
+
return result[0][0] if result else "No match found"
|