import gradio as gr from detector import CustomDetector import logging # Set up logging to capture detailed errors logging.basicConfig(level=logging.ERROR) logger = logging.getLogger(__name__) # Initialize the detector try: detector = CustomDetector() logger.info("Detector initialized successfully") except Exception as e: logger.error(f"Failed to initialize detector: {str(e)}") raise RuntimeError(f"Failed to initialize detector: {str(e)}") def detect_text(text): """ Compute the AI-generated text score for the input text. Returns a string with the score and classification (AI-generated or human-generated). """ if not text.strip(): return "Please enter some text." try: logger.info(f"Processing text: {text[:50]}...") score = detector.my_detector([text])[0] # Process single text classification = "AI-generated" if score > 0.2 else "human-generated" interpretation = ( f"Score: {score:.4f}\n" f"Classification: {classification}\n" "Note: Scores above 0.2 indicate AI-generated text; scores below 0.2 indicate human-generated text." ) logger.info(f"Result: {interpretation}") return interpretation except Exception as e: error_msg = f"Error: {str(e)}" logger.error(error_msg) return error_msg # Minimal CSS for a clean, professional look custom_css = """ /* General styling */ body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; background: #f5f7fa; color: #1a1a1a; line-height: 1.6; } /* Flexible container */ .gradio-container { max-width: 95%; width: 600px; margin: 1rem auto; padding: 1rem; background: #ffffff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); } /* Title */ h1 { font-size: 2rem; font-weight: 600; text-align: center; color: #2c3e50; margin-bottom: 1rem; } /* Input textbox */ textarea { border: 1px solid #d1d5db !important; border-radius: 6px !important; padding: 0.75rem !important; font-size: 1rem !important; } /* Output textbox */ .output-text { background: #f9fafb !important; border-radius: 6px !important; padding: 1rem !important; font-size: 1rem !important; border: 1px solid #d1d5db !important; } /* Button */ button { background: #4b5563 !important; color: white !important; border: none !important; padding: 0.5rem 1rem !important; border-radius: 6px !important; font-weight: 500 !important; } /* Accordion */ .gr-accordion { margin-top: 1rem; border: 1px solid #d1d5db; border-radius: 6px; } /* Responsive design */ @media (max-width: 600px) { .gradio-container { margin: 0.5rem; padding: 0.5rem; width: 98%; } h1 { font-size: 1.6rem; } } """ # Citation for the expandable tab citation_markdown = """ ## Citation Please cite our work as: **Zero-Shot Statistical Tests for LLM-Generated Text Detection using Finite Sample Concentration Inequalities** Tara Radvand, Mojtaba Abdolmaleki, Mohamed Mostagir, Ambuj Tewari [arXiv:2501.02406](https://arxiv.org/abs/2501.02406) Year: 2025 """ # Set up the Gradio interface with gr.Blocks(css=custom_css, theme=None) as iface: gr.Markdown("# AI-Generated Text Detector") gr.Markdown("Enter text to detect if it was generated by an AI model. Powered by a custom detector using tiiuae/falcon-rw-1b.") gr.Textbox( lines=5, placeholder="Enter text here to check if it's AI-generated...", label="Input Text" ) output = gr.Textbox(label="Detection Result") gr.Button("Detect").click( fn=detect_text, inputs=gr.Textbox(), outputs=output ) gr.Accordion("Citation", open=False).markdown(citation_markdown) # Launch the app if __name__ == "__main__": iface.launch()