Upload convert_to_gguf_simple.py with huggingface_hub
Browse files- convert_to_gguf_simple.py +139 -0
convert_to_gguf_simple.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = ["torch", "transformers", "peft", "huggingface_hub", "sentencepiece", "protobuf", "gguf"]
|
| 3 |
+
# ///
|
| 4 |
+
|
| 5 |
+
import subprocess
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from huggingface_hub import HfApi, create_repo
|
| 8 |
+
from peft import AutoPeftModelForCausalLM
|
| 9 |
+
from transformers import AutoTokenizer
|
| 10 |
+
|
| 11 |
+
# Config
|
| 12 |
+
ADAPTER_REPO = "kingjux/ffmpeg-command-generator"
|
| 13 |
+
OUTPUT_REPO = "kingjux/ffmpeg-command-generator-gguf"
|
| 14 |
+
|
| 15 |
+
print("=" * 50)
|
| 16 |
+
print("GGUF Conversion for LM Studio")
|
| 17 |
+
print("=" * 50)
|
| 18 |
+
|
| 19 |
+
# Step 1: Load and merge LoRA with base model
|
| 20 |
+
print("\n[1/3] Loading adapter and merging with base model...")
|
| 21 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 22 |
+
ADAPTER_REPO,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
trust_remote_code=True,
|
| 25 |
+
)
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(ADAPTER_REPO, trust_remote_code=True)
|
| 27 |
+
|
| 28 |
+
print("Merging LoRA weights...")
|
| 29 |
+
merged_model = model.merge_and_unload()
|
| 30 |
+
|
| 31 |
+
merged_path = Path("/tmp/merged_model")
|
| 32 |
+
merged_path.mkdir(exist_ok=True)
|
| 33 |
+
print(f"Saving merged model to {merged_path}...")
|
| 34 |
+
merged_model.save_pretrained(merged_path)
|
| 35 |
+
tokenizer.save_pretrained(merged_path)
|
| 36 |
+
|
| 37 |
+
# Step 2: Convert to GGUF using llama.cpp Python converter
|
| 38 |
+
print("\n[2/3] Converting to GGUF...")
|
| 39 |
+
llama_cpp_path = Path("/tmp/llama.cpp")
|
| 40 |
+
if not llama_cpp_path.exists():
|
| 41 |
+
subprocess.run([
|
| 42 |
+
"git", "clone", "--depth", "1",
|
| 43 |
+
"https://github.com/ggerganov/llama.cpp.git",
|
| 44 |
+
str(llama_cpp_path)
|
| 45 |
+
], check=True)
|
| 46 |
+
|
| 47 |
+
# Install requirements
|
| 48 |
+
subprocess.run([
|
| 49 |
+
"pip", "install", "-q", "-r",
|
| 50 |
+
str(llama_cpp_path / "requirements" / "requirements-convert_hf_to_gguf.txt")
|
| 51 |
+
], check=True)
|
| 52 |
+
|
| 53 |
+
gguf_output_dir = Path("/tmp/gguf_output")
|
| 54 |
+
gguf_output_dir.mkdir(exist_ok=True)
|
| 55 |
+
|
| 56 |
+
# Convert to F16 GGUF (no quantization needed - LM Studio handles it)
|
| 57 |
+
f16_path = gguf_output_dir / "ffmpeg-command-generator-f16.gguf"
|
| 58 |
+
subprocess.run([
|
| 59 |
+
"python", str(llama_cpp_path / "convert_hf_to_gguf.py"),
|
| 60 |
+
str(merged_path),
|
| 61 |
+
"--outfile", str(f16_path),
|
| 62 |
+
"--outtype", "f16"
|
| 63 |
+
], check=True)
|
| 64 |
+
print(f"Created: {f16_path}")
|
| 65 |
+
|
| 66 |
+
# Step 3: Upload to Hub
|
| 67 |
+
print("\n[3/3] Uploading to Hugging Face Hub...")
|
| 68 |
+
api = HfApi()
|
| 69 |
+
create_repo(OUTPUT_REPO, repo_type="model", exist_ok=True)
|
| 70 |
+
|
| 71 |
+
# Model card
|
| 72 |
+
model_card = """---
|
| 73 |
+
license: apache-2.0
|
| 74 |
+
base_model: Qwen/Qwen2.5-0.5B-Instruct
|
| 75 |
+
tags:
|
| 76 |
+
- gguf
|
| 77 |
+
- ffmpeg
|
| 78 |
+
- command-generation
|
| 79 |
+
- lm-studio
|
| 80 |
+
- ollama
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
# FFMPEG Command Generator (GGUF)
|
| 84 |
+
|
| 85 |
+
Fine-tuned Qwen2.5-0.5B that generates FFMPEG commands from natural language with chain-of-thought reasoning.
|
| 86 |
+
|
| 87 |
+
## Quick Start
|
| 88 |
+
|
| 89 |
+
### LM Studio
|
| 90 |
+
```bash
|
| 91 |
+
lms import kingjux/ffmpeg-command-generator-gguf
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
### Ollama
|
| 95 |
+
```bash
|
| 96 |
+
ollama run hf.co/kingjux/ffmpeg-command-generator-gguf
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
## Example
|
| 100 |
+
|
| 101 |
+
**Input:** "Convert video.mp4 to webm format"
|
| 102 |
+
|
| 103 |
+
**Output:**
|
| 104 |
+
```
|
| 105 |
+
<think>
|
| 106 |
+
Task: Convert MP4 to WebM
|
| 107 |
+
- WebM uses VP9 video + Opus audio
|
| 108 |
+
- Use -c:v libvpx-vp9 for video
|
| 109 |
+
- Use -c:a libopus for audio
|
| 110 |
+
</think>
|
| 111 |
+
|
| 112 |
+
ffmpeg -i video.mp4 -c:v libvpx-vp9 -c:a libopus output.webm
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
## Training
|
| 116 |
+
- Base: Qwen2.5-0.5B-Instruct
|
| 117 |
+
- Method: LoRA fine-tuning (r=16, alpha=32)
|
| 118 |
+
- Dataset: 30 FFMPEG command examples with CoT reasoning
|
| 119 |
+
- Trained on HuggingFace Jobs (T4 GPU)
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
card_path = gguf_output_dir / "README.md"
|
| 123 |
+
card_path.write_text(model_card)
|
| 124 |
+
|
| 125 |
+
# Upload
|
| 126 |
+
for file in [card_path, f16_path]:
|
| 127 |
+
print(f"Uploading {file.name}...")
|
| 128 |
+
api.upload_file(
|
| 129 |
+
path_or_fileobj=str(file),
|
| 130 |
+
path_in_repo=file.name,
|
| 131 |
+
repo_id=OUTPUT_REPO,
|
| 132 |
+
repo_type="model"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
print("\n" + "=" * 50)
|
| 136 |
+
print("DONE!")
|
| 137 |
+
print(f"Model: https://huggingface.co/{OUTPUT_REPO}")
|
| 138 |
+
print(f"\nLM Studio: lms import {OUTPUT_REPO}")
|
| 139 |
+
print("=" * 50)
|