omni / config.py
Samfredoly's picture
Upload 14 files
2a729e6 verified
"""
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)