Spaces:
Runtime error
Runtime error
import gradio as gr | |
from MMD_calculate import MMDMPDetector | |
detector = MMDMPDetector() # Initialize your MMD-MP detector | |
MINIMUM_TOKENS = 64 # Minimum number of tokens for detection | |
def count_tokens(text): | |
return len(text.split()) # Count the number of tokens (words) in the text | |
def run_detector(input_text): | |
# Check if input meets the token requirement | |
if count_tokens(input_text) < MINIMUM_TOKENS: | |
return f"Error: Text is too short! At least {MINIMUM_TOKENS} tokens are required." | |
# Perform detection (replace this with your model's prediction logic) | |
prediction = detector.predict(input_text) | |
return f"Result: {prediction}" | |
def change_mode(mode): | |
if mode == "Low False Positive Rate": | |
detector.set_mode("low-fpr") # Adjust detector mode | |
elif mode == "High Accuracy": | |
detector.set_mode("accuracy") | |
return f"Mode set to: {mode}" | |
css = """ | |
.green { color: black!important; line-height:1.9em; padding: 0.2em 0.2em; background: #ccffcc; border-radius:0.5rem;} | |
.red { color: black!important; line-height:1.9em; padding: 0.2em 0.2em; background: #ffad99; border-radius:0.5rem;} | |
.hyperlinks { | |
display: flex; | |
align-items: center; | |
justify-content: flex-end; | |
padding: 12px; | |
margin: 0 10px; | |
text-decoration: none; | |
color: #000; | |
} | |
""" | |
with gr.Blocks(css=css, theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as app: | |
# Header Row | |
with gr.Row(): | |
with gr.Column(scale=3): | |
gr.HTML("<h1>Binoculars: Zero-Shot LLM-Text Detector</h1>") | |
with gr.Column(scale=1): | |
gr.HTML(""" | |
<p class="hyperlinks"> | |
<a href="https://arxiv.org/abs/2401.12070" target="_blank">Paper</a> | | |
<a href="https://github.com/AHans30/Binoculars" target="_blank">Code</a> | | |
<a href="mailto:[email protected]" target="_blank">Contact</a> | |
</p> | |
""") | |
# Input Section | |
with gr.Row(): | |
input_text = gr.Textbox(placeholder="Enter text here...", lines=8, label="Input Text") | |
# Mode Selector and Buttons | |
with gr.Row(): | |
mode_selector = gr.Dropdown( | |
choices=["Low False Positive Rate", "High Accuracy"], | |
label="Detection Mode", | |
value="Low False Positive Rate" | |
) | |
submit_button = gr.Button("Run Binoculars", variant="primary") | |
clear_button = gr.Button("Clear") | |
# Output Section | |
with gr.Row(): | |
output_text = gr.Textbox(label="Prediction", value="Results will appear here...") | |
# Disclaimer Section | |
with gr.Accordion("Disclaimer", open=False): | |
gr.Markdown(""" | |
- **Accuracy**: This detector uses state-of-the-art techniques, but no model is perfect. | |
- **Mode Information**: | |
- High Accuracy: Maximizes accuracy by adjusting thresholds. | |
- Low False Positive Rate: Reduces human-written text being falsely flagged as AI-generated. | |
- **Limitations**: Detection is best on texts with 64–300 tokens. Very short or extremely long texts may lead to inaccurate results. | |
""") | |
# Bind Functions to Buttons | |
submit_button.click(run_detector, inputs=input_text, outputs=output_text) | |
clear_button.click(lambda: ("", ""), outputs=[input_text, output_text]) | |
mode_selector.change(change_mode, inputs=mode_selector, outputs=mode_selector) |