Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Docker base image #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
recursive-include my_spaces/templates/ *
recursive-include my_spaces/templates/ *
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PHONY: clean all

build-and-push-base-image:
docker build -t my-spaces:base -f ./dockerfiles/Dockerfile.base .
docker tag my-spaces:base zuppif/my-spaces:base
docker push zuppif/my-spaces:base
14 changes: 14 additions & 0 deletions dockerfiles/Dockerfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM nvcr.io/nvidia/pytorch:22.10-py3
# ARG HUGGING_FACE_HUB_TOKEN
ENV DEBIAN_FRONTEND noninteractive
RUN apt update && apt install -y git-lfs ffmpeg libsm6 libxext6 cmake libgl1-mesa-glx \
&& rm -rf /var/lib/apt/lists/*
RUN git lfs install
# we will reinstall pillow using pillow-smid, for better performances
RUN pip uninstall -y pillow \
&& pip install -U --force-reinstall pillow-simd
RUN pip install "protobuf<4" "click<8.1" gradio datasets huggingface_hub ftfy GitPython
# gradio and streamlit default ports
ENV GRADIO_SERVER_NAME "0.0.0.0"
EXPOSE 7860 8501
WORKDIR /home/user/app
20 changes: 12 additions & 8 deletions my_spaces/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dataclasses import dataclass, field
from os import environ
from pathlib import Path
from typing import List, Optional
from typing import List, Optional, Set

import docker
import typer
Expand Down Expand Up @@ -60,7 +60,7 @@ def build(self, repo_url: str, template_path: Path, out_dir: Path):
image, logs = self.client.images.build(
path=str(out_dir), fileobj=f, tag=f"{self.image}:{self.tag}"
)
return self
return logs

def run(self):
container: Container = self.client.containers.run(
Expand Down Expand Up @@ -112,7 +112,7 @@ def __post_init__(self):
self.client = docker.from_env()

def run(self, idenfitier: str, force_run: bool = False):
is_image_link = "zuppif/" in idenfitier
is_image_link = f"{ORGANIZATION}/" in idenfitier
if is_image_link:
# in this case, we just pull it
image, tag = idenfitier.split(":")
Expand All @@ -121,22 +121,26 @@ def run(self, idenfitier: str, force_run: bool = False):
else:
# identifier must be a link to a girhub repo, so we create the image
self.space = LocalSpace.from_repo_url(idenfitier, self.client)
images: dict[str, Image] = {}
images: Set = set()
# let's check if we had build it before
for image in self.client.images.list():
for tag in image.tags:
images[tag] = image
if not self.space.image in images:
images.add(tag)
if not f"{self.space.image}:{self.space.tag}" in images:
logging.info(f"🔨 Building {self.space.image}:{self.space.tag} ...")
self.space.build(
logs = self.space.build(
idenfitier, self.template_path, self.folder.dockerfiles_root
)
for chunk in logs:
if "stream" in chunk:
for line in chunk["stream"].splitlines():
logging.info(f"\t{line}")
logging.info("🔨 Done! ")
logging.info("🚀 Running ...")
container = self.space.start(force_run)
logging.info("🐋 Log from container: ")
for line in container.logs(stream=True):
print("\t>", line.strip().decode("utf-8"))
logging.info(f"\t{line.strip().decode('utf-8')}")

def stop(self):
logging.info("🛑 Stopping container ... ")
Expand Down
24 changes: 2 additions & 22 deletions my_spaces/templates/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
FROM nvcr.io/nvidia/pytorch:22.10-py3
# ARG HUGGING_FACE_HUB_TOKEN
ENV DEBIAN_FRONTEND noninteractive
# gradio and streamlit default ports
EXPOSE 7860 8501
RUN apt update && apt install -y git-lfs ffmpeg libsm6 libxext6 cmake libgl1-mesa-glx \
&& rm -rf /var/lib/apt/lists/*
RUN git lfs install
WORKDIR /home/user
WORKDIR /home/user/app
# we will reinstall pillow using pillow-smid, for better performances
RUN pip uninstall -y pillow \
&& pip install -U --force-reinstall pillow-simd
RUN pip install "protobuf<4" "click<8.1" gradio datasets huggingface_hub ftfy GitPython
FROM zuppi/my-spaces:base
# clone user stuff
RUN git clone {{ repo_url }} .
RUN if [ -f "requirements.txt" ]; then pip install -r requirements.txt; fi;
RUN if [ -f "packages.txt" ]; then apt-get install $(grep -vE "^\s*#" packages.txt | tr "\n" " "); fi;
# some space had this error
# https://stackoverflow.com/questions/72706073/attributeerror-partially-initialized-module-cv2-has-no-attribute-gapi-wip-gs
# so we need to downgrade opencv
RUN pip uninstall -y opencv-python \
&& pip install opencv-python==4.5.5.64
# if hf token was passed
# run the app once for the initial setup
# RUN if [ "$HUGGING_FACE_HUB_TOKEN" ]; then python app.py; fi

ENTRYPOINT ["python", "app.py"]