Spaces:
Running on Zero

File size: 2,422 Bytes
0a96270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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