Spaces:
Running
Running
File size: 1,429 Bytes
958e345 6c56aca 958e345 6c56aca 958e345 6c56aca 958e345 6400b6d 958e345 6400b6d 958e345 6400b6d 958e345 |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import re
# Load model
MODEL = "roberta-base-openai-detector"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
def detect_ai(text):
sentences = re.split(r'(?<=[.!?]) +', text)
results = []
for sent in sentences:
if not sent.strip():
continue
inputs = tokenizer(sent, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=1)
ai_score = float(probs[0][1]) # fix here
results.append({"sentence": sent, "ai_score": ai_score})
highlighted = ""
for r in results:
color = f"rgba(255,0,0,{r['ai_score']})"
highlighted += f"<span style='background-color:{color}; padding:2px'>{r['sentence']} </span>"
return highlighted, results
with gr.Blocks() as demo:
gr.Markdown("## 🤖 AI Detector (like ZeroGPT)")
gr.Markdown("Paste your text below. Redder highlights = more AI-like.")
input_text = gr.Textbox(lines=8, placeholder="Enter text here...")
output_html = gr.HTML()
output_json = gr.JSON()
run_btn = gr.Button("Detect AI")
run_btn.click(detect_ai, inputs=input_text, outputs=[output_html, output_json])
demo.launch()
|