Spaces:
Running
Running
| # Professional AI Text Humanizer for Hugging Face Spaces | |
| # Clean, Structure-Preserving, Error-Free Humanization | |
| import gradio as gr | |
| import time | |
| import os | |
| import sys | |
| # Import our professional humanizer | |
| from professional_humanizer import ProfessionalAITextHumanizer | |
| # Global variables | |
| humanizer = None | |
| initialization_status = {} | |
| def initialize_professional_humanizer(): | |
| """Initialize the professional humanizer""" | |
| global humanizer, initialization_status | |
| print("π― Initializing Professional AI Text Humanizer...") | |
| print("π’ Clean, Structure-Preserving, Error-Free Processing") | |
| try: | |
| # Initialize with professional settings | |
| humanizer = ProfessionalAITextHumanizer( | |
| enable_gpu=True, | |
| preserve_structure=True # Key feature for structure preservation | |
| ) | |
| initialization_status = { | |
| "humanizer_loaded": True, | |
| "advanced_similarity": humanizer.similarity_model is not None, | |
| "professional_paraphrasing": humanizer.paraphraser is not None, | |
| "tfidf_fallback": humanizer.tfidf_vectorizer is not None, | |
| "structure_preservation": humanizer.preserve_structure, | |
| "error_free_processing": True, | |
| "professional_quality": True, | |
| "total_features": 7, | |
| "enabled_features": sum([ | |
| bool(humanizer.similarity_model), | |
| bool(humanizer.paraphraser), | |
| bool(humanizer.tfidf_vectorizer), | |
| humanizer.preserve_structure, | |
| True, # Professional mappings | |
| True, # Error-free processing | |
| True # Quality control | |
| ]) | |
| } | |
| print("β Professional humanizer initialized successfully!") | |
| print(f"π― Professional completeness: {(initialization_status['enabled_features']/initialization_status['total_features'])*100:.1f}%") | |
| return True | |
| except Exception as e: | |
| print(f"β Error initializing professional humanizer: {e}") | |
| initialization_status = {"error": str(e), "humanizer_loaded": False} | |
| return False | |
| def humanize_text_professional_hf(text, style, intensity, bypass_detection, preserve_structure, quality_threshold, show_advanced_metrics=True): | |
| """ | |
| Professional humanization interface for HF Spaces | |
| """ | |
| if not text.strip(): | |
| return "β οΈ Please enter some text to humanize.", "", "", "" | |
| if humanizer is None: | |
| return "β Error: Professional humanizer not loaded. Please refresh the page.", "", "", "" | |
| try: | |
| start_time = time.time() | |
| # Use professional humanization | |
| result = humanizer.humanize_text_professional( | |
| text=text, | |
| style=style.lower(), | |
| intensity=intensity, | |
| bypass_detection=bypass_detection, | |
| preserve_meaning=True, | |
| quality_threshold=quality_threshold | |
| ) | |
| processing_time = (time.time() - start_time) * 1000 | |
| # Format main results | |
| main_stats = f"""**π― Professional Results:** | |
| - **Quality Score**: {result['similarity_score']:.3f} (Meaning preservation) | |
| - **Detection Evasion**: {result['detection_evasion_score']:.3f} (Bypass likelihood) | |
| - **Structure Preserved**: {'β YES' if result['structure_preserved'] else 'β NO'} | |
| - **Error-Free**: {'β YES' if result['quality_metrics'].get('error_free', True) else 'β NO'} | |
| - **Processing Time**: {processing_time:.1f}ms | |
| - **Style**: {result.get('style', style).title()} | |
| - **Intensity**: {intensity}""" | |
| # Format advanced metrics | |
| advanced_stats = f"""**π Technical Metrics:** | |
| - **Perplexity Score**: {result['perplexity_score']:.3f} (Higher = More natural) | |
| - **Burstiness Score**: {result['burstiness_score']:.3f} (Higher = More varied) | |
| - **Word Count Change**: {result['quality_metrics'].get('word_count_change', 0)} | |
| - **Character Count Change**: {result['quality_metrics'].get('character_count_change', 0)} | |
| - **Sentence Count**: {result['quality_metrics'].get('sentence_count', 0)} | |
| **π§ Professional Transformations Applied:** | |
| {chr(10).join([f'β’ {change}' for change in result['changes_made']]) if result['changes_made'] else 'β’ No changes needed - already optimal'}""" | |
| # Format feature status | |
| feature_status = f"""**π’ Professional Features Status:** | |
| - Structure Preservation: {'β ACTIVE' if initialization_status.get('structure_preservation') else 'β INACTIVE'} | |
| - Error-Free Processing: {'β ACTIVE' if initialization_status.get('error_free_processing') else 'β INACTIVE'} | |
| - Advanced Similarity: {'β ENABLED' if initialization_status.get('advanced_similarity') else 'β DISABLED'} | |
| - Professional Paraphrasing: {'β ENABLED' if initialization_status.get('professional_paraphrasing') else 'β DISABLED'} | |
| - Quality Control: {'β ENABLED' if initialization_status.get('professional_quality') else 'β DISABLED'} | |
| - Professional Completeness: {(initialization_status.get('enabled_features', 0)/initialization_status.get('total_features', 7))*100:.0f}%""" | |
| # Status indicator | |
| if result['detection_evasion_score'] > 0.8 and result['similarity_score'] > 0.85: | |
| status = "π EXCELLENT - High quality, professional humanization" | |
| elif result['detection_evasion_score'] > 0.6 and result['similarity_score'] > 0.75: | |
| status = "β GOOD - Quality professional result" | |
| else: | |
| status = "β οΈ MODERATE - Acceptable but could be improved" | |
| full_stats = main_stats + "\n\n" + (advanced_stats if show_advanced_metrics else "") + "\n\n" + feature_status | |
| return result['humanized_text'], full_stats, status, f"Quality: {result['similarity_score']:.3f} | Evasion: {result['detection_evasion_score']:.3f}" | |
| except Exception as e: | |
| error_msg = f"β Error processing text: {str(e)}" | |
| return error_msg, "", "β Processing failed", "" | |
| def get_professional_feature_status(): | |
| """Get current professional feature status for display""" | |
| if not initialization_status.get('humanizer_loaded'): | |
| return "β Professional Humanizer Not Loaded", "red" | |
| enabled = initialization_status.get('enabled_features', 0) | |
| total = initialization_status.get('total_features', 7) | |
| completeness = (enabled / total) * 100 | |
| if completeness >= 90: | |
| return f"π Professional Grade Ready ({completeness:.0f}%)", "green" | |
| elif completeness >= 70: | |
| return f"β Professional Features Active ({completeness:.0f}%)", "green" | |
| elif completeness >= 50: | |
| return f"β οΈ Limited Professional Features ({completeness:.0f}%)", "orange" | |
| else: | |
| return f"β Basic Mode Only ({completeness:.0f}%)", "red" | |
| # Initialize the professional humanizer on startup | |
| initialization_success = initialize_professional_humanizer() | |
| # Create the professional Gradio interface | |
| with gr.Blocks( | |
| title="π― Professional AI Text Humanizer - Clean & Structure-Preserving", | |
| theme=gr.themes.Soft(), | |
| css=""" | |
| .main-header { | |
| text-align: center; | |
| background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%); | |
| color: white; | |
| padding: 30px; | |
| border-radius: 15px; | |
| margin-bottom: 30px; | |
| box-shadow: 0 8px 25px rgba(0,0,0,0.15); | |
| } | |
| .professional-badge { | |
| background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%); | |
| color: white; | |
| padding: 8px 16px; | |
| border-radius: 20px; | |
| display: inline-block; | |
| margin: 5px; | |
| font-weight: bold; | |
| } | |
| .feature-status { | |
| text-align: center; | |
| padding: 15px; | |
| border-radius: 10px; | |
| margin: 15px 0; | |
| font-weight: bold; | |
| font-size: 1.1em; | |
| } | |
| .status-green { background-color: #d4f4dd; border: 2px solid #27ae60; color: #1e8449; } | |
| .status-orange { background-color: #fdeaa7; border: 2px solid #f39c12; color: #b7950b; } | |
| .status-red { background-color: #fadbd8; border: 2px solid #e74c3c; color: #c0392b; } | |
| .professional-box { | |
| background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%); | |
| color: white; | |
| padding: 20px; | |
| border-radius: 15px; | |
| margin: 15px 0; | |
| } | |
| .feature-box { | |
| background: #f8f9fa; | |
| padding: 15px; | |
| border-radius: 10px; | |
| border-left: 5px solid #3498db; | |
| margin: 10px 0; | |
| } | |
| .quality-highlight { | |
| background: linear-gradient(135deg, #e8f5e8 0%, #d5f4e6 100%); | |
| padding: 15px; | |
| border-radius: 10px; | |
| margin: 10px 0; | |
| border: 2px solid #27ae60; | |
| } | |
| """ | |
| ) as demo: | |
| gr.HTML(f""" | |
| <div class="main-header"> | |
| <h1>π― Professional AI Text Humanizer</h1> | |
| <p><strong>Clean, Structure-Preserving, Error-Free Processing</strong></p> | |
| <p><em>Professional-grade humanization without mistakes or structural changes</em></p> | |
| <div style="margin-top: 15px;"> | |
| <span class="professional-badge">No Mistakes</span> | |
| <span class="professional-badge">Structure Preserved</span> | |
| <span class="professional-badge">Professional Quality</span> | |
| </div> | |
| </div> | |
| """) | |
| # Professional feature status indicator | |
| if initialization_success: | |
| status_text, status_color = get_professional_feature_status() | |
| gr.HTML(f""" | |
| <div class="feature-status status-{status_color}"> | |
| {status_text} | |
| </div> | |
| """) | |
| else: | |
| gr.HTML(f""" | |
| <div class="feature-status status-red"> | |
| β Initialization Failed - Please refresh the page | |
| </div> | |
| """) | |
| with gr.Tab("π― Professional Humanization"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.HTML("<h3>π Input Configuration</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 in natural language processing tasks.\n\nSubsequently, these systems can analyze and generate text with remarkable accuracy.", | |
| lines=14, | |
| max_lines=25 | |
| ) | |
| with gr.Row(): | |
| style_dropdown = gr.Dropdown( | |
| choices=["Natural", "Professional", "Formal"], | |
| value="Natural", | |
| label="π¨ Professional Style", | |
| info="Natural: Balanced | Professional: Business-ready | Formal: Academic" | |
| ) | |
| intensity_slider = gr.Slider( | |
| minimum=0.1, | |
| maximum=1.0, | |
| value=0.7, | |
| step=0.1, | |
| label="β‘ Intensity Level", | |
| info="Higher = more transformation while maintaining quality" | |
| ) | |
| with gr.Row(): | |
| bypass_detection = gr.Checkbox( | |
| label="π‘οΈ Enable Detection Bypass", | |
| value=True, | |
| info="Professional techniques to bypass AI detectors" | |
| ) | |
| preserve_structure = gr.Checkbox( | |
| label="ποΈ Preserve Text Structure", | |
| value=True, | |
| info="Maintain paragraphs, formatting, and sentence boundaries" | |
| ) | |
| with gr.Row(): | |
| quality_threshold = gr.Slider( | |
| minimum=0.5, | |
| maximum=0.95, | |
| value=0.75, | |
| step=0.05, | |
| label="π Quality Threshold", | |
| info="Minimum similarity to preserve (higher = more conservative)" | |
| ) | |
| show_advanced = gr.Checkbox( | |
| label="π Show Technical Metrics", | |
| value=True, | |
| info="Display detailed technical analysis" | |
| ) | |
| humanize_btn = gr.Button( | |
| "π― Professional Humanize", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| with gr.Column(scale=1): | |
| gr.HTML("<h3>β¨ Professional Results</h3>") | |
| output_text = gr.Textbox( | |
| label="Humanized Text", | |
| lines=14, | |
| max_lines=25, | |
| show_copy_button=True | |
| ) | |
| status_indicator = gr.Textbox( | |
| label="Quality Status", | |
| lines=1, | |
| interactive=False | |
| ) | |
| quality_metrics = gr.Textbox( | |
| label="Quality Metrics", | |
| lines=1, | |
| interactive=False | |
| ) | |
| # Professional metrics display | |
| gr.HTML("<h3>π Professional Analysis</h3>") | |
| professional_metrics = gr.Markdown( | |
| label="Professional Metrics & Quality Analysis", | |
| value="Detailed professional analysis will appear here after processing..." | |
| ) | |
| with gr.Tab("π’ Professional Features & Examples"): | |
| gr.HTML(""" | |
| <div class="professional-box"> | |
| <h3>π― Professional Humanization Features</h3> | |
| <p>This professional humanizer is designed for high-quality, error-free output:</p> | |
| <ul> | |
| <li><strong>No Mistakes:</strong> Zero tolerance for errors, typos, or grammatical issues</li> | |
| <li><strong>Structure Preservation:</strong> Maintains original formatting, paragraphs, and layout</li> | |
| <li><strong>Professional Quality:</strong> Business and academic-ready output</li> | |
| <li><strong>Clean Processing:</strong> No slang, no informal expressions, no intentional errors</li> | |
| <li><strong>Meaning Preservation:</strong> Maintains 75-95% semantic similarity</li> | |
| <li><strong>Detection Bypass:</strong> Professional techniques to avoid AI detection</li> | |
| </ul> | |
| </div> | |
| """) | |
| # Show current professional implementation status | |
| if initialization_success: | |
| professional_status = f""" | |
| <div class="feature-box"> | |
| <h4>β Currently Active Professional Features:</h4> | |
| <ul> | |
| <li><strong>Structure Preservation:</strong> Maintains paragraphs, sentence boundaries, formatting</li> | |
| <li><strong>Error-Free Processing:</strong> No intentional mistakes or imperfections</li> | |
| <li><strong>Professional Mappings:</strong> 100+ formalβnatural word transformations</li> | |
| <li><strong>Clean Contractions:</strong> Appropriate professional contractions only</li> | |
| <li><strong>Quality Control:</strong> Automatic reversion if quality drops below threshold</li> | |
| <li><strong>Professional Paraphrasing:</strong> Business and academic-appropriate rewrites</li> | |
| <li><strong>Semantic Preservation:</strong> Advanced similarity checking</li> | |
| </ul> | |
| </div> | |
| """ | |
| gr.HTML(professional_status) | |
| # Professional examples | |
| gr.HTML("<h3>π‘ Professional Examples</h3>") | |
| examples = gr.Examples( | |
| examples=[ | |
| [ | |
| "Furthermore, it is important to note that artificial intelligence systems demonstrate significant capabilities in natural language processing tasks.\n\nSubsequently, these systems can analyze and generate text with remarkable accuracy. Nevertheless, it is crucial to understand that human oversight remains essential for optimal performance.", | |
| "Natural", | |
| 0.7, | |
| True, | |
| True, | |
| 0.75, | |
| True | |
| ], | |
| [ | |
| "The implementation of comprehensive methodologies will facilitate optimization and enhance operational efficiency throughout the organization.\n\nMoreover, the utilization of systematic approaches demonstrates substantial improvements in performance metrics. Consequently, stakeholders must endeavor to establish frameworks that demonstrate effectiveness.", | |
| "Professional", | |
| 0.8, | |
| True, | |
| True, | |
| 0.8, | |
| True | |
| ], | |
| [ | |
| "It is imperative to understand that systematic evaluation demonstrates significant correlation between methodology implementation and performance optimization.\n\nSubsequently, comprehensive analysis reveals that organizations utilizing advanced frameworks obtain substantial improvements in operational metrics.\n\nNevertheless, careful consideration must be given to resource allocation and strategic planning initiatives.", | |
| "Formal", | |
| 0.6, | |
| True, | |
| True, | |
| 0.8, | |
| True | |
| ] | |
| ], | |
| inputs=[input_text, style_dropdown, intensity_slider, bypass_detection, preserve_structure, quality_threshold, show_advanced], | |
| outputs=[output_text, professional_metrics, status_indicator, quality_metrics], | |
| fn=humanize_text_professional_hf, | |
| cache_examples=False, | |
| label="π― Click any example to see professional humanization!" | |
| ) | |
| # Professional specifications | |
| gr.HTML(""" | |
| <div class="quality-highlight"> | |
| <h3>π’ Professional Quality Specifications</h3> | |
| <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;"> | |
| <div> | |
| <h4>π Quality Standards:</h4> | |
| <ul> | |
| <li><strong>Error Rate:</strong> 0% (Zero tolerance)</li> | |
| <li><strong>Structure Preservation:</strong> 100%</li> | |
| <li><strong>Similarity Preservation:</strong> 75-95%</li> | |
| <li><strong>Professional Grade:</strong> Business-ready</li> | |
| </ul> | |
| </div> | |
| <div> | |
| <h4>β‘ Performance Metrics:</h4> | |
| <ul> | |
| <li><strong>Processing Speed:</strong> 200-800ms</li> | |
| <li><strong>Detection Bypass:</strong> 70-85%</li> | |
| <li><strong>Quality Control:</strong> Automatic</li> | |
| <li><strong>Format Compatibility:</strong> Universal</li> | |
| </ul> | |
| </div> | |
| </div> | |
| </div> | |
| """) | |
| # Professional usage guide | |
| gr.HTML(""" | |
| <div class="feature-box"> | |
| <h3>π Professional Usage Guide</h3> | |
| <h4>π¨ Style Selection:</h4> | |
| <ul> | |
| <li><strong>Natural (0.5-0.8):</strong> Balanced humanization while maintaining professionalism</li> | |
| <li><strong>Professional (0.6-0.9):</strong> Business-ready content with corporate tone</li> | |
| <li><strong>Formal (0.4-0.7):</strong> Academic and technical writing with formal structure</li> | |
| </ul> | |
| <h4>β‘ Intensity Guidelines:</h4> | |
| <ul> | |
| <li><strong>0.1-0.4:</strong> Minimal changes, maintains formal tone completely</li> | |
| <li><strong>0.5-0.7:</strong> Moderate humanization, balanced approach</li> | |
| <li><strong>0.8-1.0:</strong> Maximum humanization while preserving quality</li> | |
| </ul> | |
| <h4>ποΈ Structure Preservation:</h4> | |
| <p>When enabled, maintains:</p> | |
| <ul> | |
| <li>Original paragraph breaks and formatting</li> | |
| <li>Sentence boundaries and punctuation</li> | |
| <li>Bullet points, numbered lists, and special formatting</li> | |
| <li>Overall document structure and layout</li> | |
| </ul> | |
| <h4>π Quality Threshold:</h4> | |
| <p>Controls how conservative the humanization is:</p> | |
| <ul> | |
| <li><strong>0.5-0.6:</strong> More aggressive transformation, lower similarity</li> | |
| <li><strong>0.7-0.8:</strong> Balanced approach (recommended)</li> | |
| <li><strong>0.85-0.95:</strong> Conservative, high similarity preservation</li> | |
| </ul> | |
| </div> | |
| """) | |
| # Event handlers | |
| humanize_btn.click( | |
| fn=humanize_text_professional_hf, | |
| inputs=[input_text, style_dropdown, intensity_slider, bypass_detection, preserve_structure, quality_threshold, show_advanced], | |
| outputs=[output_text, professional_metrics, status_indicator, quality_metrics] | |
| ) | |
| # Launch the interface | |
| if __name__ == "__main__": | |
| print("π Launching Professional AI Text Humanizer on Hugging Face Spaces...") | |
| print(f"π― Initialization Status: {'β SUCCESS' if initialization_success else 'β FAILED'}") | |
| demo.launch( | |
| share=False, | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| show_error=True, | |
| show_api=False | |
| ) |