jer233 commited on
Commit
35af015
·
verified ·
1 Parent(s): 478b3e9

Create demo.py

Browse files
Files changed (1) hide show
  1. demo/demo.py +86 -0
demo/demo.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from MMD_calculate import MMDMPDetector
4
+
5
+ detector = MMDMPDetector() # Initialize your MMD-MP detector
6
+ MINIMUM_TOKENS = 64 # Minimum number of tokens for detection
7
+
8
+ def count_tokens(text):
9
+ return len(text.split()) # Count the number of tokens (words) in the text
10
+
11
+ def run_detector(input_text):
12
+ # Check if input meets the token requirement
13
+ if count_tokens(input_text) < MINIMUM_TOKENS:
14
+ return f"Error: Text is too short! At least {MINIMUM_TOKENS} tokens are required."
15
+
16
+ # Perform detection (replace this with your model's prediction logic)
17
+ prediction = detector.predict(input_text)
18
+ return f"Result: {prediction}"
19
+
20
+ def change_mode(mode):
21
+ if mode == "Low False Positive Rate":
22
+ detector.set_mode("low-fpr") # Adjust detector mode
23
+ elif mode == "High Accuracy":
24
+ detector.set_mode("accuracy")
25
+ return f"Mode set to: {mode}"
26
+
27
+ css = """
28
+ .green { color: black!important; line-height:1.9em; padding: 0.2em 0.2em; background: #ccffcc; border-radius:0.5rem;}
29
+ .red { color: black!important; line-height:1.9em; padding: 0.2em 0.2em; background: #ffad99; border-radius:0.5rem;}
30
+ .hyperlinks {
31
+ display: flex;
32
+ align-items: center;
33
+ justify-content: flex-end;
34
+ padding: 12px;
35
+ margin: 0 10px;
36
+ text-decoration: none;
37
+ color: #000;
38
+ }
39
+ """
40
+
41
+ with gr.Blocks(css=css, theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as app:
42
+ # Header Row
43
+ with gr.Row():
44
+ with gr.Column(scale=3):
45
+ gr.HTML("<h1>Binoculars: Zero-Shot LLM-Text Detector</h1>")
46
+ with gr.Column(scale=1):
47
+ gr.HTML("""
48
+ <p class="hyperlinks">
49
+ <a href="https://arxiv.org/abs/2401.12070" target="_blank">Paper</a> |
50
+ <a href="https://github.com/AHans30/Binoculars" target="_blank">Code</a> |
51
+ <a href="mailto:[email protected]" target="_blank">Contact</a>
52
+ </p>
53
+ """)
54
+
55
+ # Input Section
56
+ with gr.Row():
57
+ input_text = gr.Textbox(placeholder="Enter text here...", lines=8, label="Input Text")
58
+
59
+ # Mode Selector and Buttons
60
+ with gr.Row():
61
+ mode_selector = gr.Dropdown(
62
+ choices=["Low False Positive Rate", "High Accuracy"],
63
+ label="Detection Mode",
64
+ value="Low False Positive Rate"
65
+ )
66
+ submit_button = gr.Button("Run Binoculars", variant="primary")
67
+ clear_button = gr.Button("Clear")
68
+
69
+ # Output Section
70
+ with gr.Row():
71
+ output_text = gr.Textbox(label="Prediction", value="Results will appear here...")
72
+
73
+ # Disclaimer Section
74
+ with gr.Accordion("Disclaimer", open=False):
75
+ gr.Markdown("""
76
+ - **Accuracy**: This detector uses state-of-the-art techniques, but no model is perfect.
77
+ - **Mode Information**:
78
+ - High Accuracy: Maximizes accuracy by adjusting thresholds.
79
+ - Low False Positive Rate: Reduces human-written text being falsely flagged as AI-generated.
80
+ - **Limitations**: Detection is best on texts with 64–300 tokens. Very short or extremely long texts may lead to inaccurate results.
81
+ """)
82
+
83
+ # Bind Functions to Buttons
84
+ submit_button.click(run_detector, inputs=input_text, outputs=output_text)
85
+ clear_button.click(lambda: ("", ""), outputs=[input_text, output_text])
86
+ mode_selector.change(change_mode, inputs=mode_selector, outputs=mode_selector)