Spaces:
Runtime error
Runtime error
Update utils.py from anycoder
Browse files
utils.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility functions for the Revert text transformation application.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import re
|
| 6 |
+
from typing import List, Tuple
|
| 7 |
+
|
| 8 |
+
def sanitize_input(text: str) -> str:
|
| 9 |
+
"""
|
| 10 |
+
Basic input sanitization for text processing.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
text (str): Input text to sanitize
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
str: Sanitized text
|
| 17 |
+
"""
|
| 18 |
+
if not text:
|
| 19 |
+
return ""
|
| 20 |
+
|
| 21 |
+
# Remove potentially harmful characters while preserving text structure
|
| 22 |
+
sanitized = re.sub(r'[<>"\']', '', text)
|
| 23 |
+
return sanitized.strip()
|
| 24 |
+
|
| 25 |
+
def validate_intensity(intensity: float) -> float:
|
| 26 |
+
"""
|
| 27 |
+
Ensure intensity is within valid range.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
intensity (float): Input intensity value
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
float: Validated intensity value
|
| 34 |
+
"""
|
| 35 |
+
return max(0.0, min(1.0, intensity))
|
| 36 |
+
|
| 37 |
+
def get_operation_description(operation: str) -> str:
|
| 38 |
+
"""
|
| 39 |
+
Get a human-readable description of each operation.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
operation (str): Name of the operation
|
| 43 |
+
|
| 44 |
+
Returns:
|
| 45 |
+
str: Description of what the operation does
|
| 46 |
+
"""
|
| 47 |
+
descriptions = {
|
| 48 |
+
"Reverse": "Reverses the entire text character by character",
|
| 49 |
+
"Uppercase": "Converts all text to upper case",
|
| 50 |
+
"Lowercase": "Converts all text to lower case",
|
| 51 |
+
"Title Case": "Capitalizes the first letter of each word",
|
| 52 |
+
"Random Case": "Randomly capitalizes letters based on intensity",
|
| 53 |
+
"Alternating Case": "Alternates between upper and lower case",
|
| 54 |
+
"Spongebob Mock": "Applies the Spongebob mocking text style",
|
| 55 |
+
"Leet Speak": "Converts text to leet (1337) speak",
|
| 56 |
+
"Add Emojis": "Adds random emojis throughout the text",
|
| 57 |
+
"Shuffle Words": "Randomly reorders words in the text",
|
| 58 |
+
"Duplicate Letters": "Duplicates random letters based on intensity"
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
return descriptions.get(operation, "No description available.")
|
| 62 |
+
|
| 63 |
+
This Gradio 6 application "Revert" provides:
|
| 64 |
+
|
| 65 |
+
**Features:**
|
| 66 |
+
- Text reversal (classic "revert" functionality)
|
| 67 |
+
- Case transformations (upper, lower, title)
|
| 68 |
+
- Random case with adjustable intensity
|
| 69 |
+
- Alternating case patterns
|
| 70 |
+
- Spongebob mocking text style
|
| 71 |
+
- Leet speak conversion
|
| 72 |
+
- Word shuffling
|
| 73 |
+
- Letter duplication
|
| 74 |
+
|
| 75 |
+
**Gradio 6 Modern Features:**
|
| 76 |
+
- Custom theme with Soft design
|
| 77 |
+
- Google Font integration (Inter)
|
| 78 |
+
- Professional color scheme (indigo primary)
|
| 79 |
+
- Responsive layout with rows and columns
|
| 80 |
+
- Interactive sliders for intensity control
|
| 81 |
+
- Example generation
|
| 82 |
+
- Built-in error handling
|
| 83 |
+
- Clean, modern UI with proper spacing and sizing
|
| 84 |
+
|
| 85 |
+
**User Experience:**
|
| 86 |
+
- Intuitive interface with clear labels
|
| 87 |
+
- Example section for quick testing
|
| 88 |
+
- Real-time feedback
|
| 89 |
+
- Multiple transformation options
|
| 90 |
+
- Adjustable intensity for fine control
|
| 91 |
+
|
| 92 |
+
The app uses proper Gradio 6 syntax with theme customization in `demo.launch()` and modern component APIs.
|