Skip to content

Commit

Permalink
feat (docker): onnx model and font embedded (Byaidu#276)
Browse files Browse the repository at this point in the history
* 将onnx模型和字体打包进docker镜像

* 调整docker构建顺序,先下模型后装包,并将预热更改为dockerfile RUN指令

* 移除无用import

* 正确处理authorized为None

* translate函数中更好的处理envs和prompt不存在的情况

* 移除docker镜像中无用文件

---------

Co-authored-by: Byaidu <909756245@qq.com>
  • Loading branch information
2 people authored and hellofinch committed Dec 19, 2024
1 parent daa9c6f commit b04d2c1
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 8 deletions.
175 changes: 175 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
.github
docs
.git
.pre-commit-config.yaml
uv.lock
pdf2zh_files
gui/pdf2zh_files
gradio_files
tmp
gui/gradio_files
gui/tmp
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.vscode
.DS_Store
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
.vscode
.DS_Store
uv.lock
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
FROM python:3.12
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

WORKDIR /app

COPY . .

EXPOSE 7860

ENV PYTHONUNBUFFERED=1
ADD "https://github.com/satbyy/go-noto-universal/releases/download/v7.0/GoNotoKurrent-Regular.ttf" /app
RUN apt-get update && \
apt-get install --no-install-recommends -y libgl1 && \
rm -rf /var/lib/apt/lists/* && uv pip install --system --no-cache huggingface-hub && \
python3 -c "from huggingface_hub import hf_hub_download; hf_hub_download('wybxc/DocLayout-YOLO-DocStructBench-onnx','doclayout_yolo_docstructbench_imgsz1024.onnx');"

RUN apt-get update && apt-get install -y libgl1
COPY . .

RUN pip install .
RUN uv pip install --system --no-cache .

CMD ["pdf2zh", "-i"]
2 changes: 1 addition & 1 deletion pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def on_select_filetype(file_type):
def readuserandpasswd(file_path):
tuple_list = []
content = ""
if file_path is None or len(file_path) == 0:
if file_path is None:
return tuple_list, content
if len(file_path) == 2:
try:
Expand Down
7 changes: 5 additions & 2 deletions pdf2zh/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def translate_stream(
font_list.append((resfont, None))
elif lang_out.lower() in noto_list: # noto
resfont = "noto"
ttf_path = os.path.join(tempfile.gettempdir(), "GoNotoKurrent-Regular.ttf")
# docker
ttf_path = '/app/GoNotoKurrent-Regular.ttf'
if not os.path.exists(ttf_path):
ttf_path = os.path.join(tempfile.gettempdir(), "GoNotoKurrent-Regular.ttf")
if not os.path.exists(ttf_path):
print("Downloading Noto font...")
urllib.request.urlretrieve(
Expand Down Expand Up @@ -294,7 +297,7 @@ def translate(
doc_raw = open(file, "rb")
s_raw = doc_raw.read()
s_mono, s_dual = translate_stream(
s_raw, envs=kwarg.get("envs"), prompt=kwarg["prompt"], **locals()
s_raw, envs=kwarg.get("envs", {}), prompt=kwarg.get("prompt", []), **locals()
)
file_mono = Path(output) / f"{filename}-mono.pdf"
file_dual = Path(output) / f"{filename}-dual.pdf"
Expand Down

0 comments on commit b04d2c1

Please sign in to comment.