# 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")