Skip to content

Instantly share code, notes, and snippets.

@mrcrilly
Created April 9, 2022 03:26
Show Gist options
  • Save mrcrilly/0b1f671536526a2c09568df6814a8392 to your computer and use it in GitHub Desktop.
Save mrcrilly/0b1f671536526a2c09568df6814a8392 to your computer and use it in GitHub Desktop.
Annoying Azure API
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type Document struct {
Language string `json:"language"`
ID int `json:"id"`
Text string `json:"text"`
}
type ExtractiveSummarizationTask struct {
Parameters map[string]interface{} `json:"parameters"`
}
type AnalysisInput struct {
Documents []Document `json:"documents"`
}
type Request struct {
Input AnalysisInput `json:"analysisInput"`
Tasks map[string][]interface{} `json:"tasks"`
}
type Result struct {
Created time.Time `json:"createdDateTime"`
Updated time.Time `json:"lastUpdateDateTime"`
Errors []interface{} `json:"errors"`
Status string `json:"status"`
Tasks map[string]int `json:"tasks"`
}
const (
subscriptionKey = "abc123"
endpoint = "https://abc123.cognitiveservices.azure.com"
uriPath = "/text/analytics/v3.2-preview.1/analyze"
apiURL = endpoint + uriPath
)
func getHttpClient() *http.Client {
return &http.Client{
Timeout: time.Second * 10,
}
}
func makeHttpRequest(verb, url string, body interface{}) *http.Request {
var err error
var bodyAsJson []byte
if body != nil {
bodyAsJson, err = json.Marshal(body)
if err != nil {
log.Fatalln(err)
}
}
var httpRequest *http.Request
if body != nil {
httpRequest, err = http.NewRequest(verb, url, bytes.NewBuffer(bodyAsJson))
} else {
httpRequest, err = http.NewRequest(verb, url, nil)
}
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return nil
}
httpRequest.Header.Add("Content-Type", "application/json")
httpRequest.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
return httpRequest
}
func main() {
input := Request{
Input: AnalysisInput{
Documents: []Document{
{
Language: "en",
ID: 1,
Text: "At Microsoft, we have been on a quest to advance AI beyond existing techniques, by taking a more holistic, human-centric approach to learning and understanding. As Chief Technology Officer of Azure AI Cognitive Services, I have been working with a team of amazing scientists and engineers to turn this quest into a reality. In my role, I enjoy a unique perspective in viewing the relationship among three attributes of human cognition: monolingual text (X), audio or visual sensory signals, (Y) and multilingual (Z). At the intersection of all three, there’s magic—what we call XYZ-code as illustrated in Figure 1—a joint representation to create more powerful AI that can speak, hear, see, and understand humans better. We believe XYZ-code will enable us to fulfill our long-term vision: cross-domain transfer learning, spanning modalities and languages. The goal is to have pretrained models that can jointly learn representations to support a broad range of downstream AI tasks, much in the way humans do today. Over the past five years, we have achieved human performance on benchmarks in conversational speech recognition, machine translation, conversational question answering, machine reading comprehension, and image captioning. These five breakthroughs provided us with strong signals toward our more ambitious aspiration to produce a leap in AI capabilities, achieving multisensory and multilingual learning that is closer in line with how humans learn and understand. I believe the joint XYZ-code is a foundational component of this aspiration, if grounded with external knowledge sources in the downstream AI tasks.",
},
},
},
Tasks: map[string][]interface{}{
"extractiveSummarizationTasks": {
ExtractiveSummarizationTask{
Parameters: map[string]interface{}{
"model-version": "latest",
"sentenceCount": 3,
"sortBy": "Offset",
},
},
},
},
}
fmt.Printf("Issuing POST of analysis request to %s\n", apiURL)
request := makeHttpRequest("POST", apiURL, input)
httpClient := getHttpClient()
resp, err := httpClient.Do(request)
if err != nil {
fmt.Printf("Error on request: %v\n", err)
return
}
resultsUrl := resp.Header["Operation-Location"][0]
fmt.Printf("Request was successful. Got results URL back: %s\n", resultsUrl)
for {
request = makeHttpRequest("GET", resultsUrl, nil)
result := checkAnalysisTaskResults(resultsUrl)
finish := false
switch result.Status {
case "notStarted":
fmt.Println("Task hasn't started yet, sleeping...")
time.Sleep(time.Second * 5)
continue
case "running":
fmt.Println("Task is running...")
time.Sleep(time.Second * 5)
continue
case "succeeded":
fmt.Println("Task was successful")
fmt.Printf("Task :%#v\n\n", result.Tasks)
finish = true
break
default:
fmt.Printf("Unknown task status: %s\n", result.Status)
finish = true
break
}
if finish {
break
}
}
fmt.Println("Finished.")
// fmt.Printf("%s:\n%#v\n\n\n", resp.Status, f)
}
func checkAnalysisTaskResults(url string) *Result {
fmt.Printf("issuing GET to results URL to fetch results: %s\n", url)
httpClient := getHttpClient()
resultsRequest := makeHttpRequest("GET", url, nil)
resp, err := httpClient.Do(resultsRequest)
if err != nil {
fmt.Println(err.Error())
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %v\n", err)
return nil
}
var r *Result
json.Unmarshal(body, &r)
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment