Part 4 - Manage, Monitor, and Secure an Azure AI Service
Your first test at running some python code.
Sample Python Code here
- Run this
from dotenv import load_dotenv
import os
import http.client, base64, json, urllib
from urllib import request, parse, error
def main():
global cog_endpoint
global cog_key
try:
# Get Configuration Settings
load_dotenv()
cog_endpoint = os.getenv('COG_SERVICE_ENDPOINT')
cog_key = os.getenv('COG_SERVICE_KEY')
# Get user input (until they enter "quit")
userText =''
while userText.lower() != 'quit':
userText = input('Enter some text ("quit" to stop)\n')
if userText.lower() != 'quit':
GetLanguage(userText)
except Exception as ex:
print(ex)
def GetLanguage(text):
try:
# Construct the JSON request body (a collection of documents, each with an ID and text)
jsonBody = {
"documents":[
{"id": 1,
"text": text}
]
}
# Let's take a look at the JSON we'll send to the service
print(json.dumps(jsonBody, indent=2))
# Make an HTTP request to the REST interface
uri = cog_endpoint.rstrip('/').replace('https://', '')
conn = http.client.HTTPSConnection(uri)
# Add the authentication key to the request header
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': cog_key
}
# Use the Text Analytics language API
conn.request("POST", "/text/analytics/v3.1/languages?", str(jsonBody).encode('utf-8'), headers)
# Send the request
response = conn.getresponse()
data = response.read().decode("UTF-8")
# If the call was successful, get the response
if response.status == 200:
# Display the JSON response in full (just so we can see it)
results = json.loads(data)
print(json.dumps(results, indent=2))
# Extract the detected language name for each document
for document in results["documents"]:
print("\nLanguage:", document["detectedLanguage"]["name"])
else:
# Something went wrong, write the whole response
print(data)
conn.close()
except Exception as ex:
print(ex)
if __name__ == "__main__":
main()
- Then Run
/src/components/test.py
from dotenv import load_dotenv
import os
import http.client, base64, json, urllib
from urllib import request, parse, error
def main():
global cog_endpoint
global cog_key
try:
# Get Configuration Settings
load_dotenv()
cog_endpoint = os.getenv('COG_SERVICE_ENDPOINT')
cog_key = os.getenv('COG_SERVICE_KEY')
# Get user input (until they enter "quit")
userText =''
while userText.lower() != 'quit':
userText = input('Enter some text ("quit" to stop)\n')
if userText.lower() != 'quit':
GetLanguage(userText)
except Exception as ex:
print(ex)
def GetLanguage(text):
try:
# Construct the JSON request body (a collection of documents, each with an ID and text)
jsonBody = {
"documents":[
{"id": 1,
"text": text}
]
}
# Let's take a look at the JSON we'll send to the service
print(json.dumps(jsonBody, indent=2))
# Make an HTTP request to the REST interface
uri = cog_endpoint.rstrip('/').replace('https://', '')
conn = http.client.HTTPSConnection(uri)
# Add the authentication key to the request header
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': cog_key
}
# Use the Text Analytics language API
conn.request("POST", "/text/analytics/v3.1/languages?", str(jsonBody).encode('utf-8'), headers)
# Send the request
response = conn.getresponse()
data = response.read().decode("UTF-8")
# If the call was successful, get the response
if response.status == 200:
# Display the JSON response in full (just so we can see it)
results = json.loads(data)
print(json.dumps(results, indent=2))
# Extract the detected language name for each document
for document in results["documents"]:
print("\nLanguage:", document["detectedLanguage"]["name"])
else:
# Something went wrong, write the whole response
print(data)
conn.close()
except Exception as ex:
print(ex)
if __name__ == "__main__":
main()