File size: 3,094 Bytes
c07ca4c
 
 
 
 
 
 
 
 
 
 
 
 
dba502d
c07ca4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
license: apache-2.0
language:
- en
base_model:
- microsoft/deberta-v3-base
---
# Slop Classifier for Roleplay Characters

> This model can detect characters that are created using AI.

Part of [CharGen](https://huggingface.co/kubernetes-bad/chargen-v2) project - it is used to detect and filter out low-effort, LLM-made characters intended for role playing.

*Slop* refers to over-used phrases that models like GPT3.5 like to use very much and that do not add any value to the text. "Shivers down her spine", "enigma wrapped in mystery", "half-lidded eyes", etc. Classifier is trained on set of synthetic characters generated with GPT3.5 and GPT4, and a subset of CharGen dataset.

## Usage

```py
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
from litserve import LitAPI, LitServer

MODEL_NAME = "kubernetes-bad/character-slop-classifier"

class CHARLitAPI(LitAPI):
    def setup(self, device):
        self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
        self.model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
        self.model.to(device)
        self.model.eval()

    def decode_request(self, request):
        if "text" in request:
            inputs = self.tokenizer(request["text"], return_tensors="pt", padding=True, truncation=True, max_length=512)
        elif "texts" in request:
            inputs = self.tokenizer(request["texts"], return_tensors="pt", padding=True, truncation=True, max_length=512)
        else:
            raise ValueError("Invalid request format. Expected 'text' or 'texts' field.")
        return inputs

    def predict(self, inputs):
        with torch.no_grad():
            inputs = {k: v.to(self.model.device) for k, v in inputs.items()}
            outputs = self.model(**inputs)
        return outputs.logits

    def encode_response(self, logits):
        probabilities = torch.nn.functional.softmax(logits, dim=-1)
        if probabilities.shape[0] == 1:
            response = {
                "positive": probabilities[:, 1].item(),
                "negative": probabilities[:, 0].item()
            }
        else:
            response = [
                {
                    "positive": prob[1].item(),
                    "negative": prob[0].item()
                }
                for prob in probabilities
            ]
        return response


if __name__ == "__main__":
    api = CHARLitAPI()
    server = LitServer(api, accelerator='cuda')
    server.run(port=9000)
```

```bash
curl --location 'http://localhost:9000/predict' \
--header 'Content-Type: application/json' \
--data '{
    "text": "Hermione, the seductive intellectual enchantress, is the secret sin of Hogwarts. Beneath her seemingly innocent scholarly facade lies a tantalizing world of forbidden desires. In the hallowed halls of the wizarding world, she conceals her lewd nature from her peers, maintaining a pristine reputation as the most brilliant witch of her age."
}'
```
Example response:
```json
{
    "positive": 0.9975564479827881,
    "negative": 0.0024435613304376602
}
```