Spaces:
Running
on
A10G
Running
on
A10G
Oleg Shulyakov
commited on
Commit
·
a3b65e2
1
Parent(s):
b7bd975
Re-write app to OOP with additional options
Browse files- app.py +845 -415
- groups_merged.txt → calibration_data_v5_rc.txt +0 -0
app.py
CHANGED
|
@@ -1,443 +1,873 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import signal
|
| 4 |
-
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 5 |
-
import gradio as gr
|
| 6 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
from huggingface_hub import HfApi, ModelCard, whoami
|
| 9 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
from textwrap import dedent
|
| 12 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 13 |
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
-
process.wait(timeout=
|
| 51 |
except subprocess.TimeoutExpired:
|
| 52 |
-
print("
|
| 53 |
-
process.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
result = subprocess.run(split_cmd, shell=False, capture_output=True, text=True)
|
| 83 |
-
print(f"Split command stdout: {result.stdout}")
|
| 84 |
-
print(f"Split command stderr: {result.stderr}")
|
| 85 |
-
|
| 86 |
-
if result.returncode != 0:
|
| 87 |
-
stderr_str = result.stderr.decode("utf-8")
|
| 88 |
-
raise Exception(f"Error splitting the model: {stderr_str}")
|
| 89 |
-
print("Model split successfully!")
|
| 90 |
-
|
| 91 |
-
# remove the original model file if needed
|
| 92 |
-
if os.path.exists(model_path):
|
| 93 |
-
os.remove(model_path)
|
| 94 |
-
|
| 95 |
-
model_file_prefix = model_path_prefix.split('/')[-1]
|
| 96 |
-
print(f"Model file name prefix: {model_file_prefix}")
|
| 97 |
-
sharded_model_files = [f for f in os.listdir(outdir) if f.startswith(model_file_prefix) and f.endswith(".gguf")]
|
| 98 |
-
if sharded_model_files:
|
| 99 |
print(f"Sharded model files: {sharded_model_files}")
|
| 100 |
-
|
| 101 |
for file in sharded_model_files:
|
| 102 |
-
file_path = os.path.join(outdir, file)
|
| 103 |
-
print(f"Uploading file: {file_path}")
|
| 104 |
try:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
path_in_repo=file,
|
| 108 |
-
repo_id=repo_id,
|
| 109 |
-
)
|
| 110 |
except Exception as e:
|
| 111 |
-
raise
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
for file in api.list_repo_tree(
|
| 139 |
-
repo_id=model_id,
|
| 140 |
-
recursive=True,
|
| 141 |
)
|
|
|
|
| 142 |
)
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
)
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
os.makedirs("outputs")
|
| 153 |
-
|
| 154 |
-
with tempfile.TemporaryDirectory(dir="outputs") as outdir:
|
| 155 |
-
fp16 = str(Path(outdir)/f"{model_name}.fp16.gguf")
|
| 156 |
-
|
| 157 |
-
with tempfile.TemporaryDirectory(dir="downloads") as tmpdir:
|
| 158 |
-
# Keep the model name as the dirname so the model name metadata is populated correctly
|
| 159 |
-
local_dir = Path(tmpdir)/model_name
|
| 160 |
-
print(local_dir)
|
| 161 |
-
api.snapshot_download(repo_id=model_id, local_dir=local_dir, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
|
| 162 |
-
print("Model downloaded successfully!")
|
| 163 |
-
print(f"Current working directory: {os.getcwd()}")
|
| 164 |
-
print(f"Model directory contents: {os.listdir(local_dir)}")
|
| 165 |
-
|
| 166 |
-
config_dir = local_dir/"config.json"
|
| 167 |
-
adapter_config_dir = local_dir/"adapter_config.json"
|
| 168 |
-
if os.path.exists(adapter_config_dir) and not os.path.exists(config_dir):
|
| 169 |
-
raise Exception('adapter_config.json is present.<br/><br/>If you are converting a LoRA adapter to GGUF, please use <a href="https://huggingface.co/spaces/ggml-org/gguf-my-lora" target="_blank" style="text-decoration:underline">GGUF-my-lora</a>.')
|
| 170 |
-
|
| 171 |
-
result = subprocess.run([
|
| 172 |
-
"python", CONVERSION_SCRIPT, local_dir, "--outtype", "f16", "--outfile", fp16
|
| 173 |
-
], shell=False, capture_output=True)
|
| 174 |
-
print(result)
|
| 175 |
-
if result.returncode != 0:
|
| 176 |
-
stderr_str = result.stderr.decode("utf-8")
|
| 177 |
-
raise Exception(f"Error converting to fp16: {stderr_str}")
|
| 178 |
-
print("Model converted to fp16 successfully!")
|
| 179 |
-
print(f"Converted model path: {fp16}")
|
| 180 |
-
|
| 181 |
-
imatrix_path = Path(outdir)/"imatrix.dat"
|
| 182 |
-
|
| 183 |
-
if use_imatrix:
|
| 184 |
-
if train_data_file:
|
| 185 |
-
train_data_path = train_data_file.name
|
| 186 |
-
else:
|
| 187 |
-
train_data_path = "llama.cpp/groups_merged.txt" #fallback calibration dataset
|
| 188 |
-
|
| 189 |
-
print(f"Training data file path: {train_data_path}")
|
| 190 |
-
|
| 191 |
-
if not os.path.isfile(train_data_path):
|
| 192 |
-
raise Exception(f"Training data file not found: {train_data_path}")
|
| 193 |
-
|
| 194 |
-
generate_importance_matrix(fp16, train_data_path, imatrix_path)
|
| 195 |
-
else:
|
| 196 |
-
print("Not using imatrix quantization.")
|
| 197 |
-
|
| 198 |
-
# Quantize the model
|
| 199 |
-
quantized_gguf_name = f"{model_name.lower()}-{imatrix_q_method.lower()}-imat.gguf" if use_imatrix else f"{model_name.lower()}-{q_method.lower()}.gguf"
|
| 200 |
-
quantized_gguf_path = str(Path(outdir)/quantized_gguf_name)
|
| 201 |
-
if use_imatrix:
|
| 202 |
-
quantise_ggml = [
|
| 203 |
-
"./llama.cpp/llama-quantize",
|
| 204 |
-
"--imatrix", imatrix_path, fp16, quantized_gguf_path, imatrix_q_method
|
| 205 |
-
]
|
| 206 |
-
else:
|
| 207 |
-
quantise_ggml = [
|
| 208 |
-
"./llama.cpp/llama-quantize",
|
| 209 |
-
fp16, quantized_gguf_path, q_method
|
| 210 |
-
]
|
| 211 |
-
result = subprocess.run(quantise_ggml, shell=False, capture_output=True)
|
| 212 |
-
if result.returncode != 0:
|
| 213 |
-
stderr_str = result.stderr.decode("utf-8")
|
| 214 |
-
raise Exception(f"Error quantizing: {stderr_str}")
|
| 215 |
-
print(f"Quantized successfully with {imatrix_q_method if use_imatrix else q_method} option!")
|
| 216 |
-
print(f"Quantized model path: {quantized_gguf_path}")
|
| 217 |
-
|
| 218 |
-
# Create empty repo
|
| 219 |
-
username = whoami(oauth_token.token)["name"]
|
| 220 |
-
new_repo_url = api.create_repo(repo_id=f"{username}/{model_name}-{imatrix_q_method if use_imatrix else q_method}-GGUF", exist_ok=True, private=private_repo)
|
| 221 |
-
new_repo_id = new_repo_url.repo_id
|
| 222 |
-
print("Repo created successfully!", new_repo_url)
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
try:
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
)
|
| 280 |
-
readme_path = Path(outdir)/"README.md"
|
| 281 |
-
card.save(readme_path)
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
)
|
| 312 |
-
print(f"Uploaded successfully with {imatrix_q_method if use_imatrix else q_method} option!")
|
| 313 |
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
)
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
)
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
value=256,
|
| 377 |
-
label="Max Tensors per File",
|
| 378 |
-
info="Maximum number of tensors per file when splitting model.",
|
| 379 |
-
visible=False
|
| 380 |
-
)
|
| 381 |
-
|
| 382 |
-
split_max_size = gr.Textbox(
|
| 383 |
-
label="Max File Size",
|
| 384 |
-
info="Maximum file size when splitting model (--split-max-size). May leave empty to use the default. Accepted suffixes: M, G. Example: 256M, 5G",
|
| 385 |
-
visible=False
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
iface = gr.Interface(
|
| 389 |
-
fn=process_model,
|
| 390 |
-
inputs=[
|
| 391 |
-
model_id,
|
| 392 |
-
q_method,
|
| 393 |
-
use_imatrix,
|
| 394 |
-
imatrix_q_method,
|
| 395 |
-
private_repo,
|
| 396 |
-
train_data_file,
|
| 397 |
-
split_model,
|
| 398 |
-
split_max_tensors,
|
| 399 |
-
split_max_size,
|
| 400 |
-
],
|
| 401 |
-
outputs=[
|
| 402 |
-
gr.Markdown(label="output"),
|
| 403 |
-
gr.Image(show_label=False),
|
| 404 |
-
],
|
| 405 |
-
title="Create your own GGUF Quants, blazingly fast ⚡!",
|
| 406 |
-
description="The space takes an HF repo as an input, quantizes it and creates a Public repo containing the selected quant under your HF user namespace.",
|
| 407 |
-
api_name=False
|
| 408 |
-
)
|
| 409 |
-
|
| 410 |
-
# Create Gradio interface
|
| 411 |
-
with gr.Blocks(css=css) as demo:
|
| 412 |
-
gr.Markdown("You must be logged in to use GGUF-my-repo.")
|
| 413 |
-
gr.LoginButton(min_width=250)
|
| 414 |
-
|
| 415 |
-
iface.render()
|
| 416 |
-
|
| 417 |
-
def update_split_visibility(split_model):
|
| 418 |
-
return gr.update(visible=split_model), gr.update(visible=split_model)
|
| 419 |
-
|
| 420 |
-
split_model.change(
|
| 421 |
-
fn=update_split_visibility,
|
| 422 |
-
inputs=split_model,
|
| 423 |
-
outputs=[split_max_tensors, split_max_size]
|
| 424 |
-
)
|
| 425 |
-
|
| 426 |
-
def update_visibility(use_imatrix):
|
| 427 |
-
return gr.update(visible=not use_imatrix), gr.update(visible=use_imatrix), gr.update(visible=use_imatrix)
|
| 428 |
-
|
| 429 |
-
use_imatrix.change(
|
| 430 |
-
fn=update_visibility,
|
| 431 |
-
inputs=use_imatrix,
|
| 432 |
-
outputs=[q_method, imatrix_q_method, train_data_file]
|
| 433 |
-
)
|
| 434 |
-
|
| 435 |
-
def restart_space():
|
| 436 |
-
HfApi().restart_space(repo_id="ggml-org/gguf-my-repo", token=HF_TOKEN, factory_reboot=True)
|
| 437 |
-
|
| 438 |
-
scheduler = BackgroundScheduler()
|
| 439 |
-
scheduler.add_job(restart_space, "interval", seconds=21600)
|
| 440 |
-
scheduler.start()
|
| 441 |
-
|
| 442 |
-
# Launch the interface
|
| 443 |
-
demo.queue(default_concurrency_limit=1, max_size=5).launch(debug=True, show_api=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import signal
|
|
|
|
|
|
|
| 4 |
import tempfile
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from textwrap import dedent
|
| 7 |
+
from typing import Optional, Tuple, List, Union
|
| 8 |
+
from dataclasses import dataclass, field
|
| 9 |
|
| 10 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
from huggingface_hub import HfApi, ModelCard, whoami
|
| 14 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
|
|
|
|
|
|
| 15 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 16 |
|
| 17 |
|
| 18 |
+
@dataclass
|
| 19 |
+
class QuantizationConfig:
|
| 20 |
+
"""Configuration for model quantization."""
|
| 21 |
+
method: str
|
| 22 |
+
use_imatrix: bool = False
|
| 23 |
+
imatrix_method: str = "IQ4_NL"
|
| 24 |
+
quant_embedding: bool = False
|
| 25 |
+
embedding_tensor_method: str = "Q8_0"
|
| 26 |
+
leave_output: bool = False
|
| 27 |
+
quant_output: bool = False
|
| 28 |
+
output_tensor_method: str = "Q8_0"
|
| 29 |
+
# Generated values - These will be set during processing
|
| 30 |
+
fp16_model: str = field(default="", init=False)
|
| 31 |
+
quantized_gguf: str = field(default="", init=False)
|
| 32 |
+
imatrix_file: str = field(default="", init=False)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@dataclass
|
| 36 |
+
class SplitConfig:
|
| 37 |
+
"""Configuration for model splitting."""
|
| 38 |
+
enabled: bool = False
|
| 39 |
+
max_tensors: int = 256
|
| 40 |
+
max_size: Optional[str] = None
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@dataclass
|
| 44 |
+
class OutputConfig:
|
| 45 |
+
"""Configuration for output settings."""
|
| 46 |
+
private_repo: bool = False
|
| 47 |
+
repo_name: str = ""
|
| 48 |
+
filename: str = ""
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@dataclass
|
| 52 |
+
class ModelProcessingConfig:
|
| 53 |
+
"""Configuration for the entire model processing pipeline."""
|
| 54 |
+
token: str
|
| 55 |
+
model_id: str
|
| 56 |
+
model_name: str
|
| 57 |
+
outdir: str
|
| 58 |
+
quant_config: QuantizationConfig
|
| 59 |
+
split_config: SplitConfig
|
| 60 |
+
output_config: OutputConfig
|
| 61 |
+
# Generated values - These will be set during processing
|
| 62 |
+
new_repo_url: str = field(default="", init=False)
|
| 63 |
+
new_repo_id: str = field(default="", init=False)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class GGUFConverterError(Exception):
|
| 67 |
+
"""Custom exception for GGUF conversion errors."""
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class HuggingFaceModelProcessor:
|
| 72 |
+
"""Handles the processing of Hugging Face models to GGUF format."""
|
| 73 |
+
|
| 74 |
+
ERROR_LOGIN = "You must be logged in to use GGUF-my-repo."
|
| 75 |
+
DOWNLOAD_FOLDER = "./downloads"
|
| 76 |
+
OUTPUT_FOLDER = "./outputs"
|
| 77 |
+
CALIBRATION_FILE = "calibration_data_v5_rc.txt"
|
| 78 |
+
|
| 79 |
+
QUANTIZE_TIMEOUT=86400
|
| 80 |
+
HF_TO_GGUF_TIMEOUT=3600
|
| 81 |
+
IMATRIX_TIMEOUT=86400
|
| 82 |
+
SPLIT_TIMEOUT=3600
|
| 83 |
+
KILL_TIMEOUT=5
|
| 84 |
+
|
| 85 |
+
def __init__(self):
|
| 86 |
+
self.SPACE_ID = os.environ.get("SPACE_ID", "")
|
| 87 |
+
self.SPACE_URL = f"https://{self.SPACE_ID.replace('/', '-')}.hf.space/" if self.SPACE_ID else "http://localhost:7860/"
|
| 88 |
+
self.HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 89 |
+
self.RUN_LOCALLY = os.environ.get("RUN_LOCALLY")
|
| 90 |
+
|
| 91 |
+
# Create necessary folders
|
| 92 |
+
self._create_folder(self.DOWNLOAD_FOLDER)
|
| 93 |
+
self._create_folder(self.OUTPUT_FOLDER)
|
| 94 |
+
|
| 95 |
+
def _create_folder(self, folder_name: str) -> str:
|
| 96 |
+
"""Create a folder if it doesn't exist."""
|
| 97 |
+
if not os.path.exists(folder_name):
|
| 98 |
+
print(f"Creating folder: {folder_name}")
|
| 99 |
+
os.makedirs(folder_name)
|
| 100 |
+
return folder_name
|
| 101 |
+
|
| 102 |
+
def _validate_token(self, oauth_token: Optional[gr.OAuthToken]) -> str:
|
| 103 |
+
"""Validate the OAuth token and return the token string."""
|
| 104 |
+
if oauth_token is None or oauth_token.token is None:
|
| 105 |
+
raise GGUFConverterError(self.ERROR_LOGIN)
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
whoami(oauth_token.token)
|
| 109 |
+
return oauth_token.token
|
| 110 |
+
except Exception as e:
|
| 111 |
+
raise GGUFConverterError(self.ERROR_LOGIN)
|
| 112 |
+
|
| 113 |
+
def _escape_html(self, s: str) -> str:
|
| 114 |
+
"""Escape HTML characters for safe display."""
|
| 115 |
+
replacements = [
|
| 116 |
+
("&", "&"),
|
| 117 |
+
("<", "<"),
|
| 118 |
+
(">", ">"),
|
| 119 |
+
('"', """),
|
| 120 |
+
("\n", "<br/>")
|
| 121 |
+
]
|
| 122 |
+
for old, new in replacements:
|
| 123 |
+
s = s.replace(old, new)
|
| 124 |
+
return s
|
| 125 |
+
|
| 126 |
+
def _get_model_creator(self, model_id: str) -> str:
|
| 127 |
+
"""Extract model creator from model ID."""
|
| 128 |
+
return model_id.split('/')[0]
|
| 129 |
+
|
| 130 |
+
def _get_model_name(self, model_id: str) -> str:
|
| 131 |
+
"""Extract model name from model ID."""
|
| 132 |
+
return model_id.split('/')[-1]
|
| 133 |
+
|
| 134 |
+
def _upload_file(self, processing_config: ModelProcessingConfig, path_or_fileobj: str, path_in_repo: str) -> None:
|
| 135 |
+
"""Upload a file to Hugging Face repository."""
|
| 136 |
+
if self.RUN_LOCALLY == "1":
|
| 137 |
+
print("Skipping upload...")
|
| 138 |
+
return
|
| 139 |
+
|
| 140 |
+
api = HfApi(token=processing_config.token)
|
| 141 |
+
api.upload_file(
|
| 142 |
+
path_or_fileobj=path_or_fileobj,
|
| 143 |
+
path_in_repo=path_in_repo,
|
| 144 |
+
repo_id=processing_config.new_repo_id,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
def _generate_importance_matrix(self, quant_config: QuantizationConfig) -> None:
|
| 148 |
+
"""Generate importance matrix for quantization."""
|
| 149 |
+
if not os.path.isfile(quant_config.fp16_model):
|
| 150 |
+
raise GGUFConverterError(f"Model file not found: {quant_config.fp16_model}")
|
| 151 |
+
|
| 152 |
+
train_data_path = self.CALIBRATION_FILE
|
| 153 |
+
if not os.path.isfile(train_data_path):
|
| 154 |
+
raise GGUFConverterError(f"Training data file not found: {train_data_path}")
|
| 155 |
+
|
| 156 |
+
print(f"Training data file path: {train_data_path}")
|
| 157 |
+
print("Running imatrix command...")
|
| 158 |
+
|
| 159 |
+
imatrix_command = [
|
| 160 |
+
"llama-imatrix",
|
| 161 |
+
"-m", quant_config.fp16_model,
|
| 162 |
+
"-f", train_data_path,
|
| 163 |
+
"-ngl", "99",
|
| 164 |
+
"--output-frequency", "10",
|
| 165 |
+
"-o", quant_config.imatrix_file,
|
| 166 |
+
]
|
| 167 |
+
|
| 168 |
+
process = subprocess.Popen(imatrix_command, shell=False, stderr=subprocess.STDOUT)
|
| 169 |
+
try:
|
| 170 |
+
process.wait(timeout=self.IMATRIX_TIMEOUT)
|
| 171 |
+
except subprocess.TimeoutExpired:
|
| 172 |
+
print("Imatrix computation timed out. Sending SIGINT to allow graceful termination...")
|
| 173 |
+
process.send_signal(signal.SIGINT)
|
| 174 |
+
try:
|
| 175 |
+
process.wait(timeout=self.KILL_TIMEOUT)
|
| 176 |
+
except subprocess.TimeoutExpired:
|
| 177 |
+
print("Imatrix proc still didn't term. Forcefully terminating process...")
|
| 178 |
+
process.kill()
|
| 179 |
+
raise GGUFConverterError("Error generating imatrix: Operation timed out.")
|
| 180 |
+
|
| 181 |
+
if process.returncode != 0:
|
| 182 |
+
raise GGUFConverterError(f"Error generating imatrix: code={process.returncode}.")
|
| 183 |
+
|
| 184 |
+
print(f"Importance matrix generation completed: {os.path.abspath(quant_config.imatrix_file)}")
|
| 185 |
+
|
| 186 |
+
def _split_and_upload_model(self, processing_config: ModelProcessingConfig) -> None:
|
| 187 |
+
"""Split large model files and upload shards."""
|
| 188 |
+
quant_config = processing_config.quant_config
|
| 189 |
+
split_config = processing_config.split_config
|
| 190 |
+
|
| 191 |
+
print(f"Model path: {quant_config.quantized_gguf}")
|
| 192 |
+
print(f"Output dir: {processing_config.outdir}")
|
| 193 |
+
|
| 194 |
+
split_cmd = ["llama-gguf-split", "--split"]
|
| 195 |
+
|
| 196 |
+
if split_config.max_size:
|
| 197 |
+
split_cmd.extend(["--split-max-size", split_config.max_size])
|
| 198 |
+
else:
|
| 199 |
+
split_cmd.extend(["--split-max-tensors", str(split_config.max_tensors)])
|
| 200 |
+
|
| 201 |
+
model_path_prefix = '.'.join(quant_config.quantized_gguf.split('.')[:-1])
|
| 202 |
+
split_cmd.extend([quant_config.quantized_gguf, model_path_prefix])
|
| 203 |
+
|
| 204 |
+
print(f"Split command: {split_cmd}")
|
| 205 |
+
process = subprocess.Popen(split_cmd, shell=False, stderr=subprocess.STDOUT)
|
| 206 |
try:
|
| 207 |
+
process.wait(timeout=self.SPLIT_TIMEOUT)
|
| 208 |
except subprocess.TimeoutExpired:
|
| 209 |
+
print("Splitting timed out. Sending SIGINT to allow graceful termination...")
|
| 210 |
+
process.send_signal(signal.SIGINT)
|
| 211 |
+
try:
|
| 212 |
+
process.wait(timeout=self.KILL_TIMEOUT)
|
| 213 |
+
except subprocess.TimeoutExpired:
|
| 214 |
+
print("Splitting timed out. Killing process...")
|
| 215 |
+
process.kill()
|
| 216 |
+
raise GGUFConverterError("Error splitting the model: Operation timed out.")
|
| 217 |
+
|
| 218 |
+
if process.returncode != 0:
|
| 219 |
+
raise GGUFConverterError(f"Error splitting the model: code={process.returncode}")
|
| 220 |
+
|
| 221 |
+
print("Model split successfully!")
|
| 222 |
+
|
| 223 |
+
# Remove original model file
|
| 224 |
+
if os.path.exists(quant_config.quantized_gguf):
|
| 225 |
+
os.remove(quant_config.quantized_gguf)
|
| 226 |
+
|
| 227 |
+
model_file_prefix = model_path_prefix.split('/')[-1]
|
| 228 |
+
print(f"Model file name prefix: {model_file_prefix}")
|
| 229 |
+
|
| 230 |
+
sharded_model_files = [
|
| 231 |
+
f for f in os.listdir(processing_config.outdir)
|
| 232 |
+
if f.startswith(model_file_prefix) and f.endswith(".gguf")
|
| 233 |
+
]
|
| 234 |
+
|
| 235 |
+
if not sharded_model_files:
|
| 236 |
+
raise GGUFConverterError("No sharded files found.")
|
| 237 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
print(f"Sharded model files: {sharded_model_files}")
|
| 239 |
+
|
| 240 |
for file in sharded_model_files:
|
| 241 |
+
file_path = os.path.join(processing_config.outdir, file)
|
|
|
|
| 242 |
try:
|
| 243 |
+
print(f"Uploading file: {file_path}")
|
| 244 |
+
self._upload_file(processing_config, file_path, file)
|
|
|
|
|
|
|
|
|
|
| 245 |
except Exception as e:
|
| 246 |
+
raise GGUFConverterError(f"Error uploading file {file_path}: {e}")
|
| 247 |
+
|
| 248 |
+
print("Sharded model has been uploaded successfully!")
|
| 249 |
+
|
| 250 |
+
def _download_base_model(self, processing_config: ModelProcessingConfig) -> str:
|
| 251 |
+
"""Download and convert Hugging Face model to GGUF FP16 format."""
|
| 252 |
+
print(f"Downloading model {processing_config.model_name}")
|
| 253 |
+
|
| 254 |
+
if os.path.exists(processing_config.quant_config.fp16_model):
|
| 255 |
+
print("Skipping fp16 conversion...")
|
| 256 |
+
print(f"Converted model path: {os.path.abspath(processing_config.quant_config.fp16_model)}")
|
| 257 |
+
return processing_config.quant_config.fp16_model
|
| 258 |
+
|
| 259 |
+
with tempfile.TemporaryDirectory(dir=self.DOWNLOAD_FOLDER) as tmpdir:
|
| 260 |
+
local_dir = f"{Path(tmpdir)}/{processing_config.model_name}"
|
| 261 |
+
print(f"Local directory: {os.path.abspath(local_dir)}")
|
| 262 |
+
|
| 263 |
+
# Download model
|
| 264 |
+
api = HfApi(token=processing_config.token)
|
| 265 |
+
pattern = (
|
| 266 |
+
"*.safetensors"
|
| 267 |
+
if any(
|
| 268 |
+
file.path.endswith(".safetensors")
|
| 269 |
+
for file in api.list_repo_tree(
|
| 270 |
+
repo_id=processing_config.model_id,
|
| 271 |
+
recursive=True,
|
| 272 |
+
)
|
|
|
|
|
|
|
|
|
|
| 273 |
)
|
| 274 |
+
else "*.bin"
|
| 275 |
)
|
| 276 |
+
dl_pattern = ["*.md", "*.json", "*.model"]
|
| 277 |
+
dl_pattern += [pattern]
|
| 278 |
+
api.snapshot_download(repo_id=processing_config.model_id, local_dir=local_dir, allow_patterns=dl_pattern)
|
| 279 |
+
print("Model downloaded successfully!")
|
| 280 |
+
print(f"Model directory contents: {os.listdir(local_dir)}")
|
| 281 |
+
|
| 282 |
+
config_dir = os.path.join(local_dir, "config.json")
|
| 283 |
+
adapter_config_dir = os.path.join(local_dir, "adapter_config.json")
|
| 284 |
+
if os.path.exists(adapter_config_dir) and not os.path.exists(config_dir):
|
| 285 |
+
raise GGUFConverterError(
|
| 286 |
+
'adapter_config.json is present.<br/><br/>If you are converting a LoRA adapter to GGUF, '
|
| 287 |
+
'please use <a href="https://huggingface.co/spaces/ggml-org/gguf-my-lora" target="_blank" '
|
| 288 |
+
'style="text-decoration:underline">GGUF-my-lora</a>.'
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
# Convert HF to GGUF
|
| 292 |
+
print(f"Converting to GGUF FP16: {os.path.abspath(processing_config.quant_config.fp16_model)}")
|
| 293 |
+
convert_command = [
|
| 294 |
+
"python3", "/app/convert_hf_to_gguf.py", local_dir,
|
| 295 |
+
"--outtype", "f16", "--outfile", processing_config.quant_config.fp16_model
|
| 296 |
+
]
|
| 297 |
+
process = subprocess.Popen(convert_command, shell=False, stderr=subprocess.STDOUT)
|
| 298 |
+
try:
|
| 299 |
+
process.wait(timeout=self.HF_TO_GGUF_TIMEOUT)
|
| 300 |
+
except subprocess.TimeoutExpired:
|
| 301 |
+
print("Conversion timed out. Sending SIGINT to allow graceful termination...")
|
| 302 |
+
process.send_signal(signal.SIGINT)
|
| 303 |
+
try:
|
| 304 |
+
process.wait(timeout=self.KILL_TIMEOUT)
|
| 305 |
+
except subprocess.TimeoutExpired:
|
| 306 |
+
print("Conversion timed out. Killing process...")
|
| 307 |
+
process.kill()
|
| 308 |
+
raise GGUFConverterError("Error converting to fp16: Operation timed out.")
|
| 309 |
+
|
| 310 |
+
if process.returncode != 0:
|
| 311 |
+
raise GGUFConverterError(f"Error converting to fp16: code={process.returncode}")
|
| 312 |
+
|
| 313 |
+
print("Model converted to fp16 successfully!")
|
| 314 |
+
print(f"Converted model path: {os.path.abspath(processing_config.quant_config.fp16_model)}")
|
| 315 |
+
return processing_config.quant_config.fp16_model
|
| 316 |
+
|
| 317 |
+
def _quantize_model(self, quant_config: QuantizationConfig) -> str:
|
| 318 |
+
"""Quantize the GGUF model."""
|
| 319 |
+
quantize_cmd = ["llama-quantize"]
|
| 320 |
+
|
| 321 |
+
if quant_config.quant_embedding:
|
| 322 |
+
quantize_cmd.extend(["--token-embedding-type", quant_config.embedding_tensor_method])
|
| 323 |
+
|
| 324 |
+
if quant_config.leave_output:
|
| 325 |
+
quantize_cmd.append("--leave-output-tensor")
|
| 326 |
+
else:
|
| 327 |
+
if quant_config.quant_output:
|
| 328 |
+
quantize_cmd.extend(["--output-tensor-type", quant_config.output_tensor_method])
|
| 329 |
+
|
| 330 |
+
# Set imatrix file path if needed
|
| 331 |
+
if quant_config.use_imatrix:
|
| 332 |
+
self._generate_importance_matrix(quant_config)
|
| 333 |
+
quantize_cmd.extend(["--imatrix", quant_config.imatrix_file])
|
| 334 |
+
else:
|
| 335 |
+
print("Not using imatrix quantization.")
|
| 336 |
+
|
| 337 |
+
quantize_cmd.append(quant_config.fp16_model)
|
| 338 |
+
quantize_cmd.append(quant_config.quantized_gguf)
|
| 339 |
+
|
| 340 |
+
if quant_config.use_imatrix:
|
| 341 |
+
quantize_cmd.append(quant_config.imatrix_method)
|
| 342 |
+
else:
|
| 343 |
+
quantize_cmd.append(quant_config.method)
|
| 344 |
+
|
| 345 |
+
print(f"Quantizing model with {quantize_cmd}")
|
| 346 |
+
|
| 347 |
+
# Use Popen for quantization
|
| 348 |
+
process = subprocess.Popen(quantize_cmd, shell=False, stderr=subprocess.STDOUT)
|
| 349 |
+
try:
|
| 350 |
+
process.wait(timeout=self.QUANTIZE_TIMEOUT)
|
| 351 |
+
except subprocess.TimeoutExpired:
|
| 352 |
+
print("Quantization timed out. Sending SIGINT to allow graceful termination...")
|
| 353 |
+
process.send_signal(signal.SIGINT)
|
| 354 |
+
try:
|
| 355 |
+
process.wait(timeout=self.KILL_TIMEOUT)
|
| 356 |
+
except subprocess.TimeoutExpired:
|
| 357 |
+
print("Quantization timed out. Killing process...")
|
| 358 |
+
process.kill()
|
| 359 |
+
raise GGUFConverterError("Error quantizing: Operation timed out.")
|
| 360 |
+
|
| 361 |
+
if process.returncode != 0:
|
| 362 |
+
raise GGUFConverterError(f"Error quantizing: code={process.returncode}")
|
| 363 |
+
|
| 364 |
+
print(f"Quantized successfully with {quant_config.imatrix_method if quant_config.use_imatrix else quant_config.method} option!")
|
| 365 |
+
print(f"Quantized model path: {os.path.abspath(quant_config.quantized_gguf)}")
|
| 366 |
+
return quant_config.quantized_gguf
|
| 367 |
+
|
| 368 |
+
def _create_empty_repo(self, processing_config: ModelProcessingConfig):
|
| 369 |
+
api = HfApi(token=processing_config.token)
|
| 370 |
+
new_repo_url = api.create_repo(
|
| 371 |
+
repo_id=processing_config.output_config.repo_name,
|
| 372 |
+
exist_ok=True,
|
| 373 |
+
private=processing_config.output_config.private_repo
|
| 374 |
)
|
| 375 |
+
processing_config.new_repo_url = new_repo_url.url
|
| 376 |
+
processing_config.new_repo_id = new_repo_url.repo_id
|
| 377 |
+
print("Repo created successfully!", processing_config.new_repo_url)
|
| 378 |
|
| 379 |
+
return new_repo_url
|
| 380 |
+
|
| 381 |
+
def _generate_readme(self, processing_config: ModelProcessingConfig) -> str:
|
| 382 |
+
"""Generate README.md for the quantized model."""
|
| 383 |
+
creator = self._get_model_creator(processing_config.model_id)
|
| 384 |
+
username = whoami(processing_config.token)["name"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
+
try:
|
| 387 |
+
card = ModelCard.load(processing_config.model_id, token=processing_config.token)
|
| 388 |
+
except:
|
| 389 |
+
card = ModelCard("")
|
| 390 |
+
|
| 391 |
+
if card.data.tags is None:
|
| 392 |
+
card.data.tags = []
|
| 393 |
+
card.data.tags.extend(["llama-cpp", "gguf-my-repo"])
|
| 394 |
+
card.data.base_model = processing_config.model_id
|
| 395 |
+
|
| 396 |
+
card.text = dedent(
|
| 397 |
+
f"""
|
| 398 |
+
# {processing_config.model_name}
|
| 399 |
+
**Model creator:** [{creator}](https://huggingface.co/{creator})<br/>
|
| 400 |
+
**Original model**: [{processing_config.model_id}](https://huggingface.co/{processing_config.model_id})<br/>
|
| 401 |
+
**GGUF quantization:** provided by [{username}](https:/huggingface.co/{username}) using `llama.cpp`<br/>
|
| 402 |
+
## Special thanks
|
| 403 |
+
🙏 Special thanks to [Georgi Gerganov](https://github.com/ggerganov) and the whole team working on [llama.cpp](https://github.com/ggerganov/llama.cpp/) for making all of this possible.
|
| 404 |
+
## Use with Ollama
|
| 405 |
+
```bash
|
| 406 |
+
ollama run "hf.co/{processing_config.new_repo_id}:<quantization>"
|
| 407 |
+
```
|
| 408 |
+
## Use with LM Studio
|
| 409 |
+
```bash
|
| 410 |
+
lms load "{processing_config.new_repo_id}"
|
| 411 |
+
```
|
| 412 |
+
## Use with llama.cpp CLI
|
| 413 |
+
```bash
|
| 414 |
+
llama-cli --hf-repo "{processing_config.new_repo_id}" --hf-file "{processing_config.output_config.filename}" -p "The meaning to life and the universe is"
|
| 415 |
+
```
|
| 416 |
+
## Use with llama.cpp Server:
|
| 417 |
+
```bash
|
| 418 |
+
llama-server --hf-repo "{processing_config.new_repo_id}" --hf-file "{processing_config.output_config.filename}" -c 4096
|
| 419 |
+
```
|
| 420 |
+
"""
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
readme_path = f"{processing_config.outdir}/README.md"
|
| 424 |
+
card.save(readme_path)
|
| 425 |
+
return readme_path
|
| 426 |
+
|
| 427 |
+
def process_model(self, processing_config: ModelProcessingConfig) -> Tuple[str, str]:
|
| 428 |
+
"""Main method to process a model through the entire pipeline."""
|
| 429 |
+
quant_config = processing_config.quant_config
|
| 430 |
+
split_config = processing_config.split_config
|
| 431 |
+
output_config = processing_config.output_config
|
| 432 |
+
|
| 433 |
+
print(f"Current working directory: {os.path.abspath(os.getcwd())}")
|
| 434 |
+
|
| 435 |
+
# Download and convert base model
|
| 436 |
+
self._download_base_model(processing_config)
|
| 437 |
+
|
| 438 |
+
# Quantize the model
|
| 439 |
+
self._quantize_model(quant_config)
|
| 440 |
+
|
| 441 |
+
# Create empty repo
|
| 442 |
+
self._create_empty_repo(processing_config)
|
| 443 |
+
|
| 444 |
+
# Upload model
|
| 445 |
+
if split_config.enabled:
|
| 446 |
+
print(f"Splitting quantized model: {os.path.abspath(quant_config.quantized_gguf)}")
|
| 447 |
+
self._split_and_upload_model(processing_config)
|
| 448 |
+
else:
|
| 449 |
+
try:
|
| 450 |
+
print(f"Uploading quantized model: {os.path.abspath(quant_config.quantized_gguf)}")
|
| 451 |
+
self._upload_file(processing_config, quant_config.quantized_gguf, output_config.filename)
|
| 452 |
+
except Exception as e:
|
| 453 |
+
raise GGUFConverterError(f"Error uploading quantized model: {e}")
|
| 454 |
+
|
| 455 |
+
# Upload imatrix if it exists
|
| 456 |
+
if quant_config.use_imatrix and os.path.isfile(quant_config.imatrix_file):
|
| 457 |
try:
|
| 458 |
+
print(f"Uploading imatrix.dat: {os.path.abspath(quant_config.imatrix_file)}")
|
| 459 |
+
self._upload_file(processing_config, quant_config.imatrix_file, f"{processing_config.model_name}-imatrix.gguf")
|
| 460 |
+
except Exception as e:
|
| 461 |
+
raise GGUFConverterError(f"Error uploading imatrix.dat: {e}")
|
| 462 |
+
|
| 463 |
+
# Upload README.md
|
| 464 |
+
readme_path = self._generate_readme(processing_config)
|
| 465 |
+
self._upload_file(processing_config, readme_path, "README.md")
|
| 466 |
+
|
| 467 |
+
print(f"Uploaded successfully with {quant_config.imatrix_method if quant_config.use_imatrix else quant_config.method} option!")
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
class GGUFConverterUI:
|
| 471 |
+
"""Gradio UI for the GGUF Converter."""
|
| 472 |
+
|
| 473 |
+
def __init__(self):
|
| 474 |
+
self.processor = HuggingFaceModelProcessor()
|
| 475 |
+
self.css = """/* Custom CSS to allow scrolling */
|
| 476 |
+
.gradio-container {overflow-y: auto;}
|
| 477 |
+
"""
|
| 478 |
+
|
| 479 |
+
# Initialize components
|
| 480 |
+
self._initialize_components()
|
| 481 |
+
self._setup_interface()
|
| 482 |
+
|
| 483 |
+
def _initialize_components(self):
|
| 484 |
+
"""Initialize all UI components."""
|
| 485 |
+
#####
|
| 486 |
+
# Base model section
|
| 487 |
+
#####
|
| 488 |
+
self.model_id = HuggingfaceHubSearch(
|
| 489 |
+
label="Hub Model ID",
|
| 490 |
+
placeholder="Search for model id on Huggingface",
|
| 491 |
+
search_type="model",
|
| 492 |
+
)
|
| 493 |
+
|
| 494 |
+
#####
|
| 495 |
+
# Quantization section
|
| 496 |
+
#####
|
| 497 |
+
self.use_imatrix = gr.Checkbox(
|
| 498 |
+
value=False,
|
| 499 |
+
label="Use Imatrix Quantization",
|
| 500 |
+
info="Use importance matrix for quantization."
|
| 501 |
+
)
|
| 502 |
+
self.q_method = gr.Dropdown(
|
| 503 |
+
choices=["Q2_K", "Q3_K_S", "Q3_K_M", "Q3_K_L", "Q4_0", "Q4_K_S", "Q4_K_M", "Q5_0", "Q5_K_S", "Q5_K_M", "Q6_K", "Q8_0", "F16", "BF16"],
|
| 504 |
+
label="Quantization Method",
|
| 505 |
+
info="GGML quantization type",
|
| 506 |
+
value="Q4_K_M",
|
| 507 |
+
filterable=False,
|
| 508 |
+
visible=True
|
| 509 |
+
)
|
| 510 |
+
self.imatrix_q_method = gr.Dropdown(
|
| 511 |
+
choices=["IQ3_M", "IQ3_XXS", "Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS", "Q5_K_M", "Q5_K_S"],
|
| 512 |
+
label="Imatrix Quantization Method",
|
| 513 |
+
info="GGML imatrix quants type",
|
| 514 |
+
value="IQ4_NL",
|
| 515 |
+
filterable=False,
|
| 516 |
+
visible=False
|
| 517 |
+
)
|
| 518 |
+
self.train_data_file = gr.File(
|
| 519 |
+
label="Training Data File",
|
| 520 |
+
file_types=[".txt"],
|
| 521 |
+
visible=False
|
| 522 |
+
)
|
| 523 |
+
|
| 524 |
+
#####
|
| 525 |
+
# Advanced Options section
|
| 526 |
+
#####
|
| 527 |
+
self.split_model = gr.Checkbox(
|
| 528 |
+
value=False,
|
| 529 |
+
label="Split Model",
|
| 530 |
+
info="Shard the model using gguf-split."
|
| 531 |
+
)
|
| 532 |
+
self.split_max_tensors = gr.Number(
|
| 533 |
+
value=256,
|
| 534 |
+
label="Max Tensors per File",
|
| 535 |
+
info="Maximum number of tensors per file when splitting model.",
|
| 536 |
+
visible=False
|
| 537 |
+
)
|
| 538 |
+
self.split_max_size = gr.Textbox(
|
| 539 |
+
label="Max File Size",
|
| 540 |
+
info="Maximum file size when splitting model (--split-max-size). May leave empty to use the default. Accepted suffixes: M, G. Example: 256M, 5G",
|
| 541 |
+
visible=False
|
| 542 |
+
)
|
| 543 |
+
self.leave_output = gr.Checkbox(
|
| 544 |
+
value=False,
|
| 545 |
+
label="Leave output tensor",
|
| 546 |
+
info="Leaves output.weight un(re)quantized"
|
| 547 |
+
)
|
| 548 |
+
self.quant_embedding = gr.Checkbox(
|
| 549 |
+
value=False,
|
| 550 |
+
label="Quant embeddings tensor",
|
| 551 |
+
info="Quantize embeddings tensor separately"
|
| 552 |
+
)
|
| 553 |
+
self.embedding_tensor_method = gr.Dropdown(
|
| 554 |
+
choices=["Q2_K", "Q3_K", "Q4_K", "Q5_K", "Q6_K", "Q8_0"],
|
| 555 |
+
label="Embeddings Quantization Method",
|
| 556 |
+
info="use a specific quant type for the token embeddings tensor",
|
| 557 |
+
value="Q8_0",
|
| 558 |
+
filterable=False,
|
| 559 |
+
visible=False
|
| 560 |
+
)
|
| 561 |
+
self.quant_output = gr.Checkbox(
|
| 562 |
+
value=False,
|
| 563 |
+
label="Quant output tensor",
|
| 564 |
+
info="Quantize output tensor separately"
|
| 565 |
+
)
|
| 566 |
+
self.output_tensor_method = gr.Dropdown(
|
| 567 |
+
choices=["Q2_K", "Q3_K", "Q4_K", "Q5_K", "Q6_K", "Q8_0"],
|
| 568 |
+
label="Output Quantization Method",
|
| 569 |
+
info="use a specific quant type for the output.weight tensor",
|
| 570 |
+
value="Q8_0",
|
| 571 |
+
filterable=False,
|
| 572 |
+
visible=False
|
| 573 |
+
)
|
| 574 |
+
|
| 575 |
+
#####
|
| 576 |
+
# Output Settings section
|
| 577 |
+
#####
|
| 578 |
+
self.private_repo = gr.Checkbox(
|
| 579 |
+
value=False,
|
| 580 |
+
label="Private Repo",
|
| 581 |
+
info="Create a private repo under your username."
|
| 582 |
+
)
|
| 583 |
+
self.repo_name = gr.Textbox(
|
| 584 |
+
label="Output Repository Name",
|
| 585 |
+
info="Set your repository name",
|
| 586 |
+
max_lines=1
|
| 587 |
+
)
|
| 588 |
+
self.gguf_name = gr.Textbox(
|
| 589 |
+
label="Output File Name",
|
| 590 |
+
info="Set output file name",
|
| 591 |
+
max_lines=1
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
#####
|
| 595 |
+
# Buttons section
|
| 596 |
+
#####
|
| 597 |
+
self.clear_btn = gr.ClearButton(
|
| 598 |
+
value="Clear",
|
| 599 |
+
variant="secondary",
|
| 600 |
+
components=[
|
| 601 |
+
self.model_id,
|
| 602 |
+
self.q_method,
|
| 603 |
+
self.use_imatrix,
|
| 604 |
+
self.imatrix_q_method,
|
| 605 |
+
self.private_repo,
|
| 606 |
+
self.train_data_file,
|
| 607 |
+
self.leave_output,
|
| 608 |
+
self.quant_embedding,
|
| 609 |
+
self.embedding_tensor_method,
|
| 610 |
+
self.quant_output,
|
| 611 |
+
self.output_tensor_method,
|
| 612 |
+
self.split_model,
|
| 613 |
+
self.split_max_tensors,
|
| 614 |
+
self.split_max_size,
|
| 615 |
+
self.repo_name,
|
| 616 |
+
self.gguf_name,
|
| 617 |
+
]
|
| 618 |
+
)
|
| 619 |
+
self.submit_btn = gr.Button(
|
| 620 |
+
value="Submit",
|
| 621 |
+
variant="primary"
|
| 622 |
+
)
|
| 623 |
+
|
| 624 |
+
#####
|
| 625 |
+
# Outputs section
|
| 626 |
+
#####
|
| 627 |
+
self.output_label = gr.Markdown(label="output")
|
| 628 |
+
self.output_image = gr.Image(
|
| 629 |
+
show_label=False,
|
| 630 |
+
show_download_button=False,
|
| 631 |
+
interactive=False
|
| 632 |
+
)
|
| 633 |
+
|
| 634 |
+
@staticmethod
|
| 635 |
+
def _update_output_repo(model_id: str, oauth_token: Optional[gr.OAuthToken]) -> str:
|
| 636 |
+
"""Update output repository name based on model and user."""
|
| 637 |
+
if oauth_token is None or not oauth_token.token:
|
| 638 |
+
return ""
|
| 639 |
+
if not model_id:
|
| 640 |
+
return ""
|
| 641 |
+
try:
|
| 642 |
+
username = whoami(oauth_token.token)["name"]
|
| 643 |
+
model_name = model_id.split('/')[-1]
|
| 644 |
+
return f"{username}/{model_name}-GGUF"
|
| 645 |
+
except:
|
| 646 |
+
return ""
|
| 647 |
+
|
| 648 |
+
@staticmethod
|
| 649 |
+
def _update_output_filename(model_id: str, use_imatrix: bool, q_method: str, imatrix_q_method: str) -> str:
|
| 650 |
+
"""Update output filename based on model and quantization settings."""
|
| 651 |
+
if not model_id:
|
| 652 |
+
return ""
|
| 653 |
+
model_name = model_id.split('/')[-1]
|
| 654 |
+
if use_imatrix:
|
| 655 |
+
return f"{model_name}-{imatrix_q_method.upper()}-imat.gguf"
|
| 656 |
+
return f"{model_name}-{q_method.upper()}.gguf"
|
| 657 |
+
|
| 658 |
+
def _setup_interface(self):
|
| 659 |
+
"""Set up the Gradio interface."""
|
| 660 |
+
with gr.Blocks(css=self.css) as self.demo:
|
| 661 |
+
#####
|
| 662 |
+
# Layout
|
| 663 |
+
#####
|
| 664 |
+
gr.Markdown(HuggingFaceModelProcessor.ERROR_LOGIN)
|
| 665 |
+
gr.LoginButton(min_width=250)
|
| 666 |
+
gr.HTML("<h1 style=\"text-aling:center;\">Create your own GGUF Quants!</h1>")
|
| 667 |
+
gr.Markdown(f"The space takes an HF repo as an input, quantizes it and creates a Public repo containing the selected quant under your HF user namespace.<br/>Use via {self.processor.SPACE_URL}")
|
| 668 |
+
|
| 669 |
+
with gr.Row():
|
| 670 |
+
with gr.Column() as inputs:
|
| 671 |
+
gr.Markdown("### Model Configuration")
|
| 672 |
+
self.model_id.render()
|
| 673 |
+
with gr.Column():
|
| 674 |
+
self.use_imatrix.render()
|
| 675 |
+
self.q_method.render()
|
| 676 |
+
self.imatrix_q_method.render()
|
| 677 |
+
self.train_data_file.render()
|
| 678 |
+
gr.Markdown("### Advanced Options")
|
| 679 |
+
self.quant_embedding.render()
|
| 680 |
+
self.embedding_tensor_method.render()
|
| 681 |
+
self.leave_output.render()
|
| 682 |
+
self.quant_output.render()
|
| 683 |
+
self.output_tensor_method.render()
|
| 684 |
+
self.split_model.render()
|
| 685 |
+
with gr.Row() as split_options:
|
| 686 |
+
self.split_max_tensors.render()
|
| 687 |
+
self.split_max_size.render()
|
| 688 |
+
gr.Markdown("### Output Settings")
|
| 689 |
+
gr.Markdown("You can customize settings for your GGUF repo.")
|
| 690 |
+
self.private_repo.render()
|
| 691 |
+
with gr.Row():
|
| 692 |
+
self.repo_name.render()
|
| 693 |
+
self.gguf_name.render()
|
| 694 |
+
# Buttons
|
| 695 |
+
with gr.Row() as buttons:
|
| 696 |
+
self.clear_btn.render()
|
| 697 |
+
self.submit_btn.render()
|
| 698 |
+
with gr.Column() as outputs:
|
| 699 |
+
self.output_label.render()
|
| 700 |
+
self.output_image.render()
|
| 701 |
+
|
| 702 |
+
#####
|
| 703 |
+
# Event handlers
|
| 704 |
+
#####
|
| 705 |
+
self.submit_btn.click(
|
| 706 |
+
fn=self._process_model_wrapper,
|
| 707 |
+
inputs=[
|
| 708 |
+
self.model_id,
|
| 709 |
+
self.q_method,
|
| 710 |
+
self.use_imatrix,
|
| 711 |
+
self.imatrix_q_method,
|
| 712 |
+
self.private_repo,
|
| 713 |
+
self.train_data_file,
|
| 714 |
+
self.repo_name,
|
| 715 |
+
self.gguf_name,
|
| 716 |
+
self.quant_embedding,
|
| 717 |
+
self.embedding_tensor_method,
|
| 718 |
+
self.leave_output,
|
| 719 |
+
self.quant_output,
|
| 720 |
+
self.output_tensor_method,
|
| 721 |
+
self.split_model,
|
| 722 |
+
self.split_max_tensors,
|
| 723 |
+
self.split_max_size
|
| 724 |
+
],
|
| 725 |
+
outputs=[
|
| 726 |
+
self.output_label,
|
| 727 |
+
self.output_image,
|
| 728 |
+
],
|
| 729 |
)
|
|
|
|
|
|
|
| 730 |
|
| 731 |
+
#####
|
| 732 |
+
# OnChange handlers
|
| 733 |
+
#####
|
| 734 |
+
self.use_imatrix.change(
|
| 735 |
+
fn=lambda use_imatrix: [gr.update(visible=not use_imatrix), gr.update(visible=use_imatrix), gr.update(visible=use_imatrix)],
|
| 736 |
+
inputs=self.use_imatrix,
|
| 737 |
+
outputs=[self.q_method, self.imatrix_q_method, self.train_data_file]
|
| 738 |
+
)
|
| 739 |
+
self.split_model.change(
|
| 740 |
+
fn=lambda split_model: [gr.update(visible=split_model), gr.update(visible=split_model)],
|
| 741 |
+
inputs=self.split_model,
|
| 742 |
+
outputs=[self.split_max_tensors, self.split_max_size]
|
| 743 |
+
)
|
| 744 |
+
self.quant_embedding.change(
|
| 745 |
+
fn=lambda quant_embedding: gr.update(visible=quant_embedding),
|
| 746 |
+
inputs=self.quant_embedding,
|
| 747 |
+
outputs=[self.embedding_tensor_method]
|
| 748 |
+
)
|
| 749 |
+
self.leave_output.change(
|
| 750 |
+
fn=lambda leave_output, quant_output: [gr.update(visible=not leave_output), gr.update(visible=not leave_output and quant_output)],
|
| 751 |
+
inputs=[self.leave_output, self.leave_output],
|
| 752 |
+
outputs=[self.quant_output, self.output_tensor_method]
|
| 753 |
+
)
|
| 754 |
+
self.quant_output.change(
|
| 755 |
+
fn=lambda quant_output: [gr.update(visible=not quant_output), gr.update(visible=quant_output)],
|
| 756 |
+
inputs=self.quant_output,
|
| 757 |
+
outputs=[self.leave_output, self.output_tensor_method]
|
| 758 |
+
)
|
| 759 |
+
self.model_id.change(
|
| 760 |
+
fn=self._update_output_repo,
|
| 761 |
+
inputs=[self.model_id],
|
| 762 |
+
outputs=[self.repo_name]
|
| 763 |
+
)
|
| 764 |
+
self.model_id.change(
|
| 765 |
+
fn=self._update_output_filename,
|
| 766 |
+
inputs=[self.model_id, self.use_imatrix, self.q_method, self.imatrix_q_method],
|
| 767 |
+
outputs=[self.gguf_name]
|
| 768 |
+
)
|
| 769 |
+
self.use_imatrix.change(
|
| 770 |
+
fn=self._update_output_filename,
|
| 771 |
+
inputs=[self.model_id, self.use_imatrix, self.q_method, self.imatrix_q_method],
|
| 772 |
+
outputs=[self.gguf_name]
|
| 773 |
+
)
|
| 774 |
+
self.q_method.change(
|
| 775 |
+
fn=self._update_output_filename,
|
| 776 |
+
inputs=[self.model_id, self.use_imatrix, self.q_method, self.imatrix_q_method],
|
| 777 |
+
outputs=[self.gguf_name]
|
| 778 |
+
)
|
| 779 |
+
self.imatrix_q_method.change(
|
| 780 |
+
fn=self._update_output_filename,
|
| 781 |
+
inputs=[self.model_id, self.use_imatrix, self.q_method, self.imatrix_q_method],
|
| 782 |
+
outputs=[self.gguf_name]
|
| 783 |
+
)
|
| 784 |
|
| 785 |
+
def _process_model_wrapper(self, model_id: str, q_method: str, use_imatrix: bool,
|
| 786 |
+
imatrix_q_method: str, private_repo: bool, train_data_file,
|
| 787 |
+
repo_name: str, gguf_name: str, quant_embedding: bool,
|
| 788 |
+
embedding_tensor_method: str, leave_output: bool,
|
| 789 |
+
quant_output: bool, output_tensor_method: str,
|
| 790 |
+
split_model: bool, split_max_tensors, split_max_size: str, oauth_token: Optional[gr.OAuthToken]) -> Tuple[str, str]:
|
| 791 |
+
"""Wrapper for the process_model method to handle the conversion using ModelProcessingConfig."""
|
| 792 |
+
try:
|
| 793 |
+
# Validate token and get token string
|
| 794 |
+
token = self.processor._validate_token(oauth_token)
|
| 795 |
+
|
| 796 |
+
# Create configuration objects
|
| 797 |
+
quant_config = QuantizationConfig(
|
| 798 |
+
method=q_method,
|
| 799 |
+
use_imatrix=use_imatrix,
|
| 800 |
+
imatrix_method=imatrix_q_method,
|
| 801 |
+
quant_embedding=quant_embedding,
|
| 802 |
+
embedding_tensor_method=embedding_tensor_method,
|
| 803 |
+
leave_output=leave_output,
|
| 804 |
+
quant_output=quant_output,
|
| 805 |
+
output_tensor_method=output_tensor_method
|
| 806 |
)
|
|
|
|
| 807 |
|
| 808 |
+
split_config = SplitConfig(
|
| 809 |
+
enabled=split_model,
|
| 810 |
+
max_tensors=split_max_tensors if isinstance(split_max_tensors, int) else 256,
|
| 811 |
+
max_size=split_max_size
|
| 812 |
+
)
|
| 813 |
|
| 814 |
+
output_config = OutputConfig(
|
| 815 |
+
private_repo=private_repo,
|
| 816 |
+
repo_name=repo_name,
|
| 817 |
+
filename=gguf_name
|
| 818 |
+
)
|
| 819 |
+
|
| 820 |
+
model_name = self.processor._get_model_name(model_id)
|
| 821 |
+
|
| 822 |
+
with tempfile.TemporaryDirectory(dir=self.processor.OUTPUT_FOLDER) as outDirObj:
|
| 823 |
+
outdir = (
|
| 824 |
+
self.processor._create_folder(os.path.join(self.processor.OUTPUT_FOLDER, model_name))
|
| 825 |
+
if self.processor.RUN_LOCALLY == "1"
|
| 826 |
+
else Path(outDirObj)
|
| 827 |
+
)
|
| 828 |
+
|
| 829 |
+
quant_config.fp16_model = f"{outdir}/{model_name}-fp16.gguf"
|
| 830 |
+
quant_config.imatrix_file = f"{outdir}/{model_name}-imatrix.gguf"
|
| 831 |
+
quant_config.quantized_gguf = f"{outdir}/{gguf_name}"
|
| 832 |
+
|
| 833 |
+
processing_config = ModelProcessingConfig(
|
| 834 |
+
token=token,
|
| 835 |
+
model_id=model_id,
|
| 836 |
+
model_name=model_name,
|
| 837 |
+
outdir=outdir,
|
| 838 |
+
quant_config=quant_config,
|
| 839 |
+
split_config=split_config,
|
| 840 |
+
output_config=output_config
|
| 841 |
+
)
|
| 842 |
+
|
| 843 |
+
# Call the processor's main method with the config object
|
| 844 |
+
self.processor.process_model(processing_config)
|
| 845 |
+
|
| 846 |
+
return (
|
| 847 |
+
f'<h1>✅ DONE</h1><br/>Find your repo here: <a href="{processing_config.new_repo_url}" target="_blank" style="text-decoration:underline">{processing_config.new_repo_id}</a>',
|
| 848 |
+
"llama.png",
|
| 849 |
+
)
|
| 850 |
+
|
| 851 |
+
except Exception as e:
|
| 852 |
+
print(f"Error processing model: {e}")
|
| 853 |
+
return (f'<h1>❌ ERROR</h1><br/><pre style="white-space:pre-wrap;">{self.processor._escape_html(str(e))}</pre>', "error.png")
|
| 854 |
+
|
| 855 |
+
|
| 856 |
+
def launch(self):
|
| 857 |
+
"""Launch the Gradio interface."""
|
| 858 |
+
# Set up space restart scheduler
|
| 859 |
+
def restart_space():
|
| 860 |
+
HfApi().restart_space(repo_id=self.processor.SPACE_ID, token=self.processor.HF_TOKEN, factory_reboot=True)
|
| 861 |
+
|
| 862 |
+
scheduler = BackgroundScheduler()
|
| 863 |
+
scheduler.add_job(restart_space, "interval", seconds=21600)
|
| 864 |
+
scheduler.start()
|
| 865 |
+
|
| 866 |
+
# Launch the interface
|
| 867 |
+
self.demo.queue(default_concurrency_limit=1, max_size=5).launch(debug=True, show_api=False)
|
| 868 |
+
|
| 869 |
+
|
| 870 |
+
# Main execution
|
| 871 |
+
if __name__ == "__main__":
|
| 872 |
+
ui = GGUFConverterUI()
|
| 873 |
+
ui.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
groups_merged.txt → calibration_data_v5_rc.txt
RENAMED
|
The diff for this file is too large to render.
See raw diff
|
|
|