Resolve an ONNX file path across the paths LocalAI might deliver it from. Search order: 1. The path itself if it already resolves (absolute, or relative to CWD). 2. `model_dir` (typically `os.path.dirname(ModelOptions.ModelFile)`) — this is how LocalAI surfaces gallery-mana
(path: str, model_dir: str | None = None)
| 532 | |
| 533 | |
| 534 | def _resolve_model_path(path: str, model_dir: str | None = None) -> str: |
| 535 | """Resolve an ONNX file path across the paths LocalAI might deliver it from. |
| 536 | |
| 537 | Search order: |
| 538 | 1. The path itself if it already resolves (absolute, or relative to CWD). |
| 539 | 2. `model_dir` (typically `os.path.dirname(ModelOptions.ModelFile)`) — |
| 540 | this is how LocalAI surfaces gallery-managed files. When the gallery |
| 541 | entry lists `files:`, each one lands under the models directory and |
| 542 | backends load them via filename anchored by ModelFile. |
| 543 | 3. `<script_dir>/<path-without-leading-slash>` — covers dev layouts |
| 544 | where someone manually dropped weights inside the backend dir. |
| 545 | |
| 546 | If none hit, return the literal input so cv2/insightface surfaces a |
| 547 | clearer error naming the actually-attempted path. |
| 548 | """ |
| 549 | import os |
| 550 | |
| 551 | if os.path.isfile(path): |
| 552 | return path |
| 553 | stripped = path.lstrip("/") |
| 554 | candidates: list[str] = [] |
| 555 | if model_dir: |
| 556 | candidates.append(os.path.join(model_dir, os.path.basename(path))) |
| 557 | candidates.append(os.path.join(model_dir, stripped)) |
| 558 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 559 | candidates.append(os.path.join(script_dir, stripped)) |
| 560 | for c in candidates: |
| 561 | if os.path.isfile(c): |
| 562 | return c |
| 563 | return path |
| 564 | |
| 565 | |
| 566 | def build_engine(name: str) -> FaceEngine: |
no test coverage detected