cantremember commited on
Commit
c178b11
·
1 Parent(s): 0479bd7

very close to deployable

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .DS_Store
2
+ .cache
3
+ .vscode
Dockerfile CHANGED
@@ -1,15 +1,74 @@
1
- FROM alpine:latest
 
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # https://huggingface.co/docs/hub/spaces-sdks-docker#permissions
5
- RUN adduser -D -u 1000 sleepbotzz
 
 
 
 
 
 
 
 
 
 
 
6
  USER sleepbotzz
7
  ENV \
8
  HOME=/home/sleepbotzz \
9
  PATH=/home/sleepbotzz/.local/bin:$PATH
10
 
11
  WORKDIR $HOME/app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  COPY --chown=sleepbotzz assets assets
13
 
14
 
15
- ENTRYPOINT [ "/home/sleepbotzz/app/assets/script/test.sh" ]
 
 
 
 
1
+ # # this was going down a rabbit-hole
2
+ # # particularly with Apple M1 & aarch64 & QEMU
3
+ # FROM alpine:latest
4
 
5
+ # https://github.com/pytorch/serve
6
+ #FROM pytorch/pytorch
7
+ # pinned to 2.3.1,
8
+ # also see 'assets/bootstrap'
9
+ FROM pytorch/pytorch:2.3.1-cuda11.8-cudnn8-runtime
10
+
11
+ RUN apt update && apt-get clean
12
+ RUN set -ex && \
13
+ apt install -y sudo && \
14
+ apt-get clean
15
+
16
+ COPY linux/sudoers-sleepbotzz /etc/sudoers.d/sleepbotzz
17
+ RUN chmod 0440 /etc/sudoers.d/sleepbotzz
18
+
19
+
20
+ # User constraints
21
 
22
  # https://huggingface.co/docs/hub/spaces-sdks-docker#permissions
23
+ RUN useradd -m -u 1000 -s /bin/bash -p "${BUILD_USER_PASSWORD}" sleepbotzz
24
+
25
+ # password
26
+ # https://askubuntu.com/questions/752500/how-do-i-encrypt-a-new-users-password-using-the-useradd-command
27
+ # received as clear text from HF secrets
28
+ # encrypted with $6 (SHA-512)
29
+ # direct use of `python` is to avoid escaping the '$'s
30
+ ARG BUILD_USER_PASSWORD
31
+ RUN usermod --password \
32
+ $(python -c "import crypt; import os; print(crypt.crypt(os.getenv('BUILD_USER_PASSWORD'), \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\"))") \
33
+ sleepbotzz
34
+
35
  USER sleepbotzz
36
  ENV \
37
  HOME=/home/sleepbotzz \
38
  PATH=/home/sleepbotzz/.local/bin:$PATH
39
 
40
  WORKDIR $HOME/app
41
+ COPY --chown=sleepbotzz assets/bootstrap assets/bootstrap
42
+
43
+
44
+ # already has: bash python3
45
+ RUN set -ex && \
46
+ sudo apt install -y \
47
+ vim git curl && \
48
+ sudo apt-get clean
49
+
50
+ # already has: conda
51
+ #RUN curl -o '/tmp/Anaconda3-2024.02-1-Linux-x86_64.sh' 'https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh'
52
+ #RUN test "$(sha256sum /tmp/Anaconda3-2024.02-1-Linux-x86_64.sh | cut -d ' ' -f 1)" = \
53
+ # "c536ddb7b4ba738bddbd4e581b29308cb332fa12ae3fa2cd66814bd735dff231"
54
+ #
55
+ #RUN bash /tmp/Anaconda3-2024.02-1-Linux-x86_64.sh -f -b -p /usr/local/anaconda3
56
+
57
+ RUN conda init
58
+ RUN conda config --set verbosity 2 --env
59
+ RUN test "$(conda list pytorch | grep '^pytorch ' | awk -F ' ' '{ printf "%s", $2 }')" = \
60
+ "2.3.1"
61
+ RUN conda env create -f ./assets/bootstrap/anaconda.environment.yml
62
+
63
+ #
64
+ # !!! DO NOT ADD NEW LINES ABOVE THIS POINT !!!
65
+ #
66
+
67
+
68
  COPY --chown=sleepbotzz assets assets
69
 
70
 
71
+
72
+
73
+ CMD [ "bash" ]
74
+ #ENTRYPOINT [ "/home/sleepbotzz/app/assets/script/entrypoint.sh" ]
README.md CHANGED
@@ -1,9 +1,15 @@
1
  ---
2
  title: My First Docker
3
  emoji: 🔮
4
- colorFrom: green
5
- colorTo: green
 
 
 
6
  sdk: docker
 
 
 
7
  pinned: false
8
  license: cc
9
  ---
@@ -13,6 +19,16 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
13
  ## Notes To myself
14
 
15
  ```bash
16
- docker build . cantremember/hf:latest
17
-
 
 
 
18
  ```
 
 
 
 
 
 
 
 
1
  ---
2
  title: My First Docker
3
  emoji: 🔮
4
+ models: []
5
+ datasets: []
6
+ preload_from_hub: []
7
+ colorFrom: purple
8
+ colorTo: purple
9
  sdk: docker
10
+ app_port: 7860
11
+ # sdk_version
12
+ # python_version
13
  pinned: false
14
  license: cc
15
  ---
 
19
  ## Notes To myself
20
 
21
  ```bash
22
+ # vs. 'linux/arm64'
23
+ docker buildx build . -t cantremember/hf:latest \
24
+ --platform linux/amd64 \
25
+ --build-arg BUILD_USER_PASSWORD=___
26
+ docker run -it --platform linux/amd64 cantremember/hf:latest bash
27
  ```
28
+
29
+
30
+
31
+ docker buildx build . -t cantremember/hf:latest \
32
+ --platform linux/arm64 \
33
+ --build-arg BUILD_USER_PASSWORD=___
34
+ docker run -it --platform linux/arm64 cantremember/hf:latest bash
assets/bootstrap/anaconda.environment.yml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-file-manually
2
+ name: my-first-docker
3
+ prefix: /opt/conda/envs/my-first-docker
4
+ channels:
5
+ - defaults
6
+ - conda-forge
7
+ - pytorch
8
+ dependencies:
9
+ # # https://huggingface.co/docs/transformers/quicktour
10
+ # - transformers=4.14.1
11
+ - transformers
12
+ - datasets
13
+ - evaluate
14
+ - accelerate
15
+
16
+ # .
17
+ # "install your preferred machine learning framework"
18
+ # https://pytorch.org/get-started/locally/
19
+ # `conda install pytorch torchvision torchaudio cpuonly -c pytorch`
20
+ # in this case, it's our base Docker image, and asserted in the Dockerfile
21
+ #- pytorch=2.3
22
+
23
+ # version must align with above
24
+ # https://github.com/pytorch/vision#installation
25
+ # ... but the image never builds !!!
26
+ #- torchvision=0.18
27
+ #- cpuonly
assets/main.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+
3
+ from transformers.utils import logging
4
+ logging.set_verbosity_error()
5
+
6
+ from transformers import BlipForImageTextRetrieval
7
+ from transformers import AutoProcessor
8
+ from PIL import Image
9
+ import math, random, time
10
+ # import random
11
+ # import time
12
+ import torch
13
+
14
+
15
+ # multi-modal Model
16
+ # accepts both text and image content (or audio, etc.)
17
+ model = BlipForImageTextRetrieval.from_pretrained("Salesforce/blip-itm-base-coco")
18
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-itm-base-coco")
19
+
20
+ raw_image = Image.open('./assets/pot-o-gold-my-little-pony-Derpy.jpeg').convert('RGB')
21
+
22
+ statements = [
23
+ "an image of a horse",
24
+ "a horse and a rainbow",
25
+ "a pony and a rainbow",
26
+ "a unicorn and a rainbow",
27
+ "a pony in a forest",
28
+ "a rainbox over a lake",
29
+ "a horse running through the forest",
30
+ "two eyes that do not match",
31
+ "equine joy",
32
+ "a stallion and gold coins",
33
+ "a mare and gold coins"
34
+ ]
35
+ while True:
36
+ index = math.floor(random.random() * len(statements))
37
+ text = statements[index]
38
+
39
+ inputs = processor(images=raw_image,
40
+ text=text,
41
+ return_tensors="pt") # PyTorch tensors
42
+ itm_scores = model(**inputs)[0]
43
+ itm_score = torch.nn.functional.softmax(itm_scores, dim=1)
44
+
45
+ print(f"""'{text}' => {itm_score[0][1]:.2f}""")
assets/pot-o-gold-my-little-pony-Derpy.jpeg ADDED
assets/script/entrypoint.sh ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ ROOT=$(cd `dirname $0` && pwd)
3
+
4
+ set -e
5
+
6
+ python $ROOT/../main.py
assets/script/test.sh DELETED
@@ -1,3 +0,0 @@
1
- #!/bin/sh
2
-
3
- echo IT WORKED
 
 
 
 
linux/sudoers-sleepbotzz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ sleepbotzz ALL=(ALL) NOPASSWD: SETENV: /usr/bin/apt
2
+ sleepbotzz ALL=(ALL) NOPASSWD: SETENV: /usr/bin/apt-get
3
+ sleepbotzz ALL=(ALL) NOPASSWD: SETENV: /opt/conda/bin/conda