Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import nltk
|
| 5 |
+
|
| 6 |
+
nltk.download("punkt")
|
| 7 |
+
|
| 8 |
+
# Load model
|
| 9 |
+
MODEL = "roberta-base-openai-detector"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 12 |
+
|
| 13 |
+
def detect_ai(text):
|
| 14 |
+
sentences = nltk.sent_tokenize(text)
|
| 15 |
+
results = []
|
| 16 |
+
|
| 17 |
+
for sent in sentences:
|
| 18 |
+
inputs = tokenizer(sent, return_tensors="pt", truncation=True, max_length=512)
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 22 |
+
ai_score = float(probs[1])
|
| 23 |
+
results.append({"sentence": sent, "ai_score": ai_score})
|
| 24 |
+
|
| 25 |
+
# Build highlighted HTML
|
| 26 |
+
highlighted = ""
|
| 27 |
+
for r in results:
|
| 28 |
+
color = f"rgba(255,0,0,{r['ai_score']})" # more red = more AI-like
|
| 29 |
+
highlighted += f"<span style='background-color:{color}; padding:2px'>{r['sentence']} </span>"
|
| 30 |
+
|
| 31 |
+
return highlighted, results
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("## 🤖 AI Detector (like ZeroGPT)")
|
| 35 |
+
gr.Markdown("Paste your text below. Redder highlights = more AI-like.")
|
| 36 |
+
input_text = gr.Textbox(lines=8, placeholder="Enter text here...")
|
| 37 |
+
output_html = gr.HTML()
|
| 38 |
+
output_json = gr.JSON()
|
| 39 |
+
run_btn = gr.Button("Detect AI")
|
| 40 |
+
|
| 41 |
+
run_btn.click(detect_ai, inputs=input_text, outputs=[output_html, output_json])
|
| 42 |
+
|
| 43 |
+
demo.launch()
|