|
|
|
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
import torch |
|
|
|
|
|
def classify_text(text, model_path=None): |
|
|
|
|
|
model_path = model_path or "." |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
|
model = AutoModelForSequenceClassification.from_pretrained(model_path) |
|
|
|
|
|
|
|
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True) |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
outputs = model(**inputs) |
|
|
logits = outputs.logits |
|
|
|
|
|
|
|
|
probabilities = torch.nn.functional.softmax(logits, dim=1) |
|
|
predicted_class_idx = torch.argmax(probabilities, dim=1).item() |
|
|
confidence = probabilities[0][predicted_class_idx].item() |
|
|
|
|
|
|
|
|
labels = ["Human-written", "AI-generated"] |
|
|
predicted_label = labels[predicted_class_idx] |
|
|
|
|
|
return predicted_label, confidence |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
text = "Enter your text here to test if it's AI-generated or human-written." |
|
|
result, confidence = classify_text(text) |
|
|
print(f"This text appears to be: {result}") |
|
|
print(f"Confidence: {confidence:.4f}") |
|
|
|