Azure Language Model consume from Databricks Notebook

Aziz Öztürk 20 Zuverlässigkeitspunkte
2024-04-18T10:55:31.9233333+00:00

Dear together,

within Language Studio , i trained a Custom text single label classification model which i want to consume on databricks notebook, i get always following error , kindly need your help.

Thanks.

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"}}

herewith consumption code:

%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 SDKs
Azure SDKs
Eine Reihe von Visual Studio-Tools, Befehlszeilentools, Laufzeitbinärdateien und Clientbibliotheken, die Clients beim Entwickeln, Testen und Bereitstellen von Apps unterstützen, die in Azure ausgeführt werden.
26 Fragen
0 Kommentare Keine Kommentare
{count} Stimmen

1 Antwort

Sortieren nach: Am hilfreichsten
  1. Dimitar Denkov (Convergys Corporation) 880 Zuverlässigkeitspunkte Microsoft-Anbieter
    2024-04-19T07:54:52.89+00:00

    Hallo Aziz,

    Da Du die Frage in den deutschsprachigen Q&A gestellt hast, kann die Diskussion auf Deutsch weitergehen. Würdest Du überprüfen, ob Deine Version von Databricks Runtime 8.4 oder höher ist? Dies ist für die Kompatibilität mit Nicht-Notebookdateien erforderlich, wie in diesem Thread erklärt wird:

    https://learn.microsoft.com/de-de/azure/databricks/repos/errors-troubleshooting#resource-not-found-errors-with-non-notebook-files

    Gruß,

    Dimitar