Spaces:
Running
Running
Update utils.py from anycoder
Browse files
utils.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Utility functions for the Gradio application
|
| 2 |
+
# This file contains helper functions that can be used in the main app
|
| 3 |
+
|
| 4 |
+
def validate_input(text: str) -> bool:
|
| 5 |
+
"""
|
| 6 |
+
Validate that the input text is not empty.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
text: The text to validate
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
bool: True if text is not empty, False otherwise
|
| 13 |
+
"""
|
| 14 |
+
return bool(text.strip())
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def sanitize_text(text: str) -> str:
|
| 18 |
+
"""
|
| 19 |
+
Sanitize input text by removing leading/trailing whitespace.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
text: The text to sanitize
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
str: The sanitized text
|
| 26 |
+
"""
|
| 27 |
+
return text.strip()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def format_greeting(name: str, language: str = "ru") -> str:
|
| 31 |
+
"""
|
| 32 |
+
Format a greeting message based on the selected language.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
name: The name to greet
|
| 36 |
+
language: The language code ('ru' for Russian, 'en' for English)
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
str: The formatted greeting message
|
| 40 |
+
"""
|
| 41 |
+
if language == "ru":
|
| 42 |
+
return f"Привет, {name}! Добро пожаловать!"
|
| 43 |
+
else:
|
| 44 |
+
return f"Hello, {name}! Welcome!"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def calculate_word_count(text: str) -> int:
|
| 48 |
+
"""
|
| 49 |
+
Calculate the number of words in the text.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
text: The text to analyze
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
int: The word count
|
| 56 |
+
"""
|
| 57 |
+
if not text:
|
| 58 |
+
return 0
|
| 59 |
+
words = text.split()
|
| 60 |
+
return len(words)
|