ii5's picture
Update app.py
5afc33c verified
"""
Simple AI Text Humanizer using Gradio
A clean text-to-text interface for humanizing AI-generated content.
"""
import gradio as gr
import time
from typing import Optional
from transformer.app import AdvancedAcademicTextHumanizer, download_nltk_resources
# Global humanizer instance
humanizer_instance = None
def initialize_humanizer():
"""Initialize the humanizer model."""
global humanizer_instance
if humanizer_instance is None:
try:
print("🔄 Downloading NLTK resources...")
# Download NLTK resources
download_nltk_resources()
print("🔄 Initializing lightweight models...")
# Initialize humanizer with lightweight, fast settings
humanizer_instance = AdvancedAcademicTextHumanizer(
sentence_model="fast", # Uses all-MiniLM-L6-v2 (lightweight)
paraphrase_model="fast", # Uses t5-small (fast)
enable_advanced_models=True,
ai_avoidance_mode=True
)
print("✅ All models loaded successfully and ready!")
return "✅ Models loaded successfully"
except Exception as e:
error_msg = f"❌ Error loading models: {str(e)}"
print(error_msg)
return error_msg
return "✅ Models already loaded"
def humanize_text(input_text: str, use_passive: bool, use_synonyms: bool, use_paraphrasing: bool) -> str:
"""Transform AI text to human-like text."""
if not input_text.strip():
return "Please enter some text to transform."
global humanizer_instance
if humanizer_instance is None:
init_result = initialize_humanizer()
if "Error" in init_result:
return init_result
try:
# Transform the text
transformed = humanizer_instance.humanize_text(
input_text,
use_passive=use_passive,
use_synonyms=use_synonyms,
use_paraphrasing=use_paraphrasing
)
return transformed
except Exception as e:
return f"❌ Error during transformation: {str(e)}"
def create_interface():
"""Create the Gradio interface."""
with gr.Blocks(title="AI Text Humanizer", theme=gr.themes.Soft()) as interface:
gr.Markdown("# 🤖➡️🧔🏻‍♂️ AI Text Humanizer")
gr.Markdown("Transform AI-generated text into human-like content using advanced ML models.")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Text",
placeholder="Paste your AI-generated text here...",
lines=10,
max_lines=20
)
with gr.Row():
use_passive = gr.Checkbox(
label="Passive Voice Transformation",
value=False,
info="Convert active voice to passive"
)
use_synonyms = gr.Checkbox(
label="Synonym Replacement",
value=True,
info="AI-powered contextual synonyms"
)
use_paraphrasing = gr.Checkbox(
label="Neural Paraphrasing",
value=True,
info="T5 sentence-level rewriting"
)
transform_btn = gr.Button("🚀 Transform Text", variant="primary")
with gr.Column():
output_text = gr.Textbox(
label="Transformed Text",
lines=10,
max_lines=20,
interactive=False
)
# Initialize models on startup
gr.Markdown("### Model Status")
status_text = gr.Textbox(
label="Initialization Status",
value="Click 'Transform Text' to load models...",
interactive=False
)
# Connect the transformation function
transform_btn.click(
fn=humanize_text,
inputs=[input_text, use_passive, use_synonyms, use_paraphrasing],
outputs=output_text
)
# Initialize models when interface loads
interface.load(
fn=initialize_humanizer,
outputs=status_text
)
gr.Markdown("---")
gr.Markdown("**Note:** First-time model loading may take a few moments.")
return interface
def main():
"""Launch the Gradio interface."""
interface = create_interface()
# Launch with Mac-optimized settings
interface.launch(
show_api=False,
share=False,
debug=False,
show_error=True
)
if __name__ == "__main__":
main()