PowerShell-клиент predict.ps1 + charset=utf-8 в ответах сервиса (фикс кириллицы в PS 5.1)

This commit is contained in:
DUB-AI committed 2026-10-27 16:47:45 +07:00
1 parent 8dbca3f4a3
commit 98e4031037
4 files changed
+91 -1

No files matched your search

+1
View File
@@ -7,6 +7,7 @@
| `typescript/predict.mjs` | Node.js >= 18 | HTTP | CLI-клиент сервиса: `--text` / `--html`, без зависимостей |
| `typescript/predict.ts` | TypeScript | HTTP | то же с типами (`npx tsx predict.ts --text "..."`) |
| `typescript/onnx_local.mjs` | Node.js | ONNX | локальный инференс без Python (`onnxruntime-node` + `@huggingface/transformers`) |
| `powershell/predict.ps1` | PowerShell 5.1/7 | HTTP | `-Text "..."` / `-HtmlFile страница.html` (кириллица и кодировки учтены) |
| `rust/http_client` | Rust | HTTP | `cargo run -- --text "..."` / `--html файл` (reqwest + tokio) |
| `rust/onnx_local` | Rust | ONNX | локальный инференс (`ort` + `tokenizers`) |
+56
View File
@@ -0,0 +1,56 @@
# Вызов сервиса 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 {
"дорожки озвучки не найдены"
}