Skip to content

Commit

Permalink
feat: Inline help in Scratch 3 for missing ML models
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dalelane committed Mar 16, 2019
1 parent 4a4a58c commit adaec31
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
32 changes: 32 additions & 0 deletions public/components/help/help-scratch3-nomodel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html>
<head>
<link href="help-scratch.css" rel="stylesheet">
<title>Using machine learning models</title>
</head>
<body>
<div>
<img class="help-block" valign="middle" src="../../static/images/scratch3-recognise-label-images.png" alt="recognise images">
</div>
<div class="help-section">
This block lets you use machine learning models in your Scratch projects.
However, when you tried to use it, it didn't work because <strong>you don't seem to have a machine learning model</strong>.
</div>
<div class="help-section">
There are a few reasons why this might have happened:
</div>
<div class="help-section">
<div class="help-item">
<span class="help-number">1.</span> You haven't trained a machine learning model yet.
</div>
<div class="help-item">
<span class="help-number">2.</span> You've started training a machine learning model, but it is still training and isn't ready yet.
</div>
<div class="help-item">
<span class="help-number">3.</span> 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).
</div>
</div>
<div class="help-section">
You can check this on the <strong>Learn &amp; Test</strong> page for your project.
</div>
</body>
</html>
7 changes: 7 additions & 0 deletions resources/scratch3-images-classify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/lib/training/visualrecognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
};


Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
}


Expand Down Expand Up @@ -1000,5 +1000,3 @@ export interface VisualRecogApiResponsePayloadClassifyFile {
}>;
readonly images_processed: number;
}


0 comments on commit adaec31

Please sign in to comment.