File size: 2,033 Bytes
2a729e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Configuration management for OmniParser API

"""
from pydantic_settings import BaseSettings
from typing import Optional
from pathlib import Path


class Settings(BaseSettings):
    """Application settings"""
    
    # Server
    host: str = "0.0.0.0"
    port: int = 8000
    debug: bool = False
    
    # Model
    model_name: str = "microsoft/OmniParser-v2.0"
    device: str = "cpu"  # "cpu" or "cuda"
    
    # File handling
    max_file_size: int = 52428800  # 50MB
    allowed_extensions: tuple = ("jpg", "jpeg", "png", "bmp", "gif")
    
    # HuggingFace
    huggingface_token: Optional[str] = None
    cache_dir: Path = Path("./models")
    
    # Processing
    enable_caching: bool = False
    max_workers: int = 4
    
    # CORS
    cors_origins: list = ["*"]
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = False


# Global settings instance
settings = Settings()


def get_settings() -> Settings:
    """Get current settings"""
    return settings


def validate_image_file(filename: str) -> bool:
    """Validate if file is allowed image format"""
    ext = Path(filename).suffix.lower().lstrip(".")
    return ext in settings.allowed_extensions


def get_device():
    """Get computation device"""
    device = settings.device.lower()
    if device == "cuda":
        try:
            import torch
            if torch.cuda.is_available():
                return "cuda"
            else:
                print("⚠️  CUDA requested but not available, falling back to CPU")
                return "cpu"
        except ImportError:
            print("⚠️  torch not installed, using CPU")
            return "cpu"
    return "cpu"


if __name__ == "__main__":
    print("Current Configuration:")
    print("=" * 60)
    for key, value in settings.dict().items():
        if key not in ["huggingface_token"]:
            print(f"{key}: {value}")
    print("=" * 60)