verseagility/notebook/Score - Request to Endpoint...

121 строка
3.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Verseagility - Single endpoint request to deployed model\r\n",
"- A notebook to score your endpoint for testing purpose with a single request\r\n",
"- For batch scoring, see the Batch Scoring notebook in the same folder"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"source": [
"# Import packages\r\n",
"import json\r\n",
"import requests\r\n",
"import configparser"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"# Get config file\r\n",
"config = configparser.ConfigParser()\r\n",
"config.read('../config.ini')\r\n",
"endpoint = config['API']['endpoint']\r\n",
"region = config['API']['region']\r\n",
"key = config['API']['key']"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"def score_model(text, endpoint=endpoint, region=region, key=key):\r\n",
" '''Score model endpoint with a single request'''\r\n",
" # URL for the web service\r\n",
" scoring_uri = f'http://{endpoint}.{region}.azurecontainer.io/score'\r\n",
" # Set the content type\r\n",
" headers = {'Content-Type': 'application/json'}\r\n",
" # If authentication is enabled, set the authorization header\r\n",
" headers['Authorization'] = f'Bearer {key}'\r\n",
" scores = []\r\n",
" # Iterate\r\n",
" data = [{\r\n",
" \"subject\": \"\",\r\n",
" \"body\": text.replace(\"'\", \"\").replace('\"', '')\r\n",
" }]\r\n",
" # Convert to JSON string\r\n",
" input_data = json.dumps(data)\r\n",
" # Make the request and display the response\r\n",
" resp = requests.post(scoring_uri, input_data, headers=headers)\r\n",
" try:\r\n",
" pred = json.loads(resp.text)[0]['result'][0]['category']\r\n",
" except:\r\n",
" pred = \"None\"\r\n",
" return pred"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 6,
"source": [
"score_model('windows is such a great product and i love using it, who does agree with me?')"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Windows'"
]
},
"metadata": {},
"execution_count": 6
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"orig_nbformat": 4,
"language_info": {
"name": "python",
"version": "3.7.9",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.9 64-bit ('.venv': venv)"
},
"interpreter": {
"hash": "8f6a11dde7052ac1dd90d2a6ad2b6809c8f3f725167bd8fcdc8934151368885b"
}
},
"nbformat": 4,
"nbformat_minor": 2
}