Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
+
|
| 4 |
+
# 1. Load model & tokenizer
|
| 5 |
+
MODEL = "microsoft/deberta-v3-small" # you can fine-tune later on BiScope_Data
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL, num_labels=2)
|
| 8 |
+
|
| 9 |
+
# 2. Build pipeline
|
| 10 |
+
detector = pipeline("text-classification", model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 11 |
+
|
| 12 |
+
# 3. Detection function
|
| 13 |
+
def detect_ai(text):
|
| 14 |
+
results = detector(text)[0]
|
| 15 |
+
# Assuming label 0 = Human, 1 = AI
|
| 16 |
+
human_score = [r["score"] for r in results if r["label"] in ["LABEL_0", "0"]][0]
|
| 17 |
+
ai_score = [r["score"] for r in results if r["label"] in ["LABEL_1", "1"]][0]
|
| 18 |
+
|
| 19 |
+
prediction = "🧑 Human" if human_score > ai_score else "🤖 AI"
|
| 20 |
+
return {
|
| 21 |
+
"Prediction": prediction,
|
| 22 |
+
"Human Probability": round(human_score * 100, 2),
|
| 23 |
+
"AI Probability": round(ai_score * 100, 2)
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# 4. Gradio UI
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=detect_ai,
|
| 29 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
|
| 30 |
+
outputs="json",
|
| 31 |
+
title="AI vs Human Text Detector",
|
| 32 |
+
description="Demo AI text detection using Hugging Face Transformers.\n"
|
| 33 |
+
"Trained/Fine-tuned models can be swapped in for better accuracy on BiScope_Data."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
demo.launch()
|