Files
miracles_in_motion/src/ai/OptimizedStudentAssistanceAI.ts
T

94 lines
3.5 KiB
TypeScript

// AI Model Lazy Loading Implementation
import * as tf from '@tensorflow/tfjs'
import type { StudentRequest, MatchResult } from './types'
export class OptimizedStudentAssistanceAI {
private static models: Map<string, any> = new Map()
private static modelUrls: Record<string, string> = {
'text-vectorization': '/models/text-vectorizer.json',
'matching-engine': '/models/matcher.json',
'priority-classifier': '/models/priority.json'
}
// Lazy load models on demand
private static async loadModel(modelType: string) {
if (this.models.has(modelType)) {
return this.models.get(modelType)
}
try {
console.log(`🤖 Loading AI model: ${modelType}`)
const model = await tf.loadLayersModel(this.modelUrls[modelType])
this.models.set(modelType, model)
console.log(`✅ Model ${modelType} loaded successfully`)
return model
} catch (error) {
console.error(`❌ Failed to load model ${modelType}:`, error)
// Fallback to rule-based system
return null
}
}
// Preload critical models in background
static async preloadCriticalModels() {
try {
// Load text vectorization model first (most commonly used)
await this.loadModel('text-vectorization')
// Load others in background
setTimeout(() => this.loadModel('matching-engine'), 2000)
setTimeout(() => this.loadModel('priority-classifier'), 4000)
} catch (error) {
console.warn('Background model preloading failed:', error)
}
}
async processRequest(request: StudentRequest): Promise<MatchResult[]> {
// Load models as needed
const textModel = await OptimizedStudentAssistanceAI.loadModel('text-vectorization')
const matchingModel = await OptimizedStudentAssistanceAI.loadModel('matching-engine')
// Process with loaded models or fallback to rule-based
if (textModel && matchingModel) {
return this.aiBasedMatching(request, textModel, matchingModel)
} else {
return this.ruleBasedMatching(request)
}
}
private async aiBasedMatching(request: StudentRequest, textModel: any, matchingModel: any): Promise<MatchResult[]> {
// AI-powered matching logic
console.log('🤖 Using AI-powered matching', { request: request.id, textModel: !!textModel, matchingModel: !!matchingModel })
// Mock implementation for now
return [{
resourceId: 'resource-1',
resourceName: 'School Supply Kit',
resourceType: 'supplies',
confidenceScore: 0.85,
estimatedImpact: 8.5,
logisticalComplexity: 2.1,
estimatedCost: 50,
fulfillmentTimeline: '2-3 days',
reasoningFactors: ['AI-based match using TensorFlow models', 'High confidence score'],
riskFactors: ['Low risk - standard supplies']
}]
}
private async ruleBasedMatching(request: StudentRequest): Promise<MatchResult[]> {
// Fallback rule-based matching
console.log('📏 Using rule-based matching (fallback)', { request: request.id })
// Mock implementation for now
return [{
resourceId: 'resource-1',
resourceName: 'Basic Supply Kit',
resourceType: 'supplies',
confidenceScore: 0.65,
estimatedImpact: 6.5,
logisticalComplexity: 3.2,
estimatedCost: 50,
fulfillmentTimeline: '3-5 days',
reasoningFactors: ['Rule-based fallback matching', 'Basic category match'],
riskFactors: ['Medium risk - manual verification needed']
}]
}
}