Spaces:
Sleeping
Sleeping
Boukabous
commited on
Commit
·
cf08f42
1
Parent(s):
aa4ad5f
first commit
Browse files- Makefile +14 -0
- app.py +42 -0
- requirements.txt +7 -0
- test_app.py +34 -0
Makefile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
install:
|
2 |
+
pip install --upgrade pip &&\
|
3 |
+
pip install -r requirements.txt
|
4 |
+
|
5 |
+
test:
|
6 |
+
python -m pytest -vv --cov=app test_app.py
|
7 |
+
|
8 |
+
format:
|
9 |
+
black *.py
|
10 |
+
|
11 |
+
lint:
|
12 |
+
pylint --disable=R,C app.py
|
13 |
+
|
14 |
+
all: install lint test format
|
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
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(
|
15 |
+
"text-classification",
|
16 |
+
model=model,
|
17 |
+
tokenizer=tokenizer,
|
18 |
+
device=0 if torch.cuda.is_available() else -1,
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
# Define function for real-time AI text detection
|
23 |
+
def predict_ai_text(input_text):
|
24 |
+
result = classifier(input_text)
|
25 |
+
label = "AI-Generated" if result[0]["label"] == "LABEL_1" else "Human-Written"
|
26 |
+
confidence = np.round(result[0]["score"], 3)
|
27 |
+
return f"{label} (Confidence: {confidence})"
|
28 |
+
|
29 |
+
|
30 |
+
# Create Gradio interface
|
31 |
+
app = gr.Interface(
|
32 |
+
fn=predict_ai_text,
|
33 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter your text here..."),
|
34 |
+
outputs=gr.Textbox(),
|
35 |
+
title="AI Text Detector",
|
36 |
+
description="Detect whether a given text is AI-generated or human-written.",
|
37 |
+
allow_flagging="never",
|
38 |
+
)
|
39 |
+
|
40 |
+
# Launch app
|
41 |
+
if __name__ == "__main__":
|
42 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
pytest
|
3 |
+
pytest-cov
|
4 |
+
transformers
|
5 |
+
torch
|
6 |
+
gradio
|
7 |
+
accelerate
|
test_app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
from app import predict_ai_text
|
3 |
+
|
4 |
+
|
5 |
+
def test_basic_ai_detection():
|
6 |
+
# Simple test input
|
7 |
+
text = "This article explores the evolution of artificial intelligence in modern society."
|
8 |
+
|
9 |
+
result = predict_ai_text(text)
|
10 |
+
|
11 |
+
# Check it's a string and includes expected keywords
|
12 |
+
assert isinstance(result, str)
|
13 |
+
assert "Confidence" in result
|
14 |
+
assert any(label in result for label in ["AI-Generated", "Human-Written"])
|
15 |
+
|
16 |
+
|
17 |
+
def test_empty_input():
|
18 |
+
result = predict_ai_text("")
|
19 |
+
assert isinstance(result, str)
|
20 |
+
assert "Confidence" in result
|
21 |
+
|
22 |
+
|
23 |
+
@pytest.mark.parametrize(
|
24 |
+
"text",
|
25 |
+
[
|
26 |
+
"The quick brown fox jumps over the lazy dog.",
|
27 |
+
"As technology advances, so does our understanding of machine learning.",
|
28 |
+
"Hello world!",
|
29 |
+
],
|
30 |
+
)
|
31 |
+
def test_multiple_inputs(text):
|
32 |
+
result = predict_ai_text(text)
|
33 |
+
assert isinstance(result, str)
|
34 |
+
assert "Confidence" in result
|