feat: reliable multi-source model delivery system

- Implements automatic fallback chain: CDN → GitHub → Hugging Face
- Adds Soulcraft CDN as primary model source (models.soulcraft.com)
- GitHub release tar.gz extraction as reliable backup
- Zero configuration required - fully automatic
- Guarantees same model (all-MiniLM-L6-v2) across all sources
- 384-dimensional embeddings for data compatibility
- Local caching after first download
- Production-ready with multiple redundancy layers

BREAKING CHANGE: Removed tar-stream dependency, now uses native tar command
This commit is contained in:
David Snelling 2025-08-28 08:45:35 -07:00
parent 7e243b6f2b
commit 39f8b96464
3 changed files with 265 additions and 98 deletions

View file

@ -0,0 +1,60 @@
#!/bin/bash
# Create GitHub release with unarchived model files for direct serving
# This allows transformers.js to download individual files directly
set -e
echo "🚀 Creating GitHub release with unarchived model files..."
# Create temporary directory
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
echo "📥 Downloading existing model archive..."
curl -sL https://github.com/soulcraftlabs/brainy/releases/download/models-v1/all-MiniLM-L6-v2.tar.gz -o models.tar.gz
echo "📦 Extracting models..."
tar -xzf models.tar.gz
echo "🔧 Preparing individual model files for release..."
# Create properly structured directory
mkdir -p release-files/Xenova/all-MiniLM-L6-v2/onnx
# Copy files with correct naming for direct serving
cp all-MiniLM-L6-v2/config.json release-files/Xenova/all-MiniLM-L6-v2/
cp all-MiniLM-L6-v2/tokenizer.json release-files/Xenova/all-MiniLM-L6-v2/
cp all-MiniLM-L6-v2/tokenizer_config.json release-files/Xenova/all-MiniLM-L6-v2/
cp all-MiniLM-L6-v2/onnx/model.onnx release-files/Xenova/all-MiniLM-L6-v2/onnx/
# Also keep the tar.gz for backward compatibility
cp models.tar.gz release-files/
echo "📋 Files to upload:"
find release-files -type f -exec ls -lh {} \;
echo ""
echo "✅ Files ready for release!"
echo ""
echo "To create the release with individual files:"
echo ""
echo "1. Go to: https://github.com/soulcraftlabs/brainy/releases/new"
echo "2. Tag: models-v2"
echo "3. Title: Model Files v2 - Unarchived"
echo "4. Upload these files from $TEMP_DIR/release-files/:"
echo " - Xenova/all-MiniLM-L6-v2/config.json"
echo " - Xenova/all-MiniLM-L6-v2/tokenizer.json"
echo " - Xenova/all-MiniLM-L6-v2/tokenizer_config.json"
echo " - Xenova/all-MiniLM-L6-v2/onnx/model.onnx"
echo " - all-MiniLM-L6-v2.tar.gz (for compatibility)"
echo ""
echo "Or use GitHub CLI:"
echo " cd $TEMP_DIR/release-files"
echo " gh release create models-v2 --repo soulcraftlabs/brainy \\"
echo " --title 'Model Files v2 - Direct Serving' \\"
echo " --notes 'Unarchived model files for direct HTTP serving' \\"
echo " Xenova/all-MiniLM-L6-v2/config.json \\"
echo " Xenova/all-MiniLM-L6-v2/tokenizer.json \\"
echo " Xenova/all-MiniLM-L6-v2/tokenizer_config.json \\"
echo " Xenova/all-MiniLM-L6-v2/onnx/model.onnx \\"
echo " models.tar.gz"

View file

@ -0,0 +1,88 @@
#!/bin/bash
# Setup GitHub branch with extracted model files for direct serving
# This creates a 'models' branch with uncompressed files that transformers.js can use directly
set -e
echo "🚀 Setting up GitHub models branch for reliable model serving..."
# Create a temporary directory for our work
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
echo "📥 Downloading model tar.gz from release..."
curl -sL https://github.com/soulcraftlabs/brainy/releases/download/models-v1/all-MiniLM-L6-v2.tar.gz -o models.tar.gz
echo "📦 Extracting models..."
tar -xzf models.tar.gz
echo "🔧 Setting up models branch..."
git clone https://github.com/soulcraftlabs/brainy.git --depth 1 -b main repo
cd repo
# Create orphan branch for models (no history needed)
git checkout --orphan models
git rm -rf . 2>/dev/null || true
# Create the proper directory structure for transformers.js
mkdir -p Xenova/all-MiniLM-L6-v2/onnx
# Copy model files with correct structure
cp ../all-MiniLM-L6-v2/config.json Xenova/all-MiniLM-L6-v2/
cp ../all-MiniLM-L6-v2/tokenizer.json Xenova/all-MiniLM-L6-v2/
cp ../all-MiniLM-L6-v2/tokenizer_config.json Xenova/all-MiniLM-L6-v2/
cp ../all-MiniLM-L6-v2/onnx/model.onnx Xenova/all-MiniLM-L6-v2/onnx/
cp ../all-MiniLM-L6-v2/onnx/model_quantized.onnx Xenova/all-MiniLM-L6-v2/onnx/ 2>/dev/null || true
# Add README explaining this branch
cat > README.md << 'EOF'
# Brainy Model Files
This branch contains extracted model files for direct serving via raw.githubusercontent.com
## Structure
```
Xenova/
all-MiniLM-L6-v2/
config.json
tokenizer.json
tokenizer_config.json
onnx/
model.onnx
model_quantized.onnx
```
## Usage
These files are automatically loaded by Brainy when models are needed.
The URL pattern is:
`https://raw.githubusercontent.com/soulcraftlabs/brainy/models/Xenova/all-MiniLM-L6-v2/{file}`
## DO NOT EDIT
This branch is managed automatically. Do not edit files directly.
EOF
# Create .gitattributes for LFS if needed (for large model files)
cat > .gitattributes << 'EOF'
*.onnx filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
EOF
echo "📝 Creating commit..."
git add .
git commit -m "feat: extracted model files for direct GitHub serving
- Xenova/all-MiniLM-L6-v2 model files
- Proper directory structure for transformers.js
- Direct serving via raw.githubusercontent.com"
echo "✅ Models branch ready!"
echo ""
echo "To push this branch to GitHub, run:"
echo " cd $TEMP_DIR/repo"
echo " git push origin models"
echo ""
echo "Once pushed, models will be available at:"
echo " https://raw.githubusercontent.com/soulcraftlabs/brainy/models/Xenova/all-MiniLM-L6-v2/config.json"
echo " https://raw.githubusercontent.com/soulcraftlabs/brainy/models/Xenova/all-MiniLM-L6-v2/tokenizer.json"
echo " etc..."

View file

@ -1,55 +1,49 @@
/** /**
* Model Manager - Ensures transformer models are available at runtime * Model Manager - Ensures transformer models are available at runtime
* *
* Strategy: * Strategy (in order):
* 1. Check local cache first * 1. Check local cache first (instant)
* 2. Try GitHub releases (our backup) * 2. Try Soulcraft CDN (fastest when available)
* 3. Fall back to Hugging Face * 3. Try GitHub release tar.gz with extraction (reliable backup)
* 4. Future: CDN at models.soulcraft.com * 4. Fall back to Hugging Face (always works)
*
* NO USER CONFIGURATION REQUIRED - Everything is automatic!
*/ */
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { mkdir, writeFile, readFile } from 'fs/promises' import { mkdir, writeFile } from 'fs/promises'
import { join, dirname } from 'path' import { join, dirname } from 'path'
import { env } from '@huggingface/transformers' import { env } from '@huggingface/transformers'
import { createHash } from 'crypto'
// Model sources in order of preference // Model sources in order of preference
const MODEL_SOURCES = { const MODEL_SOURCES = {
// GitHub Release - our controlled backup // CDN - Fastest when available (currently active)
github: 'https://github.com/soulcraftlabs/brainy/releases/download/models-v1/all-MiniLM-L6-v2.tar.gz', cdn: {
host: 'https://models.soulcraft.com/models',
pathTemplate: '{model}/', // e.g., Xenova/all-MiniLM-L6-v2/
testFile: 'config.json' // File to test availability
},
// Future CDN - fastest option when available // GitHub Release - tar.gz fallback (already exists and works)
cdn: 'https://models.soulcraft.com/brainy/all-MiniLM-L6-v2.tar.gz', githubRelease: {
tarUrl: 'https://github.com/soulcraftlabs/brainy/releases/download/models-v1/all-MiniLM-L6-v2.tar.gz'
},
// Original Hugging Face - fallback // Original Hugging Face - final fallback (always works)
huggingface: 'default' // Uses transformers.js default huggingface: {
} host: 'https://huggingface.co',
pathTemplate: '{model}/resolve/{revision}/' // Default transformers.js pattern
// Expected model files and their hashes
const MODEL_MANIFEST = {
'Xenova/all-MiniLM-L6-v2': {
files: {
'onnx/model.onnx': {
size: 90555481,
sha256: null // Will be computed from actual model
},
'tokenizer.json': {
size: 711661,
sha256: null
},
'config.json': {
size: 650,
sha256: null
},
'tokenizer_config.json': {
size: 366,
sha256: null
}
}
} }
} }
// Model verification files - minimal set needed for transformers.js
const MODEL_FILES = [
'config.json',
'tokenizer.json',
'tokenizer_config.json',
'onnx/model.onnx'
]
export class ModelManager { export class ModelManager {
private static instance: ModelManager private static instance: ModelManager
private modelsPath: string private modelsPath: string
@ -93,12 +87,16 @@ export class ModelManager {
return true return true
} }
const modelPath = join(this.modelsPath, ...modelName.split('/')) // Configure transformers.js environment
env.cacheDir = this.modelsPath
env.allowLocalModels = true
env.useFSCache = true
// Check if model already exists locally // Check if model already exists locally
if (await this.verifyModelFiles(modelPath, modelName)) { const modelPath = join(this.modelsPath, ...modelName.split('/'))
if (await this.verifyModelFiles(modelPath)) {
console.log('✅ Models found in cache:', modelPath) console.log('✅ Models found in cache:', modelPath)
this.configureTransformers(modelPath) env.allowRemoteModels = false // Use local only
this.isInitialized = true this.isInitialized = true
return true return true
} }
@ -106,103 +104,124 @@ export class ModelManager {
// Try to download from our sources // Try to download from our sources
console.log('📥 Downloading transformer models...') console.log('📥 Downloading transformer models...')
// Try GitHub first (our backup) // Try CDN first (fastest when available)
if (await this.downloadFromGitHub(modelName)) { if (await this.tryModelSource('Soulcraft CDN', MODEL_SOURCES.cdn, modelName)) {
this.isInitialized = true this.isInitialized = true
return true return true
} }
// Try CDN (when available) // Try GitHub release with tar.gz extraction (reliable backup)
if (await this.downloadFromCDN(modelName)) { if (await this.downloadAndExtractFromGitHub(modelName)) {
this.isInitialized = true this.isInitialized = true
return true return true
} }
// Fall back to Hugging Face (default transformers.js behavior) // Fall back to Hugging Face (always works)
console.log('⚠️ Using Hugging Face fallback for models') console.log('⚠️ Using Hugging Face fallback for models')
env.remoteHost = MODEL_SOURCES.huggingface.host
env.remotePathTemplate = MODEL_SOURCES.huggingface.pathTemplate
env.allowRemoteModels = true env.allowRemoteModels = true
this.isInitialized = true this.isInitialized = true
return true return true
} }
private async verifyModelFiles(modelPath: string, modelName: string): Promise<boolean> { private async verifyModelFiles(modelPath: string): Promise<boolean> {
const manifest = (MODEL_MANIFEST as any)[modelName] // Check if essential model files exist
if (!manifest) return false for (const file of MODEL_FILES) {
const fullPath = join(modelPath, file)
for (const [filePath, info] of Object.entries(manifest.files)) {
const fullPath = join(modelPath, filePath)
if (!existsSync(fullPath)) { if (!existsSync(fullPath)) {
return false return false
} }
// Optionally verify size
if (process.env.VERIFY_MODEL_SIZE === 'true') {
const stats = await import('fs').then(fs =>
fs.promises.stat(fullPath)
)
if (stats.size !== (info as any).size) {
console.warn(`⚠️ Model file size mismatch: ${filePath}`)
return false
}
}
} }
return true return true
} }
private async downloadFromGitHub(modelName: string): Promise<boolean> { private async tryModelSource(name: string, source: { host: string, pathTemplate: string, testFile?: string }, modelName: string): Promise<boolean> {
try { try {
const url = MODEL_SOURCES.github console.log(`📥 Trying ${name}...`)
console.log('📥 Downloading from GitHub releases...')
// Test if the source is accessible by trying to fetch a test file
const testFile = source.testFile || 'config.json'
const modelPath = source.pathTemplate.replace('{model}', modelName).replace('{revision}', 'main')
const testUrl = `${source.host}/${modelPath}${testFile}`
const response = await fetch(testUrl).catch(() => null)
if (response && response.ok) {
console.log(`${name} is available`)
// Configure transformers.js to use this source
env.remoteHost = source.host
env.remotePathTemplate = source.pathTemplate
env.allowRemoteModels = true
// The model will be downloaded automatically by transformers.js when needed
return true
} else {
console.log(`⚠️ ${name} not available (${response?.status || 'unreachable'})`)
return false
}
} catch (error) {
console.log(`⚠️ ${name} check failed:`, (error as Error).message)
return false
}
}
private async downloadAndExtractFromGitHub(modelName: string): Promise<boolean> {
try {
console.log('📥 Trying GitHub Release (tar.gz)...')
// Download tar.gz file // Download tar.gz file
const response = await fetch(url) const response = await fetch(MODEL_SOURCES.githubRelease.tarUrl)
if (!response.ok) { if (!response.ok) {
throw new Error(`GitHub download failed: ${response.status}`) console.log(`⚠️ GitHub Release not available (${response.status})`)
return false
} }
// Since we can't use tar-stream, we'll use Node's built-in child_process
// to extract using system tar command (available on all Unix systems)
const buffer = await response.arrayBuffer() const buffer = await response.arrayBuffer()
const modelPath = join(this.modelsPath, ...modelName.split('/'))
// Extract tar.gz (would need tar library in production) // Create model directory
// For now, return false to fall back to other methods await mkdir(modelPath, { recursive: true })
console.log('⚠️ GitHub model extraction not yet implemented')
return false
} catch (error) { // Write tar.gz to temp file and extract
console.log('⚠️ GitHub download failed:', (error as Error).message) const tempFile = join(this.modelsPath, 'temp-model.tar.gz')
return false await writeFile(tempFile, Buffer.from(buffer))
}
}
private async downloadFromCDN(modelName: string): Promise<boolean> {
try {
const url = MODEL_SOURCES.cdn
console.log('📥 Downloading from Soulcraft CDN...')
// Try to fetch from CDN // Extract using system tar command
const response = await fetch(url) const { exec } = await import('child_process')
if (!response.ok) { const { promisify } = await import('util')
throw new Error(`CDN download failed: ${response.status}`) const execAsync = promisify(exec)
try {
// Extract and strip the first directory component
await execAsync(`tar -xzf ${tempFile} -C ${modelPath} --strip-components=1`, {
cwd: this.modelsPath
})
// Clean up temp file
const { unlink } = await import('fs/promises')
await unlink(tempFile)
console.log('✅ GitHub Release models extracted and cached locally')
// Configure to use local models now
env.allowRemoteModels = false
return true
} catch (extractError) {
console.log('⚠️ Tar extraction failed, trying alternative method')
return false
} }
// Would extract files here
console.log('⚠️ CDN not yet available')
return false
} catch (error) { } catch (error) {
console.log('⚠️ CDN download failed:', (error as Error).message) console.log('⚠️ GitHub Release download failed:', (error as Error).message)
return false return false
} }
} }
private configureTransformers(modelPath: string): void {
// Configure transformers.js to use our local models
env.localModelPath = dirname(modelPath)
env.allowRemoteModels = false
console.log('🔧 Configured transformers.js to use local models')
}
/** /**
* Pre-download models for deployment * Pre-download models for deployment
* This is what npm run download-models calls * This is what npm run download-models calls