Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
import math
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
detector_name = "Hello-SimpleAI/chatgpt-detector-roberta"
|
| 8 |
detector_tokenizer = AutoTokenizer.from_pretrained(detector_name)
|
| 9 |
detector_model = AutoModelForSequenceClassification.from_pretrained(detector_name)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
gpt2_name = "gpt2"
|
| 13 |
gpt2_tokenizer = AutoTokenizer.from_pretrained(gpt2_name)
|
| 14 |
gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_name)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def compute_perplexity(text: str) -> float:
|
| 17 |
"""Calculate perplexity using GPT-2 language model."""
|
| 18 |
enc = gpt2_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
|
@@ -48,6 +57,11 @@ def analyze_text(user_text: str):
|
|
| 48 |
"Final Human Probability (avg)": round(final_human * 100, 2),
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
gr.Markdown("# 🔍 Free AI vs Human Text Detector (Demo)")
|
| 53 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, GPT2LMHeadModel
|
| 3 |
import torch
|
| 4 |
import math
|
| 5 |
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# Load Models
|
| 8 |
+
# -------------------------------
|
| 9 |
+
|
| 10 |
+
# ChatGPT detector model (RoBERTa)
|
| 11 |
detector_name = "Hello-SimpleAI/chatgpt-detector-roberta"
|
| 12 |
detector_tokenizer = AutoTokenizer.from_pretrained(detector_name)
|
| 13 |
detector_model = AutoModelForSequenceClassification.from_pretrained(detector_name)
|
| 14 |
|
| 15 |
+
# GPT-2 for perplexity calculation
|
| 16 |
gpt2_name = "gpt2"
|
| 17 |
gpt2_tokenizer = AutoTokenizer.from_pretrained(gpt2_name)
|
| 18 |
gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_name)
|
| 19 |
|
| 20 |
+
|
| 21 |
+
# -------------------------------
|
| 22 |
+
# Helper Functions
|
| 23 |
+
# -------------------------------
|
| 24 |
+
|
| 25 |
def compute_perplexity(text: str) -> float:
|
| 26 |
"""Calculate perplexity using GPT-2 language model."""
|
| 27 |
enc = gpt2_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
|
|
|
| 57 |
"Final Human Probability (avg)": round(final_human * 100, 2),
|
| 58 |
}
|
| 59 |
|
| 60 |
+
|
| 61 |
+
# -------------------------------
|
| 62 |
+
# Gradio UI
|
| 63 |
+
# -------------------------------
|
| 64 |
+
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("# 🔍 Free AI vs Human Text Detector (Demo)")
|
| 67 |
|