import os import subprocess import pynvml cdef class HardwareService: @staticmethod cdef has_nvidia_gpu(): try: pynvml.nvmlInit() device_count = pynvml.nvmlDeviceGetCount() if device_count > 0: print(f"Found NVIDIA GPU(s).") return True else: print("No NVIDIA GPUs found by NVML.") return False except pynvml.NVMLError as error: print(f"Failed to find NVIDIA GPU") return False finally: try: pynvml.nvmlShutdown() except: print('Failed to shutdown pynvml cause probably no NVidia GPU') pass cdef str get_hardware_info(self): if os.name == 'nt': # windows os_command = ( "powershell -Command \"" "Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name | Write-Output; " "Get-CimInstance -ClassName Win32_VideoController | Select-Object -ExpandProperty Name | Write-Output; " "Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize | Write-Output; " "(Get-Disk | Where-Object {$_.IsSystem -eq $true}).SerialNumber" "\"" ) else: os_command = ( "lscpu | grep 'Model name:' | cut -d':' -f2 && " "lspci | grep VGA | cut -d':' -f3 && " "free -k | awk '/^Mem:/ {print $2}' && " "cat /sys/block/sda/device/vpd_pg80 2>/dev/null || cat /sys/block/sda/device/serial 2>/dev/null" ) result = subprocess.check_output(os_command, shell=True).decode('utf-8', errors='ignore') lines = [line.replace(" ", " ").replace("Name=", "").strip('\x00\x14 \t\n\r\v\f') for line in result.splitlines() if line.strip()] cdef str cpu = lines[0] cdef str gpu = lines[1] # could be multiple gpus len_lines = len(lines) cdef str memory = lines[len_lines-2].replace("TotalVisibleMemorySize=", "").replace(" ", " ") cdef str drive_serial = lines[len_lines-1] cdef str res = f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. DriveSerial: {drive_serial}' return res