import os import shutil import subprocess import signal import gradio as gr from huggingface_hub import create_repo, HfApi, snapshot_download, whoami, ModelCard from gradio_huggingfacehub_search import HuggingfaceHubSearch from apscheduler.schedulers.background import BackgroundScheduler from textwrap import dedent HF_TOKEN = os.environ.get("HF_TOKEN") def generate_importance_matrix(model_path, train_data_path): imatrix_command = f"./llama-imatrix -m ../{model_path} -f {train_data_path} -ngl 99 --output-frequency 10" os.chdir("llama.cpp") print(f"Current working directory: {os.getcwd()}") print(f"Files in the current directory: {os.listdir('.')}") if not os.path.isfile(f"../{model_path}"): raise Exception(f"Model file not found: {model_path}") print("Running imatrix command...") process = subprocess.Popen(imatrix_command, shell=True) try: process.wait(timeout=60) # added wait except subprocess.TimeoutExpired: print("Imatrix computation timed out. Sending SIGINT to allow graceful termination...") process.send_signal(signal.SIGINT) try: process.wait(timeout=5) # grace period except subprocess.TimeoutExpired: print("Imatrix proc still didn't term. Forcefully terminating process...") process.kill() os.chdir("..") print("Importance matrix generation completed.") def split_upload_model(model_path, repo_id, oauth_token: gr.oauth.OAuthToken | None, split_max_tensors=256, split_max_size=None): if oauth_token.token is None: raise ValueError("You have to be logged in.") split_cmd = f"llama.cpp/llama-gguf-split --split --split-max-tensors {split_max_tensors}" if split_max_size: split_cmd += f" --split-max-size {split_max_size}" split_cmd += f" {model_path} {model_path.split('.')[0]}" print(f"Split command: {split_cmd}") result = subprocess.run(split_cmd, shell=True, capture_output=True, text=True) print(f"Split command stdout: {result.stdout}") print(f"Split command stderr: {result.stderr}") if result.returncode != 0: raise Exception(f"Error splitting the model: {result.stderr}") print("Model split successfully!") sharded_model_files = [f for f in os.listdir('.') if f.startswith(model_path.split('.')[0])] if sharded_model_files: print(f"Sharded model files: {sharded_model_files}") api = HfApi(token=oauth_token.token) for file in sharded_model_files: file_path = os.path.join('.', file) print(f"Uploading file: {file_path}") try: api.upload_file( path_or_fileobj=file_path, path_in_repo=file, repo_id=repo_id, ) except Exception as e: raise Exception(f"Error uploading file {file_path}: {e}") else: raise Exception("No sharded files found.") print("Sharded model has been uploaded successfully!") def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_repo, train_data_file, split_model, split_max_tensors, split_max_size, oauth_token: gr.oauth.OAuthToken | None): if oauth_token.token is None: raise ValueError("You must be logged in to use GGUF-my-repo") model_name = model_id.split('/')[-1] fp16 = f"{model_name}.fp16.gguf" try: api = HfApi(token=oauth_token.token) dl_pattern = ["*.md", "*.json", "*.model"] model_types = ["*.safetensors", "*.bin", "*.pt", "*.onnx", "*.h5", "*.tflite", "*.ckpt", "*.pb", "*.tar", "*.xml", "*.caffemodel"] pattern = ( "*.safetensors" if any( file.path.endswith(".safetensors") for file in api.list_repo_tree( repo_id=model_id, recursive=True, ) ) else "*.bin" ) dl_pattern += pattern dl_pattern += model_types api.snapshot_download(repo_id=model_id, local_dir=model_name, local_dir_use_symlinks=False, allow_patterns=dl_pattern) print("Model downloaded successfully!") print(f"Current working directory: {os.getcwd()}") print(f"Model directory contents: {os.listdir(model_name)}") conversion_script = "convert_hf_to_gguf.py" fp16_conversion = f"python llama.cpp/{conversion_script} {model_name} --outtype f16 --outfile {fp16}" result = subprocess.run(fp16_conversion, shell=True, capture_output=True) print(result) if result.returncode != 0: raise Exception(f"Error converting to fp16: {result.stderr}") print("Model converted to fp16 successfully!") print(f"Converted model path: {fp16}") imatrix_path = "llama.cpp/imatrix.dat" if use_imatrix: if train_data_file: train_data_path = train_data_file.name else: train_data_path = "groups_merged.txt" # fallback calibration dataset print(f"Training data file path: {train_data_path}") if not os.path.isfile(train_data_path): raise Exception(f"Training data file not found: {train_data_path}") generate_importance_matrix(fp16, train_data_path) else: print("Not using imatrix quantization.") username = whoami(oauth_token.token)["name"] 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" quantized_gguf_path = quantized_gguf_name if use_imatrix: quantise_ggml = f"./llama.cpp/llama-quantize --imatrix {imatrix_path} {fp16} {quantized_gguf_path} {imatrix_q_method}" else: quantise_ggml = f"./llama.cpp/llama-quantize {fp16} {quantized_gguf_path} {q_method}" result = subprocess.run(quantise_ggml, shell=True, capture_output=True) if result.returncode != 0: raise Exception(f"Error quantizing: {result.stderr}") print(f"Quantized successfully with {imatrix_q_method if use_imatrix else q_method} option!") print(f"Quantized model path: {quantized_gguf_path}") # Create empty repo 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) new_repo_id = new_repo_url.repo_id print("Repo created successfully!", new_repo_url) try: card = ModelCard.load(model_id, token=oauth_token.token) except: card = ModelCard("") if card.data.tags is None: card.data.tags = [] card.data.tags.append("llama-cpp") card.data.tags.append("gguf-my-repo") card.data.base_model = model_id card.text = dedent( f""" # {new_repo_id} This model was converted to GGUF format from [`{model_id}`](https://huggingface.co/{model_id}) using llama.cpp via the ggml.ai's [GGUF-my-repo](https://huggingface.co/spaces/ggml-org/gguf-my-repo) space. Refer to the [original model card](https://huggingface.co/{model_id}) for more details on the model. ## Use with llama.cpp Install llama.cpp through brew (works on Mac and Linux) ```bash brew install llama.cpp ``` Invoke the llama.cpp server or the CLI. ### CLI: ```bash llama-cli --hf-repo {new_repo_id} --hf-file {quantized_gguf_name} -p "The meaning to life and the universe is" ``` ### Server: ```bash llama-server --hf-repo {new_repo_id} --hf-file {quantized_gguf_name} -c 2048 ``` Note: You can also use this checkpoint directly through the [usage steps](https://github.com/ggerganov/llama.cpp?tab=readme-ov-file#usage) listed in the Llama.cpp repo as well. Step 1: Clone llama.cpp from GitHub. ``` git clone https://github.com/ggerganov/llama.cpp ``` Step 2: Move into the llama.cpp folder and build it with `LLAMA_CURL=1` flag along with other hardware-specific flags (for ex: LLAMA_CUDA=1 for Nvidia GPUs on Linux). ``` cd llama.cpp && LLAMA_CURL=1 make ``` Step 3: Fetch model weights from HF using curl command and use them with the above `llama_cli` or `llama_server`. ``` curl -L {new_repo_id} > .gguf/{quantized_gguf_name} ``` """ ) if use_imatrix: card.text += "\nNote: This model was quantized using imatrix." card.push_to_hub(repo_id=new_repo_id, token=oauth_token.token) api.upload_file( path_or_fileobj=quantized_gguf_path, path_in_repo=quantized_gguf_name, repo_id=new_repo_id, token=oauth_token.token, ) print("Pushed model to the hub!") if split_model: split_upload_model(quantized_gguf_name, new_repo_id, oauth_token, split_max_tensors=split_max_tensors, split_max_size=split_max_size) except Exception as e: print("Error in process_model:", e) raise e finally: os.makedirs("model_cache", exist_ok=True) shutil.move(model_name, f"model_cache/{model_name}") shutil.move(fp16, f"model_cache/{fp16}") shutil.move(quantized_gguf_path, f"model_cache/{quantized_gguf_path}") print("Moved model files to model_cache.") print("Process completed successfully!")