Commit
·
0eb4b43
1
Parent(s):
91036d0
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
5 |
+
|
6 |
+
# Load fine-tuned model and tokenizer from Hugging Face Hub
|
7 |
+
model_name = "AICodexLab/answerdotai-ModernBERT-base-ai-detector"
|
8 |
+
|
9 |
+
# Load model and tokenizer
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# Use pipeline for text classification
|
14 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
15 |
+
|
16 |
+
# Define function for real-time AI text detection
|
17 |
+
def predict_ai_text(input_text):
|
18 |
+
result = classifier(input_text)
|
19 |
+
label = "AI-Generated" if result[0]["label"] == "LABEL_1" else "Human-Written"
|
20 |
+
confidence = np.round(result[0]["score"], 3)
|
21 |
+
return f"{label} (Confidence: {confidence})"
|
22 |
+
|
23 |
+
# Create Gradio interface
|
24 |
+
app = gr.Interface(
|
25 |
+
fn=predict_ai_text,
|
26 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter your text here..."),
|
27 |
+
outputs=gr.Textbox(),
|
28 |
+
title="AI Text Detector",
|
29 |
+
description="Detect whether a given text is AI-generated or human-written.",
|
30 |
+
allow_flagging="never"
|
31 |
+
)
|
32 |
+
|
33 |
+
# Launch app
|
34 |
+
if __name__ == "__main__":
|
35 |
+
app.launch()
|