Spaces:
Runtime error
Runtime error
File size: 9,550 Bytes
9e7dc23 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
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
) |