{"id":22615604,"url":"https://github.com/marcellodesales/replicated-cog-server-docker","last_synced_at":"2026-04-29T18:34:02.729Z","repository":{"id":142072983,"uuid":"449114993","full_name":"marcellodesales/replicated-cog-server-docker","owner":"marcellodesales","description":"Custom Dockerized Replicated Cog Server, used for running Machine Learning models in a reusable Docker container that exposes your model as an API. Example: Run GFP-GAN restoration!","archived":false,"fork":false,"pushed_at":"2022-01-19T00:16:08.000Z","size":42,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T00:42:35.056Z","etag":null,"topics":["base","cog","docker","docker-compose","machine-learning","ml","ops"],"latest_commit_sha":null,"homepage":"","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcellodesales.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-18T02:32:01.000Z","updated_at":"2023-12-19T17:27:00.000Z","dependencies_parsed_at":"2023-07-09T05:31:33.737Z","dependency_job_id":null,"html_url":"https://github.com/marcellodesales/replicated-cog-server-docker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/marcellodesales/replicated-cog-server-docker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Freplicated-cog-server-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Freplicated-cog-server-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Freplicated-cog-server-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Freplicated-cog-server-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcellodesales","download_url":"https://codeload.github.com/marcellodesales/replicated-cog-server-docker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcellodesales%2Freplicated-cog-server-docker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32439208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T18:12:22.909Z","status":"ssl_error","status_checked_at":"2026-04-29T18:11:33.322Z","response_time":110,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["base","cog","docker","docker-compose","machine-learning","ml","ops"],"created_at":"2024-12-08T19:08:29.917Z","updated_at":"2026-04-29T18:34:02.703Z","avatar_url":"https://github.com/marcellodesales.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cog-replicated-docker\n\nThe Cog server from replicated https://github.com/replicate/cog that helps running Machine Learning applications using an API server through a well-defined interface. \nThis is an optional image in case you need to customize your base image.\n\n\u003e **Base images**: The base of our project this image has the supporting base binaries that's hard to install on a host.\n\u003e * https://github.com/marcellodesales/nvidea-cuda-ubuntu-docker \n\u003e   * NVidea CUDA \n\u003e   * python\n\n# How to Use\n\nThere are a few steps to run you Machine Learning model using cog:\n\n* Create the driver `predict.py`\n* Create the builder `cog.yaml`\n\n\u003e **DOCS**: More at https://github.com/replicate/cog/blob/main/docs/getting-started-own-model.md.\n\n## Create a driver `predict.py`\n\n* It will define your arguments, their respective types, etc.\n* You will implement the call to your model library\n* You will have an interface to return the types such as images, texts, etc.\n\n## Create teh builder `cog.yaml` \n\nIt helps describing your dependencies such as system-level, python, and others.\n\n* System-dependencies: what needs to be in the container to run your model.\n  * For instance,\n* Model dependencies: pypi dependendencies that is part of your implementation\n  * For instance, the correct versions should be properly described.\n  * An example of failure: versions of pytorch must match the python version used: https://github.com/pytorch/vision#installation\n* Pre-install dependencies: Those that are required to be installed after\n\n# Example: Face improvement\n\n* https://replicate.com/tencentarc/gfpgan/examples\n\n## predict.py\n\n* Define a cog input like `@cog.input(\"image\", type=Path, help=\"input image\")`\n  * https://github.com/TencentARC/GFPGAN/pull/67/files#diff-73c1982d8a085dc10fda2ac7b6f202ae3ff9530ee6a15991c5339051eb10a49aR79\n\n```python\n# import subprocess\n# subprocess.call(['sh', './run_setup.sh'])\n\nimport cog\nimport tempfile\nimport os\nfrom pathlib import Path\nimport argparse\nimport cv2\nimport shutil\nfrom basicsr.utils import imwrite\nimport torch\nfrom gfpgan import GFPGANer\nimport glob\nimport numpy as np\n\n\nclass Predictor(cog.Predictor):\n    def setup(self):\n        parser = argparse.ArgumentParser()\n\n        parser.add_argument('--upscale', type=int, default=2)\n        parser.add_argument('--arch', type=str, default='clean')\n        parser.add_argument('--channel', type=int, default=2)\n        parser.add_argument('--model_path', type=str, default='experiments/pretrained_models/GFPGANCleanv1-NoCE-C2.pth')\n        parser.add_argument('--bg_upsampler', type=str, default='realesrgan')\n        parser.add_argument('--bg_tile', type=int, default=400)\n        parser.add_argument('--test_path', type=str, default='inputs/whole_imgs')\n        parser.add_argument('--suffix', type=str, default=None, help='Suffix of the restored faces')\n        parser.add_argument('--only_center_face', action='store_true')\n        parser.add_argument('--aligned', action='store_true')\n        parser.add_argument('--paste_back', action='store_false')\n        parser.add_argument('--save_root', type=str, default='results')\n\n        self.args = parser.parse_args([\"--upscale\", \"2\", \"--test_path\", \"cog_temp\", \"--save_root\", \"results\"])\n        os.makedirs(self.args.test_path, exist_ok=True)\n        # background upsampler\n        if self.args.bg_upsampler == 'realesrgan':\n            if not torch.cuda.is_available():  # CPU\n                import warnings\n                warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '\n                              'If you really want to use it, please modify the corresponding codes.')\n                bg_upsampler = None\n            else:\n                from realesrgan import RealESRGANer\n                bg_upsampler = RealESRGANer(\n                    scale=2,\n                    model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',\n                    tile=self.args.bg_tile,\n                    tile_pad=10,\n                    pre_pad=0,\n                    half=True)  # need to set False in CPU mode\n        else:\n            bg_upsampler = None\n\n        # set up GFPGAN restorer\n        self.restorer = GFPGANer(\n            model_path=self.args.model_path,\n            upscale=self.args.upscale,\n            arch=self.args.arch,\n            channel_multiplier=self.args.channel,\n            bg_upsampler=bg_upsampler)\n\n    @cog.input(\"image\", type=Path, help=\"input image\")\n    def predict(self, image):\n        input_dir = self.args.test_path\n\n        input_path = os.path.join(input_dir, os.path.basename(image))\n        shutil.copy(str(image), input_path)\n\n        os.makedirs(self.args.save_root, exist_ok=True)\n\n        img_list = sorted(glob.glob(os.path.join(input_dir, '*')))\n\n        out_path = Path(tempfile.mkdtemp()) / \"output.png\"\n\n        for img_path in img_list:\n            # read image\n            img_name = os.path.basename(img_path)\n            print(f'Processing {img_name} ...')\n            basename, ext = os.path.splitext(img_name)\n            input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)\n\n            cropped_faces, restored_faces, restored_img = self.restorer.enhance(\n                input_img, has_aligned=self.args.aligned, only_center_face=self.args.only_center_face, paste_back=self.args.paste_back)\n\n            # save faces\n            for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)):\n                # save cropped face\n                save_crop_path = os.path.join(self.args.save_root, 'cropped_faces', f'{basename}_{idx:02d}.png')\n                imwrite(cropped_face, save_crop_path)\n                # save restored face\n                if self.args.suffix is not None:\n                    save_face_name = f'{basename}_{idx:02d}_{self.args.suffix}.png'\n                else:\n                    save_face_name = f'{basename}_{idx:02d}.png'\n                save_restore_path = os.path.join(self.args.save_root, 'restored_faces', save_face_name)\n                imwrite(restored_face, save_restore_path)\n                # save cmp image\n                cmp_img = np.concatenate((cropped_face, restored_face), axis=1)\n                imwrite(restored_img, str(out_path))\n                clean_folder(self.args.test_path)\n\n        return out_path\n\ndef clean_folder(folder):\n    for filename in os.listdir(folder):\n        file_path = os.path.join(folder, filename)\n        try:\n            if os.path.isfile(file_path) or os.path.islink(file_path):\n                os.unlink(file_path)\n            elif os.path.isdir(file_path):\n                shutil.rmtree(file_path)\n        except Exception as e:\n            print('Failed to delete %s. Reason: %s' % (file_path, e))\n\n```\n\n## cog.yaml\n\n```yaml\nbuild:\n  gpu: true\n  python_version: \"3.8\"\n  system_packages:\n    - \"libgl1-mesa-glx\"\n    - \"libglib2.0-0\"\n    - \"libtinfo5\"\n  python_packages:\n    # Make sure to use the correct combination of https://github.com/pytorch/vision#installation\n    - \"torch==1.7.0\"\n    - \"torchvision==0.8.1\"\n    - \"numpy==1.21.1\"\n    - \"ipython==7.21.0\"\n    - \"lmdb==1.2.1\"\n    - \"opencv-python==4.5.3.56\"\n    - \"PyYAML==5.4.1\"\n    - \"tqdm==4.62.2\"\n    - \"yapf==0.31.0\"\n    - \"tb-nightly==2.7.0a20210825\"\n  pre_install:\n    - pip install facexlib==0.2.1.1\n    - pip install realesrgan\n\npredict: \"predict.py:Predictor\"\n```\n\n## Dockerfile\n\n* You can define your dockerfile with the parent image from this repo\n\n```dockerfile\n$ cat Dockerfile\nARG TENCENT_ARC_BASE_IMAGE\nARG TRAINING_FILE1\nARG TRAINING_FILE2\n\nFROM ${TENCENT_ARC_BASE_IMAGE}\n\n# weights\nARG TRAINING_FILE1\nENV TRAINING_FILE1 ${TRAINING_FILE1:-v0.2.0/GFPGANCleanv1-NoCE-C2.pth}\nRUN echo \"Downloading training file '${TRAINING_FILE1}'\" \u0026\u0026 \\\n    wget https://github.com/TencentARC/GFPGAN/releases/download/${TRAINING_FILE1} -P experiments/pretrained_models\n\nARG TRAINING_FILE2\nENV TRAINING_FILE2 ${TRAINING_FILE2:-v0.1.0/GFPGANv1.pth}\nRUN echo \"Downloading training file '${TRAINING_FILE1}'\" \u0026\u0026 \\\n    wget https://github.com/TencentARC/GFPGAN/releases/download/${TRAINING_FILE2} -P experiments/pretrained_models\n```\n\n## Docker-Compose\n\n```yaml\nversion: \"3.8\"\n\n###\n### Running in MacOS\n### https://stackoverflow.com/questions/64439278/gpg-invalid-signature-error-while-running-apt-update-inside-arm32v7-ubuntu20-04/64553153#64553153\n###\nservices:\n\n  GFPGAN:\n    image: marcellodesales/tencent-arc-gfpgan-runtime\n    build:\n      context: .\n      args:\n        TENCENT_ARC_BASE_IMAGE: marcellodesales/replicated-cog-server:python3.8_nvidea1.11.1\n        TRAINING_FILE1: v0.2.0/GFPGANCleanv1-NoCE-C2.pth\n        TRAINING_FILE2: v0.1.0/GFPGANv1.pth\n```\n\n## Building\n\n\u003e NOTE: Make sure to have disk space and memory. (15GB)\n\u003e * The first time running it might takes more than 10min depending on your location. \n\u003e   * Subsequent Builds take advantage of Docker Caches when specific layers aren't invalidated\n\u003e * Problem running: \"RGPG invalid signature error while running `apt-get update`\": running in MacOS you can have errors like disk space, etc. Just make sure you have enough. \n\u003e   * https://stackoverflow.com/questions/64439278/gpg-invalid-signature-error-while-running-apt-update-inside-arm32v7-ubuntu20-04/64553153#64553153\n\n```console\n$ docker-compose build\nBuilding GFPGAN\n[+] Building 0.2s (18/18) FINISHED\n =\u003e [internal] load build definition from Dockerfile                                                                                                          0\n =\u003e =\u003e transferring dockerfile: 674B                                                                                                                          0\n =\u003e [internal] load .dockerignore                                                                                                                             0\n =\u003e =\u003e transferring context: 35B                                                                                                                              0\n =\u003e [internal] load metadata for docker.io/marcellodesales/replicated-cog-server:python3.8_nvidea1.11.1                                                       0\n =\u003e [1/3] FROM docker.io/marcellodesales/replicated-cog-server:python3.8_nvidea1.11.1                                                                         0\n =\u003e [internal] load build context                                                                                                                             0\n =\u003e =\u003e transferring context: 4.33kB                                                                                                                           0\n =\u003e CACHED [2/3] COPY cog.yaml .                                                                                                                              0\n =\u003e CACHED [3/3] RUN cat cog.yaml | yq e . - -o json | jq -r -c '.build.system_packages[]' | sed -r 's/^([^,]*)(,?)$/ \\1 \\2/' | tr -d '\\n' \u003e cog.pkgs \u0026\u0026      0\n =\u003e CACHED [4/3] RUN apt-get update -qq \u0026\u0026 apt-get install -qqy $(cat cog.pkgs) \u0026\u0026     rm -rf /var/lib/apt/lists/* # buildkit 85.8MB buildkit.dockerfile.v0   0\n =\u003e CACHED [5/3] RUN cat cog.yaml | yq e . - -o json | jq -r -c '.build.python_packages[]' | sed -r 's/^([^,]*)(,?)$/\\1 \\2/' | tr -d '\\n' \u003e cog.python-pkgs   0\n =\u003e CACHED [6/3] RUN pip install -f https://download.pytorch.org/whl/torch_stable.html $(cat cog.python-pkgs)                                                 0\n =\u003e CACHED [7/3] RUN cat cog.yaml | yq e . - -o json | jq -r -c '.build.pre_install[]' \u003e cog.pre-inst \u0026\u0026     echo \"Installing the pre-install packages: $(ca  0\n =\u003e CACHED [8/3] RUN sh cog.pre-inst                                                                                                                          0\n =\u003e CACHED [9/3] WORKDIR /src                                                                                                                                 0\n =\u003e CACHED [10/3] COPY predict.py .                                                                                                                           0\n =\u003e CACHED [11/3] COPY . .                                                                                                                                    0\n =\u003e CACHED [12/3] RUN echo \"Downloading training file 'v0.2.0/GFPGANCleanv1-NoCE-C2.pth'\" \u0026\u0026     wget https://github.com/TencentARC/GFPGAN/releases/download  0\n =\u003e CACHED [13/3] RUN echo \"Downloading training file 'v0.2.0/GFPGANCleanv1-NoCE-C2.pth'\" \u0026\u0026     wget https://github.com/TencentARC/GFPGAN/releases/download  0\n =\u003e exporting to image                                                                                                                                        0\n =\u003e =\u003e exporting layers                                                                                                                                       0\n =\u003e =\u003e writing image sha256:71684982ed27156781c54ef5e2f7d18a110a7aa0e150bfb49b207e1709102ceb                                                                  0\n =\u003e =\u003e naming to docker.io/marcellodesales/tencent-arc-gfpgan-runtime                                                                                         0\n```\n\n## Running\n\nYou can just create a container in the background\n\n```console\n$ docker-compose up -d\nRecreating gfpgan_GFPGAN_1 ... done\n```\n\n* You can make sure that the container loaded your app and models...\n\n```console\n$ docker-compose logs -f\nAttaching to gfpgan_GFPGAN_1\nGFPGAN_1  | /root/.pyenv/versions/3.8.12/lib/python3.8/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:100.)\nGFPGAN_1  |   return torch._C._cuda_getDeviceCount() \u003e 0\nGFPGAN_1  | /src/predict.py:41: UserWarning: The unoptimized RealESRGAN is very slow on CPU. We do not use it. If you really want to use it, please modify the corresponding codes.\nGFPGAN_1  |   warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '\nGFPGAN_1  | Downloading: \"https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth\" to /root/.pyenv/versions/3.8.12/lib/python3.8/site-packages/facexlib/weights/detection_Resnet50_Final.pth\nGFPGAN_1  |\n100%|------| 104M/104M [00:04\u003c00:00, 22.7MB/s]\nGFPGAN_1  |  * Serving Flask app 'http' (lazy loading)\nGFPGAN_1  |  * Environment: production\nGFPGAN_1  |    WARNING: This is a development server. Do not use it in a production deployment.\nGFPGAN_1  |    Use a production WSGI server instead.\nGFPGAN_1  |  * Debug mode: off\nGFPGAN_1  |  * Running on all addresses.\nGFPGAN_1  |    WARNING: This is a development server. Do not use it in a production deployment.\nGFPGAN_1  |  * Running on http://172.19.0.2:5000/ (Press CTRL+C to quit)\n```\n\n## Testing: HTTP POST image=PATH\n\n* Choose an image as the input\n\n\u003e Using [viu](https://github.com/atanunq/viu) to open the image on terminal\n\n![Screen Shot 2022-01-18 at 1 56 22 PM](https://user-images.githubusercontent.com/131457/150036554-da9e637b-1b3f-4950-ae18-4b8d236e113e.png)\n\n* Execute the Machine Learning service using the interface built by cog, which exposes the user-defined parameters.\n  * In this example, `image` is a parameter\n\n```console\n$ curl http://localhost:5000/predict -X POST -F image=@$(pwd)/inputs/whole_imgs/Blake_Lively.jpg -o $(pwd)/super.jpg\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n100 2087k  100 1996k  100 93345   276k  12943  0:00:07  0:00:07 --:--:--  499k\n```\n\n### Output\n\n\u003e Using [viu](https://github.com/atanunq/viu) to open the image on terminal\n\n![Screen Shot 2022-01-18 at 1 56 17 PM](https://user-images.githubusercontent.com/131457/150036575-7f60da84-b89e-4a1a-abcd-084472cebf80.png)\n\n# Development \n\n## Build this image\n\n* You can specify the python version, etc.\n\n```console\ndocker-compose build\n```\n\n# Thoughts\n\n# Research\n\n* Expose Cog as a Kubernetes CRD\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcellodesales%2Freplicated-cog-server-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcellodesales%2Freplicated-cog-server-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcellodesales%2Freplicated-cog-server-docker/lists"}