Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,14 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
from datasets import load_dataset
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
MODEL = "yagnik12/AI_Text_Detecter_HanxiGuo_BiScope-Data"
|
| 7 |
-
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL, use_auth_token=os.getenv("HF_TOKEN"))
|
| 9 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL, use_auth_token=os.getenv("HF_TOKEN"))
|
| 10 |
-
|
| 11 |
-
detector = pipeline("text-classification", model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 12 |
-
|
| 13 |
-
# Load BiScope test samples
|
| 14 |
-
biscope = load_dataset("HanxiGuo/BiScope_Data", split="test[:20]")
|
| 15 |
-
|
| 16 |
-
def detect_ai(text):
|
| 17 |
-
results = detector(text)[0]
|
| 18 |
-
human_score = [r["score"] for r in results if r["label"] in ["LABEL_0", "0"]][0]
|
| 19 |
-
ai_score = [r["score"] for r in results if r["label"] in ["LABEL_1", "1"]][0]
|
| 20 |
-
prediction = "🧑 Human" if human_score > ai_score else "🤖 AI"
|
| 21 |
-
return {
|
| 22 |
-
"Prediction": prediction,
|
| 23 |
-
"Human Probability": round(human_score * 100, 2),
|
| 24 |
-
"AI Probability": round(ai_score * 100, 2),
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
with gr.Blocks() as demo:
|
| 28 |
-
gr.Markdown("# AI vs Human Text Detector (BiScope Dataset)")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
out = gr.JSON()
|
| 33 |
-
btn = gr.Button("Detect")
|
| 34 |
-
btn.click(fn=detect_ai, inputs=inp, outputs=out)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
|
|
|
|
|
|
| 40 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
from datasets import load_dataset
|
| 2 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Load MAGE dataset
|
| 5 |
+
dataset = load_dataset("yaful/MAGE")
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Simple function to display a sample
|
| 8 |
+
def show_sample(index):
|
| 9 |
+
sample = dataset['train'][index]
|
| 10 |
+
return f"Text: {sample['text']}\nLabel: {sample['label']}"
|
| 11 |
|
| 12 |
+
# Create Gradio interface
|
| 13 |
+
demo = gr.Interface(fn=show_sample, inputs=gr.Number(label="Sample Index"), outputs="text")
|
| 14 |
demo.launch()
|