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