Spaces:
Runtime error
Runtime error
import gradio as gr | |
import time | |
from text_humanizer import AITextHumanizer | |
# Initialize the humanizer | |
print("Loading AI Text Humanizer...") | |
try: | |
humanizer = AITextHumanizer() | |
print("β Humanizer loaded successfully!") | |
except Exception as e: | |
print(f"β Error loading humanizer: {e}") | |
humanizer = None | |
def humanize_text_gradio(text, style, intensity): | |
""" | |
Gradio interface function for text humanization | |
""" | |
if not text.strip(): | |
return "Please enter some text to humanize.", "", 0.0, [], 0.0 | |
if humanizer is None: | |
return "Error: Humanizer not loaded properly.", "", 0.0, [], 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 | |
return ( | |
result["humanized_text"], | |
f"**Original Text:**\n{result['original_text']}\n\n**Humanized Text:**\n{result['humanized_text']}", | |
result["similarity_score"], | |
result["changes_made"], | |
processing_time | |
) | |
except Exception as e: | |
return f"Error processing text: {str(e)}", "", 0.0, [], 0.0 | |
def compare_texts(original, humanized): | |
"""Compare original and humanized texts side by side""" | |
if not humanized: | |
return "No humanized text to compare." | |
comparison = f""" | |
## Text Comparison | |
### Original Text: | |
{original} | |
### Humanized Text: | |
{humanized} | |
""" | |
return comparison | |
# Create Gradio interface | |
with gr.Blocks( | |
title="π€β‘οΈπ€ AI Text Humanizer", | |
theme=gr.themes.Soft(), | |
css=""" | |
.gradio-container { | |
max-width: 1200px !important; | |
} | |
.main-header { | |
text-align: center; | |
margin-bottom: 30px; | |
} | |
.feature-box { | |
border: 1px solid #ddd; | |
padding: 15px; | |
border-radius: 8px; | |
margin: 10px 0; | |
} | |
""" | |
) as demo: | |
gr.HTML(""" | |
<div class="main-header"> | |
<h1>π€β‘οΈπ€ AI Text Humanizer</h1> | |
<p>Transform AI-generated text to sound more natural and human-like</p> | |
</div> | |
""") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
gr.HTML("<h3>π Input</h3>") | |
input_text = gr.Textbox( | |
label="Text to Humanize", | |
placeholder="Paste your AI-generated text here...", | |
lines=8, | |
max_lines=15 | |
) | |
with gr.Row(): | |
style_dropdown = gr.Dropdown( | |
choices=["Natural", "Casual", "Conversational"], | |
value="Natural", | |
label="Humanization Style", | |
info="Choose how natural you want the text to sound" | |
) | |
intensity_slider = gr.Slider( | |
minimum=0.1, | |
maximum=1.0, | |
value=0.7, | |
step=0.1, | |
label="Intensity", | |
info="How much to humanize (0.1 = subtle, 1.0 = maximum)" | |
) | |
humanize_btn = gr.Button( | |
"π Humanize Text", | |
variant="primary", | |
size="lg" | |
) | |
with gr.Column(scale=2): | |
gr.HTML("<h3>β¨ Output</h3>") | |
output_text = gr.Textbox( | |
label="Humanized Text", | |
lines=8, | |
max_lines=15, | |
show_copy_button=True | |
) | |
with gr.Row(): | |
similarity_score = gr.Number( | |
label="Similarity Score", | |
info="How similar the output is to the input (higher = more similar)", | |
precision=3 | |
) | |
processing_time = gr.Number( | |
label="Processing Time (ms)", | |
info="Time taken to process the text", | |
precision=1 | |
) | |
changes_made = gr.JSON( | |
label="Changes Made", | |
info="List of transformations applied to the text" | |
) | |
with gr.Row(): | |
gr.HTML("<h3>π Comparison</h3>") | |
comparison_output = gr.Markdown( | |
label="Text Comparison", | |
value="Results will appear here after humanization..." | |
) | |
# Example texts | |
gr.HTML("<h3>π‘ Try These Examples</h3>") | |
example_texts = [ | |
[ | |
"""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 solution will facilitate the optimization of business processes. Moreover, it will demonstrate substantial improvements in operational efficiency. Therefore, organizations should consider utilizing this technology to achieve their strategic objectives.""", | |
"Natural", | |
0.6 | |
], | |
[ | |
"""In conclusion, the comprehensive analysis reveals that the proposed methodology demonstrates significant potential for enhancing performance metrics. Additionally, the systematic approach ensures optimal resource utilization while maintaining quality standards.""", | |
"Casual", | |
0.7 | |
] | |
] | |
gr.Examples( | |
examples=example_texts, | |
inputs=[input_text, style_dropdown, intensity_slider], | |
outputs=[output_text, comparison_output, similarity_score, changes_made, processing_time], | |
fn=humanize_text_gradio, | |
cache_examples=True | |
) | |
# Event handlers | |
humanize_btn.click( | |
fn=humanize_text_gradio, | |
inputs=[input_text, style_dropdown, intensity_slider], | |
outputs=[output_text, comparison_output, similarity_score, changes_made, processing_time] | |
) | |
# Auto-update comparison when output changes | |
output_text.change( | |
fn=lambda orig, human: compare_texts(orig, human), | |
inputs=[input_text, output_text], | |
outputs=[comparison_output] | |
) | |
# Features section | |
gr.HTML(""" | |
<div style="margin-top: 40px;"> | |
<h3>π― Features</h3> | |
<div class="feature-box"> | |
<h4>π Smart Word Replacement</h4> | |
<p>Replaces formal words with casual alternatives (utilize β use, demonstrate β show)</p> | |
</div> | |
<div class="feature-box"> | |
<h4>π Contraction Addition</h4> | |
<p>Adds natural contractions (do not β don't, it is β it's)</p> | |
</div> | |
<div class="feature-box"> | |
<h4>π Transition Word Improvement</h4> | |
<p>Replaces AI-like transitions with natural alternatives</p> | |
</div> | |
<div class="feature-box"> | |
<h4>π Sentence Structure Variation</h4> | |
<p>Varies sentence length and structure for more natural flow</p> | |
</div> | |
<div class="feature-box"> | |
<h4>βοΈ Natural Imperfections</h4> | |
<p>Adds subtle imperfections to mimic human writing patterns</p> | |
</div> | |
<div class="feature-box"> | |
<h4>π Semantic Similarity</h4> | |
<p>Ensures the meaning is preserved while making text more human-like</p> | |
</div> | |
</div> | |
""") | |
# Instructions | |
gr.HTML(""" | |
<div style="margin-top: 30px; padding: 20px; background-color: #f8f9fa; border-radius: 10px;"> | |
<h3>π How to Use</h3> | |
<ol> | |
<li><strong>Paste your text:</strong> Copy and paste AI-generated text into the input box</li> | |
<li><strong>Choose style:</strong> Select Natural, Casual, or Conversational based on your needs</li> | |
<li><strong>Set intensity:</strong> Adjust how much humanization you want (0.1-1.0)</li> | |
<li><strong>Click Humanize:</strong> Process your text and see the results</li> | |
<li><strong>Review changes:</strong> Check the similarity score and changes made</li> | |
</ol> | |
<h4>π‘ Tips</h4> | |
<ul> | |
<li><strong>Natural (0.5-0.7):</strong> Good for professional content that needs to sound human</li> | |
<li><strong>Casual (0.6-0.8):</strong> Perfect for blog posts and informal content</li> | |
<li><strong>Conversational (0.7-1.0):</strong> Best for social media and very informal content</li> | |
</ul> | |
</div> | |
""") | |
# Launch the interface | |
if __name__ == "__main__": | |
print("\nπ Starting AI Text Humanizer Gradio Interface...") | |
print("π Interface will be available at the URL shown below") | |
print("\n" + "="*50 + "\n") | |
demo.launch( | |
share=True, # Creates a public link | |
server_name="0.0.0.0", | |
server_port=7860, | |
show_error=True | |
) |