Codex Codex commited on
Commit ·
f9ab250
1
Parent(s): f6c987a
Select llama.cpp GGUF on CPU Basic Spaces
Browse filesCo-authored-by: Codex <chatgpt-codex-connector[bot]@users.noreply.github.com>
Files changed (10)
- DEPLOY.md+32-15
- README.md+1-1
- RUNBOOK.md+29-13
- requirements.txt+2-2
- src/extraction/__init__.py+2-2
- src/extraction/factory.py+24-4
- src/extraction/llamacpp_gpu.py+57-4
- src/space_runtime.pyAdded+47
- tests/test_extraction_factory.pyAdded+32
- tests/test_llamacpp_gpu.py+25
123456789101112131415161715 unmodified lines33343536373839404142434416 unmodified lines61626364656667686970711 unmodified line73747576777879808145 unmodified lines127128129130131132133134135# Deploying the Space with ZeroGPUThe active Hugging Face deployment is a **Gradio ZeroGPU Space** with **Transformers vision** as the default extraction backend.This workflow is intentionally fixed:1. The Space must stay a Gradio Space, not a Docker Space.2. Runtime extraction should use the Transformers backend on ZeroGPU by default.3. The extraction call must run behind `@spaces.GPU` so Hugging Face allocates ZeroGPU only while the model is needed.4. Model files must not be committed to the Space git repo.5. When the fine-tuned model is ready, replace only the model variables for the active lanes.6. The llama.cpp lane is optional and must be enabled explicitly with environment variables.Do not change this architecture unless the project intentionally gives up ZeroGPU. To swap models, change `ZEROGPU_MODEL_ID` (or `DEFAULT_HF_REPO` in `src/model_paths.py`) and optional `LLAMACPP_*` for the llama.cpp lane.## 1. Space Metadata15 unmodified linesZeroGPU is Gradio-only on Hugging Face. It is not available for Docker Spaces, which is why the previous Docker + `llama-server` deployment was replaced.## 2. Default Model Serving (Transformers)The production Space path is the fine-tuned Transformers repo:```textEXTRACTOR_BACKEND=transformersZEROGPU_MODEL_ID=build-small-hackathon/blood-test-minicpmv-4_6-medreason```16 unmodified linesThis is the correct runtime for PDF/image blood-test uploads on ZeroGPU because the GPU is allocated only inside the decorated worker.Aliases `auto`, `zerogpu`, and `zero-gpu` resolve to the same Transformers path in `src/extraction/factory.py`.## 3. Optional llama.cpp LaneThe app ships a second extraction lane for hackathon badges and GGUF deployment experiments. It is **not enabled by default**.### Why it exists1 unmodified line- **Fine-tuned GGUF swap** — deploy a quantized model without changing the Gradio app structure.- **Text-only fallback** — lighter lane for plain-text lab exports when vision is not needed.For normal PDF/image uploads, keep `EXTRACTOR_BACKEND=transformers`.### Enable vision llama.cpp on the SpaceSet these variables in the Space settings:45 unmodified linesThis architecture keeps:- Free ZeroGPU eligibility.- No external hosted inference API calls.- The fine-tuned Transformers runtime on ZeroGPU for PDF/image lab reports.- An optional llama.cpp / GGUF lane for badges and fine-tuned GGUF deployment.- A clean model swap by changing `ZEROGPU_MODEL_ID` / `DEFAULT_HF_REPO` and optional `LLAMACPP_*` variables.## 6. Local Development123456789101112131415161718192015 unmodified lines3637383940414243444546474849505152535455565758596016 unmodified lines77787980818283848586871 unmodified line89909192939495969745 unmodified lines143144145146147148149150151152# Deploying the SpaceThe active Hugging Face deployment is a **Gradio Space** with hardware-aware extraction:- **CPU Basic:** llama.cpp with the base MiniCPM-V GGUF model.- **ZeroGPU / GPU:** Transformers vision with the fine-tuned MiniCPM-V checkpoint.This workflow is intentionally fixed:1. The Space must stay a Gradio Space, not a Docker Space.2. Runtime extraction should use `EXTRACTOR_BACKEND=auto` unless a lane is being forced for testing.3. ZeroGPU extraction calls must run behind `@spaces.GPU`; CPU Basic llama.cpp calls must not require ZeroGPU.4. Model files must not be committed to the Space git repo.5. When the fine-tuned model is ready, replace only the model variables for the active lanes.6. The llama.cpp lane is automatic on CPU Basic and can still be enabled explicitly with environment variables.Do not change this architecture unless the project intentionally gives up hardware-aware deployment. To swap models, change `ZEROGPU_MODEL_ID` (or `DEFAULT_HF_REPO` in `src/model_paths.py`) and optional `LLAMACPP_*` for the llama.cpp lane.## 1. Space Metadata15 unmodified linesZeroGPU is Gradio-only on Hugging Face. It is not available for Docker Spaces, which is why the previous Docker + `llama-server` deployment was replaced.## 2. Default Model ServingLeave `EXTRACTOR_BACKEND` unset or set it to:```textEXTRACTOR_BACKEND=auto```On **CPU Basic**, `auto` detects `cpu-basic` and selects llama.cpp vision with the base GGUF model:```textLLAMACPP_GGUF_REPO=openbmb/MiniCPM-V-4.6-ggufLLAMACPP_MODEL_FILE=MiniCPM-V-4_6-Q4_K_M.ggufLLAMACPP_MMPROJ_FILE=mmproj-model-f16.gguf```On **ZeroGPU/GPU**, `auto` selects the fine-tuned Transformers repo:```textZEROGPU_MODEL_ID=build-small-hackathon/blood-test-minicpmv-4_6-medreason```16 unmodified linesThis is the correct runtime for PDF/image blood-test uploads on ZeroGPU because the GPU is allocated only inside the decorated worker.Aliases `zerogpu` and `zero-gpu` force the Transformers path in `src/extraction/factory.py`; `auto` is hardware-aware.## 3. llama.cpp LaneThe app ships a second extraction lane for CPU Basic, hackathon badges, and GGUF deployment experiments. It is automatic on CPU Basic.### Why it exists1 unmodified line- **Fine-tuned GGUF swap** — deploy a quantized model without changing the Gradio app structure.- **Text-only fallback** — lighter lane for plain-text lab exports when vision is not needed.For normal CPU Basic PDF/image uploads, keep `EXTRACTOR_BACKEND=auto` and let the code select llama.cpp vision.### Force vision llama.cpp on the SpaceSet these variables in the Space settings:45 unmodified linesThis architecture keeps:- Free ZeroGPU eligibility.- A CPU Basic fallback that uses llama.cpp + base GGUF instead of Transformers.- No external hosted inference API calls.- The fine-tuned Transformers runtime on ZeroGPU for PDF/image lab reports.- A llama.cpp / GGUF lane for CPU Basic, badges, and fine-tuned GGUF deployment.- A clean model swap by changing `ZEROGPU_MODEL_ID` / `DEFAULT_HF_REPO` and optional `LLAMACPP_*` variables.## 6. Local Development
41 unmodified lines4243444546474841 unmodified lines## 🦙 Llama ChampionThe app ships two interchangeable backends behind one interface. The default runs the model through Transformers, and a second backend runs the same MiniCPM-V through the **llama.cpp** runtime, selectable with a single environment variable. We install the prebuilt llama.cpp wheel so the Space builds without a slow source compile, which means the model can run through the llama.cpp runtime when we point it there, while Transformers stays the default for the hosted demo.## 🎨 Off-Brand41 unmodified lines4243444546474841 unmodified lines## 🦙 Llama ChampionThe app ships two interchangeable backends behind one interface. On CPU Basic Spaces, the default `auto` backend runs the base MiniCPM-V 4.6 GGUF through **llama.cpp** on CPU. On ZeroGPU/GPU, the same `auto` setting uses the fine-tuned Transformers model. We install the prebuilt llama.cpp wheel so the Space builds without a slow source compile, and the operator can still force the GGUF lane explicitly with `EXTRACTOR_BACKEND=llamacpp-gpu` and `LLAMACPP_VISION=1`.## 🎨 Off-Brand
1234562 unmodified lines910111213141516177 unmodified lines25262728293031322 unmodified lines35363738394041424344454647484949 unmodified lines991001011021031041055 unmodified lines11111211311411511611711811912060 unmodified lines181182183184# Runbook — Extraction Backends + Fine-Tuned Model SwapThe active deployment path is **Gradio with Transformers vision on ZeroGPU**.This replaced the Docker + `llama-server` path because ZeroGPU is only available for Gradio SDK Spaces. The Docker build was also failing on free CPU hardware with `OOMKilled`.2 unmodified lines| Area | Current choice ||---|---|| Space SDK | `gradio` || Default extraction | Fine-tuned MiniCPM-V 4.6 (`build-small-hackathon/blood-test-minicpmv-4_6-medreason`) || ZeroGPU worker | `@spaces.GPU` in `src/extraction/zerogpu_transformers.py` || Optional llama.cpp lane | `EXTRACTOR_BACKEND=llamacpp-gpu` (+ `LLAMACPP_VISION=1` for PDF/images) || Transformers variables | `ZEROGPU_MODEL_ID`, `ZEROGPU_MAX_NEW_TOKENS`, `ZEROGPU_DOWNSAMPLE_MODE` || llama.cpp variables | `LLAMACPP_GGUF_REPO`, `LLAMACPP_MODEL_FILE`, `LLAMACPP_MMPROJ_FILE`, `LLAMACPP_VISION` || Extraction backends | `src/extraction/factory.py`, `src/extraction/zerogpu_transformers.py`, `src/extraction/llamacpp_gpu.py` |7 unmodified lines| Value | Behavior ||---|---|| `transformers` (default) | Fine-tuned MiniCPM-V through Transformers vision || `auto`, `zerogpu`, `zero-gpu` | Same as `transformers` || `llamacpp-gpu`, `llama-champion` | GGUF through `llama-cpp-python` || `local`, `server` | Local `llama-server` HTTP backend || `llamacpp` | In-process local GGUF + mmproj |2 unmodified lines### Default path (production)```bashEXTRACTOR_BACKEND=transformersZEROGPU_MODEL_ID=build-small-hackathon/blood-test-minicpmv-4_6-medreason```This is what the HF Space should use for PDF/image blood-test uploads. The env var is optional when it matches `DEFAULT_HF_REPO` in `src/model_paths.py`.### Optional llama.cpp pathThe llama.cpp lane is **opt-in**. It is not selected automatically.**Why keep it:**49 unmodified linesllama-cpp-python```Transformers runs on ZeroGPU through `@spaces.GPU(duration=120)` (or longer for cold starts). The optional llama.cpp lane uses `@spaces.GPU(duration=600)` because GGUF inference can be slower.On Linux x86_64 Spaces, `llama-cpp-python` comes from the prebuilt CPU manylinux wheel:5 unmodified lines## Current Model DefaultsPrimary lane:```bashEXTRACTOR_BACKEND=transformersZEROGPU_MODEL_ID=build-small-hackathon/blood-test-minicpmv-4_6-medreason```60 unmodified lines.venv/bin/python -m pytest tests/test_report_pipeline.py tests/test_llamacpp_gpu.py```Then verify the Space build uses Gradio, not Docker, and that the default backend extracts PDF/image uploads through Transformers.1234567892 unmodified lines1213141516171819207 unmodified lines28293031323334352 unmodified lines383940414243444546474849505152535455565758596049 unmodified lines1101111121131141151165 unmodified lines12212312412512612712812913013113213313413513660 unmodified lines197198199200# Runbook — Extraction Backends + Fine-Tuned Model SwapThe active deployment path is **hardware-aware Gradio**:- **CPU Basic:** llama.cpp with the base MiniCPM-V GGUF model.- **ZeroGPU / GPU:** Transformers vision with the fine-tuned MiniCPM-V checkpoint.This replaced the Docker + `llama-server` path because ZeroGPU is only available for Gradio SDK Spaces. The Docker build was also failing on free CPU hardware with `OOMKilled`.2 unmodified lines| Area | Current choice ||---|---|| Space SDK | `gradio` || Default extraction | `auto`: CPU Basic uses base GGUF through llama.cpp; ZeroGPU/GPU uses fine-tuned Transformers || ZeroGPU worker | `@spaces.GPU` in `src/extraction/zerogpu_transformers.py` || llama.cpp lane | Automatic on CPU Basic, or forced with `EXTRACTOR_BACKEND=llamacpp-gpu` (+ `LLAMACPP_VISION=1` for PDF/images) || Transformers variables | `ZEROGPU_MODEL_ID`, `ZEROGPU_MAX_NEW_TOKENS`, `ZEROGPU_DOWNSAMPLE_MODE` || llama.cpp variables | `LLAMACPP_GGUF_REPO`, `LLAMACPP_MODEL_FILE`, `LLAMACPP_MMPROJ_FILE`, `LLAMACPP_VISION` || Extraction backends | `src/extraction/factory.py`, `src/extraction/zerogpu_transformers.py`, `src/extraction/llamacpp_gpu.py` |7 unmodified lines| Value | Behavior ||---|---|| unset / `auto` (default) | Hardware-aware: CPU Basic -> llama.cpp base GGUF; otherwise Transformers || `transformers`, `zerogpu`, `zero-gpu` | Force fine-tuned MiniCPM-V through Transformers vision || `llamacpp-gpu`, `llama-champion` | GGUF through `llama-cpp-python` || `local`, `server` | Local `llama-server` HTTP backend || `llamacpp` | In-process local GGUF + mmproj |2 unmodified lines### Default path (production)```bash# Usually leave EXTRACTOR_BACKEND unset, or set:EXTRACTOR_BACKEND=auto```On the current CPU Basic Space, `auto` selects llama.cpp vision with the base GGUF defaults:```bashLLAMACPP_GGUF_REPO=openbmb/MiniCPM-V-4.6-ggufLLAMACPP_MODEL_FILE=MiniCPM-V-4_6-Q4_K_M.ggufLLAMACPP_MMPROJ_FILE=mmproj-model-f16.gguf```When the Space is moved to ZeroGPU/GPU, `auto` selects Transformers and uses `ZEROGPU_MODEL_ID` or the default fine-tuned repo in `src/model_paths.py`.### Optional llama.cpp pathThe llama.cpp lane is selected automatically on CPU Basic. It can also be forced explicitly.**Why keep it:**49 unmodified linesllama-cpp-python```Transformers runs on ZeroGPU through `@spaces.GPU(duration=120)` (or longer for cold starts). llama.cpp bypasses `@spaces.GPU` on CPU Basic and runs as CPU inference; when forced on GPU/ZeroGPU it uses `@spaces.GPU(duration=600)`.On Linux x86_64 Spaces, `llama-cpp-python` comes from the prebuilt CPU manylinux wheel:5 unmodified lines## Current Model DefaultsAuto lane:```bashEXTRACTOR_BACKEND=auto```Transformers lane:```bashZEROGPU_MODEL_ID=build-small-hackathon/blood-test-minicpmv-4_6-medreason```60 unmodified lines.venv/bin/python -m pytest tests/test_report_pipeline.py tests/test_llamacpp_gpu.py```Then verify the Space build uses Gradio, not Docker. On CPU Basic, the default backend should report `llamacpp-cpu-vision`; on ZeroGPU/GPU it should report the Transformers backend.
3 unmodified lines456789101112131415163 unmodified linespillow==12.0.0pymupdf==1.26.6json-repair==0.60.1# Local Transformers vision path (Mac + Linux GPU). Default EXTRACTOR_BACKEND=transformers.torch==2.9.1 ; sys_platform == "darwin" or (sys_platform == "linux" and platform_machine == "x86_64")transformers[torch]==5.7.0accelerate==1.12.0bitsandbytes==0.48.2 ; sys_platform == "linux" and platform_machine == "x86_64"torchvision==0.24.1 ; sys_platform == "darwin" or (sys_platform == "linux" and platform_machine == "x86_64")av==16.0.1 ; sys_platform == "linux" and platform_machine == "x86_64"# CPU fallback path: install the prebuilt manylinux wheel directly to avoid a source build on Spaces.llama-cpp-python @ https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.28/llama_cpp_python-0.3.28-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl ; sys_platform == "linux" and platform_machine == "x86_64"llama-cpp-python==0.3.28 ; sys_platform != "linux"3 unmodified lines456789101112131415163 unmodified linespillow==12.0.0pymupdf==1.26.6json-repair==0.60.1# Transformers vision path (Mac + Linux GPU, and non-CPU-Basic Space auto mode).torch==2.9.1 ; sys_platform == "darwin" or (sys_platform == "linux" and platform_machine == "x86_64")transformers[torch]==5.7.0accelerate==1.12.0bitsandbytes==0.48.2 ; sys_platform == "linux" and platform_machine == "x86_64"torchvision==0.24.1 ; sys_platform == "darwin" or (sys_platform == "linux" and platform_machine == "x86_64")av==16.0.1 ; sys_platform == "linux" and platform_machine == "x86_64"# CPU Basic / llama.cpp path: install the prebuilt manylinux wheel directly to avoid a source build on Spaces.llama-cpp-python @ https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.28/llama_cpp_python-0.3.28-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl ; sys_platform == "linux" and platform_machine == "x86_64"llama-cpp-python==0.3.28 ; sys_platform != "linux"
12345678"""Extraction backends behind one interface.`build_extractor()` returns the right backend for the environment:- **transformers** (default): local OpenBMB MiniCPM-V through Transformers.- **auto**: same as transformers.- **llamacpp-gpu** / **llama-champion**: force GGUF through llama.cpp. Set `LLAMACPP_VISION=1`to run the same PDF/image vision pipeline as Transformers (requires mmproj).- **local**: local llama-server / llama.cpp backends for local experimentation.12345678"""Extraction backends behind one interface.`build_extractor()` returns the right backend for the environment:- unset / **auto** (default): CPU Basic Spaces use llama.cpp + base GGUF; other runtimes use Transformers.- **transformers**: force OpenBMB MiniCPM-V through Transformers.- **llamacpp-gpu** / **llama-champion**: force GGUF through llama.cpp. Set `LLAMACPP_VISION=1`to run the same PDF/image vision pipeline as Transformers (requires mmproj).- **local**: local llama-server / llama.cpp backends for local experimentation.
1234567812 unmodified lines21222324252627286 unmodified lines35363738394041"""Backend selection.`EXTRACTOR_BACKEND` env:- `transformers` (default): local OpenBMB MiniCPM-V through Transformers.- `auto`: same as `transformers`.- `zerogpu` / `zero-gpu`: alias for `transformers`.- `llamacpp-gpu` / `llama-champion`: llama.cpp GGUF badge path. Add `LLAMACPP_VISION=1`for PDF/image vision via mmproj.12 unmodified linesfrom src.extraction.llamacpp_gpu import LlamaCppGPUExtractorfrom src.extraction.local_minicpmv import LocalMiniCPMVExtractorfrom src.extraction.local_server import LocalServerExtractor_DEFAULT_BACKEND = "transformers"_DISABLED_BACKENDS = {"api", "openbmb", "hosted"}6 unmodified lines"Use EXTRACTOR_BACKEND=transformers for local MiniCPM-V extraction.")if backend in ("auto", "zerogpu", "zero-gpu", "transformers"):return AutoExtractor(model_id=model)if backend in ("llamacpp-gpu", "gpu-llamacpp", "llama-champion"):from src.extraction.llamacpp_vision import llamacpp_vision_enabled12345678912 unmodified lines2223242526272829306 unmodified lines37383940414243444546474849505152535455565758596061"""Backend selection.`EXTRACTOR_BACKEND` env:- unset / `auto` (default): hardware-aware Space selection.CPU Basic Spaces use llama.cpp + base MiniCPM-V GGUF; other runtimes use Transformers.- `transformers`: local/OpenBMB MiniCPM-V through Transformers.- `zerogpu` / `zero-gpu`: alias for `transformers`.- `llamacpp-gpu` / `llama-champion`: llama.cpp GGUF badge path. Add `LLAMACPP_VISION=1`for PDF/image vision via mmproj.12 unmodified linesfrom src.extraction.llamacpp_gpu import LlamaCppGPUExtractorfrom src.extraction.local_minicpmv import LocalMiniCPMVExtractorfrom src.extraction.local_server import LocalServerExtractorfrom src.space_runtime import configured_space_hardware, is_cpu_basic_space, is_huggingface_space_DEFAULT_BACKEND = "auto"_DISABLED_BACKENDS = {"api", "openbmb", "hosted"}6 unmodified lines"Use EXTRACTOR_BACKEND=transformers for local MiniCPM-V extraction.")if backend == "auto":if is_cpu_basic_space():hardware = configured_space_hardware()if hardware:os.environ.setdefault("BTE_SPACE_HARDWARE", hardware)os.environ.setdefault("LLAMACPP_VISION", "1")print("[Blood Test Explainer] CPU Basic Space detected; using llama.cpp ""(base MiniCPM-V GGUF)",flush=True,)return LlamaCppGPUExtractor()if is_huggingface_space():print("[Blood Test Explainer] non-CPU-Basic Space detected; using Transformers extractor",flush=True,)return AutoExtractor(model_id=model)if backend in ("zerogpu", "zero-gpu", "transformers"):return AutoExtractor(model_id=model)if backend in ("llamacpp-gpu", "gpu-llamacpp", "llama-champion"):from src.extraction.llamacpp_vision import llamacpp_vision_enabled
38 unmodified lines39404142434424 unmodified lines6970717273741 unmodified line767778798081823 unmodified lines86878889909192939495961 unmodified line98991001011021031043 unmodified lines10810911011111211363 unmodified lines17717817918018118234 unmodified lines21721821922022122238 unmodified lines_parse_json_response,summarize_document_parts,)DEFAULT_GGUF_REPO = "openbmb/MiniCPM-V-4.6-gguf"DEFAULT_MODEL_FILE = "MiniCPM-V-4_6-Q4_K_M.gguf"24 unmodified linesself.n_ctx = int(os.getenv("LLAMACPP_N_CTX", "8192"))self.n_gpu_layers = int(os.getenv("LLAMACPP_N_GPU_LAYERS", "0"))self.vision_enabled = llamacpp_vision_enabled()if self.vision_enabled and not self.mmproj_file:raise ValueError("LLAMACPP_VISION=1 requires LLAMACPP_MMPROJ_FILE.")1 unmodified lineparts = document_to_payload_parts(file_path, max_pages=max_pages)started = time.perf_counter()if self.vision_enabled:raw = _run_llamacpp_vision_generation(parts=parts,repo=self.repo,model_file=self.model_file,3 unmodified linesn_ctx=self.n_ctx,n_gpu_layers=self.n_gpu_layers,)backend = "llamacpp-gpu-vision"composed_prompt = Noneelse:prompt_text = _compose_prompt(parts)raw = _run_llamacpp_generation(prompt_text=prompt_text,repo=self.repo,model_file=self.model_file,1 unmodified linen_ctx=self.n_ctx,n_gpu_layers=self.n_gpu_layers,)backend = "llamacpp-gpu"composed_prompt = prompt_textduration_ms = int((time.perf_counter() - started) * 1000)3 unmodified lines"repo": self.repo,"model": self.model_file,"vision_enabled": self.vision_enabled,"document_parts": len(parts),"max_pages": max_pages,"extraction_prompt": EXTRACTION_PROMPT,63 unmodified linesmax_tokens: int,n_ctx: int,n_gpu_layers: int,) -> str:try:model_path = download_hf_file(repo, model_file)34 unmodified linesmax_tokens: int,n_ctx: int,n_gpu_layers: int,) -> str:try:model_path = download_hf_file(repo, model_file)38 unmodified lines3940414243444524 unmodified lines707172737475761 unmodified line7879808182838485868788893 unmodified lines939495969798991001011021031041051061071081 unmodified line1101111121131141151163 unmodified lines12012112212312412512663 unmodified lines19019119219319419519619719819920020120220320420520620720820921021121221321421521621734 unmodified lines25225325425525625725825926026126226326426526626726826927027127227327427538 unmodified lines_parse_json_response,summarize_document_parts,)from src.space_runtime import is_cpu_basic_spaceDEFAULT_GGUF_REPO = "openbmb/MiniCPM-V-4.6-gguf"DEFAULT_MODEL_FILE = "MiniCPM-V-4_6-Q4_K_M.gguf"24 unmodified linesself.n_ctx = int(os.getenv("LLAMACPP_N_CTX", "8192"))self.n_gpu_layers = int(os.getenv("LLAMACPP_N_GPU_LAYERS", "0"))self.vision_enabled = llamacpp_vision_enabled()self.use_spaces_gpu = not is_cpu_basic_space()if self.vision_enabled and not self.mmproj_file:raise ValueError("LLAMACPP_VISION=1 requires LLAMACPP_MMPROJ_FILE.")1 unmodified lineparts = document_to_payload_parts(file_path, max_pages=max_pages)started = time.perf_counter()if self.vision_enabled:runner = (_run_llamacpp_vision_generationif self.use_spaces_gpuelse _run_llamacpp_vision_generation_cpu)raw = runner(parts=parts,repo=self.repo,model_file=self.model_file,3 unmodified linesn_ctx=self.n_ctx,n_gpu_layers=self.n_gpu_layers,)backend = "llamacpp-gpu-vision" if self.use_spaces_gpu else "llamacpp-cpu-vision"composed_prompt = Noneelse:prompt_text = _compose_prompt(parts)runner = (_run_llamacpp_generationif self.use_spaces_gpuelse _run_llamacpp_generation_cpu)raw = runner(prompt_text=prompt_text,repo=self.repo,model_file=self.model_file,1 unmodified linen_ctx=self.n_ctx,n_gpu_layers=self.n_gpu_layers,)backend = "llamacpp-gpu" if self.use_spaces_gpu else "llamacpp-cpu"composed_prompt = prompt_textduration_ms = int((time.perf_counter() - started) * 1000)3 unmodified lines"repo": self.repo,"model": self.model_file,"vision_enabled": self.vision_enabled,"spaces_gpu": self.use_spaces_gpu,"document_parts": len(parts),"max_pages": max_pages,"extraction_prompt": EXTRACTION_PROMPT,63 unmodified linesmax_tokens: int,n_ctx: int,n_gpu_layers: int,) -> str:return _run_llamacpp_vision_generation_cpu(parts=parts,repo=repo,model_file=model_file,mmproj_file=mmproj_file,chat_handler=chat_handler,max_tokens=max_tokens,n_ctx=n_ctx,n_gpu_layers=n_gpu_layers,)def _run_llamacpp_vision_generation_cpu(parts: list[dict[str, Any]],repo: str,model_file: str,mmproj_file: str,chat_handler: str,max_tokens: int,n_ctx: int,n_gpu_layers: int,) -> str:try:model_path = download_hf_file(repo, model_file)34 unmodified linesmax_tokens: int,n_ctx: int,n_gpu_layers: int,) -> str:return _run_llamacpp_generation_cpu(prompt_text=prompt_text,repo=repo,model_file=model_file,max_tokens=max_tokens,n_ctx=n_ctx,n_gpu_layers=n_gpu_layers,)def _run_llamacpp_generation_cpu(prompt_text: str,repo: str,model_file: str,max_tokens: int,n_ctx: int,n_gpu_layers: int,) -> str:try:model_path = download_hf_file(repo, model_file)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647"""Helpers for hardware-aware Hugging Face Space behavior."""
from __future__ import annotations
import osfrom functools import lru_cache
_HARDWARE_ENV_KEYS = ( "BTE_SPACE_HARDWARE", "SPACE_HARDWARE", "HF_SPACE_HARDWARE",)
def is_huggingface_space() -> bool: return bool(os.getenv("SPACE_ID") or os.getenv("SPACE_HOST"))
def configured_space_hardware() -> str | None: for key in _HARDWARE_ENV_KEYS: value = os.getenv(key, "").strip().lower() if value: return value return _hub_space_hardware()
def is_cpu_basic_space() -> bool: return configured_space_hardware() == "cpu-basic"
@lru_cache(maxsize=8)def _hub_space_hardware() -> str | None: repo_id = os.getenv("SPACE_ID", "").strip() if not repo_id: return None try: from huggingface_hub import HfApi
info = HfApi().space_info(repo_id=repo_id) runtime = getattr(info, "runtime", None) hardware = getattr(runtime, "hardware", None) if hardware: return str(hardware).strip().lower() except Exception: return None return None1234567891011121314151617181920212223242526272829303132import sysimport osfrom pathlib import Pathfrom unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from src.extraction.factory import build_extractor
def test_auto_uses_llamacpp_on_cpu_basic_space(monkeypatch): monkeypatch.delenv("EXTRACTOR_BACKEND", raising=False) monkeypatch.delenv("LLAMACPP_VISION", raising=False) monkeypatch.setenv("BTE_SPACE_HARDWARE", "cpu-basic")
with patch("src.extraction.factory.LlamaCppGPUExtractor") as llama_cls: extractor = build_extractor()
llama_cls.assert_called_once_with() assert extractor is llama_cls.return_value assert os.environ["LLAMACPP_VISION"] == "1"
def test_auto_uses_transformers_when_hardware_is_not_cpu_basic(monkeypatch): monkeypatch.delenv("EXTRACTOR_BACKEND", raising=False) monkeypatch.setenv("BTE_SPACE_HARDWARE", "zero-a10g")
with patch("src.extraction.factory.AutoExtractor") as auto_cls: extractor = build_extractor(model="demo/model")
auto_cls.assert_called_once_with(model_id="demo/model") assert extractor is auto_cls.return_value36 unmodified lines37383940414212 unmodified lines5556575859606162636436 unmodified linesdef test_extract_uses_vision_generation_when_enabled(monkeypatch):monkeypatch.setenv("LLAMACPP_VISION", "1")image_parts = [{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}}]with patch(12 unmodified linesassert result.request_summary["backend"] == "llamacpp-gpu-vision"assert result.request_summary["vision_enabled"] is Trueassert result.request_summary["mmproj"]def test_extract_uses_text_generation_when_vision_disabled(monkeypatch):monkeypatch.delenv("LLAMACPP_VISION", raising=False)text_parts = [{"type": "text", "text": "WBC 6.5"}]with patch(36 unmodified lines3738394041424312 unmodified lines5657585960616263646566676869707172737475767778798081828384858687888936 unmodified linesdef test_extract_uses_vision_generation_when_enabled(monkeypatch):monkeypatch.setenv("LLAMACPP_VISION", "1")monkeypatch.delenv("BTE_SPACE_HARDWARE", raising=False)image_parts = [{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}}]with patch(12 unmodified linesassert result.request_summary["backend"] == "llamacpp-gpu-vision"assert result.request_summary["vision_enabled"] is Trueassert result.request_summary["mmproj"]assert result.request_summary["spaces_gpu"] is Truedef test_cpu_basic_uses_plain_cpu_vision_runner(monkeypatch):monkeypatch.setenv("BTE_SPACE_HARDWARE", "cpu-basic")monkeypatch.setenv("LLAMACPP_VISION", "1")image_parts = [{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}}]with patch("src.extraction.llamacpp_gpu.document_to_payload_parts",return_value=image_parts,), patch("src.extraction.llamacpp_gpu._run_llamacpp_vision_generation_cpu",return_value='{"patient":{},"tests":[],"notes":[]}',) as cpu_run, patch("src.extraction.llamacpp_gpu._run_llamacpp_vision_generation",) as gpu_run:result = LlamaCppGPUExtractor().extract("/tmp/report.pdf")cpu_run.assert_called_once()gpu_run.assert_not_called()assert result.request_summary["backend"] == "llamacpp-cpu-vision"assert result.request_summary["spaces_gpu"] is Falsedef test_extract_uses_text_generation_when_vision_disabled(monkeypatch):monkeypatch.delenv("LLAMACPP_VISION", raising=False)monkeypatch.delenv("BTE_SPACE_HARDWARE", raising=False)text_parts = [{"type": "text", "text": "WBC 6.5"}]with patch(
DEPLOY.md+32-15
README.md+1-1
RUNBOOK.md+29-13
requirements.txt+2-2
src/extraction/__init__.py+2-2
src/extraction/factory.py+24-4
src/extraction/llamacpp_gpu.py+57-4
src/space_runtime.pyAdded+47
tests/test_extraction_factory.pyAdded+32
tests/test_llamacpp_gpu.py+25