diff --git a/MANIFEST.in b/MANIFEST.in index d3ec0a4..9dc64bd 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1 @@ -recursive-include my_spaces/templates/ * +recursive-include my_spaces/templates/ * \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..df4a9de --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/dockerfiles/Dockerfile.base b/dockerfiles/Dockerfile.base new file mode 100644 index 0000000..ae5ae0d --- /dev/null +++ b/dockerfiles/Dockerfile.base @@ -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 \ No newline at end of file diff --git a/my_spaces/main.py b/my_spaces/main.py index d370f7c..c2f2ffc 100644 --- a/my_spaces/main.py +++ b/my_spaces/main.py @@ -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 @@ -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( @@ -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(":") @@ -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 ... ") diff --git a/my_spaces/templates/Dockerfile b/my_spaces/templates/Dockerfile index 223ec59..093831e 100644 --- a/my_spaces/templates/Dockerfile +++ b/my_spaces/templates/Dockerfile @@ -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"] \ No newline at end of file