Spaces:
Running
Running
Dan Pro
commited on
Commit
·
63001f3
1
Parent(s):
39451ca
Initial AI detector app
Browse files- app.py +42 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_name = "Hello-SimpleAI/chatgpt-detector-roberta"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def detect_ai_text(text):
|
| 11 |
+
if not text or len(text.strip()) == 0:
|
| 12 |
+
return {"error": "Please provide text to analyze"}
|
| 13 |
+
|
| 14 |
+
# Tokenize
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
|
| 16 |
+
|
| 17 |
+
# Get prediction
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
logits = outputs.logits
|
| 21 |
+
probs = torch.softmax(logits, dim=-1)
|
| 22 |
+
|
| 23 |
+
# Get probabilities
|
| 24 |
+
human_prob = probs[0][0].item()
|
| 25 |
+
ai_prob = probs[0][1].item()
|
| 26 |
+
|
| 27 |
+
return {
|
| 28 |
+
"Human": f"{human_prob * 100:.2f}%",
|
| 29 |
+
"AI Generated": f"{ai_prob * 100:.2f}%"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=detect_ai_text,
|
| 35 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter text to analyze..."),
|
| 36 |
+
outputs=gr.JSON(label="Detection Results"),
|
| 37 |
+
title="AI Text Detector",
|
| 38 |
+
description="Detects whether text was written by a human or generated by AI"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
transformers==4.36.0
|
| 3 |
+
torch==2.1.0
|
| 4 |
+
sentencepiece==0.1.99
|
| 5 |
+
protobuf==3.20.3
|