# Вызов сервиса DUB-AI из PowerShell. # # Примеры (из корня проекта): # powershell -NoProfile -ExecutionPolicy Bypass -File examples\powershell\predict.ps1 -Text "Перевод: Дублированный | MovieDalen" # powershell -NoProfile -ExecutionPolicy Bypass -File examples\powershell\predict.ps1 -HtmlFile page.html # pwsh -File examples\powershell\predict.ps1 -Text "..." -Url http://host:8000 # # Тело запроса отправляется UTF-8 байтами — это одинаково работает и в # Windows PowerShell 5.1, и в PowerShell 7 (в 5.1 строковый -Body без этого # уходит в ISO-8859-1 и кириллица ломается). param( [string]$Url = "http://127.0.0.1:8000", [Parameter(Mandatory = $true, ParameterSetName = 'Text')] [string]$Text, [Parameter(Mandatory = $true, ParameterSetName = 'Html')] [string]$HtmlFile ) $ErrorActionPreference = 'Stop' try { [Console]::OutputEncoding = [Text.Encoding]::UTF8 } catch { } if ($HtmlFile) { # [IO.File]::ReadAllText даёт чистую строку (Get-Content прикрепляет # свойства PSPath, и ConvertTo-Json отправил бы объект вместо строки) $path = (Resolve-Path -LiteralPath $HtmlFile).Path $html = [IO.File]::ReadAllText($path, [Text.Encoding]::UTF8) if ($html.Contains([string][char]0xFFFD)) { # utf-8 не вышел — пробуем cp1251 $html = [IO.File]::ReadAllText($path, [Text.Encoding]::GetEncoding(1251)) } $payload = @{ html = $html } } else { $payload = @{ text = $Text } } $json = $payload | ConvertTo-Json $bytes = [Text.Encoding]::UTF8.GetBytes($json) try { $r = Invoke-RestMethod -Uri "$Url/predict" -Method Post ` -ContentType 'application/json' -Body $bytes } catch { Write-Error "Сервис недоступен ($Url). Запуск: python -m src.service`n$_" exit 1 } if ($r.tracks) { $r.tracks | Format-Table translation_type, studio, is_18plus, type_confidence -AutoSize foreach ($t in $r.tracks) { " текст: $($t.segment)" if ($t.studio_span) { " спан: $($t.studio_span)" } } } else { "дорожки озвучки не найдены" }