<jupyter_start><jupyter_text>5.10 Homework---In this homework, we'll use the churn prediction model trained on a smaller set of features.  Question 1---- Install Pipenv- What's the version of pipenv you installed?- Use --version to find out<jupyter_code>!pipenv --version
print("[ANSWER-1] The installed version of pipenv is: 2021.5.29")<jupyter_output>[ANSWER-1] The installed version of pipenv is: 2021.5.29<jupyter_text>Question 2---- Use Pipenv to install Scikit-Learn version 1.0- What's the first hash for scikit-learn you get in Pipfile.lock?<jupyter_code>import json

with open("Pipfile.lock") as f:
    data = json.load(f)
    first_hash = data['default']['scikit-learn']['hashes'][0]

print(f"[ANSWER-2] The first hash for scikit-learn is:\n{first_hash}")<jupyter_output>[ANSWER-2] The first hash for scikit-learn is:
sha256:121f78d6564000dc5e968394f45aac87981fcaaf2be40cfcd8f07b2baa1e1829<jupyter_text>Models---We've prepared a dictionary vectorizer and a model.They were trained (roughly) using this code:```features = ['tenure', 'monthlycharges', 'contract']dicts = df[features].to_dict(orient='records')dv = DictVectorizer(sparse=False)X = dv.fit_transform(dicts)model = LogisticRegression().fit(X, y)```And then saved with Pickle. Load them:- DictVectorizer- LogisticRegression<jupyter_code>import pickle

with open("homework/model1.bin", 'rb') as f_model:
    model = pickle.load(f_model)

with open("homework/dv.bin", 'rb') as f_dv:
    dv = pickle.load(f_dv)

model
dv<jupyter_output><empty_output><jupyter_text>Question 3---Let's use these models!- Write a script for loading these models- Score this customer:> {"contract": "two_year", "tenure": 12, "monthlycharges": 19.7}What's the probability that this customer is churning?<jupyter_code>def make_pred(customer, model, dv):
    X = dv.transform([customer])
    return model.predict_proba(X)[0, 1]
customer = {"contract": "two_year", "tenure": 12, "monthlycharges": 19.7}
pred = make_pred(customer, model, dv)

print(f"[ANSWER-3] The probability that this customer is churning is: {round(pred, 3)}")<jupyter_output>[ANSWER-3] The probability that this customer is churning is: 0.115<jupyter_text>Question 4---Now let's serve this model as a web service- Install Flask and Gunicorn (or waitress, if you're on Windows)- Write Flask code for serving the model- Now score this customer using requests:```url = "YOUR_URL"customer = {"contract": "two_year", "tenure": 1, "monthlycharges": 10}requests.post(url, json=customer).json()```What's the probability that this customer is churning? **I execute this line of code in the console**>gunicorn --bind 0.0.0.0:9999 churn_service:app<jupyter_code>import requests

url = "http://localhost:9999/predict"
customer = {"contract": "two_year", "tenure": 1, "monthlycharges": 10}
pred = requests.post(url, json=customer).json()

print(f"[ANSWER-4] The probability that this customer is churning is: {round(pred['churn_probability'], 3)}")<jupyter_output>[ANSWER-4] The probability that this customer is churning is: 0.999<jupyter_text>Docker---Install Docker. We will use it for the next two questions.For these questions, I prepared a base image: **agrigorev/zoomcamp-model:3.8.12-slim**. You'll need to use it (see Question 5 for an example).This image is based on **python:3.8.12-slim** and has a logistic regression model (a different one) as well a dictionary vectorizer inside.This is how the Dockerfile for this image looks like:```FROM python:3.8.12-slimWORKDIR /appCOPY ["model2.bin", "dv.bin", "./"]```I already built it and then pushed it to **agrigorev/zoomcamp-model:3.8.12-slim**.> Note: You don't need to build this docker image, it's just for your reference.  Question 5Now create your own Dockerfile based on the image I prepared.It should start like that:```FROM agrigorev/zoomcamp-model:3.8.12-slim add your stuff here```Now complete it:- Install all the dependencies form the Pipenv file- Copy your Flask script- Run it with gunicornWhen you build your image, what's the digest for **agrigorev/zoomcamp-model:3.8.12-slim**?Look at the first step of your build log. It should look something like that:```Step 1/3 : FROM python:3.8.12-slim ---> 2e56f6b0af69``` **After I build the image with the following commands**>docker build -t zoomcamp_homework_5 .**I got the log:**```Sending build context to Docker daemon  75.26kBStep 1/7 : FROM agrigorev/zoomcamp-model:3.8.12-slim3.8.12-slim: Pulling from agrigorev/zoomcamp-modelbd897bb914af: Pull complete aee78d822213: Pull complete 6d9f6b5c1e71: Pull complete cf9f290bd6be: Pull complete 5e4b501cbda5: Pull complete bd464adb9682: Pull complete c803b748156d: Pull complete Digest: sha256:1ee036b365452f8a1da0dbc3bf5e7dd0557cfd33f0e56b28054d1dbb9c852023Status: Downloaded newer image for agrigorev/zoomcamp-model:3.8.12-slim ---> f0f43f7bc6e0```<jupyter_code>print("[ANSWER-5] The digest for agrigorev/zoomcamp-model:3.8.12-slim is: f0f43f7bc6e0")<jupyter_output>[ANSWER-5] The digest for agrigorev/zoomcamp-model:3.8.12-slim is: f0f43f7bc6e0<jupyter_text>Question 6---Let's run your docker container!After running it, score the same customer:```url = "YOUR_URL"customer = {"contract": "two_year", "tenure": 12, "monthlycharges": 10}requests.post(url, json=customer).json()```What's the probability that this customer is churning? **I run the container with th following line**>docker run -it --rm -p 9999:9999 zoomcamp_homework_5<jupyter_code>import requests

url = "http://localhost:9999/predict"
customer = {"contract": "two_year", "tenure": 12, "monthlycharges": 10}
pred = requests.post(url, json=customer).json()

print(f"[ANSWER-6] The probability that this customer is churning is: {round(pred['churn_probability'], 3)}")<jupyter_output>[ANSWER-6] The probability that this customer is churning is: 0.728<jupyter_text>Testing Heroku Service<jupyter_code>import requests

url = "https://churn-service.herokuapp.com/predict"
customer = {"contract": "two_year", "tenure": 1, "monthlycharges": 10}
pred = requests.post(url, json=customer).json()

print(f"The probability that this customer is churning is: {round(pred['churn_probability'], 3)}")<jupyter_output>The probability that this customer is churning is: 0.999
{'fraction_non_alphanumeric': 0.1262512108492089, 'fraction_numerical': 0.05279302550855667, 'mean_word_length': 5.976351351351352, 'pattern_counts': {'":': 21, '<': 26, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '563973', 'license': 'MIT', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2086, 'n_words': 620, 'path': 'week_5_deployment/ML_ZOOMCAMP_HOMEWORK_SESSION_5.ipynb', 'repo_name': 'razekmaiden/ml_zoomcamp', 'size': '6194'}