File size: 2,656 Bytes
fcd463d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Input validation utilities
"""

import re
from typing import List


def validate_inputs(
    groq_key: str = None,
    hf_token: str = None,
    space_name: str = None,
    mode: str = "Convert Only"
) -> List[str]:
    """
    Validate all inputs before processing.
    Returns a list of error messages.
    """
    errors = []
    
    # Groq API Key validation
    if mode in ["Convert Only", "Convert & Deploy"]:
        if not groq_key:
            errors.append("Groq API Key is required for conversion")
        elif not groq_key.startswith('gsk_'):
            errors.append("Groq API Key should start with 'gsk_'")
        elif len(groq_key) < 40:
            errors.append("Invalid Groq API Key format")
    
    # Hugging Face Token validation for deployment
    if mode == "Deploy" and hf_token:
        if not hf_token.startswith('hf_'):
            errors.append("Hugging Face token should start with 'hf_'")
        elif len(hf_token) < 10:
            errors.append("Invalid Hugging Face token format")
    
    # Space Name validation for deployment
    if mode == "Deploy" and space_name:
        if len(space_name) < 3 or len(space_name) > 30:
            errors.append("Space name must be 3-30 characters")
        elif not re.match(r'^[a-z0-9-]+$', space_name):
            errors.append("Space name can only contain lowercase letters, numbers, and hyphens")
        elif space_name.startswith('-') or space_name.endswith('-'):
            errors.append("Space name cannot start or end with a hyphen")
        elif '--' in space_name:
            errors.append("Space name cannot contain consecutive hyphens")
    
    return errors


def validate_python_code(code: str) -> List[str]:
    """
    Validate Python code for common issues.
    Returns a list of warnings (not errors).
    """
    warnings = []
    
    if not code.strip():
        warnings.append("Code is empty")
        return warnings
    
    # Check for basic Python syntax markers
    if 'import' not in code and 'def' not in code and 'class' not in code:
        warnings.append("Code may not be valid Python (missing imports, functions, or classes)")
    
    # Check for potentially dangerous imports
    dangerous_imports = ['os.system', 'subprocess', 'eval', 'exec', '__import__']
    for dangerous in dangerous_imports:
        if dangerous in code:
            warnings.append(f"Code contains potentially dangerous operation: {dangerous}")
    
    return warnings


def validate_file_size(file_size: int, max_size_mb: int = 10) -> bool:
    """Validate file size."""
    max_size_bytes = max_size_mb * 1024 * 1024
    return file_size <= max_size_bytes