Upload example.py with huggingface_hub
Browse files- example.py +21 -0
example.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""AI Detector Example - Python Inference"""
|
| 3 |
+
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
def detect_ai(text, model_id="darwinkernelpanic/ai-detector-pgx"):
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 10 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
|
| 11 |
+
with torch.no_grad():
|
| 12 |
+
outputs = model(**inputs)
|
| 13 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 14 |
+
ai_prob = probs[0][1].item()
|
| 15 |
+
return {"ai_prob": ai_prob, "is_ai": ai_prob > 0.5}
|
| 16 |
+
|
| 17 |
+
if __name__ == "__main__":
|
| 18 |
+
text = "The mitochondria is the powerhouse of the cell..."
|
| 19 |
+
result = detect_ai(text)
|
| 20 |
+
print(f"AI Probability: {result['ai_prob']:.2%}")
|
| 21 |
+
print(f"Verdict: {'AI' if result['is_ai'] else 'Human'}")
|