r0mant1c Codex commited on
Commit
f9af049
ยท
1 Parent(s): 8959201

Wire llama.cpp ZeroGPU badge path

Browse files

Co-authored-by: Codex <chatgpt-codex-connector[bot]@users.noreply.github.com>

DEPLOY.md CHANGED
@@ -5,12 +5,12 @@ The active Hugging Face deployment is a **Gradio ZeroGPU Space**.
5
  This workflow is intentionally fixed:
6
 
7
  1. The Space must stay a Gradio Space, not a Docker Space.
8
- 2. Runtime extraction must use the official OpenBMB MiniCPM-V Transformers path.
9
  3. The extraction call must run behind `@spaces.GPU` so Hugging Face allocates ZeroGPU only while the model is needed.
10
  4. Model files must not be committed to the Space git repo.
11
- 5. When the fine-tuned model is ready, only replace the model repo path in `ZEROGPU_MODEL_ID`.
12
 
13
- Do not change this architecture unless the project intentionally gives up ZeroGPU. The only intended future model-serving change is inserting the fine-tuned model repository path into `ZEROGPU_MODEL_ID`.
14
 
15
  ## 1. Space Metadata
16
 
@@ -34,44 +34,53 @@ ZeroGPU is Gradio-only on Hugging Face. It is not available for Docker Spaces, w
34
 
35
  ## 2. Model Serving
36
 
37
- The current model is the official OpenBMB Transformers model:
38
 
39
  ```text
40
- openbmb/MiniCPM-V-4.6
 
 
 
41
  ```
42
 
43
  The backend lives in:
44
 
45
  ```text
46
- src/extraction/zerogpu_transformers.py
47
  ```
48
 
49
  It uses:
50
 
51
  ```python
52
- AutoProcessor.from_pretrained(model_id)
53
- AutoModelForImageTextToText.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
54
  @spaces.GPU(duration=120)
55
  ```
56
 
57
- The app selects this backend by default through:
 
 
 
58
 
59
  ```text
60
  EXTRACTOR_BACKEND=zerogpu
 
61
  ```
62
 
63
- or through `auto`, which resolves to the same ZeroGPU backend.
64
 
65
  ## 3. Future Fine-Tuned Model
66
 
67
  When the fine-tuned model is ready:
68
 
69
- 1. Upload the fine-tuned model to a Hugging Face model repo.
70
- 2. Keep the same Gradio + ZeroGPU + Transformers architecture.
71
- 3. Change only this variable:
 
72
 
73
  ```bash
74
- ZEROGPU_MODEL_ID=<owner>/<fine-tuned-minicpm-v-model>
 
 
75
  ```
76
 
77
  Do not add model files to the Space git repo. Do not reintroduce Docker or `llama-server` for the ZeroGPU deployment.
@@ -84,8 +93,8 @@ This architecture keeps:
84
 
85
  - Free ZeroGPU eligibility.
86
  - No external hosted inference API calls.
87
- - Official OpenBMB model loading through Transformers.
88
- - A clean future swap to a fine-tuned model by changing only `ZEROGPU_MODEL_ID`.
89
 
90
  ## 5. Local Development
91
 
@@ -93,7 +102,7 @@ Local development can run the same backend, although the model may be slow or to
93
 
94
  ```bash
95
  pip install -r requirements.txt
96
- EXTRACTOR_BACKEND=zerogpu python app.py
97
  ```
98
 
99
  For quick UI-only work, continue using the static reference report without triggering extraction.
 
5
  This workflow is intentionally fixed:
6
 
7
  1. The Space must stay a Gradio Space, not a Docker Space.
8
+ 2. Runtime extraction should use the `llamacpp-gpu` backend when we are targeting the Llama Champion badge.
9
  3. The extraction call must run behind `@spaces.GPU` so Hugging Face allocates ZeroGPU only while the model is needed.
10
  4. Model files must not be committed to the Space git repo.
11
+ 5. When the fine-tuned GGUF model is ready, only replace the `LLAMACPP_*` model variables.
12
 
13
+ Do not change this architecture unless the project intentionally gives up ZeroGPU or the llama.cpp backend proves incompatible with ZeroGPU. The intended future model-serving change is inserting the fine-tuned GGUF repository path into the existing `LLAMACPP_*` variables.
14
 
15
  ## 1. Space Metadata
16
 
 
34
 
35
  ## 2. Model Serving
36
 
37
+ The badge-target model path is the official OpenBMB GGUF repo running through llama.cpp:
38
 
39
  ```text
40
+ LLAMACPP_GGUF_REPO=openbmb/MiniCPM-V-4.6-gguf
41
+ LLAMACPP_MODEL_FILE=MiniCPM-V-4_6-Q4_K_M.gguf
42
+ LLAMACPP_MMPROJ_FILE=mmproj-model-f16.gguf
43
+ EXTRACTOR_BACKEND=llamacpp-gpu
44
  ```
45
 
46
  The backend lives in:
47
 
48
  ```text
49
+ src/extraction/llamacpp_gpu.py
50
  ```
51
 
52
  It uses:
53
 
54
  ```python
55
+ llama_cpp.Llama(...)
 
56
  @spaces.GPU(duration=120)
57
  ```
58
 
59
+ This is a valid hackathon badge option because the submitted app remains a Gradio ZeroGPU Space,
60
+ but the model runtime is `llama.cpp` over GGUF rather than a hosted inference API.
61
+
62
+ The safe fallback backend is the official OpenBMB Transformers model:
63
 
64
  ```text
65
  EXTRACTOR_BACKEND=zerogpu
66
+ ZEROGPU_MODEL_ID=openbmb/MiniCPM-V-4.6
67
  ```
68
 
69
+ Use the fallback only if `llama-cpp-python` cannot load MiniCPM-V 4.6 on ZeroGPU.
70
 
71
  ## 3. Future Fine-Tuned Model
72
 
73
  When the fine-tuned model is ready:
74
 
75
+ 1. Convert/quantize the fine-tuned model to GGUF.
76
+ 2. Upload the fine-tuned GGUF model and compatible mmproj file to a Hugging Face model repo.
77
+ 3. Keep the same Gradio + ZeroGPU + llama.cpp architecture.
78
+ 4. Change only these variables:
79
 
80
  ```bash
81
+ LLAMACPP_GGUF_REPO=<owner>/<fine-tuned-minicpm-v-gguf-repo>
82
+ LLAMACPP_MODEL_FILE=<fine-tuned-model>.gguf
83
+ LLAMACPP_MMPROJ_FILE=<compatible-mmproj>.gguf
84
  ```
85
 
86
  Do not add model files to the Space git repo. Do not reintroduce Docker or `llama-server` for the ZeroGPU deployment.
 
93
 
94
  - Free ZeroGPU eligibility.
95
  - No external hosted inference API calls.
96
+ - A valid `llama.cpp` / GGUF runtime path for the Llama Champion badge, if `llama-cpp-python` is compatible.
97
+ - A clean future swap to a fine-tuned GGUF model by changing only `LLAMACPP_*` variables.
98
 
99
  ## 5. Local Development
100
 
 
102
 
103
  ```bash
104
  pip install -r requirements.txt
105
+ EXTRACTOR_BACKEND=llamacpp-gpu python app.py
106
  ```
107
 
108
  For quick UI-only work, continue using the static reference report without triggering extraction.
DEPLOYMENT_LOG.md CHANGED
@@ -33,3 +33,36 @@ ZEROGPU_MODEL_ID=<owner>/<fine-tuned-minicpm-v-model>
33
  ```
34
 
35
  Do not reintroduce Docker or `llama-server` while the project is targeting ZeroGPU. Do not commit model files to the Space repository.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  ```
34
 
35
  Do not reintroduce Docker or `llama-server` while the project is targeting ZeroGPU. Do not commit model files to the Space repository.
36
+
37
+ ## 2026-06-11 โ€” Add llama.cpp badge path on ZeroGPU
38
+
39
+ Decision: keep the Space as **Gradio ZeroGPU**, but target the hackathon llama.cpp badge with the
40
+ `llamacpp-gpu` backend.
41
+
42
+ Why:
43
+
44
+ - ZeroGPU requires Gradio SDK, so Docker is still not the right deployment surface.
45
+ - The llama.cpp badge can still be targeted from a Gradio Space if inference runs through
46
+ `llama-cpp-python` over GGUF inside `@spaces.GPU`.
47
+ - The official OpenBMB GGUF repo stays outside the Space git repo and is downloaded through the
48
+ Hugging Face cache at runtime.
49
+
50
+ Current badge-target variables:
51
+
52
+ ```bash
53
+ EXTRACTOR_BACKEND=llamacpp-gpu
54
+ LLAMACPP_GGUF_REPO=openbmb/MiniCPM-V-4.6-gguf
55
+ LLAMACPP_MODEL_FILE=MiniCPM-V-4_6-Q4_K_M.gguf
56
+ LLAMACPP_MMPROJ_FILE=mmproj-model-f16.gguf
57
+ ```
58
+
59
+ Fallback if `llama-cpp-python` is incompatible with MiniCPM-V 4.6 on ZeroGPU:
60
+
61
+ ```bash
62
+ EXTRACTOR_BACKEND=zerogpu
63
+ ZEROGPU_MODEL_ID=openbmb/MiniCPM-V-4.6
64
+ ```
65
+
66
+ Future fine-tuned model:
67
+
68
+ Only change the `LLAMACPP_*` variables to point at the fine-tuned GGUF repo/files.
README.md CHANGED
@@ -48,23 +48,28 @@ The knowledge graph is educational context, not diagnosis. The lab-provided refe
48
  The Hugging Face Space is intentionally deployed as a **Gradio ZeroGPU Space**. This is the active
49
  deployment path.
50
 
51
- The app uses the official OpenBMB Transformers model path for MiniCPM-V 4.6 and allocates a ZeroGPU
52
- worker only for the extraction call through `@spaces.GPU`. The deterministic knowledge-graph
53
  enrichment and UI rendering stay in normal Gradio/Python code.
54
 
55
  This workflow should not be further changed back to Docker unless the project intentionally gives up
56
- ZeroGPU. When the fine-tuned model is ready, only replace the model variable:
57
 
58
  ```bash
59
- ZEROGPU_MODEL_ID=openbmb/MiniCPM-V-4.6
 
 
 
60
  ```
61
 
62
- The future fine-tuned deployment should keep the same Gradio + ZeroGPU + Transformers architecture
63
- and only insert the fine-tuned model repository path into `ZEROGPU_MODEL_ID`.
 
 
64
 
65
  ## Local Setup
66
 
67
  ```bash
68
  pip install -r requirements.txt
69
- EXTRACTOR_BACKEND=zerogpu python app.py
70
  ```
 
48
  The Hugging Face Space is intentionally deployed as a **Gradio ZeroGPU Space**. This is the active
49
  deployment path.
50
 
51
+ The badge-target backend runs the official OpenBMB MiniCPM-V 4.6 GGUF through `llama.cpp`
52
+ (`llama-cpp-python`) inside a ZeroGPU-managed function. The deterministic knowledge-graph
53
  enrichment and UI rendering stay in normal Gradio/Python code.
54
 
55
  This workflow should not be further changed back to Docker unless the project intentionally gives up
56
+ ZeroGPU. When the fine-tuned GGUF model is ready, only replace the model variables:
57
 
58
  ```bash
59
+ EXTRACTOR_BACKEND=llamacpp-gpu
60
+ LLAMACPP_GGUF_REPO=openbmb/MiniCPM-V-4.6-gguf
61
+ LLAMACPP_MODEL_FILE=MiniCPM-V-4_6-Q4_K_M.gguf
62
+ LLAMACPP_MMPROJ_FILE=mmproj-model-f16.gguf
63
  ```
64
 
65
+ The future fine-tuned deployment should keep the same Gradio + ZeroGPU + `llama.cpp` architecture
66
+ and only insert the fine-tuned GGUF repository/path into the `LLAMACPP_*` variables. If
67
+ `llama-cpp-python` proves incompatible with MiniCPM-V 4.6 on ZeroGPU, the fallback backend is
68
+ `EXTRACTOR_BACKEND=zerogpu` with `ZEROGPU_MODEL_ID=openbmb/MiniCPM-V-4.6`.
69
 
70
  ## Local Setup
71
 
72
  ```bash
73
  pip install -r requirements.txt
74
+ EXTRACTOR_BACKEND=llamacpp-gpu python app.py
75
  ```
RUNBOOK.md CHANGED
@@ -10,10 +10,11 @@ This replaced the Docker + `llama-server` path because ZeroGPU is only available
10
  |---|---|
11
  | Space SDK | `gradio` |
12
  | Hardware | ZeroGPU |
13
- | Model runtime | Official OpenBMB Transformers path |
14
- | Default backend | `EXTRACTOR_BACKEND=auto`, resolving to `zerogpu` |
15
- | Model variable | `ZEROGPU_MODEL_ID` |
16
- | Extraction backend | `src/extraction/zerogpu_transformers.py` |
 
17
  | Report enrichment | `src/report_pipeline.py` + `kb/cbc_knowledge_graph.json` |
18
 
19
  Do not switch the Space back to Docker unless the project intentionally gives up ZeroGPU.
@@ -22,8 +23,9 @@ Do not switch the Space back to Docker unless the project intentionally gives up
22
 
23
  `EXTRACTOR_BACKEND`:
24
 
25
- - `auto`: default, uses ZeroGPU Transformers.
26
- - `zerogpu`: force the ZeroGPU Transformers backend.
 
27
  - `api`: hosted OpenBMB endpoint for development fallback only.
28
  - `local` / `server` / `llamacpp`: local experimental backends, not the active HF Space path.
29
 
@@ -49,6 +51,7 @@ Install dependencies from `requirements.txt`, including:
49
 
50
  ```text
51
  spaces
 
52
  transformers[torch]>=5.7.0
53
  torch
54
  torchvision
@@ -56,28 +59,41 @@ av
56
  accelerate
57
  ```
58
 
59
- The backend uses `@spaces.GPU(duration=120)` for the model generation call.
60
 
61
  ## Current Model
62
 
63
- Current default:
64
 
65
  ```bash
66
- ZEROGPU_MODEL_ID=openbmb/MiniCPM-V-4.6
 
 
 
67
  ```
68
 
69
- This is the official OpenBMB model. No model files are committed to the Space repo.
 
 
 
 
 
 
 
70
 
71
  ## Fine-Tuned Model Swap
72
 
73
  When the fine-tuned model is ready:
74
 
75
- 1. Upload it to a Hugging Face model repo.
76
- 2. Keep the same Gradio + ZeroGPU + Transformers architecture.
77
- 3. Change only:
 
78
 
79
  ```bash
80
- ZEROGPU_MODEL_ID=<owner>/<fine-tuned-minicpm-v-model>
 
 
81
  ```
82
 
83
  Do not add model files to the Space git repo. Do not reintroduce Docker or `llama-server` for the ZeroGPU deployment.
 
10
  |---|---|
11
  | Space SDK | `gradio` |
12
  | Hardware | ZeroGPU |
13
+ | Badge-target runtime | `llama.cpp` through `llama-cpp-python` |
14
+ | Badge-target backend | `EXTRACTOR_BACKEND=llamacpp-gpu` |
15
+ | Fallback backend | `EXTRACTOR_BACKEND=zerogpu` with Transformers |
16
+ | Model variables | `LLAMACPP_GGUF_REPO`, `LLAMACPP_MODEL_FILE`, `LLAMACPP_MMPROJ_FILE` |
17
+ | Extraction backends | `src/extraction/llamacpp_gpu.py`, `src/extraction/zerogpu_transformers.py` |
18
  | Report enrichment | `src/report_pipeline.py` + `kb/cbc_knowledge_graph.json` |
19
 
20
  Do not switch the Space back to Docker unless the project intentionally gives up ZeroGPU.
 
23
 
24
  `EXTRACTOR_BACKEND`:
25
 
26
+ - `auto`: default, uses ZeroGPU Transformers as the safest startup path.
27
+ - `llamacpp-gpu`: badge-target path, runs GGUF through `llama.cpp` inside `@spaces.GPU`.
28
+ - `zerogpu`: force the ZeroGPU Transformers fallback backend.
29
  - `api`: hosted OpenBMB endpoint for development fallback only.
30
  - `local` / `server` / `llamacpp`: local experimental backends, not the active HF Space path.
31
 
 
51
 
52
  ```text
53
  spaces
54
+ llama-cpp-python
55
  transformers[torch]>=5.7.0
56
  torch
57
  torchvision
 
59
  accelerate
60
  ```
61
 
62
+ Both ZeroGPU backends use `@spaces.GPU(duration=120)` for the model generation call.
63
 
64
  ## Current Model
65
 
66
+ Badge-target Space variables:
67
 
68
  ```bash
69
+ EXTRACTOR_BACKEND=llamacpp-gpu
70
+ LLAMACPP_GGUF_REPO=openbmb/MiniCPM-V-4.6-gguf
71
+ LLAMACPP_MODEL_FILE=MiniCPM-V-4_6-Q4_K_M.gguf
72
+ LLAMACPP_MMPROJ_FILE=mmproj-model-f16.gguf
73
  ```
74
 
75
+ This is the official OpenBMB GGUF model path. No model files are committed to the Space repo.
76
+
77
+ Fallback variables if llama.cpp is incompatible on ZeroGPU:
78
+
79
+ ```bash
80
+ EXTRACTOR_BACKEND=zerogpu
81
+ ZEROGPU_MODEL_ID=openbmb/MiniCPM-V-4.6
82
+ ```
83
 
84
  ## Fine-Tuned Model Swap
85
 
86
  When the fine-tuned model is ready:
87
 
88
+ 1. Convert/quantize it to GGUF.
89
+ 2. Upload it and the compatible mmproj to a Hugging Face model repo.
90
+ 3. Keep the same Gradio + ZeroGPU + llama.cpp architecture.
91
+ 4. Change only:
92
 
93
  ```bash
94
+ LLAMACPP_GGUF_REPO=<owner>/<fine-tuned-minicpm-v-gguf-repo>
95
+ LLAMACPP_MODEL_FILE=<fine-tuned-model>.gguf
96
+ LLAMACPP_MMPROJ_FILE=<compatible-mmproj>.gguf
97
  ```
98
 
99
  Do not add model files to the Space git repo. Do not reintroduce Docker or `llama-server` for the ZeroGPU deployment.
requirements.txt CHANGED
@@ -10,3 +10,8 @@ torch
10
  torchvision
11
  av
12
  transformers[torch]>=5.7.0
 
 
 
 
 
 
10
  torchvision
11
  av
12
  transformers[torch]>=5.7.0
13
+
14
+ # Llama Champion path: run the official OpenBMB GGUF through llama.cpp on ZeroGPU.
15
+ # The extra index provides prebuilt CUDA wheels so the Space does not compile llama.cpp.
16
+ --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121
17
+ llama-cpp-python==0.3.16
src/extraction/factory.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  `EXTRACTOR_BACKEND` env:
4
  - `auto` / `zerogpu` (default): HF ZeroGPU + official OpenBMB Transformers model.
 
5
  - `api`: hosted OpenBMB endpoint (dev fallback only).
6
  - `local` / `server`: local llama-server backend for local development.
7
  - `llamacpp`: in-process llama-cpp-python backend for local development.
 
2
 
3
  `EXTRACTOR_BACKEND` env:
4
  - `auto` / `zerogpu` (default): HF ZeroGPU + official OpenBMB Transformers model.
5
+ - `llamacpp-gpu` / `llama-champion`: HF ZeroGPU + llama.cpp GGUF badge path.
6
  - `api`: hosted OpenBMB endpoint (dev fallback only).
7
  - `local` / `server`: local llama-server backend for local development.
8
  - `llamacpp`: in-process llama-cpp-python backend for local development.
src/extraction/llamacpp_gpu.py CHANGED
@@ -7,7 +7,7 @@ the model + vision projector are downloaded from the Hub, all layers are offload
7
  - ๐Ÿ”Œ Off the Grid (model in the Space, no external inference API)
8
  - quantized GGUF on GPU (fast, low VRAM)
9
 
10
- Requires a llama-cpp-python build with MiniCPM-V 4.6 support (>= 0.3.39) and a CUDA wheel.
11
 
12
  Config (env):
13
  LLAMACPP_GGUF_REPO HF repo with the GGUF + mmproj (default: openbmb/MiniCPM-V-4.6-gguf;
@@ -30,6 +30,7 @@ from src.openbmb_client import (
30
  EXTRACTION_PROMPT,
31
  ExtractionResult,
32
  _normalize_notes,
 
33
  _normalize_tests,
34
  _parse_json_response,
35
  )
@@ -74,6 +75,7 @@ class LlamaCppGPUExtractor:
74
  )
75
  parsed = _parse_json_response(raw)
76
  return ExtractionResult(
 
77
  tests=_normalize_tests(parsed.get("tests", [])),
78
  notes=_normalize_notes(parsed.get("notes", [])),
79
  raw_response=raw,
 
7
  - ๐Ÿ”Œ Off the Grid (model in the Space, no external inference API)
8
  - quantized GGUF on GPU (fast, low VRAM)
9
 
10
+ Requires a llama-cpp-python CUDA wheel with the MiniCPM-V chat handler available.
11
 
12
  Config (env):
13
  LLAMACPP_GGUF_REPO HF repo with the GGUF + mmproj (default: openbmb/MiniCPM-V-4.6-gguf;
 
30
  EXTRACTION_PROMPT,
31
  ExtractionResult,
32
  _normalize_notes,
33
+ _normalize_patient,
34
  _normalize_tests,
35
  _parse_json_response,
36
  )
 
75
  )
76
  parsed = _parse_json_response(raw)
77
  return ExtractionResult(
78
+ patient=_normalize_patient(parsed.get("patient", {})),
79
  tests=_normalize_tests(parsed.get("tests", [])),
80
  notes=_normalize_notes(parsed.get("notes", [])),
81
  raw_response=raw,