import torch
from transformers import pipeline

# Load the model for AI text detection
text_classifier = pipeline("text-classification", model="roberta-large-openai-detector")

def is_generated_by_ai(paragraph):
    inputs = text_classifier.tokenizer(paragraph, padding=True, truncation=True, max_length=512, return_tensors="pt")
    result = text_classifier.model(**inputs)
    logits = result.logits
    confidence = torch.softmax(logits, dim=-1).max().item()

    confidence_percentage = confidence * 100

    # Adjust the threshold as needed
    confidence_threshold = 70  # Raise the threshold to 70% for AI detection
    return {
        "is_ai_generated": confidence_percentage >= confidence_threshold,
        "confidence_percentage": confidence_percentage
    }