|
import gradio as gr |
|
import numpy as np |
|
import torch |
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline |
|
|
|
|
|
model_name = "AICodexLab/answerdotai-ModernBERT-base-ai-detector" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1) |
|
|
|
|
|
def predict_ai_text(input_text): |
|
result = classifier(input_text) |
|
label = "AI-Generated" if result[0]["label"] == "LABEL_1" else "Human-Written" |
|
confidence = np.round(result[0]["score"], 3) |
|
return f"{label} (Confidence: {confidence})" |
|
|
|
|
|
app = gr.Interface( |
|
fn=predict_ai_text, |
|
inputs=gr.Textbox(lines=5, placeholder="Enter your text here..."), |
|
outputs=gr.Textbox(), |
|
title="AI Text Detector", |
|
description="Detect whether a given text is AI-generated or human-written.", |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.launch() |
|
|