AIHumanizer / app.py
Jay-Rajput's picture
auth humanizer
e683cda
import gradio as gr
import time
import os
# Import our humanizer
from text_humanizer import AITextHumanizer
# Initialize the humanizer
print("πŸš€ Loading AI Text Humanizer for Hugging Face Spaces...")
try:
humanizer = AITextHumanizer()
print("βœ… Humanizer loaded successfully!")
except Exception as e:
print(f"❌ Error loading humanizer: {e}")
humanizer = None
def humanize_text_hf(text, style, intensity):
"""
Hugging Face Spaces interface function for text humanization
"""
if not text.strip():
return "⚠️ Please enter some text to humanize.", "", 0.0, "No changes made", 0.0
if humanizer is None:
return "❌ Error: Humanizer not loaded properly.", "", 0.0, "System error", 0.0
try:
start_time = time.time()
# Humanize the text
result = humanizer.humanize_text(
text=text,
style=style.lower(),
intensity=intensity
)
processing_time = (time.time() - start_time) * 1000
changes_text = ", ".join(result["changes_made"]) if result["changes_made"] else "No significant changes made"
return (
result["humanized_text"],
f"**πŸ“Š Processing Results:**\n- **Similarity Score:** {result['similarity_score']:.3f}\n- **Processing Time:** {processing_time:.1f}ms\n- **Style:** {result['style'].title()}\n- **Intensity:** {result['intensity']}\n\n**πŸ”„ Changes Made:** {changes_text}",
result["similarity_score"],
changes_text,
processing_time
)
except Exception as e:
return f"❌ Error processing text: {str(e)}", "", 0.0, "Processing error", 0.0
# Create the Hugging Face Spaces interface
with gr.Blocks(
title="πŸ€–βž‘οΈπŸ‘€ AI Text Humanizer",
theme=gr.themes.Soft(),
css="""
.main-header {
text-align: center;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
}
.stats-box {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
border-left: 4px solid #667eea;
}
"""
) as iface:
gr.HTML("""
<div class="main-header">
<h1>πŸ€–βž‘οΈπŸ‘€ AI Text Humanizer</h1>
<p>Transform AI-generated text to sound more natural and human-like</p>
<p><em>Powered by advanced NLP techniques and transformers</em></p>
</div>
""")
with gr.Tab("🎯 Humanize Text"):
with gr.Row():
with gr.Column(scale=1):
gr.HTML("<h3>πŸ“ Input</h3>")
input_text = gr.Textbox(
label="Text to Humanize",
placeholder="Paste your AI-generated text here...\n\nExample: Furthermore, it is important to note that artificial intelligence systems demonstrate significant capabilities...",
lines=10,
max_lines=20
)
with gr.Row():
style_dropdown = gr.Dropdown(
choices=["Natural", "Casual", "Conversational"],
value="Natural",
label="🎨 Humanization Style"
)
intensity_slider = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.1,
label="⚑ Intensity Level"
)
humanize_btn = gr.Button(
"πŸš€ Humanize Text",
variant="primary",
size="lg"
)
with gr.Column(scale=1):
gr.HTML("<h3>✨ Output</h3>")
output_text = gr.Textbox(
label="Humanized Text",
lines=10,
max_lines=20,
show_copy_button=True
)
stats_output = gr.Markdown(
label="πŸ“Š Processing Statistics",
value="Results will appear here after processing..."
)
with gr.Tab("πŸ“Š Examples & Guide"):
gr.HTML("<h3>πŸ’‘ Try These Examples</h3>")
# Examples
examples = gr.Examples(
examples=[
[
"Furthermore, it is important to note that artificial intelligence systems demonstrate significant capabilities in natural language processing tasks. Subsequently, these systems can analyze and generate text with remarkable accuracy. Nevertheless, it is crucial to understand that human oversight remains essential for optimal performance.",
"Conversational",
0.8
],
[
"The implementation of this comprehensive solution will facilitate the optimization of business processes and operational workflows. Moreover, it will demonstrate substantial improvements in efficiency metrics while maintaining quality standards.",
"Natural",
0.6
],
[
"In conclusion, the systematic analysis reveals that the proposed methodology demonstrates significant potential for enhancing performance indicators. Additionally, the structured approach ensures optimal resource utilization.",
"Casual",
0.7
]
],
inputs=[input_text, style_dropdown, intensity_slider],
outputs=[output_text, stats_output],
fn=humanize_text_hf,
cache_examples=False
)
gr.HTML("""
<div style="margin-top: 30px;">
<h3>🎯 How It Works</h3>
<div class="stats-box">
<h4>πŸ”§ Transformation Techniques:</h4>
<ul>
<li><strong>Smart Word Replacement:</strong> formal words β†’ casual alternatives</li>
<li><strong>Contraction Addition:</strong> "do not" β†’ "don't", "it is" β†’ "it's"</li>
<li><strong>AI Transition Removal:</strong> removes robotic transition phrases</li>
<li><strong>Sentence Restructuring:</strong> varies length and structure</li>
<li><strong>Natural Imperfections:</strong> adds human-like variations</li>
<li><strong>Context-Aware Paraphrasing:</strong> maintains meaning while improving flow</li>
</ul>
</div>
<div class="stats-box" style="margin-top: 15px;">
<h4>🎨 Style Guide:</h4>
<ul>
<li><strong>Natural (0.5-0.7):</strong> Professional content with human touch</li>
<li><strong>Casual (0.6-0.8):</strong> Blog posts, articles, informal content</li>
<li><strong>Conversational (0.7-1.0):</strong> Social media, very informal text</li>
</ul>
</div>
<div class="stats-box" style="margin-top: 15px;">
<h4>⚑ Performance:</h4>
<ul>
<li><strong>Similarity Preservation:</strong> Maintains 85-95% semantic similarity</li>
<li><strong>Processing Speed:</strong> ~500ms for typical paragraphs</li>
<li><strong>Quality:</strong> Advanced NLP models ensure high-quality output</li>
</ul>
</div>
</div>
""")
# Event handlers
humanize_btn.click(
fn=humanize_text_hf,
inputs=[input_text, style_dropdown, intensity_slider],
outputs=[output_text, stats_output]
)
# Launch for Hugging Face Spaces
if __name__ == "__main__":
print("🌐 Launching AI Text Humanizer on Hugging Face Spaces...")
iface.launch(
share=False, # HF Spaces handles sharing
server_name="0.0.0.0",
server_port=7860,
show_error=True
)