Created
October 28, 2022 15:50
-
-
Save WoolDoughnut310/19f289275f22346ebb12ffc9c40521f7 to your computer and use it in GitHub Desktop.
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
import { ObjectId } from "mongodb"; | |
import { KMeans, StandardScaler } from "scikitjs"; | |
import getDb, { getBucket } from "../db"; | |
import { Song } from "../db/types"; | |
import extractFeatures, { decodeAudio } from "./features"; | |
export const predictFromGridFS = async ( | |
model: KMeans, | |
filename: string | |
): Promise<number> => { | |
const bucket = getBucket(); | |
const stream = bucket.openDownloadStreamByName(filename); | |
// Decode file stream from GridFS | |
const audioData = await decodeAudio(stream); | |
const features = extractFeatures(audioData); | |
// Standardize features | |
const data = new StandardScaler().fitTransform([features]); | |
const output = await model.predict(data).array(); | |
return output[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment