[AI] PowerShell 腳本筆記: Whisper 模型快取檔案
PowerShell 腳本筆記:清除 Whisper 模型快取檔案
Whisper / WhisperX 模型會快取在 C:\Users\你的帳號\.cache\huggingface
中,可能佔用大量磁碟空間。
以下腳本可自動掃描快取目錄中與 Whisper 相關的模型,列出清單並可手動選擇刪除,有助於釋放磁碟空間。
📜 腳本內容
# PowerShell script to find and optionally delete cached Whisper model directories from Hugging Face
$cacheDir = Join-Path $Env:USERPROFILE '.cache\huggingface\hub'
if (-not (Test-Path $cacheDir)) {
Write-Host "Hugging Face cache directory not found."
return
}
# Get all model cache directories under the hub that contain "whisper"
$whisperDirs = Get-ChildItem -Path $cacheDir -Directory | Where-Object { $_.Name -like '*whisper*' } | Sort-Object Name
if ($whisperDirs.Count -eq 0) {
Write-Host "No Whisper model caches found."
return
}
# List found model caches with their sizes
Write-Host "Whisper model caches found:"
$index = 1
foreach ($dir in $whisperDirs) {
# Extract model name from directory (everything after the second "--")
$parts = $dir.Name -split '--'
$modelName = if ($parts.Length -ge 3) { ($parts[2..($parts.Length - 1)] -join '--') } else { $dir.Name }
# Calculate directory size in MB (including contents)
$sizeBytes = (Get-ChildItem -LiteralPath $dir.FullName -File -Recurse | Measure-Object -Property Length -Sum).Sum
if ($null -eq $sizeBytes) { $sizeBytes = 0 }
$sizeMB = [Math]::Round($sizeBytes / 1MB, 2)
Write-Host "[$index] $modelName - $sizeMB MB"
$index++
}
# Prompt user for selection
$selection = Read-Host "Enter the number(s) of model caches to delete (e.g. 1,3,5), or press Enter to cancel:"
if ([string]::IsNullOrWhiteSpace($selection)) {
Write-Host "No selection made. Exiting."
return
}
# Parse the input into a list of indices
$tokens = $selection -split '[, \t]+'
$tokens = $tokens | Where-Object { $_ -ne '' }
$indices = @()
foreach ($t in $tokens) {
if ($t -match '^\d+$') {
$indices += [int]$t
}
}
$indices = $indices | Sort-Object -Unique
if ($indices.Count -eq 0) {
Write-Host "No valid numbers entered. Exiting."
return
}
# Delete selected model caches
foreach ($idx in $indices) {
if ($idx -lt 1 -or $idx -gt $whisperDirs.Count) {
Write-Host "Selection $idx is not a valid choice."
continue
}
$dir = $whisperDirs[$idx - 1]
$parts = $dir.Name -split '--'
$modelName = if ($parts.Length -ge 3) { ($parts[2..($parts.Length - 1)] -join '--') } else { $dir.Name }
try {
Remove-Item -LiteralPath $dir.FullName -Recurse -Force -ErrorAction Stop
Write-Host "Deleted model cache '$modelName' successfully."
}
catch {
Write-Host "Failed to delete '$modelName': $($_.Exception.Message)"
}
}
🛠️ 使用方式
- 將上述內容儲存為
clear_whisper_cache.ps1
- 開啟 PowerShell(建議使用系統管理員身份)
- 執行以下命令:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\clear_whisper_cache.ps1
📌 備註
支援列出所有包含 whisper 的 Hugging Face 快取模型,並可選擇清除以釋放空間。
撰寫日期:2025-04-16|作者:KingChang with ChatGPT
留言
張貼留言