new-dim / export_model.sh
splendidcomputer's picture
Upload folder using huggingface_hub
fa9878d verified
#!/bin/bash
# Script to prepare Ollama model for Hugging Face upload
# This script helps export the Ollama model and prepare it for Hugging Face
set -e
MODEL_NAME="llama3-dementia-care:latest"
EXPORT_DIR="./model_export"
CURRENT_DIR=$(pwd)
echo "πŸš€ Preparing Llama 3 Dementia Care model for Hugging Face upload..."
echo "=================================================="
# Check if Ollama is installed
if ! command -v ollama &> /dev/null; then
echo "❌ Error: Ollama is not installed or not in PATH"
echo "Please install Ollama first: https://ollama.com"
exit 1
fi
# Check if the model exists
if ! ollama list | grep -q "$MODEL_NAME"; then
echo "❌ Error: Model $MODEL_NAME not found"
echo "Available models:"
ollama list
exit 1
fi
echo "βœ… Found model: $MODEL_NAME"
# Create export directory
mkdir -p "$EXPORT_DIR"
cd "$EXPORT_DIR"
echo "πŸ“ Created export directory: $EXPORT_DIR"
# Export model information
echo "πŸ“‹ Exporting model information..."
ollama show "$MODEL_NAME" > model_details.txt
ollama show "$MODEL_NAME" --modelfile > exported_modelfile.txt
echo "πŸ“Š Model details saved to:"
echo " - model_details.txt"
echo " - exported_modelfile.txt"
# Create a README for the export
cat > export_README.md << 'EOF'
# Exported Ollama Model Files
This directory contains the exported files from your Ollama model that need to be converted for Hugging Face.
## Files:
- `model_details.txt` - Detailed model information from Ollama
- `exported_modelfile.txt` - The Modelfile configuration
- `export_README.md` - This file
## Next Steps:
### Option 1: Manual Conversion
1. You'll need to manually extract the model weights from Ollama's blob storage
2. Convert them to PyTorch/Safetensors format
3. Create proper tokenizer files
### Option 2: Use Conversion Tools
1. Install ollama-python: `pip install ollama`
2. Use conversion scripts like:
- https://github.com/ollama/ollama/blob/main/docs/modelfile.md
- Community conversion tools
### Option 3: Re-train/Fine-tune
1. Start with the base Llama 3 8B model from Hugging Face
2. Fine-tune it with your dementia care dataset
3. Upload the fine-tuned model
## Important Notes:
- Ollama stores models in a specific format that may require conversion
- The model weights are typically in `/Users/[username]/.ollama/models/blobs/`
- You may need to use specialized tools to extract and convert the weights
For more information, visit: https://ollama.com/blog/modelfile
EOF
echo "πŸ“‹ Created export_README.md with next steps"
# Try to locate the actual model blob
echo "πŸ” Locating model blob files..."
OLLAMA_MODELS_DIR="$HOME/.ollama/models"
if [ -d "$OLLAMA_MODELS_DIR" ]; then
echo "πŸ“ Ollama models directory: $OLLAMA_MODELS_DIR"
# Extract the blob SHA from the Modelfile
BLOB_SHA=$(grep "^FROM" exported_modelfile.txt | grep "sha256" | awk -F'sha256-' '{print $2}')
if [ -n "$BLOB_SHA" ]; then
echo "πŸ” Model blob SHA: $BLOB_SHA"
BLOB_PATH="$OLLAMA_MODELS_DIR/blobs/sha256-$BLOB_SHA"
if [ -f "$BLOB_PATH" ]; then
echo "βœ… Found model blob: $BLOB_PATH"
echo "πŸ“Š Blob size: $(ls -lh "$BLOB_PATH" | awk '{print $5}')"
# Copy blob info to export
echo "Model Blob Information:" > blob_info.txt
echo "SHA256: $BLOB_SHA" >> blob_info.txt
echo "Path: $BLOB_PATH" >> blob_info.txt
echo "Size: $(ls -lh "$BLOB_PATH" | awk '{print $5}')" >> blob_info.txt
echo "Modified: $(ls -l "$BLOB_PATH" | awk '{print $6, $7, $8}')" >> blob_info.txt
else
echo "❌ Model blob not found at expected location"
fi
else
echo "❌ Could not extract blob SHA from Modelfile"
fi
else
echo "❌ Ollama models directory not found"
fi
cd "$CURRENT_DIR"
echo ""
echo "πŸŽ‰ Export preparation complete!"
echo "=================================================="
echo "πŸ“ Files exported to: $EXPORT_DIR"
echo ""
echo "⚠️ IMPORTANT: Converting Ollama models to Hugging Face format requires additional steps:"
echo ""
echo "πŸ”„ Conversion Options:"
echo "1. Use ollama-python and conversion tools"
echo "2. Extract and convert model weights manually"
echo "3. Re-train using the base Llama 3 model on Hugging Face"
echo ""
echo "πŸ“š Resources:"
echo "- Ollama documentation: https://ollama.com/blog/modelfile"
echo "- Hugging Face model upload: https://huggingface.co/docs/transformers/model_sharing"
echo ""
echo "βœ… Your repository structure is ready for Hugging Face!"
echo "πŸ“ Repository files created:"
ls -la "$CURRENT_DIR" | grep -E '\.(md|json|txt|py)$|Modelfile|NOTICE'
echo ""
echo "πŸš€ Next: Upload your repository to Hugging Face and add the converted model weights."