Hello together,
i trained a model within Azure Language Studio , Custom text single label classification - and i want to consume Model API within Databricks Notebook.
I get always below given error, kindly asking for your help.
Thanks.
Error:
HTTP Response Status: HTTP/1.1 202 Accepted HTTP Response Body: Job Status Check HTTP Response: {"error":{"code":"404","message": "Resource not found"}} Job Status Check HTTP Response: {"error":{"code":"404","message": "Resource not found"}} Job Status Check HTTP Response: {"error":{"code":"404","message": "Resource not found"}} Job Status Check HTTP Response: {"error":{"code":"404","message": "Resource not found"}}
Cancelled
Consumption Code within Databricks Notebook:
%scala
import org.apache.http.impl.client.HttpClients
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.util.EntityUtils
import org.apache.http.client.methods.HttpGet
import scala.util.parsing.json.JSON
val apiKey = "xxxxx"
val modelUrl = "https://xxxxxx"
def submitClassificationJob(comment: String): String = {
val httpClient = HttpClients.createDefault()
val httpPost = new HttpPost(modelUrl)
val postData = s"""{
"tasks": [
{
"kind": "CustomSingleLabelClassification",
"parameters": {
"projectName": "xxx",
"deploymentName": "xxxx"
}
}
],
"displayName": "CustomTextPortal_CustomSingleLabelClassification",
"analysisInput": {
"documents": [
{
"id": "1",
"text": "$comment",
"language": "de"
}
]
}
}"""
httpPost.setEntity(new StringEntity(postData))
httpPost.setHeader("Content-Type", "application/json")
httpPost.setHeader("Ocp-Apim-Subscription-Key", apiKey)
val response = httpClient.execute(httpPost)
val entity = EntityUtils.toString(response.getEntity)
println(s"HTTP Response Status: ${response.getStatusLine}")
println(s"HTTP Response Body: $entity")
val jsonResponse = JSON.parseFull(entity).getOrElse(Map())
val operationId = jsonResponse.asInstanceOf[Map[String, Any]].get("operationId").map(_.toString).getOrElse("")
httpClient.close()
operationId
}
def retrieveClassificationResult(operationId: String): String = {
val httpClient = HttpClients.createDefault()
val statusCheckUrl = modelUrl + "/" + operationId
var jobStatus = ""
var result = ""
do {
val httpGet = new HttpGet(statusCheckUrl)
httpGet.setHeader("Ocp-Apim-Subscription-Key", apiKey)
val response = httpClient.execute(httpGet)
val entity = EntityUtils.toString(response.getEntity)
println(s"Job Status Check HTTP Response: $entity")
val jsonResponse = JSON.parseFull(entity).getOrElse(Map())
jobStatus = jsonResponse.asInstanceOf[Map[String, Any]].get("status").map(_.toString).getOrElse("")
if (jobStatus == "succeeded") {
val predictions = jsonResponse.asInstanceOfMap[String, Any].asInstanceOf[List[Map[String, Any]]]
result = predictions.head("predictions").asInstanceOf[List[Map[String, String]]].head("category")
}
Thread.sleep(1000)
} while (jobStatus != "succeeded" && jobStatus != "failed")
httpClient.close()
result
}
def predictCategory(comment: String): String = {
val operationId = submitClassificationJob(comment)
val prediction = retrieveClassificationResult(operationId)
prediction
}
// Test
val testComment = "Dies ist ein Beispieltext zur Klassifizierung."
println(predictCategory(testComment))