Chapter 24
Prepare meta.json info
NotebookPython 3 (ipykernel)27 cells
In [4]python · cell 1
python
import io
import json
from pathlib import Path
from urllib.parse import urlparse, parse_qs
import pandas as pd
import numpy as np
import requests
import clipboard
from PIL import Image
from jinja2 import TemplateIn [6]python · cell 2
python
module_number = 3
module_title = 'Orchestration and ML Pipelines'
module_folder = Path('..') / '03-orchestration'
meta_json_file = module_folder / 'meta.json'
playlist_id = 'PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK'In [12]python · cell 3
python
module_number = 5
module_title = 'ML Monitoring'
module_folder = Path('..') / '05-monitoring'
meta_json_file = module_folder / 'meta.json'
playlist_id = 'PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK'In [3]python · cell 4
python
module_number = 6
module_title = 'Best Practices'
module_folder = Path('..') / '06-best-practices'
meta_json_file = module_folder / 'meta.json'
playlist_id = 'PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK'In [4]python · cell 5
python
module_number = 4
module_title = 'Model Deployment'
module_folder = Path('..') / '04-deployment'
meta_json_file = module_folder / 'meta.json'
playlist_id = 'PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK'Prepare meta.json info
In [13]python · cell 7
python
text = """
module_number module_title unit_number unit_title full_title youtube
5 Monitoring 1 Intro to ML monitoring MLOps Zoomcamp 5.1 - Intro to ML monitoring https://www.youtube.com/watch?v=SQ0jBwd_3kk
5 Monitoring 2 Environment setup MLOps Zoomcamp 5.2 - Environment setup https://www.youtube.com/watch?v=yixA3C1xSxc
5 Monitoring 3 Prepare reference and model MLOps Zoomcamp 5.3 - Prepare reference and model https://www.youtube.com/watch?v=IjNrkqMYQeQ
5 Monitoring 4 Evidently metrics calculation MLOps Zoomcamp 5.4 - Evidently metrics calculation https://www.youtube.com/watch?v=kP3lzh_HfWY
5 Monitoring 5 Dummy monitoring MLOps Zoomcamp 5.5 - Dummy monitoring https://www.youtube.com/watch?v=s3G4PMsOMOA
5 Monitoring 6 Data quality monitoring MLOps Zoomcamp 5.6 - Data quality monitoring https://www.youtube.com/watch?v=fytrmPbcLhI
5 Monitoring 7 Save Grafana Dashboard MLOps Zoomcamp 5.7 - Save Grafana Dashboard https://www.youtube.com/watch?v=-c4iumyZMyw
5 Monitoring 8 Debugging with test suites and reports MLOps Zoomcamp 5.8 - Debugging with test suites and reports https://www.youtube.com/watch?v=sNSk3ojISh8""".strip()In [14]python · cell 8
python
df = pd.read_csv(io.StringIO(text), delimiter='\t')In [15]python · cell 9
python
df['youtube'] = df['youtube'].fillna('')In [16]python · cell 10
python
df_units = df[['unit_number', 'unit_title', 'youtube']]
units = df_units \
.rename(columns={'unit_number': 'number', 'unit_title': 'title'}) \
.to_dict(orient='records')
unitsOutput
[{'number': 1,
'title': 'Intro to ML monitoring',
'youtube': 'https://www.youtube.com/watch?v=SQ0jBwd_3kk'},
{'number': 2,
'title': 'Environment setup',
'youtube': 'https://www.youtube.com/watch?v=yixA3C1xSxc'},
{'number': 3,
'title': 'Prepare reference and model',
'youtube': 'https://www.youtube.com/watch?v=IjNrkqMYQeQ'},
{'number': 4,
'title': 'Evidently metrics calculation',
'youtube': 'https://www.youtube.com/watch?v=kP3lzh_HfWY'},
{'number': 5,
'title': 'Dummy monitoring',
'youtube': 'https://www.youtube.com/watch?v=s3G4PMsOMOA'},
{'number': 6,
'title': 'Data quality monitoring',
'youtube': 'https://www.youtube.com/watch?v=fytrmPbcLhI'},
{'number': 7,
'title': 'Save Grafana Dashboard',
'youtube': 'https://www.youtube.com/watch?v=-c4iumyZMyw'},
{'number': 8,
'title': 'Debugging with test suites and reports',
'youtube': 'https://www.youtube.com/watch?v=sNSk3ojISh8'}]In [17]python · cell 11
python
meta = {
'module': {
'number': module_number,
'title': module_title
},
'units': units
}In [18]python · cell 12
python
metaOutput
{'module': {'number': 5, 'title': 'ML Monitoring'},
'units': [{'number': 1,
'title': 'Intro to ML monitoring',
'youtube': 'https://www.youtube.com/watch?v=SQ0jBwd_3kk'},
{'number': 2,
'title': 'Environment setup',
'youtube': 'https://www.youtube.com/watch?v=yixA3C1xSxc'},
{'number': 3,
'title': 'Prepare reference and model',
'youtube': 'https://www.youtube.com/watch?v=IjNrkqMYQeQ'},
{'number': 4,
'title': 'Evidently metrics calculation',
'youtube': 'https://www.youtube.com/watch?v=kP3lzh_HfWY'},
{'number': 5,
'title': 'Dummy monitoring',
'youtube': 'https://www.youtube.com/watch?v=s3G4PMsOMOA'},
{'number': 6,
'title': 'Data quality monitoring',
'youtube': 'https://www.youtube.com/watch?v=fytrmPbcLhI'},
{'number': 7,
'title': 'Save Grafana Dashboard',
'youtube': 'https://www.youtube.com/watch?v=-c4iumyZMyw'},
{'number': 8,
'title': 'Debugging with test suites and reports',
'youtube': 'https://www.youtube.com/watch?v=sNSk3ojISh8'}]}In [19]python · cell 13
python
module_folder.mkdir(parents=True, exist_ok=True)
with open(meta_json_file, 'wt') as f_out:
json.dump(meta, f_out, indent=2)In [20]python · cell 14
python
!head {meta_json_file}Output
{
"module": {
"number": 5,
"title": "ML Monitoring"
},
"units": [
{
"number": 1,
"title": "Intro to ML monitoring",
"youtube": "https://www.youtube.com/watch?v=SQ0jBwd_3kk"
Generate page
module_folder = Path('..') / '05-monitoring' meta_json_file = module_folder / 'meta.json' playlist_id = 'PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK'
In [21]python · cell 17
python
with meta_json_file.open('rt') as f_in:
meta = json.load(f_in)In [22]python · cell 18
python
module_info = meta['module']
units = meta['units']In [23]python · cell 19
python
module_infoOutput
{'number': 5, 'title': 'ML Monitoring'}In [24]python · cell 20
python
images_folder = module_folder / 'images'
images_folder.mkdir(parents=True, exist_ok=True)In [25]python · cell 21
python
template_string = """
## {{ module_number }}.{{ unit_number }} {{ unit_title }}
{% if youtube %}<a href="{{ youtube }}">
<img src="{{ thumbnail }}">
</a>{% endif %}{% if not youtube %}COMING SOON{% endif %}
""".lstrip()
template = Template(template_string)In [28]python · cell 22
python
def download_thumbnail(video, module, unit, folder):
if type(unit) in [int, np.int64]:
thumbnail_file = f'thumbnail-{module}-{unit:02d}.jpg'
else:
thumbnail_file = f'thumbnail-{module}-{unit}.jpg'
thumbnail_file = folder / thumbnail_file
if thumbnail_file.exists():
print(f'{thumbnail_file} exists')
return thumbnail_file
video_id = parse_qs(urlparse(video).query)['v'][0]
print(f'processing video {video_id}...')
thumbnail_url = f'https://img.youtube.com/vi/{video_id}/0.jpg'
response = requests.get(thumbnail_url)
thumbnail = Image.open(io.BytesIO(response.content))
w_img, h_img = thumbnail.size
play = Image.open(Path('../images/play.png'))
w_play, h_play = play.size
x0 = w_img // 2 - w_play // 2
y0 = h_img // 2 - h_play // 2
thumbnail.paste(play, (x0, y0), play)
thumbnail.save(thumbnail_file, quality=90)
print('saved to', thumbnail_file)
return thumbnail_fileIn [ ]python · cell 23
python
In [29]python · cell 24
python
module_number = module_info['number']
parts = []
for unit in units:
unit_number = unit['number']
params = {
'module_number': module_info['number'],
'module_name': module_info['title'],
'unit_number': unit['number'],
'unit_title': unit['title']
}
if 'youtube' in unit:
youtube = unit['youtube']
if len(youtube) and youtube.startswith('https'):
thumbnail = download_thumbnail(youtube, module_number, unit_number, images_folder)
thumbnail_path = '/'.join(thumbnail.parts[2:])
params['youtube'] = f'{youtube}&list={playlist_id}'
params['thumbnail'] = thumbnail_path
template_string = template.render(params)
print(template_string)
parts.append(template_string)Output
processing video SQ0jBwd_3kk... saved to ..\05-monitoring\images\thumbnail-5-01.jpg ## 5.1 Intro to ML monitoring <a href="https://www.youtube.com/watch?v=SQ0jBwd_3kk&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-01.jpg"> </a> processing video yixA3C1xSxc... saved to ..\05-monitoring\images\thumbnail-5-02.jpg ## 5.2 Environment setup <a href="https://www.youtube.com/watch?v=yixA3C1xSxc&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-02.jpg"> </a> processing video IjNrkqMYQeQ... saved to ..\05-monitoring\images\thumbnail-5-03.jpg ## 5.3 Prepare reference and model <a href="https://www.youtube.com/watch?v=IjNrkqMYQeQ&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-03.jpg"> </a> processing video kP3lzh_HfWY... saved to ..\05-monitoring\images\thumbnail-5-04.jpg ## 5.4 Evidently metrics calculation <a href="https://www.youtube.com/watch?v=kP3lzh_HfWY&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-04.jpg"> </a> processing video s3G4PMsOMOA... saved to ..\05-monitoring\images\thumbnail-5-05.jpg ## 5.5 Dummy monitoring <a href="https://www.youtube.com/watch?v=s3G4PMsOMOA&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-05.jpg"> </a> processing video fytrmPbcLhI... saved to ..\05-monitoring\images\thumbnail-5-06.jpg ## 5.6 Data quality monitoring <a href="https://www.youtube.com/watch?v=fytrmPbcLhI&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-06.jpg"> </a> processing video -c4iumyZMyw... saved to ..\05-monitoring\images\thumbnail-5-07.jpg ## 5.7 Save Grafana Dashboard <a href="https://www.youtube.com/watch?v=-c4iumyZMyw&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-07.jpg"> </a> processing video sNSk3ojISh8... saved to ..\05-monitoring\images\thumbnail-5-08.jpg ## 5.8 Debugging with test suites and reports <a href="https://www.youtube.com/watch?v=sNSk3ojISh8&list=PL3MmuxUbc_hIUISrluw_A7wDSmfOhErJK"> <img src="images/thumbnail-5-08.jpg"> </a>
In [30]python · cell 25
python
prefix = f"""
# {module_info['number']}. {module_info['title']}
""".strip()
final_result = '\n\n'.join([prefix] + parts)In [31]python · cell 26
python
clipboard.copy(final_result)In [ ]python · cell 27
python
