anycoder-319bed1e / utils.py
Ivan1008's picture
Update utils.py from anycoder
9d861ad verified
# Utility functions for the Gradio application
# This file contains helper functions that can be used in the main app
def validate_input(text: str) -> bool:
"""
Validate that the input text is not empty.
Args:
text: The text to validate
Returns:
bool: True if text is not empty, False otherwise
"""
return bool(text.strip())
def sanitize_text(text: str) -> str:
"""
Sanitize input text by removing leading/trailing whitespace.
Args:
text: The text to sanitize
Returns:
str: The sanitized text
"""
return text.strip()
def format_greeting(name: str, language: str = "ru") -> str:
"""
Format a greeting message based on the selected language.
Args:
name: The name to greet
language: The language code ('ru' for Russian, 'en' for English)
Returns:
str: The formatted greeting message
"""
if language == "ru":
return f"Привет, {name}! Добро пожаловать!"
else:
return f"Hello, {name}! Welcome!"
def calculate_word_count(text: str) -> int:
"""
Calculate the number of words in the text.
Args:
text: The text to analyze
Returns:
int: The word count
"""
if not text:
return 0
words = text.split()
return len(words)