Skip to content

Instantly share code, notes, and snippets.

@afawcett
Last active August 4, 2017 13:55
Show Gist options
  • Save afawcett/321750da5c186488d4b2aaead7a2a12c to your computer and use it in GitHub Desktop.
Save afawcett/321750da5c186488d4b2aaead7a2a12c to your computer and use it in GitHub Desktop.
public with sharing class EinsteinAction {
public class Prediction {
@InvocableVariable
public String label;
@InvocableVariable
public Double probability;
}
@InvocableMethod(label='Classify the given files' description='Calls the Einsten API to classify the given ContentVersion files.')
public static List<EinsteinAction.Prediction> classifyFiles(List<ID> contentVersionIds) {
String access_token = new VisionController().getAccessToken();
ContentVersion content = [SELECT Title,VersionData FROM ContentVersion where Id in :contentVersionIds LIMIT 1];
List<EinsteinAction.Prediction> predictions = new List<EinsteinAction.Prediction>();
for(Vision.Prediction vp : Vision.predictBlob(content.VersionData, access_token, 'GeneralImageClassifier')) {
EinsteinAction.Prediction p = new EinsteinAction.Prediction();
p.label = vp.label;
p.probability = vp.probability;
predictions.add(p);
break; // Just take the most probable
}
return predictions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment