Spaces:
Running
Running
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() | |