Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacobsapps/4b2f018c55165e637ceadbd33c59864f to your computer and use it in GitHub Desktop.
Save jacobsapps/4b2f018c55165e637ceadbd33c59864f to your computer and use it in GitHub Desktop.
// CoreMLProcessing.swift
private var statsEmbeddings: [StatEmbedding] = []
private var titleEmbeddings: [TitleEmbedding] = []
func loadCLIPModelAndEmbeddings() {
model = nil
statsEmbeddings = []
titleEmbeddings = []
let defaultConfig = MLModelConfiguration()
guard let modelURL = Bundle.main.url(forResource: "mobileclip_blt_image", withExtension: "mlmodelc") else {
fatalError("Model file not found in the bundle.")
}
let imageClassifierModel: MLModel
do {
imageClassifierModel = try MLModel(contentsOf: modelURL, configuration: defaultConfig)
} catch {
fatalError("Failed to load the model from file: \(error.localizedDescription)")
}
guard let imageClassifierVisionModel = try? VNCoreMLModel(for: imageClassifierModel) else {
fatalError("App failed to create a `VNCoreMLModel` instance.")
}
self.model = imageClassifierVisionModel
do {
guard let titleURL = Bundle.main.url(forResource: "titles_embeddings", withExtension: "json") else {
fatalError("Embedding file titles_embeddings.json not found.")
}
let titleData = try Data(contentsOf: titleURL)
titleEmbeddings = try JSONDecoder().decode([TitleEmbedding].self, from: titleData)
guard let statsURL = Bundle.main.url(forResource: "stats_embeddings", withExtension: "json") else {
fatalError("Embedding file stats_embeddings.json not found.")
}
let statsData = try Data(contentsOf: statsURL)
statsEmbeddings = try JSONDecoder().decode([StatEmbedding].self, from: statsData)
} catch {
print(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment