File size: 7,635 Bytes
346d87a | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | """
System Resource Detection Module
Detects VRAM, RAM, CPU cores and recommends appropriate ASR models
"""
import psutil
import torch
import os
import sys
from pathlib import Path
# Add project root to path for imports
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).parent.parent))
from config.config import ASR_MODEL_VRAM_MB, ASR_MODEL_RAM_MB
def get_gpu_memory():
"""Get total and available GPU memory in MB"""
try:
if torch.cuda.is_available():
gpu_count = torch.cuda.device_count()
if gpu_count > 0:
# Use first GPU
total_vram = torch.cuda.get_device_properties(0).total_memory
allocated_vram = torch.cuda.memory_allocated(0)
available_vram = total_vram - allocated_vram
return {
'total_mb': total_vram // 1024 // 1024,
'available_mb': available_vram // 1024 // 1024,
'allocated_mb': allocated_vram // 1024 // 1024
}
except:
pass
return {'total_mb': 0, 'available_mb': 0, 'allocated_mb': 0}
def get_system_memory():
"""Get total and available system RAM in MB"""
try:
memory = psutil.virtual_memory()
return {
'total_mb': memory.total // 1024 // 1024,
'available_mb': memory.available // 1024 // 1024,
'used_mb': memory.used // 1024 // 1024
}
except:
return {'total_mb': 0, 'available_mb': 0, 'used_mb': 0}
def get_cpu_cores():
"""Get number of CPU cores"""
try:
return psutil.cpu_count(logical=False) or psutil.cpu_count()
except:
return 1
def estimate_tts_vram_usage():
"""Estimate VRAM usage by ChatterboxTTS (updated based on real usage)"""
return 5500 # 5.5GB in MB (was 7GB, adjusted based on actual 3.5GB usage + buffer)
def get_system_profile():
"""Get complete system resource profile"""
gpu_info = get_gpu_memory()
ram_info = get_system_memory()
cpu_cores = get_cpu_cores()
# Estimate available resources after TTS loading
tts_vram_estimate = estimate_tts_vram_usage()
available_vram_after_tts = max(0, gpu_info['available_mb'] - tts_vram_estimate)
return {
'gpu': gpu_info,
'ram': ram_info,
'cpu_cores': cpu_cores,
'available_vram_after_tts': available_vram_after_tts,
'has_gpu': gpu_info['total_mb'] > 0
}
def categorize_system(profile):
"""Categorize system capabilities"""
gpu_total = profile['gpu']['total_mb']
ram_total = profile['ram']['total_mb']
cpu_cores = profile['cpu_cores']
# VRAM categories
if gpu_total < 4000:
vram_category = "low"
elif gpu_total <= 12000:
vram_category = "medium"
else:
vram_category = "high"
# RAM categories
if ram_total < 16000:
ram_category = "low"
elif ram_total <= 64000:
ram_category = "medium"
else:
ram_category = "high"
# CPU categories
if cpu_cores < 6:
cpu_category = "low"
elif cpu_cores <= 16:
cpu_category = "medium"
else:
cpu_category = "high"
return {
'vram': vram_category,
'ram': ram_category,
'cpu': cpu_category
}
def get_safe_asr_models(profile):
"""Get ASR models that can safely run on GPU with available VRAM"""
available_vram = profile['available_vram_after_tts']
safe_models = []
for model, vram_req in ASR_MODEL_VRAM_MB.items():
if vram_req <= available_vram:
safe_models.append(model)
return safe_models
def get_safe_cpu_models(profile):
"""Get ASR models that can safely run on CPU with available RAM"""
available_ram = profile['ram']['available_mb']
safe_models = []
for model, ram_req in ASR_MODEL_RAM_MB.items():
if ram_req <= available_ram:
safe_models.append(model)
return safe_models
def recommend_asr_models(profile):
"""Recommend Safe/Moderate/Insane ASR model configurations"""
categories = categorize_system(profile)
safe_gpu_models = get_safe_asr_models(profile)
safe_cpu_models = get_safe_cpu_models(profile)
recommendations = {}
# Model priority order (best to worst)
model_priority = ["large-v3", "large", "large-v2", "medium", "small", "base", "tiny"]
# Safe: Conservative choice
safe_gpu = None
safe_cpu = None
for model in reversed(model_priority): # Start from smallest
if model in safe_gpu_models and not safe_gpu:
safe_gpu = model
if model in safe_cpu_models and not safe_cpu:
safe_cpu = model
if safe_gpu and safe_cpu:
break
# Moderate: Balanced choice
moderate_gpu = None
moderate_cpu = None
# Try to get a model 1-2 steps up from safe
safe_idx = model_priority.index(safe_gpu) if safe_gpu else len(model_priority)
moderate_idx = max(0, safe_idx - 2)
for i in range(moderate_idx, len(model_priority)):
model = model_priority[i]
if model in safe_gpu_models and not moderate_gpu:
moderate_gpu = model
if model in safe_cpu_models and not moderate_cpu:
moderate_cpu = model
if moderate_gpu and moderate_cpu:
break
# Insane: Push the limits (best available models)
insane_gpu = None
insane_cpu = None
# Get the best (largest) models that are safe
for model in model_priority: # Start from best
if model in safe_gpu_models and not insane_gpu:
insane_gpu = model
if model in safe_cpu_models and not insane_cpu:
insane_cpu = model
if insane_gpu and insane_cpu:
break
# Build recommendations
recommendations['safe'] = {
'primary': {'model': safe_gpu or safe_cpu, 'device': 'gpu' if safe_gpu else 'cpu'},
'fallback': {'model': safe_cpu, 'device': 'cpu'}
}
recommendations['moderate'] = {
'primary': {'model': moderate_gpu or moderate_cpu, 'device': 'gpu' if moderate_gpu else 'cpu'},
'fallback': {'model': moderate_cpu, 'device': 'cpu'}
}
recommendations['insane'] = {
'primary': {'model': insane_gpu or insane_cpu, 'device': 'gpu' if insane_gpu else 'cpu'},
'fallback': {'model': insane_cpu, 'device': 'cpu'}
}
return recommendations
def print_system_summary(profile):
"""Print a human-readable system summary"""
categories = categorize_system(profile)
print(f"🖥️ System Profile:")
print(f" VRAM: {profile['gpu']['total_mb']:,}MB total, {profile['available_vram_after_tts']:,}MB available after TTS ({categories['vram']} class)")
print(f" RAM: {profile['ram']['total_mb']:,}MB total, {profile['ram']['available_mb']:,}MB available ({categories['ram']} class)")
print(f" CPU: {profile['cpu_cores']} cores ({categories['cpu']} class)")
if not profile['has_gpu']:
print(f" ⚠️ No CUDA GPU detected - ASR will run on CPU only")
if __name__ == "__main__":
# Test the detection
profile = get_system_profile()
print_system_summary(profile)
recommendations = recommend_asr_models(profile)
print(f"\nASR Model Recommendations:")
for level, config in recommendations.items():
primary = config['primary']
fallback = config['fallback']
print(f"🟢 {level.upper()}: {primary['model']} ({primary['device']}) + {fallback['model']} (cpu fallback)") |