Spaces:
Runtime error
Runtime error
File size: 19,730 Bytes
57cd4b7 f2c9828 e88cde7 3081403 f574f6b c12824a f574f6b a6e9176 a628a9f 47e5bd9 d898f74 fd089ea 897dd96 f574f6b 4425968 3ea29f2 8fbdf58 3ea29f2 d898f74 3ea29f2 f574f6b d898f74 c8ce925 3ea29f2 d898f74 3ea29f2 d898f74 3ea29f2 d898f74 c8ce925 3ea29f2 c8ce925 3ea29f2 c8ce925 d898f74 f574f6b d898f74 f574f6b 4425968 d898f74 4425968 d898f74 4425968 c8ce925 f574f6b 0e2eafc f574f6b d898f74 f574f6b d898f74 c8ce925 4425968 d898f74 4425968 c8ce925 e88cde7 c8ce925 4425968 e88cde7 c8ce925 e88cde7 c8ce925 d898f74 e88cde7 c8ce925 e88cde7 c8ce925 e88cde7 c8ce925 e88cde7 d898f74 c8ce925 4425968 66cce51 441a3ff f574f6b c8ce925 441a3ff c8ce925 4425968 c8ce925 4425968 c8ce925 4425968 c8ce925 f574f6b 4425968 c8ce925 f574f6b 441a3ff c8ce925 4425968 d898f74 c8ce925 d898f74 c8ce925 47e5bd9 c8ce925 df88099 c8ce925 f574f6b c8ce925 982b426 c8ce925 f574f6b c8ce925 441a3ff d898f74 c8ce925 d898f74 c8ce925 b2c6134 0e2eafc 5f9a0f8 c8ce925 d898f74 f574f6b d898f74 c8ce925 d898f74 5f9a0f8 f574f6b d898f74 3ea29f2 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 c8ce925 d898f74 3ea29f2 d898f74 3081403 839677f d898f74 33ed555 c8ce925 a89dc66 f574f6b d898f74 f574f6b d898f74 f574f6b d898f74 f574f6b a89dc66 d898f74 a89dc66 f574f6b d898f74 a89dc66 f574f6b a89dc66 d898f74 a89dc66 d898f74 a89dc66 d898f74 a89dc66 d898f74 a89dc66 d898f74 a89dc66 d898f74 a89dc66 d898f74 a89dc66 f574f6b d898f74 f574f6b d898f74 f574f6b d898f74 f574f6b c8ce925 3ea29f2 f574f6b a89dc66 d898f74 f574f6b f2c9828 c12824a d898f74 c12824a d898f74 c12824a d898f74 |
1 2 3 4 5 6 7 8 9 10 11 12 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 50 51 52 53 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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 281 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 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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
import os
import subprocess
import signal
import tempfile
from pathlib import Path
import logging
import gradio as gr
from huggingface_hub import HfApi, ModelCard, whoami
from gradio_huggingfacehub_search import HuggingfaceHubSearch
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import numpy as np
import shutil
from copy import deepcopy
HF_TOKEN = os.environ.get("HF_TOKEN")
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
CONVERSION_SCRIPT = "./llama.cpp/convert_hf_to_gguf.py"
log_dir = "/data/logs"
downloads_dir = "/data/downloads"
outputs_dir = "/data/outputs"
os.makedirs(log_dir, exist_ok=True)
logging.basicConfig(
filename=os.path.join(log_dir, "app.log"),
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
def get_llama_cpp_version():
try:
result = subprocess.run(
["git", "-C", "./llama.cpp", "describe", "--tags", "--always"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
text=True,
)
version = result.stdout.strip().split("-")[0]
return version
except subprocess.CalledProcessError as e:
logger.error("Error getting llama.cpp version: %s", e.stderr.strip())
return None
def get_repo_namespace(repo_owner: str, username: str, user_orgs: list) -> str:
if repo_owner == "self":
return username
for org in user_orgs:
if org["name"] == repo_owner:
return org["name"]
raise ValueError(f"Invalid repo_owner: {repo_owner}")
def escape(s: str) -> str:
return (
s.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace('"', """)
.replace("\n", "<br/>")
)
def toggle_repo_owner(export_to_org: bool, oauth_token: gr.OAuthToken | None) -> tuple:
if oauth_token is None or oauth_token.token is None:
raise gr.Error("You must be logged in to use quantize-my-repo")
if not export_to_org:
return gr.update(visible=False, choices=["self"], value="self"), gr.update(
visible=False, value=""
)
info = whoami(oauth_token.token)
orgs = [org["name"] for org in info.get("orgs", [])]
return gr.update(visible=True, choices=["self"] + orgs, value="self"), gr.update(
visible=True
)
def generate_importance_matrix(
model_path: str, train_data_path: str, output_path: str
) -> None:
imatrix_command = [
"./llama.cpp/llama-imatrix",
"-m",
model_path,
"-f",
train_data_path,
"-ngl",
"99",
"--output-frequency",
"10",
"-o",
output_path,
]
if not os.path.isfile(model_path):
raise FileNotFoundError(f"Model file not found: {model_path}")
logger.info("Running imatrix command...")
process = subprocess.Popen(imatrix_command, shell=False)
try:
process.wait(timeout=60)
except subprocess.TimeoutExpired:
logger.warning(
"Imatrix computation timed out. Sending SIGINT to allow graceful termination..."
)
process.send_signal(signal.SIGINT)
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
logger.error(
"Imatrix proc still didn't term. Forecfully terming process..."
)
process.kill()
logger.info("Importance matrix generation completed.")
def split_upload_model(
model_path: str,
outdir: str,
repo_id: str,
oauth_token: gr.OAuthToken | None,
split_max_tensors: int = 256,
split_max_size: str | None = None,
org_token: str | None = None,
export_to_org: bool = False,
) -> None:
logger.info("Model path: %s", model_path)
logger.info("Output dir: %s", outdir)
if oauth_token is None or oauth_token.token is None:
raise ValueError("You have to be logged in.")
split_cmd = ["./llama.cpp/llama-gguf-split", "--split"]
if split_max_size:
split_cmd.extend(["--split-max-size", split_max_size])
else:
split_cmd.extend(["--split-max-tensors", str(split_max_tensors)])
model_path_prefix = ".".join(model_path.split(".")[:-1])
split_cmd.extend([model_path, model_path_prefix])
logger.info("Split command: %s", split_cmd)
result = subprocess.run(split_cmd, shell=False, capture_output=True, text=True)
logger.info("Split command stdout: %s", result.stdout)
logger.info("Split command stderr: %s", result.stderr)
if result.returncode != 0:
raise RuntimeError(f"Error splitting the model: {result.stderr}")
logger.info("Model split successfully!")
if os.path.exists(model_path):
os.remove(model_path)
model_file_prefix = model_path_prefix.split("/")[-1]
logger.info("Model file name prefix: %s", model_file_prefix)
sharded_model_files = [
f
for f in os.listdir(outdir)
if f.startswith(model_file_prefix) and f.endswith(".gguf")
]
if not sharded_model_files:
raise RuntimeError("No sharded files found.")
logger.info("Sharded model files: %s", sharded_model_files)
api = HfApi(token=org_token if (export_to_org and org_token) else oauth_token.token)
for file in sharded_model_files:
file_path = os.path.join(outdir, file)
logger.info("Uploading file: %s", file_path)
try:
api.upload_file(
path_or_fileobj=file_path,
path_in_repo=file,
repo_id=repo_id,
)
except Exception as e:
raise RuntimeError(f"Error uploading file {file_path}: {e}") from e
logger.info("Sharded model has been uploaded successfully!")
def get_new_model_card(
original_card: ModelCard,
original_model_id: str,
gguf_files: list,
new_repo_url: str,
split_model: bool,
) -> ModelCard:
version = get_llama_cpp_version()
model_card = deepcopy(original_card)
model_card.data.tags = (model_card.data.tags or []) + [
"antigma",
"quantize-my-repo",
]
model_card.data.base_model = original_model_id
# Format the table rows
table_rows = []
for file_info in gguf_files:
name, _, size, method = file_info
if split_model:
display_name = name[:-5]
else:
display_name = f"[{name}]({new_repo_url}/blob/main/{name})"
table_rows.append(f"{display_name}|{method}|{size:.2f} GB|{split_model}|\n")
model_card.text = f"""
*Produced by [Antigma Labs](https://antigma.ai), [Antigma Quantize Space](https://huggingface.co/spaces/Antigma/quantize-my-repo)*
*Follow Antigma Labs in X [https://x.com/antigma_labs](https://x.com/antigma_labs)*
*Antigma's GitHub Homepage [https://github.com/AntigmaLabs](https://github.com/AntigmaLabs)*
## Quantization Format (GGUF)
We use <a href="https://github.com/ggml-org/llama.cpp">llama.cpp</a> release <a href="https://github.com/ggml-org/llama.cpp/releases/tag/{version}">{version}</a> for quantization.
Original model: https://huggingface.co/{original_model_id}
## Download a file (not the whole branch) from below:
| Filename | Quant type | File Size | Split |
| -------- | ---------- | --------- | ----- |
| {'|'.join(table_rows)}
## Original Model Card
{original_card.text}
## Downloading using huggingface-cli
<details>
<summary>Click to view download instructions</summary>
First, make sure you have hugginface-cli installed:
```
pip install -U "huggingface_hub[cli]"
```
Then, you can target the specific file you want:
```
huggingface-cli download {new_repo_url} --include "{gguf_files[0][0]}" --local-dir ./
```
If the model is bigger than 50GB, it will have been split into multiple files. In order to download them all to a local folder, run:
```
huggingface-cli download {new_repo_url} --include "{gguf_files[0][0]}/*" --local-dir ./
```
You can either specify a new local-dir (e.g. deepseek-ai_DeepSeek-V3-0324-Q8_0) or it will be in default hugging face cache
</details>
"""
return model_card
def process_model(
model_id: str,
q_method: str | list,
use_imatrix: bool,
imatrix_q_method: str,
private_repo: bool,
train_data_file: gr.File | None,
split_model: bool,
split_max_tensors: int,
split_max_size: str | None,
export_to_org: bool,
repo_owner: str,
org_token: str | None,
oauth_token: gr.OAuthToken | None,
) -> tuple[str, str]:
if oauth_token is None or oauth_token.token is None:
raise gr.Error("You must be logged in to use quantize-my-repo")
try:
whoami(oauth_token.token)
except Exception as e:
raise gr.Error("You must be logged in to use quantize-my-repo") from e
user_info = whoami(oauth_token.token)
username = user_info["name"]
user_orgs = user_info.get("orgs", [])
if not export_to_org:
repo_owner = "self"
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logger.info(
"Time %s, Username %s, Model_ID %s, q_method %s",
current_time,
username,
model_id,
",".join(q_method) if isinstance(q_method, list) else q_method,
)
repo_namespace = get_repo_namespace(repo_owner, username, user_orgs)
model_name = model_id.split("/")[-1]
try:
api_token = org_token if (export_to_org and org_token) else oauth_token.token
api = HfApi(token=api_token)
dl_pattern = ["*.md", "*.json", "*.model"]
pattern = (
"*.safetensors"
if any(
f.path.endswith(".safetensors")
for f in api.list_repo_tree(repo_id=model_id, recursive=True)
)
else "*.bin"
)
dl_pattern.append(pattern)
os.makedirs(downloads_dir, exist_ok=True)
os.makedirs(outputs_dir, exist_ok=True)
with tempfile.TemporaryDirectory(dir=outputs_dir) as outdir:
fp16 = str(Path(outdir) / f"{model_name}.fp16.gguf")
with tempfile.TemporaryDirectory(dir=downloads_dir) as tmpdir:
logger.info("Start download")
local_dir = Path(tmpdir) / model_name
api.snapshot_download(
repo_id=model_id,
local_dir=local_dir,
local_dir_use_symlinks=False,
allow_patterns=dl_pattern,
)
config_dir = local_dir / "config.json"
adapter_config_dir = local_dir / "adapter_config.json"
if os.path.exists(adapter_config_dir) and not os.path.exists(
config_dir
):
raise RuntimeError(
"adapter_config.json is present. If converting LoRA, use GGUF-my-lora."
)
logger.info("Download successfully")
result = subprocess.run(
[
"python",
CONVERSION_SCRIPT,
local_dir,
"--outtype",
"f16",
"--outfile",
fp16,
],
shell=False,
capture_output=True,
)
logger.info("Converted to f16")
if result.returncode != 0:
raise RuntimeError(
f"Error converting to fp16: {result.stderr.decode()}"
)
shutil.rmtree(downloads_dir)
imatrix_path = Path(outdir) / "imatrix.dat"
if use_imatrix:
train_data_path = (
train_data_file.name
if train_data_file
else "llama.cpp/groups_merged.txt"
)
if not os.path.isfile(train_data_path):
raise FileNotFoundError(
f"Training data not found: {train_data_path}"
)
generate_importance_matrix(fp16, train_data_path, imatrix_path)
quant_methods = (
[imatrix_q_method]
if use_imatrix
else (q_method if isinstance(q_method, list) else [q_method])
)
suffix = "imat" if use_imatrix else None
gguf_files = []
for method in quant_methods:
logger.info("Begin quantize")
name = (
f"{model_name.lower()}-{method.lower()}-{suffix}.gguf"
if suffix
else f"{model_name.lower()}-{method.lower()}.gguf"
)
path = str(Path(outdir) / name)
quant_cmd = (
[
"./llama.cpp/llama-quantize",
"--imatrix",
imatrix_path,
fp16,
path,
method,
]
if use_imatrix
else ["./llama.cpp/llama-quantize", fp16, path, method]
)
result = subprocess.run(quant_cmd, shell=False, capture_output=True)
if result.returncode != 0:
raise RuntimeError(
f"Quantization failed ({method}): {result.stderr.decode()}"
)
size = os.path.getsize(path) / 1024 / 1024 / 1024
gguf_files.append((name, path, size, method))
logger.info("Quantize successfully!")
suffix_for_repo = (
f"{imatrix_q_method}-imat" if use_imatrix else "-".join(quant_methods)
)
repo_id = f"{repo_namespace}/{model_name}-GGUF"
new_repo_url = api.create_repo(
repo_id=repo_id, exist_ok=True, private=private_repo
)
try:
original_card = ModelCard.load(model_id, token=oauth_token.token)
except Exception:
original_card = ModelCard("")
card = get_new_model_card(
original_card, model_id, gguf_files, new_repo_url, split_model
)
readme_path = Path(outdir) / "README.md"
card.save(readme_path)
for name, path, _, _ in gguf_files:
if split_model:
split_upload_model(
path,
outdir,
repo_id,
oauth_token,
split_max_tensors,
split_max_size,
org_token,
export_to_org,
)
else:
api.upload_file(
path_or_fileobj=path, path_in_repo=name, repo_id=repo_id
)
if use_imatrix and os.path.isfile(imatrix_path):
api.upload_file(
path_or_fileobj=imatrix_path,
path_in_repo="imatrix.dat",
repo_id=repo_id,
)
api.upload_file(
path_or_fileobj=readme_path, path_in_repo="README.md", repo_id=repo_id
)
return (
f'<h1>✅ DONE</h1><br/>Repo: <a href="{new_repo_url}" target="_blank" style="text-decoration:underline">{repo_id}</a>',
f"llama{np.random.randint(9)}.png",
)
except Exception as e:
return (
f'<h1>❌ ERROR</h1><br/><pre style="white-space:pre-wrap;">{escape(str(e))}</pre>',
"error.png",
)
css = """/* Custom CSS to allow scrolling */
.gradio-container {overflow-y: auto;}
"""
model_id = HuggingfaceHubSearch(
label="Hub Model ID",
placeholder="Search for model id on Huggingface",
search_type="model",
)
export_to_org = gr.Checkbox(
label="Export to Organization Repository",
value=False,
info="If checked, you can select an organization to export to.",
)
repo_owner = gr.Dropdown(
choices=["self"], value="self", label="Repository Owner", visible=False
)
org_token = gr.Textbox(label="Org Access Token", type="password", visible=False)
q_method = gr.Dropdown(
[
"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",
],
label="Quantization Method",
info="GGML quantization type",
value="Q4_K_M",
filterable=False,
visible=True,
multiselect=True,
)
imatrix_q_method = gr.Dropdown(
["IQ3_M", "IQ3_XXS", "Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS", "Q5_K_M", "Q5_K_S"],
label="Imatrix Quantization Method",
info="GGML imatrix quants type",
value="IQ4_NL",
filterable=False,
visible=False,
)
use_imatrix = gr.Checkbox(
value=False,
label="Use Imatrix Quantization",
info="Use importance matrix for quantization.",
)
private_repo = gr.Checkbox(
value=False, label="Private Repo", info="Create a private repo under your username."
)
train_data_file = gr.File(label="Training Data File", file_types=["txt"], visible=False)
split_model = gr.Checkbox(
value=False, label="Split Model", info="Shard the model using gguf-split."
)
split_max_tensors = gr.Number(
value=256,
label="Max Tensors per File",
info="Maximum number of tensors per file when splitting model.",
visible=False,
)
split_max_size = gr.Textbox(
label="Max File Size",
info="Maximum file size when splitting model (--split-max-size). May leave empty to use the default. Accepted suffixes: M, G. Example: 256M, 5G",
visible=False,
)
iface = gr.Interface(
fn=process_model,
inputs=[
model_id,
q_method,
use_imatrix,
imatrix_q_method,
private_repo,
train_data_file,
split_model,
split_max_tensors,
split_max_size,
export_to_org,
repo_owner,
org_token,
],
outputs=[gr.Markdown(label="Output"), gr.Image(show_label=False)],
title="Make your own GGUF Quants — faster than ever before, believe me.",
description="We take your Hugging Face repo — a terrific repo — we quantize it, we package it beautifully, and we give you your very own repo. It's smart. It's efficient. It's huge. You're gonna love it.",
api_name=False,
)
with gr.Blocks(css=".gradio-container {overflow-y: auto;}") as demo:
gr.Markdown("Logged in, you must be. Classy, secure, and victorious, it keeps us.")
gr.LoginButton(min_width=250)
export_to_org.change(
fn=toggle_repo_owner, inputs=[export_to_org], outputs=[repo_owner, org_token]
)
split_model.change(
fn=lambda sm: (gr.update(visible=sm), gr.update(visible=sm)),
inputs=split_model,
outputs=[split_max_tensors, split_max_size],
)
use_imatrix.change(
fn=lambda use: (
gr.update(visible=not use),
gr.update(visible=use),
gr.update(visible=use),
),
inputs=use_imatrix,
outputs=[q_method, imatrix_q_method, train_data_file],
)
iface.render()
def restart_space():
HfApi().restart_space(
repo_id="Antigma/quantize-my-repo", token=HF_TOKEN, factory_reboot=True
)
scheduler = BackgroundScheduler()
scheduler.add_job(restart_space, "interval", seconds=86400)
scheduler.start()
demo.queue(default_concurrency_limit=1, max_size=5).launch(debug=True, show_api=False)
|