Spaces:
Running
Running
| import os, sys, pathlib, shutil | |
| from huggingface_hub import hf_hub_download | |
| def main(): | |
| MODEL_REPO = os.getenv("MODEL_REPO", "TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF") | |
| MODEL_FILE = os.getenv("MODEL_FILE", "tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf") | |
| MODEL_DIR = pathlib.Path(os.getenv("MODEL_DIR", "/tmp/models")) | |
| MODEL_PATH = MODEL_DIR / MODEL_FILE | |
| MODEL_DIR.mkdir(parents=True, exist_ok=True) | |
| try: | |
| print(f"[download] repo={MODEL_REPO} file={MODEL_FILE} -> {MODEL_PATH}", file=sys.stderr, flush=True) | |
| # Download (to cache, possibly symlinked) | |
| downloaded = hf_hub_download( | |
| repo_id=MODEL_REPO, | |
| filename=MODEL_FILE, | |
| local_dir=str(MODEL_DIR), | |
| local_dir_use_symlinks=False, | |
| token=os.getenv("HF_TOKEN") or None, | |
| ) | |
| #temporary logging | |
| print(f"[MARKER] Downloaded to: {downloaded}", file=sys.stderr, flush=True) | |
| print(f"[MARKER] Model path expected: {MODEL_PATH}", file=sys.stderr, flush=True) | |
| print(f"[MARKER] /tmp/models after copy: {os.listdir(MODEL_DIR)}", file=sys.stderr, flush=True) | |
| print(f"[download] hf_hub_download saved: {downloaded}", file=sys.stderr, flush=True) | |
| # Copy to expected flat path if different | |
| if pathlib.Path(downloaded).resolve() != MODEL_PATH.resolve(): | |
| shutil.copy2(downloaded, MODEL_PATH) | |
| print(f"[MARKER] [download] copied to: {MODEL_PATH}", file=sys.stderr, flush=True) | |
| # Write debug marker | |
| with open(MODEL_DIR / "MODEL_PATH.txt", "w") as f: | |
| f.write(str(MODEL_PATH) + "\n") | |
| print(f"[MARKER] Final /tmp/models contents: {os.listdir(MODEL_DIR)}", file=sys.stderr, flush=True) | |
| #temporary symlink info | |
| print(f"[MARKER] Symlink? {os.path.islink(downloaded)}", file=sys.stderr, flush=True) | |
| except Exception as e: | |
| print(f"!!! Exception in download_model.py: {e}", file=sys.stderr, flush=True) | |
| import traceback | |
| traceback.print_exc() | |
| if __name__ == "__main__": | |
| main() | |