ai-ickw2how / utils.py
elcrei's picture
Update Gradio app with multiple files
cf1deec verified
import os
from typing import List, Dict, Any
def validate_token() -> bool:
"""
Validate that the HF_TOKEN token is available
Returns:
bool: True if token exists, False otherwise
"""
token = os.getenv("HF_TOKEN")
return token is not None and len(token) > 0
def format_chat_history(history: List[Dict[str, Any]], current_message: str) -> List[Dict[str, str]]:
"""
Format chat history for the Hugging Face API
Args:
history: Chat history from Gradio Chatbot component (type="messages")
current_message: Current user message
Returns:
List of formatted message dictionaries
"""
messages = []
# Add system message
messages.append({
"role": "system",
"content": "You are a helpful, friendly, and knowledgeable assistant. Provide clear and accurate responses."
})
# Add conversation history
# Ensure history is a list of dictionaries with 'role' and 'content' keys
if history and isinstance(history, list):
for message in history:
if isinstance(message, dict):
role = message.get("role", "")
content = message.get("content", "")
# Only add valid messages
if role in ["user", "assistant"] and content:
messages.append({
"role": role,
"content": content
})
# Add current message
if current_message:
messages.append({
"role": "user",
"content": current_message
})
return messages
def sanitize_input(text: str) -> str:
"""
Sanitize user input to prevent injection attacks
Args:
text: Input text to sanitize
Returns:
Sanitized text
"""
# Remove potentially harmful characters
harmful_chars = ['<', '>', '&', '"', "'", '`', '$', '|', ';']
for char in harmful_chars:
text = text.replace(char, '')
return text.strip()
def validate_model_provider(model: str, provider: str) -> tuple[bool, str]:
"""
Validate model and provider names
Args:
model: Model name
provider: Provider name
Returns:
Tuple of (is_valid, error_message)
"""
if not model or not model.strip():
return False, "Model name cannot be empty"
if not provider or not provider.strip():
return False, "Provider name cannot be empty"
# Basic validation - allow alphanumeric, hyphens, underscores, and dots
import re
model_pattern = r'^[a-zA-Z0-9._-]+$'
provider_pattern = r'^[a-zA-Z0-9._-]+$'
if not re.match(model_pattern, model):
return False, "Invalid model name format"
if not re.match(provider_pattern, provider):
return False, "Invalid provider name format"
return True, ""
def format_error_message(error: Exception) -> str:
"""
Format error messages for user display
Args:
error: Exception object
Returns:
User-friendly error message
"""
error_str = str(error)
# Common error patterns and user-friendly messages
if "token" in error_str.lower():
return "Authentication error. Please check your Hugging Face token."
elif "rate limit" in error_str.lower():
return "Rate limit exceeded. Please try again later."
elif "model" in error_str.lower() and "not found" in error_str.lower():
return "Model not found. Please check the model name."
elif "provider" in error_str.lower() and "not found" in error_str.lower():
return "Provider not found. Please check the provider name."
elif "unexpected keyword argument 'provider'" in error_str.lower():
return "Provider parameter not supported in this API version. Try using provider/model format."
else:
return f"An error occurred: {error_str}"