Spaces:
Running
Running
File size: 1,616 Bytes
91c441c c1174d9 91c441c 14f2051 91c441c 14f2051 91c441c c1174d9 91c441c c1174d9 14f2051 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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()
|