Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- LICENSE.txt +21 -0
- README.md +30 -14
- app.py +94 -0
- requirements.txt +5 -0
LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Zhihui Chen
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,14 +1,30 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: DivScore
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: DivScore AI Text Detector
|
| 3 |
+
emoji: 🔍
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.19.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# DivScore AI Text Detector
|
| 13 |
+
|
| 14 |
+
This Space demonstrates the DivScore model for detecting AI-generated text. The model uses a novel zero-shot detection framework that leverages normalized entropy-based scoring to identify AI-generated content.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
- Real-time AI text detection
|
| 18 |
+
- Detailed analysis scores (DivScore, Entropy, CE)
|
| 19 |
+
- Confidence level indication
|
| 20 |
+
- Easy-to-use interface
|
| 21 |
+
|
| 22 |
+
## How to Use
|
| 23 |
+
1. Enter or paste your text in the input box
|
| 24 |
+
2. Click "Submit" to analyze
|
| 25 |
+
3. View the results showing whether the text is likely AI-generated
|
| 26 |
+
|
| 27 |
+
## Technical Details
|
| 28 |
+
- Based on Mistral-7B model
|
| 29 |
+
- Uses DivScore detection framework
|
| 30 |
+
- Threshold: 0.15 (scores below this are likely AI-generated)
|
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from divscore import DivScore
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set environment variables for Hugging Face
|
| 7 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 8 |
+
|
| 9 |
+
# Initialize the DivScore detector with loading state
|
| 10 |
+
def load_model():
|
| 11 |
+
try:
|
| 12 |
+
detector = DivScore(
|
| 13 |
+
generalLM_name_or_path="mistralai/Mistral-7B-v0.2",
|
| 14 |
+
enhancedLM_name_or_path="RichardChenZH/DivScore_combined",
|
| 15 |
+
device="cuda:0" if torch.cuda.is_available() else "cpu",
|
| 16 |
+
use_bfloat16=True # Use bfloat16 for better memory efficiency
|
| 17 |
+
)
|
| 18 |
+
return detector
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Error loading model: {str(e)}")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
# Global variable for the detector
|
| 24 |
+
detector = None
|
| 25 |
+
|
| 26 |
+
def detect_ai_text(text):
|
| 27 |
+
"""
|
| 28 |
+
Detect if the input text is AI-generated using DivScore.
|
| 29 |
+
Returns a tuple of (score, is_ai_generated, confidence)
|
| 30 |
+
"""
|
| 31 |
+
global detector
|
| 32 |
+
|
| 33 |
+
# Initialize detector if not already done
|
| 34 |
+
if detector is None:
|
| 35 |
+
detector = load_model()
|
| 36 |
+
if detector is None:
|
| 37 |
+
return "Error: Failed to load the model. Please try again later.", False, 0.0
|
| 38 |
+
|
| 39 |
+
if not text.strip():
|
| 40 |
+
return "Please enter some text to analyze.", False, 0.0
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
score, entropy_score, ce_score = detector.compute_score(text)
|
| 44 |
+
|
| 45 |
+
# Based on the paper's findings, we use 0.15 as the threshold
|
| 46 |
+
is_ai_generated = score < 0.15
|
| 47 |
+
|
| 48 |
+
result = f"DivScore: {score:.4f}\nEntropy Score: {entropy_score:.4f}\nCE Score: {ce_score:.4f}"
|
| 49 |
+
return result, is_ai_generated
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error occurred: {str(e)}", False, 0.0
|
| 53 |
+
|
| 54 |
+
# Create the Gradio interface with loading state
|
| 55 |
+
with gr.Blocks(title="DivScore AI Text Detector") as demo:
|
| 56 |
+
gr.Markdown("""
|
| 57 |
+
# DivScore AI Text Detector
|
| 58 |
+
|
| 59 |
+
This demo uses the DivScore model to detect if text was generated by an AI model.
|
| 60 |
+
Enter your text below to analyze it.
|
| 61 |
+
|
| 62 |
+
**Note:** The model may take a few moments to load on first use.
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
+
text_input = gr.Textbox(
|
| 68 |
+
label="Input Text",
|
| 69 |
+
placeholder="Enter text to analyze...",
|
| 70 |
+
lines=5
|
| 71 |
+
)
|
| 72 |
+
submit_btn = gr.Button("Analyze Text")
|
| 73 |
+
|
| 74 |
+
with gr.Column():
|
| 75 |
+
result_output = gr.Textbox(label="Analysis Results")
|
| 76 |
+
ai_generated = gr.Checkbox(label="AI Generated", interactive=False)
|
| 77 |
+
|
| 78 |
+
gr.Examples(
|
| 79 |
+
examples=[
|
| 80 |
+
["The quick brown fox jumps over the lazy dog."],
|
| 81 |
+
["Based on the analysis of the data, we can conclude that the implementation of the new protocol has resulted in a statistically significant improvement in patient outcomes."]
|
| 82 |
+
],
|
| 83 |
+
inputs=text_input
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
submit_btn.click(
|
| 87 |
+
fn=detect_ai_text,
|
| 88 |
+
inputs=text_input,
|
| 89 |
+
outputs=[result_output, ai_generated]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
demo.queue() # Enable queuing for better handling of multiple requests
|
| 94 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
transformers>=4.30.0
|
| 4 |
+
peft>=0.4.0
|
| 5 |
+
accelerate>=0.20.0
|