import gradio as gr from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch # Load the model model_name = "roberta-base-openai-detector" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) # Detection logic def detect_ai(text): inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) with torch.no_grad(): outputs = model(**inputs) probs = torch.nn.functional.softmax(outputs.logits, dim=1) ai_score = round(probs[0][1].item() * 100, 2) human_score = round(probs[0][0].item() * 100, 2) if ai_score > 80: verdict = "⚠️ Likely AI-generated" elif human_score > 80: verdict = "✅ Likely Human-written" else: verdict = "❓ Unclear — Mixed Content" return { "AI-generated (%)": ai_score, "Human-written (%)": human_score, "Verdict": verdict } # Build UI with Blocks with gr.Blocks(css="footer {display: none !important;}") as demo: gr.Markdown(""" # 🔍 AI Text Detector **Check if a text was written by AI or a human** Using `roberta-base-openai-detector` — Powered by 🤗 Hugging Face + Gradio """) with gr.Row(): input_box = gr.Textbox(label="Paste your text", placeholder="Enter at least 40 words...", lines=10) with gr.Row(): analyze_btn = gr.Button("🚀 Analyze Text") output_json = gr.JSON(label="📊 Detection Result") analyze_btn.click(fn=detect_ai, inputs=input_box, outputs=output_json) demo.launch()