electblake commited on
Commit
604e6eb
·
1 Parent(s): bbcca9d

fix: run GGUF quants directly with llama.cpp

Browse files
Files changed (4) hide show
  1. README.md +4 -2
  2. app.py +15 -67
  3. pyproject.toml +1 -12
  4. requirements.txt +2 -4
README.md CHANGED
@@ -5,10 +5,12 @@ colorFrom: blue
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 6.17.3
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
 
 
12
  models:
13
  - Spreadsheet-RL/Spreadsheet-RL-4B
14
  - mradermacher/Spreadsheet-RL-4B-GGUF
@@ -18,6 +20,6 @@ models:
18
 
19
  A basic text-and-file inference app for Spreadsheet-RL-4B, modeled on the prompt entry point in the Spreadsheet-RL agent-system diagram.
20
 
21
- The app accepts a system prompt, user prompt, and optional text or spreadsheet file. Its quantization selector exposes the 4B GGUF variants captured in the project reference material, with Q4_K_M selected by default.
22
 
23
  ZeroGPU support is enabled with the `spaces` package and `@spaces.GPU`. Select ZeroGPU in the Hugging Face Space hardware settings after deployment.
 
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 6.17.3
8
+ python_version: '3.12.12'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
12
+ preload_from_hub:
13
+ - mradermacher/Spreadsheet-RL-4B-GGUF Spreadsheet-RL-4B.Q4_K_M.gguf
14
  models:
15
  - Spreadsheet-RL/Spreadsheet-RL-4B
16
  - mradermacher/Spreadsheet-RL-4B-GGUF
 
20
 
21
  A basic text-and-file inference app for Spreadsheet-RL-4B, modeled on the prompt entry point in the Spreadsheet-RL agent-system diagram.
22
 
23
+ The app accepts a system prompt, user prompt, and optional text or spreadsheet file. Its quantization selector exposes the 4B GGUF variants captured in the project reference material, with Q4_K_M selected by default. Inference runs directly on the selected quantized tensors through llama.cpp without converting them into full PyTorch weights.
24
 
25
  ZeroGPU support is enabled with the `spaces` package and `@spaces.GPU`. Select ZeroGPU in the Hugging Face Space hardware settings after deployment.
app.py CHANGED
@@ -1,12 +1,10 @@
1
- from gc import collect
2
  from pathlib import Path
3
 
4
  import gradio as gr
5
  import pandas as pd
6
  import spaces
7
- import torch
8
  from huggingface_hub import hf_hub_download
9
- from transformers import AutoModelForCausalLM, AutoTokenizer
10
 
11
 
12
  MODEL_REPO = "mradermacher/Spreadsheet-RL-4B-GGUF"
@@ -26,7 +24,6 @@ QUANT_FILES = {
26
  }
27
 
28
  model = None
29
- tokenizer = None
30
  active_quant = None
31
 
32
 
@@ -64,25 +61,17 @@ def generate(
64
  attachment: str | None,
65
  quantization: str,
66
  ) -> str:
67
- global active_quant, model, tokenizer
68
 
69
  quant_file = QUANT_FILES[quantization]
70
  if active_quant != quantization:
71
  model = None
72
- tokenizer = None
73
  active_quant = None
74
- collect()
75
- torch.cuda.empty_cache()
76
-
77
- tokenizer = AutoTokenizer.from_pretrained(
78
- MODEL_REPO,
79
- gguf_file=quant_file,
80
- )
81
- model = AutoModelForCausalLM.from_pretrained(
82
- MODEL_REPO,
83
- gguf_file=quant_file,
84
- dtype=torch.bfloat16,
85
- device_map="cuda",
86
  )
87
  active_quant = quantization
88
 
@@ -102,55 +91,14 @@ def generate(
102
  },
103
  {"role": "user", "content": user_content},
104
  ]
105
- inputs = tokenizer.apply_chat_template(
106
- messages,
107
- add_generation_prompt=True,
108
- tokenize=True,
109
- return_dict=True,
110
- return_tensors="pt",
111
- ).to(model.device)
112
-
113
- think_end_token = tokenizer.convert_tokens_to_ids("</think>")
114
- with torch.inference_mode():
115
- reasoning_ids = model.generate(
116
- **inputs,
117
- max_new_tokens=2048,
118
- do_sample=True,
119
- temperature=0.6,
120
- top_p=0.95,
121
- top_k=20,
122
- eos_token_id=think_end_token,
123
- )
124
- if reasoning_ids[0, -1].item() != think_end_token:
125
- reasoning_ids = torch.cat(
126
- [
127
- reasoning_ids,
128
- torch.tensor([[think_end_token]], device=model.device),
129
- ],
130
- dim=-1,
131
- )
132
- reasoning_ids = torch.cat(
133
- [
134
- reasoning_ids,
135
- tokenizer.encode(
136
- "\n\n",
137
- add_special_tokens=False,
138
- return_tensors="pt",
139
- ).to(model.device),
140
- ],
141
- dim=-1,
142
- )
143
-
144
- answer_ids = model.generate(
145
- input_ids=reasoning_ids,
146
- attention_mask=torch.ones_like(reasoning_ids),
147
- max_new_tokens=512,
148
- )
149
-
150
- return tokenizer.decode(
151
- answer_ids[0, reasoning_ids.shape[-1] :],
152
- skip_special_tokens=True,
153
- ).strip()
154
 
155
 
156
  CSS = """
 
 
1
  from pathlib import Path
2
 
3
  import gradio as gr
4
  import pandas as pd
5
  import spaces
 
6
  from huggingface_hub import hf_hub_download
7
+ from llama_cpp import Llama
8
 
9
 
10
  MODEL_REPO = "mradermacher/Spreadsheet-RL-4B-GGUF"
 
24
  }
25
 
26
  model = None
 
27
  active_quant = None
28
 
29
 
 
61
  attachment: str | None,
62
  quantization: str,
63
  ) -> str:
64
+ global active_quant, model
65
 
66
  quant_file = QUANT_FILES[quantization]
67
  if active_quant != quantization:
68
  model = None
 
69
  active_quant = None
70
+ model = Llama(
71
+ model_path=hf_hub_download(repo_id=MODEL_REPO, filename=quant_file),
72
+ n_ctx=4096,
73
+ n_gpu_layers=-1,
74
+ verbose=True,
 
 
 
 
 
 
 
75
  )
76
  active_quant = quantization
77
 
 
91
  },
92
  {"role": "user", "content": user_content},
93
  ]
94
+ completion = model.create_chat_completion(
95
+ messages=messages,
96
+ max_tokens=512,
97
+ temperature=0.6,
98
+ top_p=0.95,
99
+ top_k=20,
100
+ )
101
+ return completion["choices"][0]["message"]["content"].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
 
104
  CSS = """
pyproject.toml CHANGED
@@ -5,21 +5,10 @@ description = "Add your description here"
5
  readme = "README.md"
6
  requires-python = ">=3.12.10"
7
  dependencies = [
8
- "accelerate>=1.14.0",
9
- "gguf>=0.19.0",
10
  "huggingface-hub>=0.34.0,<1.0",
 
11
  "openpyxl>=3.1.5",
12
  "pandas>=3.0.5",
13
  "spaces>=0.51.1",
14
- "torch==2.11.0",
15
- "transformers==4.57.6",
16
  "xlrd>=2.0.2",
17
  ]
18
-
19
- [tool.uv.sources]
20
- torch = { index = "pytorch-cu130" }
21
-
22
- [[tool.uv.index]]
23
- name = "pytorch-cu130"
24
- url = "https://download.pytorch.org/whl/cu130"
25
- explicit = true
 
5
  readme = "README.md"
6
  requires-python = ">=3.12.10"
7
  dependencies = [
 
 
8
  "huggingface-hub>=0.34.0,<1.0",
9
+ "llama-cpp-python==0.3.34",
10
  "openpyxl>=3.1.5",
11
  "pandas>=3.0.5",
12
  "spaces>=0.51.1",
 
 
13
  "xlrd>=2.0.2",
14
  ]
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,9 +1,7 @@
1
- accelerate
2
- gguf
3
  huggingface-hub>=0.34.0,<1.0
 
 
4
  openpyxl
5
  pandas
6
  spaces
7
- torch==2.11.0
8
- transformers==4.57.6
9
  xlrd
 
 
 
1
  huggingface-hub>=0.34.0,<1.0
2
+ --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu130
3
+ llama-cpp-python==0.3.34
4
  openpyxl
5
  pandas
6
  spaces
 
 
7
  xlrd