Spaces:
Sleeping
Sleeping
File size: 4,001 Bytes
2db0c2a 129cc42 2db0c2a cf1deec 2db0c2a bbde1d1 2db0c2a a2b072c bbde1d1 2db0c2a bbde1d1 a2b072c bbde1d1 2db0c2a a2b072c 2db0c2a cf1deec 2db0c2a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 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}" |