BinyangQiu commited on
Commit ·
c5b5c64
1
Parent(s): 1271dfc
Impl Dockerfile
Browse files- Dockerfile +103 -0
- README.md +4 -0
- fastcdm/core.py +8 -0
- scripts/app.py +12 -9
Dockerfile
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a slim Python base image for smaller size and faster builds
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set environment variables to optimize Python execution and set up user paths
|
| 5 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 6 |
+
PYTHONUNBUFFERED=1 \
|
| 7 |
+
HOME=/home/user \
|
| 8 |
+
PATH=/home/user/.local/bin:$PATH
|
| 9 |
+
|
| 10 |
+
# Create a non-root user 'user' with UID 1000 (required for Hugging Face Spaces)
|
| 11 |
+
RUN useradd -m -u 1000 user
|
| 12 |
+
|
| 13 |
+
# Set working directory
|
| 14 |
+
WORKDIR $HOME/app
|
| 15 |
+
|
| 16 |
+
# Install system dependencies
|
| 17 |
+
# Combined into a single RUN instruction to reduce image layers
|
| 18 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 19 |
+
wget \
|
| 20 |
+
curl \
|
| 21 |
+
unzip \
|
| 22 |
+
gnupg \
|
| 23 |
+
ca-certificates \
|
| 24 |
+
# Chrome dependencies
|
| 25 |
+
libx11-6 \
|
| 26 |
+
libx11-xcb1 \
|
| 27 |
+
libxcomposite1 \
|
| 28 |
+
libxcursor1 \
|
| 29 |
+
libxdamage1 \
|
| 30 |
+
libxext6 \
|
| 31 |
+
libxfixes3 \
|
| 32 |
+
libxi6 \
|
| 33 |
+
libxrandr2 \
|
| 34 |
+
libxrender1 \
|
| 35 |
+
libxss1 \
|
| 36 |
+
libxtst6 \
|
| 37 |
+
libglib2.0-0 \
|
| 38 |
+
libnss3 \
|
| 39 |
+
libnspr4 \
|
| 40 |
+
libasound2 \
|
| 41 |
+
libatk1.0-0 \
|
| 42 |
+
libatk-bridge2.0-0 \
|
| 43 |
+
libcups2 \
|
| 44 |
+
libdbus-1-3 \
|
| 45 |
+
libdrm2 \
|
| 46 |
+
libgbm1 \
|
| 47 |
+
libpango-1.0-0 \
|
| 48 |
+
libpangocairo-1.0-0 \
|
| 49 |
+
fonts-liberation \
|
| 50 |
+
xdg-utils \
|
| 51 |
+
libxkbcommon0 \
|
| 52 |
+
# OpenCV dependencies
|
| 53 |
+
libgl1 \
|
| 54 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 55 |
+
|
| 56 |
+
# Install Node.js (Version 20.x)
|
| 57 |
+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
|
| 58 |
+
&& apt-get install -y nodejs \
|
| 59 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 60 |
+
|
| 61 |
+
# Install Chrome (Version 135.0.7049.52)
|
| 62 |
+
# Using chrome-for-testing public archive
|
| 63 |
+
RUN wget -q https://storage.googleapis.com/chrome-for-testing-public/135.0.7049.52/linux64/chrome-linux64.zip \
|
| 64 |
+
&& unzip -q chrome-linux64.zip \
|
| 65 |
+
&& mv chrome-linux64 /opt/chrome \
|
| 66 |
+
&& ln -s /opt/chrome/chrome /usr/local/bin/google-chrome \
|
| 67 |
+
&& rm chrome-linux64.zip
|
| 68 |
+
|
| 69 |
+
# Install Python dependencies
|
| 70 |
+
# We install heavy dependencies first to leverage Docker layer caching
|
| 71 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 72 |
+
pip install --no-cache-dir \
|
| 73 |
+
numpy>=1.20 \
|
| 74 |
+
opencv-python-headless>=4.5 \
|
| 75 |
+
selenium>=4 \
|
| 76 |
+
webdriver-manager>=4 \
|
| 77 |
+
scikit-image \
|
| 78 |
+
gradio
|
| 79 |
+
|
| 80 |
+
# Copy the application code
|
| 81 |
+
COPY --chown=user . $HOME/app
|
| 82 |
+
|
| 83 |
+
# Install Chromedriver (Version 135.0.7049.114)
|
| 84 |
+
# Installing into 'driver/chromedriver' as expected by scripts/app.py and README
|
| 85 |
+
RUN mkdir -p driver \
|
| 86 |
+
&& wget -q https://storage.googleapis.com/chrome-for-testing-public/135.0.7049.114/linux64/chromedriver-linux64.zip \
|
| 87 |
+
&& unzip -q chromedriver-linux64.zip \
|
| 88 |
+
&& mv chromedriver-linux64/chromedriver driver/ \
|
| 89 |
+
&& chmod +x driver/chromedriver \
|
| 90 |
+
&& rm -r chromedriver-linux64 chromedriver-linux64.zip \
|
| 91 |
+
&& chown -R user:user driver
|
| 92 |
+
|
| 93 |
+
# Install the fastcdm package
|
| 94 |
+
RUN pip install --no-cache-dir .
|
| 95 |
+
|
| 96 |
+
# Switch to the non-root user
|
| 97 |
+
USER user
|
| 98 |
+
|
| 99 |
+
# Expose port 7860 for Hugging Face Spaces / Gradio
|
| 100 |
+
EXPOSE 7860
|
| 101 |
+
|
| 102 |
+
# Start the application
|
| 103 |
+
CMD ["python3", "scripts/app.py"]
|
README.md
CHANGED
|
@@ -56,6 +56,10 @@ You can check KaTeX's support coverage here: 🔗 [KaTeX Support Table](https://
|
|
| 56 |
|
| 57 |
### Installation
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
```bash
|
| 60 |
pip install fastcdm
|
| 61 |
```
|
|
|
|
| 56 |
|
| 57 |
### Installation
|
| 58 |
|
| 59 |
+
You need to install `node.js` and `chromedriver` in advance.
|
| 60 |
+
* For `node.js` installation, please refer to [here](https://nodejs.org/).
|
| 61 |
+
* For `chromedriver` installation, please refer to [here](docs/chromedriver_installation.md).
|
| 62 |
+
|
| 63 |
```bash
|
| 64 |
pip install fastcdm
|
| 65 |
```
|
fastcdm/core.py
CHANGED
|
@@ -261,6 +261,14 @@ class FastCDM:
|
|
| 261 |
print(traceback.format_exc())
|
| 262 |
return None
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
def render(self, latex_list: list) -> list:
|
| 265 |
"""
|
| 266 |
渲染 LaTeX 表达式列表。
|
|
|
|
| 261 |
print(traceback.format_exc())
|
| 262 |
return None
|
| 263 |
|
| 264 |
+
def close(self):
|
| 265 |
+
if self.render_worker:
|
| 266 |
+
self.render_worker.close()
|
| 267 |
+
self.render_worker = None
|
| 268 |
+
|
| 269 |
+
def __del__(self):
|
| 270 |
+
self.close()
|
| 271 |
+
|
| 272 |
def render(self, latex_list: list) -> list:
|
| 273 |
"""
|
| 274 |
渲染 LaTeX 表达式列表。
|
scripts/app.py
CHANGED
|
@@ -28,15 +28,18 @@ def compute_fastcdm(gt: str, pred: str):
|
|
| 28 |
|
| 29 |
driver_path = str(CHROMEDRIVER_PATH) if CHROMEDRIVER_PATH.exists() else None
|
| 30 |
fastcdm = FastCDM(chromedriver=driver_path)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
with gr.Blocks(title="FastCDM 可视化") as demo:
|
|
|
|
| 28 |
|
| 29 |
driver_path = str(CHROMEDRIVER_PATH) if CHROMEDRIVER_PATH.exists() else None
|
| 30 |
fastcdm = FastCDM(chromedriver=driver_path)
|
| 31 |
+
try:
|
| 32 |
+
f1, recall, precision, vis_img = fastcdm.compute(gt, pred, visualize=True)
|
| 33 |
+
|
| 34 |
+
if vis_img is not None:
|
| 35 |
+
vis_rgb = cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB)
|
| 36 |
+
else:
|
| 37 |
+
vis_rgb = None
|
| 38 |
+
|
| 39 |
+
metrics_md = f"**CDM得分(F1)**: {f1:.4f} \n**召回率**: {recall:.4f} \n**准确率**: {precision:.4f}"
|
| 40 |
+
return metrics_md, vis_rgb
|
| 41 |
+
finally:
|
| 42 |
+
fastcdm.close()
|
| 43 |
|
| 44 |
|
| 45 |
with gr.Blocks(title="FastCDM 可视化") as demo:
|