Nortix commited on
Commit
91c441c
·
verified ·
1 Parent(s): dee1ff7

AI_text_detector

Browse files

Detect AI-generated text using a Hugging Face model. This tool analyzes input and returns how likely it was written by AI vs a human. Built with Gradio.

Files changed (1) hide show
  1. AI_text_detector +30 -0
AI_text_detector ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+
5
+ # Load model and tokenizer
6
+ model_name = "roberta-base-openai-detector"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ def detect_ai(text):
11
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
12
+ with torch.no_grad():
13
+ outputs = model(**inputs)
14
+ logits = outputs.logits
15
+ probs = torch.softmax(logits, dim=1)
16
+ ai_score = probs[0][1].item() * 100
17
+ human_score = probs[0][0].item() * 100
18
+
19
+ result = f"AI-generated: {ai_score:.2f}%\\nHuman-written: {human_score:.2f}%"
20
+ return result
21
+
22
+ iface = gr.Interface(
23
+ fn=detect_ai,
24
+ inputs=gr.Textbox(label="Enter text to analyze", lines=10),
25
+ outputs=gr.Textbox(label="Detection Result"),
26
+ title="AI Detector",
27
+ description="Detect if a piece of text was written by AI or a human using a Hugging Face model."
28
+ )
29
+
30
+ iface.launch()