Felladrin commited on
Commit
320038e
·
1 Parent(s): 2323914

Setup transformers.js during Docker build

Browse files
Files changed (3) hide show
  1. Dockerfile +8 -1
  2. app.py +5 -58
  3. requirements.txt +0 -7
Dockerfile CHANGED
@@ -5,12 +5,19 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
5
  PORT=8501
6
 
7
  RUN apt-get update && apt-get install -y --no-install-recommends \
8
- git git-lfs ffmpeg libsm6 libxext6 cmake libgl1 build-essential \
9
  && rm -rf /var/lib/apt/lists/* \
10
  && git lfs install
11
 
12
  WORKDIR /app
13
 
 
 
 
 
 
 
 
14
  COPY requirements.txt ./
15
  RUN pip install --no-cache-dir -U pip \
16
  && pip install --no-cache-dir -r requirements.txt
 
5
  PORT=8501
6
 
7
  RUN apt-get update && apt-get install -y --no-install-recommends \
8
+ git git-lfs ffmpeg libsm6 libxext6 cmake libgl1 build-essential curl \
9
  && rm -rf /var/lib/apt/lists/* \
10
  && git lfs install
11
 
12
  WORKDIR /app
13
 
14
+ ARG TRANSFORMERS_JS_VERSION=3.7.6
15
+ RUN curl -L "https://github.com/huggingface/transformers.js/archive/refs/tags/${TRANSFORMERS_JS_VERSION}.tar.gz" -o transformers.tar.gz \
16
+ && tar -xzf transformers.tar.gz \
17
+ && mv "transformers.js-${TRANSFORMERS_JS_VERSION}" transformers.js \
18
+ && pip install --no-cache-dir -r "transformers.js/scripts/requirements.txt" \
19
+ && rm -rf transformers.tar.gz
20
+
21
  COPY requirements.txt ./
22
  RUN pip install --no-cache-dir -U pip \
23
  && pip install --no-cache-dir -r requirements.txt
app.py CHANGED
@@ -2,13 +2,10 @@ import logging
2
  import os
3
  import subprocess
4
  import sys
5
- import tempfile
6
- import tarfile
7
  import shutil
8
- from dataclasses import dataclass
9
  from pathlib import Path
10
  from typing import List, Optional, Tuple
11
- from urllib.request import urlopen, urlretrieve
12
 
13
  import streamlit as st
14
  from huggingface_hub import HfApi, whoami
@@ -24,11 +21,7 @@ class Config:
24
  hf_token: str
25
  hf_username: str
26
  is_using_user_token: bool
27
- transformers_version: str = "3.7.6"
28
  hf_base_url: str = "https://huggingface.co"
29
- transformers_base_url: str = (
30
- "https://github.com/huggingface/transformers.js/archive/refs"
31
- )
32
  repo_path: Path = Path("./transformers.js")
33
 
34
  @classmethod
@@ -65,57 +58,11 @@ class ModelConverter:
65
  self.config = config
66
  self.api = HfApi(token=config.hf_token)
67
 
68
- def _get_ref_type(self) -> str:
69
- """Determine the reference type for the transformers repository."""
70
- url = f"{self.config.transformers_base_url}/tags/{self.config.transformers_version}.tar.gz"
71
- try:
72
- return "tags" if urlopen(url).getcode() == 200 else "heads"
73
- except Exception as e:
74
- logger.warning(f"Failed to check tags, defaulting to heads: {e}")
75
- return "heads"
76
-
77
  def setup_repository(self) -> None:
78
- """Download and setup transformers repository if needed."""
79
- if self.config.repo_path.exists():
80
- return
81
-
82
- ref_type = self._get_ref_type()
83
- archive_url = f"{self.config.transformers_base_url}/{ref_type}/{self.config.transformers_version}.tar.gz"
84
- archive_path = Path(f"./transformers_{self.config.transformers_version}.tar.gz")
85
-
86
- try:
87
- urlretrieve(archive_url, archive_path)
88
- self._extract_archive(archive_path)
89
- self._install_scripts_requirements()
90
- logger.info("Repository downloaded and extracted successfully")
91
- except Exception as e:
92
- raise RuntimeError(f"Failed to setup repository: {e}")
93
- finally:
94
- archive_path.unlink(missing_ok=True)
95
-
96
- def _extract_archive(self, archive_path: Path) -> None:
97
- """Extract the downloaded archive."""
98
- with tempfile.TemporaryDirectory() as tmp_dir:
99
- with tarfile.open(archive_path, "r:gz") as tar:
100
- tar.extractall(tmp_dir)
101
-
102
- extracted_folder = next(Path(tmp_dir).iterdir())
103
- extracted_folder.rename(self.config.repo_path)
104
-
105
- def _install_scripts_requirements(self) -> None:
106
- req_path = self.config.repo_path / "scripts" / "requirements.txt"
107
- if req_path.exists():
108
- subprocess.run(
109
- [
110
- sys.executable,
111
- "-m",
112
- "pip",
113
- "install",
114
- "--no-cache-dir",
115
- "-r",
116
- str(req_path),
117
- ],
118
- check=True,
119
  )
120
 
121
  def _run_conversion_subprocess(
 
2
  import os
3
  import subprocess
4
  import sys
 
 
5
  import shutil
 
6
  from pathlib import Path
7
  from typing import List, Optional, Tuple
8
+ from dataclasses import dataclass
9
 
10
  import streamlit as st
11
  from huggingface_hub import HfApi, whoami
 
21
  hf_token: str
22
  hf_username: str
23
  is_using_user_token: bool
 
24
  hf_base_url: str = "https://huggingface.co"
 
 
 
25
  repo_path: Path = Path("./transformers.js")
26
 
27
  @classmethod
 
58
  self.config = config
59
  self.api = HfApi(token=config.hf_token)
60
 
 
 
 
 
 
 
 
 
 
61
  def setup_repository(self) -> None:
62
+ """Ensure the bundled transformers.js repository is present."""
63
+ if not self.config.repo_path.exists():
64
+ raise RuntimeError(
65
+ f"Expected transformers.js repository at {self.config.repo_path} but it was not found."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  )
67
 
68
  def _run_conversion_subprocess(
requirements.txt CHANGED
@@ -4,10 +4,3 @@ onnxscript==0.5.4
4
  onnxconverter_common==1.16.0
5
  onnx_graphsurgeon==0.5.8
6
  torch==2.5.1
7
- transformers==4.49.0
8
- onnxruntime==1.20.1
9
- optimum@git+https://github.com/huggingface/optimum.git@b04feaea78cda58d79b8da67dca3fd0c4ab33435
10
- onnx==1.17.0
11
- tqdm==4.67.1
12
- onnxslim==0.1.48
13
- numpy==2.2.6
 
4
  onnxconverter_common==1.16.0
5
  onnx_graphsurgeon==0.5.8
6
  torch==2.5.1