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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
def text_transformation(text, operation, intensity=1.0):
|
| 5 |
+
"""
|
| 6 |
+
Apply various text transformations to the input text.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
text (str): Input text to transform
|
| 10 |
+
operation (str): Type of transformation to apply
|
| 11 |
+
intensity (float): Strength of the transformation (0.0 to 1.0)
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
str: Transformed text
|
| 15 |
+
"""
|
| 16 |
+
if not text:
|
| 17 |
+
return "Please enter some text first!"
|
| 18 |
+
|
| 19 |
+
# Apply the selected operation
|
| 20 |
+
if operation == "Reverse":
|
| 21 |
+
return text[::-1]
|
| 22 |
+
elif operation == "Uppercase":
|
| 23 |
+
return text.upper()
|
| 24 |
+
elif operation == "Lowercase":
|
| 25 |
+
return text.lower()
|
| 26 |
+
elif operation == "Title Case":
|
| 27 |
+
return text.title()
|
| 28 |
+
elif operation == "Random Case":
|
| 29 |
+
return ''.join(
|
| 30 |
+
char.upper() if random.random() < intensity else char.lower()
|
| 31 |
+
for char in text
|
| 32 |
+
)
|
| 33 |
+
elif operation == "Alternating Case":
|
| 34 |
+
result = []
|
| 35 |
+
upper = True
|
| 36 |
+
for char in text:
|
| 37 |
+
if char.isalpha():
|
| 38 |
+
if random.random() < intensity:
|
| 39 |
+
result.append(char.upper())
|
| 40 |
+
else:
|
| 41 |
+
result.append(char.lower())
|
| 42 |
+
upper = not upper
|
| 43 |
+
return ''.join(result)
|
| 44 |
+
elif operation == "Spongebob Mock":
|
| 45 |
+
result = []
|
| 46 |
+
for char in text:
|
| 47 |
+
if char.isalpha():
|
| 48 |
+
if random.random() < intensity:
|
| 49 |
+
result.append(char.upper())
|
| 50 |
+
else:
|
| 51 |
+
result.append(char.lower())
|
| 52 |
+
return ''.join(result)
|
| 53 |
+
elif operation == "Leet Speak":
|
| 54 |
+
leet_dict = {
|
| 55 |
+
'a': '4', 'e': '3', 'i': '1', 'o': '0', 's': '5',
|
| 56 |
+
't': '7', 'l': '1', 'o': '0', 's': '5', 't': '7'
|
| 57 |
+
}
|
| 58 |
+
return ''.join(
|
| 59 |
+
leet_dict.get(char.lower(), char)
|
| 60 |
+
for char in text
|
| 61 |
+
)
|
| 62 |
+
elif operation == "Add Emojis":
|
| 63 |
+
emojis = ['😊', '🎉', '🔥', '✨', '🚀', '💯']
|
| 64 |
+
result = []
|
| 65 |
+
for char in text:
|
| 66 |
+
if random.random() < intensity:
|
| 67 |
+
result.append(leet_dict.get(char.lower(), char))
|
| 68 |
+
return ''.join(result)
|
| 69 |
+
elif operation == "Shuffle Words":
|
| 70 |
+
words = text.split()
|
| 71 |
+
if len(words) <= 1:
|
| 72 |
+
return text
|
| 73 |
+
random.shuffle(words)
|
| 74 |
+
return ' '.join(words)
|
| 75 |
+
elif operation == "Duplicate Letters":
|
| 76 |
+
result = []
|
| 77 |
+
for char in text:
|
| 78 |
+
if char.isalpha() and random.random() < intensity:
|
| 79 |
+
result.append(char * 2)
|
| 80 |
+
else:
|
| 81 |
+
result.append(char)
|
| 82 |
+
return ''.join(result)
|
| 83 |
+
elif operation == "Reverse Words":
|
| 84 |
+
words = text.split()
|
| 85 |
+
return ' '.join(word[::-1] for word in words)
|
| 86 |
+
else:
|
| 87 |
+
return text
|
| 88 |
+
|
| 89 |
+
def generate_example():
|
| 90 |
+
"""Generate a random example for the user"""
|
| 91 |
+
examples = [
|
| 92 |
+
"Hello World!",
|
| 93 |
+
"The quick brown fox jumps over the lazy dog.",
|
| 94 |
+
"Gradio is amazing for creating ML demos.",
|
| 95 |
+
"Text transformation made easy with Revert."
|
| 96 |
+
]
|
| 97 |
+
return random.choice(examples)
|
| 98 |
+
|
| 99 |
+
# Create custom theme for Revert app
|
| 100 |
+
revert_theme = gr.themes.Soft(
|
| 101 |
+
primary_hue="indigo",
|
| 102 |
+
secondary_hue="slate",
|
| 103 |
+
neutral_hue="stone",
|
| 104 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 105 |
+
text_size="lg",
|
| 106 |
+
spacing_size="lg",
|
| 107 |
+
radius_size="md"
|
| 108 |
+
).set(
|
| 109 |
+
button_primary_background_fill="*primary_600",
|
| 110 |
+
button_primary_background_fill_hover="*primary_700",
|
| 111 |
+
block_title_text_weight="600",
|
| 112 |
+
body_text_weight="400"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Blocks() as demo:
|
| 116 |
+
gr.Markdown(
|
| 117 |
+
"""
|
| 118 |
+
# 🔄 Revert - Text Transformation Tool
|
| 119 |
+
**Transform your text with various operations!**
|
| 120 |
+
|
| 121 |
+
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 122 |
+
"""
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
with gr.Column(scale=3):
|
| 127 |
+
input_text = gr.Textbox(
|
| 128 |
+
label="Input Text",
|
| 129 |
+
placeholder="Enter text to transform here...",
|
| 130 |
+
lines=3,
|
| 131 |
+
max_lines=10,
|
| 132 |
+
info="Type your text and choose an operation"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
operation = gr.Dropdown(
|
| 136 |
+
choices=[
|
| 137 |
+
"Reverse",
|
| 138 |
+
"Uppercase",
|
| 139 |
+
"Lowercase",
|
| 140 |
+
"Title Case",
|
| 141 |
+
"Random Case",
|
| 142 |
+
"Alternating Case",
|
| 143 |
+
"Spongebob Mock",
|
| 144 |
+
"Leet Speak",
|
| 145 |
+
"Add Emojis",
|
| 146 |
+
"Shuffle Words",
|
| 147 |
+
"Duplicate Letters",
|
| 148 |
+
"Reverse Words"
|
| 149 |
+
],
|
| 150 |
+
value="Reverse",
|
| 151 |
+
label="Transformation Type",
|
| 152 |
+
info="Choose how to transform your text"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
intensity = gr.Slider(
|
| 156 |
+
minimum=0.0,
|
| 157 |
+
maximum=1.0,
|
| 158 |
+
value=0.7,
|
| 159 |
+
step=0.1,
|
| 160 |
+
label="Intensity",
|
| 161 |
+
info="Adjust the strength of the transformation"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
with gr.Column(scale=2):
|
| 165 |
+
output_text = gr.Textbox(
|
| 166 |
+
label="Transformed Text",
|
| 167 |
+
interactive=False,
|
| 168 |
+
lines=3,
|
| 169 |
+
max_lines=10
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
with gr.Row():
|
| 173 |
+
transform_btn = gr.Button(
|
| 174 |
+
"Transform Text 🪄",
|
| 175 |
+
variant="primary",
|
| 176 |
+
size="lg"
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Example generation section
|
| 180 |
+
with gr.Row():
|
| 181 |
+
example_btn = gr.Button(
|
| 182 |
+
"Generate Example ✨",
|
| 183 |
+
variant="secondary"
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
# Examples for quick testing
|
| 187 |
+
examples = gr.Examples(
|
| 188 |
+
examples=[
|
| 189 |
+
["Hello World!", "Reverse", 1.0],
|
| 190 |
+
["PYTHON PROGRAMMING", "Lowercase", 1.0],
|
| 191 |
+
["the gradio library", "Title Case", 1.0],
|
| 192 |
+
["Machine Learning Demo", "Random Case", 0.5],
|
| 193 |
+
["text transformation", "Alternating Case", 0.8],
|
| 194 |
+
["revert app", "Leet Speak", 0.9],
|
| 195 |
+
["add some fun", "Add Emojis", 0.6]
|
| 196 |
+
],
|
| 197 |
+
inputs=[input_text, operation, intensity]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Event listeners
|
| 201 |
+
transform_btn.click(
|
| 202 |
+
fn=text_transformation,
|
| 203 |
+
inputs=[input_text, operation, intensity],
|
| 204 |
+
outputs=[output_text],
|
| 205 |
+
api_visibility="public"
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
example_btn.click(
|
| 209 |
+
fn=generate_example,
|
| 210 |
+
inputs=None,
|
| 211 |
+
outputs=[input_text],
|
| 212 |
+
api_visibility="private"
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
if __name__ == "__main__":
|
| 216 |
+
demo.launch(
|
| 217 |
+
theme=revert_theme,
|
| 218 |
+
css="""
|
| 219 |
+
.gradio-container {
|
| 220 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 221 |
+
}
|
| 222 |
+
.cool-col {
|
| 223 |
+
padding: 20px;
|
| 224 |
+
border-radius: 12px;
|
| 225 |
+
background: rgba(255, 255, 255, 0.1);
|
| 226 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 227 |
+
}
|
| 228 |
+
""",
|
| 229 |
+
footer_links=[
|
| 230 |
+
{"label": "Documentation", "url": "https://www.gradio.app"},
|
| 231 |
+
{"label": "GitHub", "url": "https://github.com/gradio-app/gradio"},
|
| 232 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 233 |
+
]
|
| 234 |
+
)
|