For the lab: 04-text-classification/classify-text.ps1, it seems the request URI and the request is not aligned with the schema. Suggested code is provided as follows:
$key="<YOUR-KEY>"
$endpoint="<YOUR-ENDPOINT>"
$projectName = "ClassifyLab"
$deploymentName = "articles"
$verbose = $true
# Set up headers for API call
$headers = @{}
$headers.add("Ocp-Apim-Subscription-Key", $key)
$headers.add("Content-Type","application/json")
# Get text to classify
$text_file = "test1.txt"
if ($args.count -gt 0)
{
$text_file = $args[0]
}
try {
$contents = get-content .\$text_file -raw -ErrorAction Stop
}
catch {
Write-Host "`nError reading provided file, please verify file exists`n"
Exit
}
# Build body of for API call
$data = @{
"displayName" = "Classifying documents"
"tasks" = @(
@{
"kind" = "CustomSingleLabelClassification"
"taskName" = "Single Classification Label"
"parameters"= @{
"projectName" = $projectName
"deploymentName" = $deploymentName
}
}
)
"analysisInput" = @{
"documents" = @(
@{
"id" = "document_singleClassification"
"language" = "en-us"
"text" = $contents
}
)
}
} | ConvertTo-Json -Depth 10
# Post text for classification
Write-Host("`n***Submitting text classification task***")
$response = Invoke-WebRequest -Method Post `
-Uri "$($endpoint)language/analyze-text/jobs?api-version=2022-05-01" `
-Headers $headers `
-Body $data
# Output response if desired
if ($verbose) {
Write-Host("`nResponse header:$($response.Headers['Operation-Location'])`n")
}
# Extract the URL from the response
# to call the API to getting the analysis results
$resultUrl = $($response.Headers['Operation-Location'])
# Create the header for the REST GET with only the subscription key
$resultHeaders = @{}
$resultHeaders.Add( "Ocp-Apim-Subscription-Key", $key )
# Get the results
# Continue to request results until the analysis is "succeeded"
Write-Host "Getting results..."
Do {
$result = Invoke-RestMethod -Method Get `
-Uri $resultUrl `
-Headers $resultHeaders | ConvertTo-Json -Depth 10
$analysis = ($result | ConvertFrom-Json)
} while ($analysis.status -ne "succeeded")
Write-Host "...Done`n"
# Access the relevant fields from the analysis
$classification = $result | ConvertFrom-Json
#$docs = $classification.tasks.customSingleClassificationTasks[0].results.documents
$docs = $classification.tasks.items[0].results.documents
# Output response if desired
if ($verbose) {
Write-Host("GET JSON Response:`n$result`n")
}
# Output response if desired
if ($verbose) {
Write-Host("GET JSON Response:`n$result`n")
}
for (($idx = 0); $idx -lt $docs.Length; $idx++) {
$item = $docs[$idx]
Write-Host ("Document #", ($idx+1))
Write-Host (" - Predicted Category: ", $($item.class.category))
Write-Host (" - Confidence: ",$($item.class.confidenceScore))
}