Latest-app / app.py
abhi099k's picture
Update app.py
bd87524 verified
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)
def analyze_text_interface(text, threshold):
"""
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=80)
if "error" in result:
return f"❌ Error: {result['error']}", ""
# Overall result - with proper text colors for dark background
overall_html = f"""
<div style="padding: 20px; border-radius: 10px; background: #2d3748; border: 1px solid #4a5568; color: white;">
<h2 style="color: white; margin-top: 0;">Analysis Result: {result['overall_type']}</h2>
<p style="color: white;"><strong>Confidence:</strong> {result['overall_confidence']:.2%}</p>
<p style="color: white;"><strong>AI Probability Score:</strong> {result['overall_score']:.3f}</p>
<p style="color: white;"><strong>AI Artifacts Detected:</strong> {'Yes' if result['has_artifacts'] else 'No'}</p>
</div>
"""
# 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'],
"text_length": len(text)
}
return overall_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(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
)
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"
)
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="Analysis Result")
raw_output = gr.Textbox(
label="Raw Data (for download)",
lines=4,
max_lines=10
)
# Footer
gr.Markdown(
"""
---
**How it works:**
- Text is analyzed by the AI detection model
- Returns overall classification (Human/AI) with confidence score
- 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],
outputs=[overall_output, raw_output]
)
# Launch the app
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)