Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import random | |
| def text_transformation(text, operation, intensity=1.0): | |
| """ | |
| Apply various text transformations to the input text. | |
| Args: | |
| text (str): Input text to transform | |
| operation (str): Type of transformation to apply | |
| intensity (float): Strength of the transformation (0.0 to 1.0) | |
| Returns: | |
| str: Transformed text | |
| """ | |
| if not text: | |
| return "Please enter some text first!" | |
| # Apply the selected operation | |
| if operation == "Reverse": | |
| return text[::-1] | |
| elif operation == "Uppercase": | |
| return text.upper() | |
| elif operation == "Lowercase": | |
| return text.lower() | |
| elif operation == "Title Case": | |
| return text.title() | |
| elif operation == "Random Case": | |
| return ''.join( | |
| char.upper() if random.random() < intensity else char.lower() | |
| for char in text | |
| ) | |
| elif operation == "Alternating Case": | |
| result = [] | |
| upper = True | |
| for char in text: | |
| if char.isalpha(): | |
| if random.random() < intensity: | |
| result.append(char.upper()) | |
| else: | |
| result.append(char.lower()) | |
| upper = not upper | |
| return ''.join(result) | |
| elif operation == "Spongebob Mock": | |
| result = [] | |
| for char in text: | |
| if char.isalpha(): | |
| if random.random() < intensity: | |
| result.append(char.upper()) | |
| else: | |
| result.append(char.lower()) | |
| return ''.join(result) | |
| elif operation == "Leet Speak": | |
| leet_dict = { | |
| 'a': '4', 'e': '3', 'i': '1', 'o': '0', 's': '5', | |
| 't': '7', 'l': '1', 'o': '0', 's': '5', 't': '7' | |
| } | |
| return ''.join( | |
| leet_dict.get(char.lower(), char) | |
| for char in text | |
| ) | |
| elif operation == "Add Emojis": | |
| emojis = ['😊', '🎉', '🔥', '✨', '🚀', '💯'] | |
| result = [] | |
| for char in text: | |
| if random.random() < intensity: | |
| result.append(leet_dict.get(char.lower(), char)) | |
| return ''.join(result) | |
| elif operation == "Shuffle Words": | |
| words = text.split() | |
| if len(words) <= 1: | |
| return text | |
| random.shuffle(words) | |
| return ' '.join(words) | |
| elif operation == "Duplicate Letters": | |
| result = [] | |
| for char in text: | |
| if char.isalpha() and random.random() < intensity: | |
| result.append(char * 2) | |
| else: | |
| result.append(char) | |
| return ''.join(result) | |
| elif operation == "Reverse Words": | |
| words = text.split() | |
| return ' '.join(word[::-1] for word in words) | |
| else: | |
| return text | |
| def generate_example(): | |
| """Generate a random example for the user""" | |
| examples = [ | |
| "Hello World!", | |
| "The quick brown fox jumps over the lazy dog.", | |
| "Gradio is amazing for creating ML demos.", | |
| "Text transformation made easy with Revert." | |
| ] | |
| return random.choice(examples) | |
| # Create custom theme for Revert app | |
| revert_theme = gr.themes.Soft( | |
| primary_hue="indigo", | |
| secondary_hue="slate", | |
| neutral_hue="stone", | |
| font=gr.themes.GoogleFont("Inter"), | |
| text_size="lg", | |
| spacing_size="lg", | |
| radius_size="md" | |
| ).set( | |
| button_primary_background_fill="*primary_600", | |
| button_primary_background_fill_hover="*primary_700", | |
| block_title_text_weight="600", | |
| body_text_weight="400" | |
| ) | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| """ | |
| # 🔄 Revert - Text Transformation Tool | |
| **Transform your text with various operations!** | |
| Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder) | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| input_text = gr.Textbox( | |
| label="Input Text", | |
| placeholder="Enter text to transform here...", | |
| lines=3, | |
| max_lines=10, | |
| info="Type your text and choose an operation" | |
| ) | |
| operation = gr.Dropdown( | |
| choices=[ | |
| "Reverse", | |
| "Uppercase", | |
| "Lowercase", | |
| "Title Case", | |
| "Random Case", | |
| "Alternating Case", | |
| "Spongebob Mock", | |
| "Leet Speak", | |
| "Add Emojis", | |
| "Shuffle Words", | |
| "Duplicate Letters", | |
| "Reverse Words" | |
| ], | |
| value="Reverse", | |
| label="Transformation Type", | |
| info="Choose how to transform your text" | |
| ) | |
| intensity = gr.Slider( | |
| minimum=0.0, | |
| maximum=1.0, | |
| value=0.7, | |
| step=0.1, | |
| label="Intensity", | |
| info="Adjust the strength of the transformation" | |
| ) | |
| with gr.Column(scale=2): | |
| output_text = gr.Textbox( | |
| label="Transformed Text", | |
| interactive=False, | |
| lines=3, | |
| max_lines=10 | |
| ) | |
| with gr.Row(): | |
| transform_btn = gr.Button( | |
| "Transform Text 🪄", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| # Example generation section | |
| with gr.Row(): | |
| example_btn = gr.Button( | |
| "Generate Example ✨", | |
| variant="secondary" | |
| ) | |
| # Examples for quick testing | |
| examples = gr.Examples( | |
| examples=[ | |
| ["Hello World!", "Reverse", 1.0], | |
| ["PYTHON PROGRAMMING", "Lowercase", 1.0], | |
| ["the gradio library", "Title Case", 1.0], | |
| ["Machine Learning Demo", "Random Case", 0.5], | |
| ["text transformation", "Alternating Case", 0.8], | |
| ["revert app", "Leet Speak", 0.9], | |
| ["add some fun", "Add Emojis", 0.6] | |
| ], | |
| inputs=[input_text, operation, intensity] | |
| ) | |
| # Event listeners | |
| transform_btn.click( | |
| fn=text_transformation, | |
| inputs=[input_text, operation, intensity], | |
| outputs=[output_text], | |
| api_visibility="public" | |
| ) | |
| example_btn.click( | |
| fn=generate_example, | |
| inputs=None, | |
| outputs=[input_text], | |
| api_visibility="private" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| theme=revert_theme, | |
| css=""" | |
| .gradio-container { | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; | |
| } | |
| .cool-col { | |
| padding: 20px; | |
| border-radius: 12px; | |
| background: rgba(255, 255, 255, 0.1); | |
| border: 1px solid rgba(255, 255, 255, 0.2); | |
| } | |
| """, | |
| footer_links=[ | |
| {"label": "Documentation", "url": "https://www.gradio.app"}, | |
| {"label": "GitHub", "url": "https://github.com/gradio-app/gradio"}, | |
| {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"} | |
| ] | |
| ) |