File size: 2,451 Bytes
523f40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copy this code to Google Colab for model conversion
# =================================================

# Cell 1: Install Unsloth
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

# Cell 2: Load your model 
from unsloth import FastLanguageModel
import torch

# Replace 'YOUR_USERNAME/YOUR_REPO_NAME' with your actual HuggingFace repo
model_name = "YOUR_USERNAME/YOUR_REPO_NAME"  # CHANGE THIS!

max_seq_length = 2048
dtype = None  # None for auto detection
load_in_4bit = True

# Load model and tokenizer
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = model_name,
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
)

print("Model loaded successfully!")
print(f"Model type: {model.config.model_type}")
print(f"Model architecture: {model.config.architectures}")

# Cell 3: Check the chat template
print("Current chat template:")
print(repr(tokenizer.chat_template))

# Cell 4: Convert to GGUF for Ollama
print("Starting GGUF conversion...")

# Save as GGUF with Ollama Modelfile
model.save_pretrained_gguf(
    "ollama_converted_model",
    tokenizer,
    quantization_method = "q4_k_m",  # Good balance of size and quality
)

print("Conversion completed!")
print("Files created:")
!ls -la ollama_converted_model/

# Cell 5: Download the converted files
from google.colab import files
import zipfile
import os

# Zip the converted model
with zipfile.ZipFile('ollama_model.zip', 'w') as zipf:
    for root, dirs, files_list in os.walk('ollama_converted_model'):
        for file in files_list:
            file_path = os.path.join(root, file)
            zipf.write(file_path, os.path.relpath(file_path, 'ollama_converted_model'))

print("Model packaged! Downloading...")
files.download('ollama_model.zip')

# Cell 6: Show Ollama Modelfile content
print("\n=== Ollama Modelfile content ===")
try:
    with open('ollama_converted_model/Modelfile', 'r') as f:
        modelfile_content = f.read()
        print(modelfile_content)
except FileNotFoundError:
    print("Modelfile not found. The model might not have Ollama support.")

print("\n=== Instructions ===")
print("1. Download the ollama_model.zip file")
print("2. Extract it on your Windows machine")
print("3. Use 'ollama create your-model-name -f Modelfile' to import")
print("4. Run 'ollama run your-model-name' to test")