From adaec31ee07a9dc336334315e56fad4aa5884d05 Mon Sep 17 00:00:00 2001 From: Dale Lane Date: Sat, 16 Mar 2019 22:01:59 +0000 Subject: [PATCH] feat: Inline help in Scratch 3 for missing ML models The recognize-image blocks in Scratch 3 will now open a Help panel if they're used when there is no machine learning model found. Signed-off-by: Dale Lane --- .../help/help-scratch3-nomodel.html | 32 +++++++++++++++++++ resources/scratch3-images-classify.js | 7 ++++ src/lib/training/visualrecognition.ts | 12 +++---- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 public/components/help/help-scratch3-nomodel.html diff --git a/public/components/help/help-scratch3-nomodel.html b/public/components/help/help-scratch3-nomodel.html new file mode 100644 index 000000000..fb293acf0 --- /dev/null +++ b/public/components/help/help-scratch3-nomodel.html @@ -0,0 +1,32 @@ + + + + Using machine learning models + + +
+ recognise images +
+
+ This block lets you use machine learning models in your Scratch projects. + However, when you tried to use it, it didn't work because you don't seem to have a machine learning model. +
+
+ There are a few reasons why this might have happened: +
+
+
+ 1. You haven't trained a machine learning model yet. +
+
+ 2. You've started training a machine learning model, but it is still training and isn't ready yet. +
+
+ 3. You did train a machine learning model, but it has been deleted (by you, your teacher, or maybe it was automatically deleted after the expiry time passed). +
+
+
+ You can check this on the Learn & Test page for your project. +
+ + diff --git a/resources/scratch3-images-classify.js b/resources/scratch3-images-classify.js index dc435d78d..402935d8d 100644 --- a/resources/scratch3-images-classify.js +++ b/resources/scratch3-images-classify.js @@ -233,6 +233,11 @@ function classifyImage(imagedata, cacheKey, lastmodified, callback) { { registerIncorrectUse(); } + else if (response.status === 400 && responseJson && + responseJson.error === 'Your machine learning model could not be found. Has it been deleted?') + { + postMessage({ mlforkids : 'mlforkids-recogniseimage-nomodel' }); + } callback({ class_name: 'Unknown', @@ -310,6 +315,8 @@ function getImageClassificationResponse(imagedata, cacheKey, valueToReturn, call // We got a randomly selected result (which means we must not // have a working classifier). console.log('randomly selected result returned by API'); + + postMessage({ mlforkids : 'mlforkids-recogniseimage-nomodel' }); } // update the timestamp to allow local throttling diff --git a/src/lib/training/visualrecognition.ts b/src/lib/training/visualrecognition.ts index 37358a417..5bd1b18fa 100644 --- a/src/lib/training/visualrecognition.ts +++ b/src/lib/training/visualrecognition.ts @@ -26,6 +26,7 @@ export const ERROR_MESSAGES = { 'at too fast a rate. ' + 'Please stop now and let your teacher or group leader know that ' + '"the Watson Visual Recognition service is currently rate limiting their API key"', + NO_MODEL : 'Your machine learning model could not be found. Has it been deleted?', }; @@ -522,8 +523,7 @@ export async function testClassifierFile( if (classifierNotFoundError(errorInfo)) { - const externalError: any = new Error('Your machine learning model could not be found. ' + - 'Has it been deleted?'); + const externalError: any = new Error(ERROR_MESSAGES.NO_MODEL); externalError.statusCode = 400; throw externalError; } @@ -646,8 +646,7 @@ export async function testClassifierURL( } else if (classifierNotFoundError(errorInfo)) { - const externalError: any = new Error('Your machine learning model could not be found. ' + - 'Has it been deleted?'); + const externalError: any = new Error(ERROR_MESSAGES.NO_MODEL); externalError.statusCode = 400; throw externalError; } @@ -682,7 +681,8 @@ function classifierNotFoundError(errorInfo: any): boolean { errorInfo.code === 404 && errorInfo.description && typeof errorInfo.description === 'string' && - errorInfo.description.startsWith('None of the requested classifier ids were found: '); + (errorInfo.description.startsWith('None of the requested classifier ids were found: ') || + errorInfo.description === 'No classifiers found'); } @@ -1000,5 +1000,3 @@ export interface VisualRecogApiResponsePayloadClassifyFile { }>; readonly images_processed: number; } - -