<jupyter_start><jupyter_text>Test the deployed web application In this notebook, we test the local web app running inside the Docker container we made previously.<jupyter_code>import os
import numpy as np
import requests
import pandas as pd
import json
from timer import elapsed

from utilities import text_to_json
%load_ext dotenv
%dotenv
image_name = os.getenv('docker_login') + os.getenv('image_repo')
image_name<jupyter_output><empty_output><jupyter_text>Run the Docker conatainer in the background and open port 80.<jupyter_code>%%bash --bg -s "$image_name"
docker run -p 5000:5000 $1<jupyter_output><empty_output><jupyter_text>Wait a few seconds for the application to spin up and then check that everything works.<jupyter_code>!curl "http://0.0.0.0:5000/"
!ping 0.0.0.0
!curl "http://0.0.0.0:80/version"<jupyter_output>curl: (7) Failed to connect to 0.0.0.0 port 80: Connection refused<jupyter_text>Now, let's use one of the duplicate questions to test our driver.<jupyter_code>dupes_test_path = 'dupes_test.tsv'
dupes_test = pd.read_csv(dupes_test_path, sep='\t', encoding='latin1')
text_to_score = dupes_test.iloc[0,4]
text_to_score
jsontext = text_to_json(text_to_score)
jsontext[:100]
headers = {'content-type': 'application/json'}
%time r = requests.post('http://0.0.0.0:80/score', data=jsontext, headers=headers)
print(r)
r.json()<jupyter_output><empty_output><jupyter_text>Let's try a few more duplicate questions and display their top 3 original matches.<jupyter_code>dupes_to_score = dupes_test.iloc[:5,4]
url = 'http://0.0.0.0:80/score'
results = [requests.post(url, data=text_to_json(text), headers=headers) for text in dupes_to_score]<jupyter_output><empty_output><jupyter_text>Let's print top 3 matches for each duplicate question.<jupyter_code>[results[i].json()['result'][0][0:3] for i in range(0, len(results))]<jupyter_output><empty_output><jupyter_text>Next let's quickly check what the request response performance is for the locally running Docker container.<jupyter_code>text_data = list(map(text_to_json, dupes_to_score)) # Retrieve the text data
timer_results = [elapsed(requests.post, return_elapsed=True)(url, data=data, headers=headers)
                 for data in text_data]
timer_results
print('Average time taken: {0:4.2f} ms'.format(10**3 * np.mean(timer_results)))
%%bash
docker stop $(docker ps -q)<jupyter_output>WARNING: Error loading config file: /home/kwarodom/.docker/config.json: stat /home/kwarodom/.docker/config.json: permission denied
"docker stop" requires at least 1 argument.
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers
{'fraction_non_alphanumeric': 0.11862485833018512, 'fraction_numerical': 0.02606724593879864, 'mean_word_length': 6.438202247191011, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '43253', 'license': 'MIT', 'n_tokens_mistral': 952, 'n_tokens_neox': 894, 'n_words': 242, 'path': '04_Test_Locally.ipynb', 'repo_name': 'kwarodom/MLAKSDeployment', 'size': '2647'}