-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38664 from code-dot-org/model-manager
[AiLab] Model Manager Dialog and autogenerated design elements and code
- Loading branch information
Showing
14 changed files
with
254 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import $ from 'jquery'; | ||
import designMode from './designMode'; | ||
|
||
function generateCodeDesignElements(modelId, modelData) { | ||
var x = 20; | ||
var y = 20; | ||
var SPACER_PIXELS = 20; | ||
designMode.onInsertEvent(`var testValues = {};`); | ||
modelData.selectedFeatures.forEach(feature => { | ||
y = y + SPACER_PIXELS; | ||
var label = designMode.createElement('LABEL', x, y); | ||
label.textContent = feature + ':'; | ||
label.id = 'design_' + feature + '_label'; | ||
label.style.width = '300px'; | ||
y = y + SPACER_PIXELS; | ||
if (Object.keys(modelData.featureNumberKey).includes(feature)) { | ||
var selectId = feature + '_dropdown'; | ||
var select = designMode.createElement('DROPDOWN', x, y); | ||
select.id = 'design_' + selectId; | ||
// App Lab automatically addss "option 1" and "option 2", remove them. | ||
select.options.remove(0); | ||
select.options.remove(0); | ||
Object.keys(modelData.featureNumberKey[feature]).forEach(option => { | ||
var optionElement = document.createElement('option'); | ||
optionElement.text = option; | ||
select.options.add(optionElement); | ||
}); | ||
} else { | ||
var input = designMode.createElement('TEXT_INPUT'); | ||
input.id = 'design_' + feature + '_input'; | ||
} | ||
var addFeature = `testValues.${feature} = getText("${selectId}");`; | ||
designMode.onInsertEvent(addFeature); | ||
}); | ||
y = y + 2 * SPACER_PIXELS; | ||
var label = designMode.createElement('LABEL', x, y); | ||
label.textContent = modelData.labelColumn; | ||
// TODO: this could be problematic if the name isn't formatted appropriately | ||
label.id = 'design_' + modelData.name + '_label'; | ||
label.style.width = '300px'; | ||
y = y + SPACER_PIXELS; | ||
var predictionId = modelData.name + '_prediction'; | ||
var prediction = designMode.createElement('TEXT_INPUT', x, y); | ||
prediction.id = 'design_' + predictionId; | ||
y = y + 2 * SPACER_PIXELS; | ||
var predictButton = designMode.createElement('BUTTON', x, y); | ||
predictButton.textContent = 'Predict'; | ||
var predictButtonId = modelData.name + '_predict'; | ||
designMode.updateProperty(predictButton, 'id', predictButtonId); | ||
var predictOnClick = `onEvent("${predictButtonId}", "click", function() { | ||
getPrediction("${ | ||
modelData.name | ||
}", "${modelId}", testValues, function(value) { | ||
setText("${predictionId}", value); | ||
}); | ||
});`; | ||
designMode.onInsertEvent(predictOnClick); | ||
} | ||
|
||
export default function autogenerateML(modelId) { | ||
return new Promise(function(resolve, reject) { | ||
$.ajax({ | ||
url: `/api/v1/ml_models/${modelId}`, | ||
method: 'GET' | ||
}) | ||
.then(modelData => { | ||
generateCodeDesignElements(modelId, modelData); | ||
return resolve(); | ||
}) | ||
.fail((jqXhr, status) => { | ||
return alert({message: 'An error occurred'}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import $ from 'jquery'; | ||
import BaseDialog from '@cdo/apps/templates/BaseDialog'; | ||
import Button from '@cdo/apps/templates/Button'; | ||
|
||
export default class ModelManagerDialog extends React.Component { | ||
static propTypes = { | ||
isOpen: PropTypes.bool.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
autogenerateML: PropTypes.func | ||
}; | ||
|
||
state = { | ||
models: [], | ||
isImportPending: false | ||
}; | ||
|
||
componentDidMount() { | ||
this.getModelList(); | ||
} | ||
|
||
closeModelManager = () => { | ||
this.props.onClose(); | ||
}; | ||
|
||
getModelList = () => { | ||
$.ajax({ | ||
url: '/api/v1/ml_models/names', | ||
method: 'GET' | ||
}).then(models => { | ||
this.setState({models}); | ||
}); | ||
}; | ||
|
||
importMLModel = async () => { | ||
this.setState({isImportPending: true}); | ||
const modelId = this.root.value; | ||
await this.props.autogenerateML(modelId); | ||
this.setState({isImportPending: false}); | ||
this.closeModelManager(); | ||
}; | ||
|
||
render() { | ||
const {isOpen} = this.props; | ||
const noModels = this.state.models.length === 0; | ||
|
||
return ( | ||
<div> | ||
<BaseDialog | ||
isOpen={isOpen} | ||
handleClose={this.closeModelManager} | ||
useUpdatedStyles | ||
> | ||
<h2>Machine Learning Models</h2> | ||
<select name="model" ref={element => (this.root = element)}> | ||
{this.state.models.map(model => ( | ||
<option key={model.id} value={model.id}> | ||
{model.name} | ||
</option> | ||
))} | ||
</select> | ||
{noModels && <div>You have not trained any AI models yet.</div>} | ||
<Button | ||
text={'Import'} | ||
color={Button.ButtonColor.orange} | ||
onClick={this.importMLModel} | ||
disabled={noModels} | ||
isPending={this.state.isImportPending} | ||
pendingText={'Importing...'} | ||
/> | ||
<h3>Model card details will go here.</h3> | ||
</BaseDialog> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* global Promise */ | ||
|
||
import $ from 'jquery'; | ||
import {predict} from '@cdo/apps/MLTrainers'; | ||
|
||
export const commands = { | ||
async getPrediction(opts) { | ||
return new Promise((resolve, reject) => { | ||
$.ajax({ | ||
url: '/api/v1/ml_models/' + opts.modelId, | ||
method: 'GET' | ||
}) | ||
.then(modelData => { | ||
const predictParams = { | ||
...modelData, | ||
testData: opts.testValues | ||
}; | ||
const result = predict(predictParams); | ||
opts.callback(result); | ||
return resolve(); | ||
}) | ||
.fail((jqXhr, status) => { | ||
opts.callback('Error: prediction failed'); | ||
return reject({message: 'An error occurred'}); | ||
}); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.