Recently Written · git

desktop-subtitle-replay

git clone https://github.com/equwal/desktop-subtitle-replay

Log | Files | Refs


get-finnish-model.ps1 (4800 bytes)

1 # Optional quality upgrade: a Finnish-fine-tuned Whisper 'small', converted to
2 # CTranslate2 int8 so faster-whisper can run it.
3 #
4 # Plain 'small' is a generalist and merely OK at Finnish. This fine-tune is the
5 # same size (= same speed on your CPU) but much better at Finnish specifically.
6 #
7 # torch + transformers are needed only for the one-off conversion, so they go
8 # into a throwaway venv under -BuildRoot, which you can delete afterwards.
9 # Needs roughly 8 GB free; use -BuildRoot to build on a roomier drive.
10 #
11 #   .\get-finnish-model.ps1
12 #   .\get-finnish-model.ps1 -BuildRoot D:\livecap-build
13 #   .\run.ps1 --model .\build\models\fi-small-ct2
14 
15 param(
16     [string]$BuildRoot = "$PSScriptRoot\build"
17 )
18 
19 $ErrorActionPreference = "Stop"
20 Set-Location $PSScriptRoot
21 
22 $src   = "RASMUS/Whisper_Finnish_finetuned_small_200k_samples"
23 $out   = Join-Path $BuildRoot "models\fi-small-ct2"
24 $venv  = Join-Path $BuildRoot ".venv-convert"
25 
26 try {
27     New-Item -ItemType Directory -Force -Path $BuildRoot -ErrorAction Stop | Out-Null
28 } catch {
29     throw "Cannot create $BuildRoot ($($_.Exception.Message)).`n" +
30           "Pick a writable location:  .\get-finnish-model.ps1 -BuildRoot D:\some\writable\dir"
31 }
32 
33 # Keep the multi-GB HF download off C: as well.
34 $env:HF_HOME = Join-Path $BuildRoot "hf-cache"
35 $env:PIP_CACHE_DIR = Join-Path $BuildRoot "pip-cache"
36 
37 if (Test-Path $out) {
38     Write-Host "$out already exists. Delete it to rebuild." -ForegroundColor Yellow
39     Write-Host "Use it with:  .\run.ps1 --model `"$out`""
40     exit 0
41 }
42 
43 $freeGB = $null
44 try {
45     $root = [System.IO.Path]::GetPathRoot((Resolve-Path $BuildRoot).Path)
46     $freeGB = [math]::Round((Get-CimInstance Win32_LogicalDisk `
47         -Filter "DeviceID='$($root.TrimEnd('\'))'").FreeSpace / 1GB, 1)
48 } catch { }
49 
50 if ($freeGB) {
51     Write-Host "Build root: $BuildRoot  ($freeGB GB free)" -ForegroundColor Cyan
52     if ($freeGB -lt 8) {
53         throw "Need ~8 GB free for the conversion; only $freeGB GB available on $root.`n" +
54               "Free up space, or use -BuildRoot on a roomier drive."
55     }
56 } else {
57     Write-Host "Build root: $BuildRoot (free space unknown; need ~8 GB)" -ForegroundColor Cyan
58 }
59 
60 if (-not (Test-Path "$venv\Scripts\python.exe")) {
61     Write-Host "Creating conversion venv (one-time, ~2.5 GB of torch)..." -ForegroundColor Cyan
62     # pip and venv both write progress to stderr, which would otherwise abort
63     # this script under ErrorActionPreference = Stop. Check exit codes instead.
64     $ErrorActionPreference = "Continue"
65     & py -3.11 -m venv $venv
66     if ($LASTEXITCODE -ne 0) { $ErrorActionPreference = "Stop"; throw "could not create venv at $venv" }
67     & "$venv\Scripts\python.exe" -m pip install --upgrade pip --quiet
68     & "$venv\Scripts\python.exe" -m pip install --index-url https://download.pytorch.org/whl/cpu torch
69     if ($LASTEXITCODE -ne 0) { $ErrorActionPreference = "Stop"; throw "torch install failed" }
70     & "$venv\Scripts\python.exe" -m pip install transformers ctranslate2 "numpy<3"
71     if ($LASTEXITCODE -ne 0) { $ErrorActionPreference = "Stop"; throw "transformers/ctranslate2 install failed" }
72     $ErrorActionPreference = "Stop"
73 }
74 
75 Write-Host "Downloading + converting $src ..." -ForegroundColor Cyan
76 
77 # Windows PowerShell turns any native stderr output into a terminating error
78 # while ErrorActionPreference is Stop, and the HF Hub always warns about
79 # unauthenticated requests. Judge these calls by their exit code instead.
80 $ErrorActionPreference = "Continue"
81 
82 & "$venv\Scripts\ct2-transformers-converter.exe" `
83     --model $src `
84     --output_dir $out `
85     --quantization int8 `
86     --copy_files preprocessor_config.json tokenizer_config.json special_tokens_map.json `
87                  vocab.json merges.txt normalizer.json added_tokens.json
88 if ($LASTEXITCODE -ne 0) { $ErrorActionPreference = "Stop"; throw "conversion failed" }
89 
90 # faster-whisper wants a real tokenizer.json, otherwise it quietly falls back to
91 # the stock openai/whisper-tiny tokenizer and mis-decodes.
92 $py = "from transformers import WhisperTokenizerFast" + "`n" +
93       "WhisperTokenizerFast.from_pretrained(r'$src').save_pretrained(r'$out')" + "`n" +
94       "print('tokenizer.json written')"
95 $py | & "$venv\Scripts\python.exe" -
96 if ($LASTEXITCODE -ne 0) { $ErrorActionPreference = "Stop"; throw "tokenizer export failed" }
97 
98 $ErrorActionPreference = "Stop"
99 if (-not (Test-Path (Join-Path $out "model.bin"))) { throw "no model.bin in $out" }
100 if (-not (Test-Path (Join-Path $out "tokenizer.json"))) {
101     Write-Host "WARNING: tokenizer.json missing; faster-whisper will fall back " -ForegroundColor Yellow
102 }
103 
104 Write-Host ""
105 Write-Host "Done -> $out" -ForegroundColor Green
106 Write-Host "Use it with:  .\run.ps1 --model `"$out`""
107 Write-Host "Reclaim space afterwards with:  Remove-Item -Recurse -Force `"$venv`""