# utils.py import numpy as np from pathlib import Path from typing import List, Tuple, Optional import torch import config def get_available_models() -> List[str]: """Lấy danh sách tất cả model trong folder models""" models_dir = Path(config.CONFIG['models_dir']) model_files = list(models_dir.glob("*.pth")) return sorted([f.name for f in model_files]) def validate_model_file(model_path: str) -> bool: """Kiểm tra xem file model có hợp lệ không""" path = Path(model_path) if not path.exists(): return False if path.suffix != '.pth': return False if path.stat().st_size < 1000: # File quá nhỏ return False return True def format_time(seconds: float) -> str: """Format thời gian từ giây sang HH:MM:SS""" hours = int(seconds // 3600) minutes = int((seconds % 3600) // 60) secs = int(seconds % 60) if hours > 0: return f"{hours}h {minutes}m {secs}s" elif minutes > 0: return f"{minutes}m {secs}s" else: return f"{secs}s" def get_color_for_value(color_val: int) -> str: """Lấy màu hex từ giá trị color""" return config.COLORS_MAP.get(color_val, '#ffffff') def create_bottle_html(bottle_state: np.ndarray, bottle_idx: int, selected: bool = False) -> str: """Tạo HTML cho một chai""" border_color = '#FFD700' if selected else '#333' border_width = '3' if selected else '2' html = f'
' html += f'
' for height_idx in range(len(bottle_state)): color_val = int(bottle_state[height_idx]) color = get_color_for_value(color_val) html += f'
' html += '
' html += f'

Chai {bottle_idx}

' html += '
' return html def get_device() -> torch.device: """Lấy device (GPU hoặc CPU)""" device_config = config.MODEL_CONFIG['device'] if device_config == 'auto': return torch.device('cuda' if torch.cuda.is_available() else 'cpu') elif device_config == 'cuda': if torch.cuda.is_available(): return torch.device('cuda') else: print("Warning: CUDA not available, falling back to CPU") return torch.device('cpu') else: return torch.device('cpu') def print_device_info(): """In thông tin về device""" device = get_device() print(f"Device: {device}") if device.type == 'cuda': print(f"GPU Name: {torch.cuda.get_device_name(0)}") print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")