Skip to content

Commit

Permalink
Model Not Initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
sonuku092 committed Mar 17, 2024
1 parent a9a4554 commit 033fce6
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions backend/src/models/heart-disease/heart-disease.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
// heart-disease.service.ts

import { Injectable } from '@nestjs/common';
import * as tf from '@tensorflow/tfjs';

@Injectable()
export class HeartDiseaseService {
private model: tf.LayersModel | null = null;

constructor() {
this.initModel();
}

private async loadKerasModel() {
try {
// Load the Keras model
const modelPath = './heart_disease_prediction_model.h5';
const model = await tf.loadLayersModel('file://' + modelPath);
return model;
} catch (error) {
console.error('Error loading Keras model:', error);
return null;
}
}

private async initModel() {
this.model = await this.loadKerasModel();
}

async predictHeartDisease(data: any): Promise<number> {
// Implement your prediction logic here
return Math.random(); // Dummy prediction for demonstration
if (!this.model) {
console.error('Model not initialized.');
return -1;
}

try {
// Convert input data to tensor
const inputData = tf.tensor2d(data, [1, data.length]);

// Perform prediction using the loaded model
const prediction = this.model.predict(inputData) as tf.Tensor;

// Get the predicted value
const predictionValue = (await prediction.data())[0];

// Clean up: dispose tensors
inputData.dispose();
prediction.dispose();

return predictionValue;
} catch (error) {
console.error('Error during prediction:', error);
return -1;
}
}
}

0 comments on commit 033fce6

Please sign in to comment.