Custom text single label classification - Model API Consumption within Databricks

Aziz Öztürk 20 Reputation points
2024-04-18T11:00:16.9066667+00:00

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))

 

 

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
1,928 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,382 questions
{count} votes

1 answer

Sort by: Most helpful
  1. PRADEEPCHEEKATLA-MSFT 77,426 Reputation points Microsoft Employee
    2024-04-18T11:54:11.46+00:00

    @Aziz Öztürk - Thanks for the question and using MS Q&A platform.

    It seems like you are trying to consume a model API within Databricks Notebook, but you are getting an error. The error message you provided indicates that the resource you are trying to access is not found. This could be due to a few reasons, such as incorrect URL or authentication issues.

    To troubleshoot this issue, you can try the following steps:

    • Double-check the URL you are using to access the model API. Make sure it is correct and complete.
    • Check if you have the necessary permissions to access the resource. You may need to authenticate yourself before accessing the resource.
    • Verify that the resource you are trying to access exists. You can check this by navigating to the resource in the Azure portal.
    • Check if there are any network or firewall issues that may be preventing you from accessing the resource.

    If you have tried these steps and are still experiencing issues, please provide more information about your setup and the error message you are receiving.

    Hope this helps. Do let us know if you any further queries.

    0 comments No comments