eheguy commited on
Commit
8194c3e
·
1 Parent(s): 4516e1c

Swap to ChatGPT-specific detector model

Browse files
Files changed (1) hide show
  1. detector.py +34 -22
detector.py CHANGED
@@ -1,28 +1,40 @@
 
1
  from transformers import pipeline
2
 
3
- # Cache the classifier pipeline globally after first load
4
- _classifier = None
5
 
6
- def get_ai_score(text: str) -> float:
7
- global _classifier
8
- if _classifier is None:
9
- # Load the pipeline using the specified model
10
- _classifier = pipeline(
11
  "text-classification",
12
- model="openai-community/roberta-base-openai-detector"
 
13
  )
14
-
15
- # Truncate input text to 512 tokens max before scoring to respect model limits
16
- results = _classifier(text, truncation=True, max_length=512)
17
- result = results[0]
18
-
19
- label = result["label"]
20
- score = float(result["score"])
21
-
22
- # Map labels:
23
- # "Fake" score represents AI probability.
24
- # "Real" score represents human probability, so AI probability = 1 - score.
25
- if label == "Fake":
26
- return score
 
 
 
 
 
 
 
 
 
 
27
  else:
28
- return 1.0 - score
 
1
+ import torch
2
  from transformers import pipeline
3
 
4
+ # Cache the pipeline at module level load once, reuse forever
5
+ _pipeline = None
6
 
7
+
8
+ def _get_pipeline():
9
+ global _pipeline
10
+ if _pipeline is None:
11
+ _pipeline = pipeline(
12
  "text-classification",
13
+ model="Hello-SimpleAI/chatgpt-detector-roberta",
14
+ device=0 if torch.cuda.is_available() else -1,
15
  )
16
+ return _pipeline
17
+
18
+
19
+ def get_ai_score(text: str) -> float:
20
+ """
21
+ Returns a float between 0.0 and 1.0 representing the probability
22
+ that the text was AI-generated.
23
+ 1.0 = definitely AI
24
+ 0.0 = definitely human
25
+ """
26
+ # Model has 512 token limit truncate to be safe
27
+ truncated = text[:512]
28
+
29
+ result = _get_pipeline()(truncated)[0]
30
+
31
+ label = result["label"].upper()
32
+ score = result["score"]
33
+
34
+ # This model returns:
35
+ # "ChatGPT" label = AI-generated
36
+ # "Human" label = human-written
37
+ if label == "CHATGPT":
38
+ return round(score, 4)
39
  else:
40
+ return round(1 - score, 4)