-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |