BinyangQiu commited on
Commit ·
02d982d
1
Parent(s): 7bc81d5
Add Env Check
Browse files- fastcdm/core.py +37 -1
fastcdm/core.py
CHANGED
|
@@ -15,6 +15,9 @@ from typing import List, Tuple
|
|
| 15 |
from pathlib import Path
|
| 16 |
from skimage.measure import ransac
|
| 17 |
import traceback
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
root_dir = Path(__file__).parent
|
| 20 |
TEMPLATE_FILE = root_dir / "render" / "templates" / "formula.html"
|
|
@@ -209,9 +212,42 @@ def postprocess(
|
|
| 209 |
class FastCDM:
|
| 210 |
def __init__(self, chromedriver: str = None) -> None:
|
| 211 |
self.chromedriver = chromedriver
|
|
|
|
| 212 |
self.render_worker = None
|
| 213 |
self.init_render_worker()
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
def init_render_worker(self) -> None:
|
| 216 |
try:
|
| 217 |
self.render_worker = RenderWorker(
|
|
@@ -222,7 +258,7 @@ class FastCDM:
|
|
| 222 |
except Exception as e:
|
| 223 |
print("Failed to init RenderWorker:")
|
| 224 |
print("=" * 30)
|
| 225 |
-
print(traceback.format_exc(
|
| 226 |
return None
|
| 227 |
|
| 228 |
def render(self, latex_list: list) -> list:
|
|
|
|
| 15 |
from pathlib import Path
|
| 16 |
from skimage.measure import ransac
|
| 17 |
import traceback
|
| 18 |
+
import subprocess
|
| 19 |
+
import shutil
|
| 20 |
+
import os
|
| 21 |
|
| 22 |
root_dir = Path(__file__).parent
|
| 23 |
TEMPLATE_FILE = root_dir / "render" / "templates" / "formula.html"
|
|
|
|
| 212 |
class FastCDM:
|
| 213 |
def __init__(self, chromedriver: str = None) -> None:
|
| 214 |
self.chromedriver = chromedriver
|
| 215 |
+
self.check_environment()
|
| 216 |
self.render_worker = None
|
| 217 |
self.init_render_worker()
|
| 218 |
|
| 219 |
+
def check_environment(self) -> None:
|
| 220 |
+
"""
|
| 221 |
+
检查运行环境是否准备就绪。
|
| 222 |
+
1. Node.js 环境是否安装。
|
| 223 |
+
2. ChromeDriver 是否可用。
|
| 224 |
+
"""
|
| 225 |
+
# 1. 检查 Node.js
|
| 226 |
+
node_path = shutil.which("node")
|
| 227 |
+
if not node_path:
|
| 228 |
+
raise RuntimeError(
|
| 229 |
+
"Node.js is not found. Please install Node.js for formula normalization. "
|
| 230 |
+
"Visit https://nodejs.org/ to install it."
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
subprocess.check_output(["node", "--version"], text=True)
|
| 235 |
+
except Exception as e:
|
| 236 |
+
raise RuntimeError(f"Node.js is found but failed to execute: {e}")
|
| 237 |
+
|
| 238 |
+
# 2. 检查 ChromeDriver
|
| 239 |
+
if self.chromedriver:
|
| 240 |
+
if not os.path.exists(self.chromedriver):
|
| 241 |
+
raise FileNotFoundError(f"Specified ChromeDriver not found at: {self.chromedriver}")
|
| 242 |
+
else:
|
| 243 |
+
chromedriver_path = shutil.which("chromedriver")
|
| 244 |
+
if not chromedriver_path:
|
| 245 |
+
raise RuntimeError(
|
| 246 |
+
"ChromeDriver not found in PATH and no path was specified. "
|
| 247 |
+
"Please install ChromeDriver or provide the path via the 'chromedriver' parameter. "
|
| 248 |
+
"You can also use 'scripts/auto_install_chromedriver.py' to download it."
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
def init_render_worker(self) -> None:
|
| 252 |
try:
|
| 253 |
self.render_worker = RenderWorker(
|
|
|
|
| 258 |
except Exception as e:
|
| 259 |
print("Failed to init RenderWorker:")
|
| 260 |
print("=" * 30)
|
| 261 |
+
print(traceback.format_exc())
|
| 262 |
return None
|
| 263 |
|
| 264 |
def render(self, latex_list: list) -> list:
|