import gradio as gr import os from detector import analyze_text, get_components # Pre-load model print("Starting AI Text Detector...") try: get_components() model_status = "✅ Model loaded successfully!" except Exception as e: model_status = f"⚠️ Model loading issue: {str(e)}" print(model_status) # Custom CSS for better styling css = """ .gradio-container { max-width: 1200px !important; } .result-human { padding: 10px; border-radius: 5px; background: #f0f8f0; border-left: 4px solid #4CAF50; } .result-ai { padding: 10px; border-radius: 5px; background: #fff0f0; border-left: 4px solid #f44336; } .chunk-human { background: #f8fff8; margin: 5px 0; padding: 8px; border-radius: 3px; border-left: 3px solid #4CAF50; } .chunk-ai { background: #fff8f8; margin: 5px 0; padding: 8px; border-radius: 3px; border-left: 3px solid #f44336; } .confidence-high { color: #388E3C; } .confidence-medium { color: #F57C00; } .confidence-low { color: #D32F2F; } """ def analyze_text_interface(text, threshold, chunk_size): """ Interface function for Gradio """ if not text or not text.strip(): return "❌ Please enter some text to analyze.", "", "" try: result = analyze_text(text, threshold=threshold, chunk_size=chunk_size) if "error" in result: return f"❌ Error: {result['error']}", "", "" # Overall result overall_html = f"""

Overall Result: {result['overall_type']}

Confidence: {result['overall_confidence']:.2%}

AI Score: {result['overall_score']:.3f}

AI Artifacts Detected: {'✅ Yes' if result['has_artifacts'] else '❌ No'}

Chunk Analysis: {result['ai_chunks']} AI / {result['human_chunks']} Human

""" # Chunk details chunk_html = "

Detailed Chunk Analysis:

" for i, chunk in enumerate(result['chunks']): confidence_class = "confidence-high" if chunk['confidence'] > 0.8 else "confidence-medium" if chunk['confidence'] > 0.6 else "confidence-low" chunk_html += f"""
Chunk {i+1}: {chunk['type']}
Confidence: {chunk['confidence']:.2%}
Text: "{chunk['text'][:100]}{'...' if len(chunk['text']) > 100 else ''}"
""" # Raw data for download raw_data = { "overall_type": result['overall_type'], "overall_confidence": result['overall_confidence'], "overall_score": result['overall_score'], "has_artifacts": result['has_artifacts'], "chunk_analysis": { "ai_chunks": result['ai_chunks'], "human_chunks": result['human_chunks'], "total_chunks": result['total_chunks'] }, "chunks": result['chunks'] } return overall_html, chunk_html, str(raw_data) except Exception as e: return f"❌ Analysis failed: {str(e)}", "", "" # Example texts examples = [ ["This is a sample text written by a human. It contains natural variations in writing style and occasional imperfections that make it authentic."], ["The aforementioned textual content exhibits characteristics consistent with AI-generated material, including syntactic patterns and lexical choices commonly associated with large language models."], ["Hello world! This is a test. I hope this works correctly. The weather is nice today."] ] # Create Gradio interface with gr.Blocks(css=css, title="AI Text Detector") as demo: gr.Markdown( """ # 🔍 AI Text Detector *Detect AI-generated text using advanced machine learning models* **Model Status:** {} """.format(model_status) ) with gr.Row(): with gr.Column(): text_input = gr.Textbox( label="Input Text", placeholder="Paste or type the text you want to analyze here...", lines=8, max_lines=20 ) with gr.Row(): threshold = gr.Slider( minimum=0.1, maximum=0.9, value=0.5, step=0.05, label="Detection Threshold", info="Higher values = more strict AI detection" ) chunk_size = gr.Slider( minimum=40, maximum=200, value=80, step=10, label="Chunk Size (tokens)", info="Smaller chunks = more detailed analysis" ) analyze_btn = gr.Button("Analyze Text", variant="primary") gr.Examples( examples=examples, inputs=text_input, label="Try these examples:" ) with gr.Column(): overall_output = gr.HTML(label="Overall Result") chunk_output = gr.HTML(label="Chunk Details") raw_output = gr.Textbox( label="Raw Data (for download)", lines=4, max_lines=10 ) # Footer gr.Markdown( """ --- **How it works:** - Text is split into meaningful chunks - Each chunk is analyzed by the AI detection model - Results are aggregated for overall classification - Built with `abhi099k/ai-text-detector-v-n4.0` model **Note:** This tool provides probabilistic estimates and should be used as one of several indicators when evaluating text authenticity. """ ) # Connect the function analyze_btn.click( fn=analyze_text_interface, inputs=[text_input, threshold, chunk_size], outputs=[overall_output, chunk_output, raw_output] ) # Launch the app if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860, share=False )