Skip to content

Instantly share code, notes, and snippets.

@WoolDoughnut310
Created October 28, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WoolDoughnut310/19f289275f22346ebb12ffc9c40521f7 to your computer and use it in GitHub Desktop.
Save WoolDoughnut310/19f289275f22346ebb12ffc9c40521f7 to your computer and use it in GitHub Desktop.
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