Spaces:
Running on Zero

Ziyuan-Wang's picture
Upload folder using huggingface_hub
0a96270 verified
Raw History Blame
2.42 kB
# SPDX-License-Identifier: Apache-2.0
"""Make the original, pinned GAIR-NLP RIBOSPAN implementation importable."""
from __future__ import annotations
import os
import sys
import tempfile
from pathlib import Path
from urllib.request import Request, urlopen
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SOURCE_REVISION = "a7ce2c62fbc5491876742893e656a29079bc5613"
SOURCE_URL = (
"https://raw.githubusercontent.com/GAIR-NLP/RIBOSPAN-FM/"
f"{SOURCE_REVISION}"
)
SOURCE_FILES = (
"__init__.py", "configuration.py", "modeling.py", "tokenization.py", "vocab.txt",
)
def _complete(root: Path) -> bool:
return all((root / "ribospan" / name).is_file() for name in SOURCE_FILES)
def ensure_import_path() -> Path:
"""Use a supplied official source tree or fetch the immutable source revision.
Import the upstream package directly rather than installing its training-era
pyproject.toml, which pins torch==2.6.0 (not a supported ZeroGPU runtime).
"""
override = os.environ.get("RIBOSPAN_SOURCE_DIR")
if override:
root = Path(override).expanduser().resolve()
if not _complete(root):
raise RuntimeError("RIBOSPAN_SOURCE_DIR does not contain the complete ribospan package")
else:
root = PROJECT_ROOT / "vendor" / SOURCE_REVISION
if not _complete(root):
root.mkdir(parents=True, exist_ok=True)
(root / "ribospan").mkdir(exist_ok=True)
for relative in [*(f"ribospan/{name}" for name in SOURCE_FILES), "LICENSE"]:
target = root / relative
request = Request(f"{SOURCE_URL}/{relative}", headers={"User-Agent": "RIBOSPAN-Inference"})
with urlopen(request, timeout=45) as response:
data = response.read(2 * 1024 * 1024)
if not data or len(data) >= 2 * 1024 * 1024:
raise RuntimeError(f"invalid pinned source response for {relative}")
with tempfile.NamedTemporaryFile(dir=target.parent, delete=False) as handle:
handle.write(data)
temporary = Path(handle.name)
temporary.replace(target)
if not _complete(root):
raise RuntimeError("the pinned upstream RIBOSPAN source is incomplete")
root_string = str(root)
if root_string not in sys.path:
sys.path.insert(0, root_string)
return root