File size: 9,023 Bytes
6bed634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2572a87
6bed634
 
 
 
 
 
 
 
 
2572a87
 
 
 
 
 
 
 
 
 
 
6bed634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d723871
6bed634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11ffd9f
6bed634
dbaa5e0
6bed634
11ffd9f
6bed634
 
dbaa5e0
6bed634
 
 
11ffd9f
 
d723871
 
 
 
 
 
 
 
 
 
 
 
11ffd9f
 
 
d723871
11ffd9f
d723871
11ffd9f
d723871
6bed634
d723871
6bed634
d723871
6bed634
d723871
 
 
 
 
6bed634
d723871
6bed634
d723871
6bed634
2572a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bed634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d723871
 
 
 
6bed634
 
 
 
 
 
 
 
 
 
 
 
 
d723871
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
=========
DevOps
=========

===============
Operations
===============

>>> Run the app locally in the terminal: 

$ mkdir -p /tmp/models && cp ../Drug-Classifier/model/drug-classifier-pipeline.skops /tmp/models
$ export MODEL_FILE_PATH=/tmp/models/drug-classifier-pipeline.skops && python app/drug_app.py 
...
[...] - [INFO] - >> Loading the model located at [/tmp/model/drug-classifier-pipeline.skops]
[...] - [DEBUG] -  Loaded training pipeline: Pipeline(steps=[('preprocessing',
...
[...] - [INFO] - Done!
...
* Running on local URL:  http://127.0.0.1:7860/
...

>>> Docker containerization: 

Before all, we need to containerize the web app any time we ear abot depeloyment. Thus let us add a Dockerfile and test if 
every thing goes fine locally.

Build Docker image and tag it: 

(...)$ docker build -t drug-classifier-webapp-img:1.0.0 .
(...)$ docker tag drug-classifier-webapp-img:1.0.0 drug-classifier-webapp-img:latest 

>>> Run the app as a container: 

(from the project top directory)

- Run: 

$ mkdir -p /tmp/models && cp ../Drug-Classifier/model/drug-classifier-pipeline.skops /tmp/models 
$ docker run --rm -t \
    -v /tmp/models:/models \
    -e MODEL_FILE_PATH=/models/drug-classifier-pipeline.skops \
    --name drug-classifier-webapp  \
    drug-classifier-webapp-img:latest

Get IP address of the container:

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' drug-classifier-webapp
172.17.0.2

Then browse: http://172.17.0.2:7860/

See [drug-classifier-gradio-gui.png] for an example of the web app. 

- To connect to the container: 

$ docker run --rm -it drug-classifier-webapp-img:latest /bin/bash
root@ed3092b01a68:/app# 
# ls -l 
total 8
-rw-rw-r-- 1 root root 3122 xxxx drug_app.py
-rw-rw-r-- 1 root root    0 xxxx models
-rw-rw-r-- 1 root root 1156 xxxx requirements.txt


                                ===============
                                    CI/CD
                                ===============

Step 0: Prerequisites
===================== 

>>> Locally

- Make sure the requirements.txt is up-to-date: 
    $ uv pip freeze >requirements.txt

>>> Hugging Face

Hugging Face Token: Profile > Settings > Access Tokens  
    + Create New Token 
        type = Write 
        name = GITHUB_ACTIONS_WRT_TOKEN

>>> On Github

Create the following secrets: Settings tab > Secrets and variables > Actions > New Repositories Secret 
    - USER_NAME = regisklaconsulting
    - USER_EMAIL = regisklaconsulting@gmail.com 
    - HF_TOKEN = value of GITHUB_ACTIONS_WRT_TOKEN 

    Note: they are used in the Makefile script we'll create later. 

Create a branch "to-be-deployed" 
    - We create this branch in order for it to always contain the last built version to be deployed 
    - Github Actions workflow: 
        (a) build the project 
        (b) if build ok then 
                commit all generated files (e.g. test reports, metrics, etc)
                push on "to-be-deployed" 
            fi 

Continuous integration (CI) 
===========================

Expected outcome: 
    - Application is compile and/or built 
    - Unit and integration tests are successfully run 
    - 

Step 1: Create a Makefile script (see file) 
    - permissions to write 
    - 

Step 2: Start with a Python workflow (wf) template: [Repo > Actions]
    - In "Suggested for this repository" 
    - Select "Python Application" 
    - Click "Configure" and adapt the script with your settings: 
        - triggers 
        - Python version
        - ... 
    => the file .github/worflows/python-app.yml is created 
    - Pull to update: 
        $ git pull 

Note: 
- Push and fix the issues in the Actions tab until the build (in Actions tab) becomes green.

Continuous Deployment (CD) 
==========================

>>> Step 1: Create the HF space 

Go to the documentation [https://huggingface.co/docs/hub/spaces-sdks-docker-first-demo] and execute the steps. 

Go to here [https://huggingface.co/new-space] and create a new Space:

- User name = regisklaconsulting
- Space Name = Drug-Classifier-Web-App 
- Space ID =  regisklaconsulting/Drug-Classifier-Web-App
- Short desk = The Drug Classifier Web App hosting space 
- SDK = docker
- Hardware = CPU ... Free
- ... 

The space is created at [https://huggingface.co/spaces/regisklaconsulting/Drug-Classifier-Web-App]

>>> Step 2: Get required content from the space

Click on the Files > README.md and copy its content at the begining of the local README.md file. 

Adapt the YAML config part of the README.md:

---
... 
app_port: 7860
...
---

>>> Step 3: Add your Spaces app as an additional remote to your existing Git repository by running the following command in your terminal:

$ git remote add space https://huggingface.co/spaces/regisklaconsulting/Drug-Classifier-Web-App

$ git remote -v
origin  https://github.com/regisklaconsulting/Drug-Classifier-WebApp.git (fetch)
origin  https://github.com/regisklaconsulting/Drug-Classifier-WebApp.git (push)
space   https://huggingface.co/spaces/regisklaconsulting/Drug-Classifier-Web-App (fetch)    <<< space
space   https://huggingface.co/spaces/regisklaconsulting/Drug-Classifier-Web-App (push)     <<< space

2. Force push to sync everything for the first time

WARNING: 
Update your local README.md file with the yaml header of the README.md file automatically created in the Space. 

$ git add .
$ git commit -m "commit for HF space deploy"

Below we push the main branch "main" (or "master") in the "space" remote repository:

$ git push --force space master:main
    
    WARNING:
    - In main:main, 1st "main" the local or source branch, 2nd "main" the target or branch in space remote repo. Thus adapt if different. 
    - Go to your space in you can see the sync is hapenning 
        - If HF Space, wlick on the "Files" to see what files are in synch with your repo 
          
Go to the space and see the Docker image being built and starting the application: 

    ... [INFO] - >> Loading the model located at [None]
    Traceback (most recent call last):
    File "/app/drug_app.py", line 45, in <module>
        training_pipeline = sio.load(str(MODEL_FILE_PATH), trusted=trusted_types)
    File "/usr/local/lib/python3.13/site-packages/skops/io/_persist.py", line 147, in load
        with ZipFile(file, "r") as input_zip:
            ~~~~~~~^^^^^^^^^^^
    File "/usr/local/lib/python3.13/zipfile/__init__.py", line 1367, in __init__
        self.fp = io.open(file, filemode)
                ~~~~~~~^^^^^^^^^^^^^^^^
    FileNotFoundError: [Errno 2] No such file or directory: 'None'

WARNING:
The deployment is not complete because the web app is looking for the model pipeline via the env. variable MODEL_FILE_PATH. 

>>> Step 4: Push the prediction pipeline to HF Space 

We will manually execute the Makefile using env variables set on the command line: 

Copy the model somewhere: 

$ mkdir -p /tmp/models && cp ../Drug-Classifier/model/drug-classifier-pipeline.skops /tmp/models

Push the model in HF space: 

$ export HF_TOKEN="xxxxxxxxxxxxxxxxx" 
$ export HF_REPO_ID="regisklaconsulting/Drug-Classifier-Web-App" \
    export MODEL_LOCAL_PATH="/tmp/models/drug-classifier-pipeline.skops" \
    export MODEL_REMOTE_PATH="/models/drug-classifier-pipeline.skops"
$ make deploy HF_TOKEN=$HF_TOKEN HF_REPO_ID=$HF_REPO_ID MODEL_LOCAL_PATH=$MODEL_LOCAL_PATH MODEL_REMOTE_PATH=$MODEL_REMOTE_PATH 

Go to your HF space, and check the "Files" tab to see if the models/ directory is created and populated. 

Now update the Dockerfile with the following line, and push it: 

...
WORKDIR /app
...
COPY models/. /app/models/
...

Build the image locally and test it (without mounting any directory, nor specifying the MODEL_FILE_PATH env var): 

$ docker build -t drug-classifier-webapp-img:1.0.0 . 
$ docker tag drug-classifier-webapp-img:1.0.0 drug-classifier-webapp-img:latest
$ docker run --rm -t \
    --name drug-classifier-webapp  \
    drug-classifier-webapp-img:latest

The app should fail but with a message specifying that the prediction pipeline is not "good": 

... [INFO] - >> Loading the model located at [./models/drug-classifier-pipeline.skops]
Traceback (most recent call last):
  File "/app/drug_app.py", line 45, in <module>
...
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Now commit and push the code to the space: 















References
==========

CI/CD for ML with HF: 
    - 


HF Spaces Docker SDK:
    - https://huggingface.co/docs/hub/spaces-sdks-docker
    - https://huggingface.co/docs/hub/spaces-sdks-docker-first-demo 
    
HF CLI:
    - https://huggingface.co/docs/huggingface_hub/guides/cli

Github Actions For python: 
    - https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-python
    - https://endjin.com/blog/2023/02/how-to-implement-continuous-deployment-of-python-packages-with-github-actions 

Github Actions & HF Spaces
    - https://github.com/ruslanmv/How-to-Sync-Hugging-Face-Spaces-with-a-GitHub-Repository (***)