Nortix's picture
Update app.py
14f2051 verified
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()