Skip to content

Commit 14a31cf

Browse files
Support python39 (#111)
* implementes python39, support for building venv with requirements.txt, updated tests * removed useless test, fixed a typo Co-authored-by: Michele Sciabarra <michele@sciabarra.com>
1 parent 0e9b0ea commit 14a31cf

File tree

37 files changed

+323
-375
lines changed

37 files changed

+323
-375
lines changed

README.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ Build using Python 3.7 (recommended)
4747
```
4848
docker build -t actionloop-python-v3.7:1.0-SNAPSHOT $(pwd)/core/python3ActionLoop
4949
```
50-
This tutorial assumes you're building with python 3.7. But if you want to use python 2.7 you can use:
51-
```
52-
docker build -t actionloop-python-v2.7:1.0-SNAPSHOT $(pwd)/core/python2ActionLoop
53-
```
50+
51+
For runtime 3.9 or 3.6-ai you need also to copy `bin` and `lib` folders from 3.7 in the Docker folder.
52+
5453

5554
2.1. Check docker `IMAGE ID` (3rd column) for repository `actionloop-python-v3.7`
5655
```
@@ -344,10 +343,10 @@ To build all those images, run the following command.
344343

345344
You can optionally build a specific image by modifying the Gradle command. For example:
346345
```
347-
./gradlew core:python3ActionLoop:distDocker
346+
./gradlew core:python3Action:distDocker
348347
```
349348

350-
The build will produce Docker images such as `actionloop-python-v3.7`
349+
The build will produce Docker images such as `action-python-v3.7`
351350
and will also tag the same image with the `whisk/` prefix. The latter
352351
is a convenience, which if you're testing with a local OpenWhisk
353352
stack, allows you to skip pushing the image to Docker Hub.
@@ -374,11 +373,11 @@ in first with the `docker` CLI.
374373
### Using Your Image as an OpenWhisk Action
375374

376375
You can now use this image as an OpenWhisk action. For example, to use
377-
the image `actionloop-python-v3.7` as an action runtime, you would run
376+
the image `action-python-v3.7` as an action runtime, you would run
378377
the following command.
379378

380379
```
381-
wsk action update myAction myAction.py --docker $DOCKER_USER/actionloop-python-v3.7
380+
wsk action update myAction myAction.py --docker $DOCKER_USER/action-python-v3.7
382381
```
383382

384383
## Test Runtimes
@@ -400,14 +399,53 @@ Gradle allows you to selectively run tests. For example, the following
400399
command runs tests which match the given pattern and excludes all
401400
others.
402401
```
403-
./gradlew :tests:test --tests *ActionLoopContainerTests*
402+
./gradlew :tests:test --tests Python*Tests
404403
```
405404

406405
## Python 3 AI Runtime
407-
This action runtime enables developers to create AI Services with OpenWhisk. It comes with preinstalled libraries useful for running machine learning and deep learning inferences. [Read more about this runtime here](./core/python3AiActionLoop).
406+
This action runtime enables developers to create AI Services with OpenWhisk. It comes with preinstalled libraries useful for running machine learning and deep learning inferences. [Read more about this runtime here](./core/python3AiAction).
408407

409408
## Import Project into IntelliJ
410409

411410
Follow these steps to import the project into your IntelliJ IDE.
412411
- Import project as gradle project.
413412
- Make sure the working directory is root of the project/repo.
413+
414+
# Using extra libraries
415+
416+
If you need more libraries for your Python action, you can include a virtualenv in the zip file of the action.
417+
418+
The requirement is that the zip file must have a subfolder named `virtualenv` with a script `virtualenv\bin\activate_this.py` working in an Linux AMD64 environment. It will be executed at start time to use your extra libraries.
419+
420+
## Using requirements.txt
421+
422+
Virtual envs are usually built listing your dependencies in a `requirements.txt`.
423+
424+
If you have an action that requires addition libraries, you can just include `requirements.txt`.
425+
426+
You have to create a folder `myaction` with at least two files:
427+
428+
```
429+
__main__.py
430+
requirements.txt
431+
```
432+
433+
Then zip your action and deploy to OpenWhisk, the requirements will be installed for you at init time, creating a suitable virtualenv.
434+
435+
Keep in mind that resolving requirements involves downloading and install software, so your action timeout limit may need to be adjusted accordingly. Instead, you should consider using precompilation to resolve the requirements at build time.
436+
437+
438+
## Precompilation of a virtualenv
439+
440+
The action containers can actually generate a virtualenv for you, provided you have a requirements.txt.
441+
442+
443+
If you have an action in the format described before (with a `requirements.txt`) you can build the zip file with the included files with:
444+
445+
```
446+
zip -j -r myaction | docker run -i action-python-v3.7 -compile main >myaction.zip
447+
```
448+
449+
You may use `v3.9` or `v3.6-ai` as well according to your Python version needs.
450+
451+
The resulting action includes a virtualenv already built for you and that is fast to deploy and start as all the dependencies are already resolved. Note that there is a limit on the size of the zip file and this approach will not work for installing large libraries like Pandas or Numpy, instead use the provide "v.3.6-ai" runtime instead which provides these libraries already for you.

core/python3AiActionLoop/Dockerfile renamed to core/python36AiAction/Dockerfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RUN curl -sL \
3737
FROM tensorflow/tensorflow:1.15.2-py3-jupyter
3838

3939
# select the builder to use
40-
ARG GO_PROXY_BUILD_FROM=release
40+
ARG GO_PROXY_BUILD_FROM=source
4141

4242
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
4343
curl \
@@ -53,15 +53,19 @@ RUN apt-get update && apt-get upgrade -y && apt-get install -y \
5353
&& rm -rf /var/lib/apt/lists/*
5454

5555
# PyTorch
56-
RUN pip3 install torch torchvision
56+
# persistent as it fails often
57+
RUN while ! pip list | grep torch ;\
58+
do pip install torch ; done ;\
59+
while ! pip list | grep torchvision ;\
60+
do pip install torchvision ; done
5761

5862
# rclone
5963
RUN curl -L https://downloads.rclone.org/rclone-current-linux-amd64.deb -o rclone.deb \
6064
&& dpkg -i rclone.deb \
6165
&& rm rclone.deb
6266

6367
COPY requirements.txt requirements.txt
64-
RUN pip3 install --upgrade pip six &&\
68+
RUN pip3 install --upgrade pip six wheel &&\
6569
pip3 install --no-cache-dir -r requirements.txt &&\
6670
ln -sf /usr/bin/python3 /usr/local/bin/python
6771

File renamed without changes.

core/python36AiAction/build.gradle

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
ext.dockerImageName = 'action-python-v3.6-ai'
19+
apply from: '../../gradle/docker.gradle'
20+
21+
distDocker.dependsOn 'copyLib'
22+
distDocker.dependsOn 'copyBin'
23+
distDocker.finalizedBy('cleanup')
24+
25+
task copyLib(type: Copy) {
26+
from '../python3Action/lib'
27+
into './lib'
28+
}
29+
30+
task copyBin(type: Copy) {
31+
from '../python3Action/bin'
32+
into './bin'
33+
}
34+
35+
task cleanup(type: Delete) {
36+
delete 'bin'
37+
delete 'lib'
38+
}
File renamed without changes.

core/python3AiActionLoop/samples/smart-body-crop/common.py renamed to core/python36AiAction/samples/smart-body-crop/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class CocoPart(Enum):
8484

8585
NMS_Threshold = 0.1
8686
InterMinAbove_Threshold = 6
87-
Inter_Threshold = 0.1
87+
Inter_Threashold = 0.1
8888
Min_Subset_Cnt = 4
8989
Min_Subset_Score = 0.8
9090
Max_Human = 96
@@ -162,7 +162,7 @@ def estimate_pose(heatMat, pafMat):
162162
# if two humans share a part (same part idx and coordinates), merge those humans
163163
if set(c1['uPartIdx']) & set(c2['uPartIdx']) != empty_set:
164164
is_merged = True
165-
# extend human1 connections with human2 connections
165+
# extend human1 connectios with human2 connections
166166
conns_by_human[h1].extend(conns_by_human[h2])
167167
conns_by_human.pop(h2) # delete human2
168168
break
@@ -243,7 +243,7 @@ def get_score(x1, y1, x2, y2, pafMatX, pafMatY):
243243
pafYs[idx] = pafMatY[my][mx]
244244

245245
local_scores = pafXs * vx + pafYs * vy
246-
thidxs = local_scores > Inter_Threshold
246+
thidxs = local_scores > Inter_Threashold
247247

248248
return sum(local_scores * thidxs), sum(thidxs)
249249

0 commit comments

Comments
 (0)