Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Central add metadata info #674

Merged
merged 15 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
*/
package ai.djl.serving.central.responseencoder;

import ai.djl.util.JsonUtils;
import ai.djl.modality.Classifications;
import ai.djl.modality.Classifications.ClassificationsSerializer;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.repository.Metadata;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
Expand All @@ -26,6 +33,7 @@
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
import java.lang.reflect.Modifier;

/**
* serialize to json and send the response to the client.
Expand All @@ -34,6 +42,26 @@
*/
public class JsonResponse {

private static final Gson GSON_WITH_TRANSIENT_FIELDS =
new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.setPrettyPrinting()
.excludeFieldsWithModifiers(Modifier.STATIC)
.registerTypeAdapter(Classifications.class, new ClassificationsSerializer())
.registerTypeAdapter(DetectedObjects.class, new ClassificationsSerializer())
.registerTypeAdapter(Metadata.class, new MetaDataSerializer())
.registerTypeAdapter(
Double.class,
(JsonSerializer<Double>)
(src, t, ctx) -> {
long v = src.longValue();
if (src.equals(Double.valueOf(String.valueOf(v)))) {
return new JsonPrimitive(v);
}
return new JsonPrimitive(src);
})
.create();

/**
* send a response to the client.
*
Expand All @@ -42,7 +70,8 @@ public class JsonResponse {
* @param entity the response
*/
public void send(ChannelHandlerContext ctx, FullHttpRequest request, Object entity) {
String serialized = JsonUtils.GSON_PRETTY.toJson(entity);

String serialized = GSON_WITH_TRANSIENT_FIELDS.toJson(entity);
ByteBuf buffer = ctx.alloc().buffer(serialized.length());
buffer.writeCharSequence(serialized, CharsetUtil.UTF_8);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.serving.central.responseencoder;

import ai.djl.repository.Metadata;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.text.DateFormat;

/**
* json custom serializer for Artifact.Metadata. custom serializer to avoid cyclic serialisation.
*
* @author erik.bamberg@web.de
*/
public class MetaDataSerializer implements JsonSerializer<Metadata> {

/** {@inheritDoc}} */
@Override
public JsonElement serialize(Metadata metadata, Type type, JsonSerializationContext ctx) {
DateFormat dateFormat = DateFormat.getInstance();
JsonObject jsonMetadata = new JsonObject();

jsonMetadata.addProperty("artifactId", metadata.getArtifactId());
jsonMetadata.addProperty("groupId", metadata.getGroupId());
jsonMetadata.addProperty("description", metadata.getDescription());
jsonMetadata.addProperty("website", metadata.getWebsite());
jsonMetadata.addProperty("resourceType", metadata.getResourceType());
jsonMetadata.addProperty("lastUpdated", dateFormat.format(metadata.getLastUpdated()));
JsonObject licenses = new JsonObject();
metadata.getLicenses()
.forEach(
(k, l) -> {
licenses.addProperty(k, l.getName());
});
jsonMetadata.add("licenses", licenses);

return jsonMetadata;
}
}
3 changes: 2 additions & 1 deletion central/src/main/webapp/components/DynForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ export default function DynForm(props) {

return (
<>
{Object.keys(props.data).map((key) => (
{Object.keys(props.data).filter(key=>key!=='licenses').map((key) => (
<div >
<TextField
id={key}
label={key}
key={props.data[key]}
fullWidth={true}
defaultValue={props.data[key]}
InputProps={{
readOnly: true,
Expand Down
46 changes: 25 additions & 21 deletions central/src/main/webapp/components/ModelView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,22 @@ import { theme } from '../css/useStyles'
const useStyles = makeStyles((theme) => ({
model_view_root: {

display: 'flex',
flexWrap: 'wrap',
justifyContent: 'space-around',
overflow: 'hidden',

order: 3,
flex: '2 1 auto',
alignSelf: 'stretch',
height: "95%",
maxHeight: '30em',

},
model_view_paper: {
minWidth: "60%",
height: "100%",
minHeight: '80%',

minHeight: '600px',
padding: '20px',
overflowY: "auto",
marginTop: '2em',
marginRight: 'auto',
marginLeft: '2em',

marginBottom: '5vh',

},


Expand Down Expand Up @@ -90,11 +81,15 @@ const useStyles = makeStyles((theme) => ({
},
tabbar: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,

display: 'flex',
height: "100%",
},
tabpanel: {
flex: '2 1 auto',
alignSelf: 'stretch',
},
dynform: {
width: '100%',
marginLeft: "2em",
},
}));
Expand Down Expand Up @@ -173,6 +168,7 @@ export default function ModelView(props) {
<div className={classes.model_view_root}>
<Paper ref={myRef} elevation={3} className={classes.model_view_paper} >
<h2>{props.model.name}</h2>
<h3>{props.model.metadata.groupId}:{props.model.metadata.artifactId}:{props.model.version}</h3>
<Chip size="small" label={props.model.properties.dataset} />
<Chip size="small" label={props.model.version} />
<div className={classes.tabbar}>
Expand All @@ -185,20 +181,22 @@ export default function ModelView(props) {
aria-label="{props.model.name}">

<Tab label="General" {...a11yProps(0)} />
<Tab label="Properties" {...a11yProps(1)} />
<Tab label="Arguments" {...a11yProps(2)} />
<Tab label="Parameters" {...a11yProps(3)} />
<Tab label="Synset" {...a11yProps(4)} />
<Tab label="Metadata" {...a11yProps(1)} />
<Tab label="Properties" {...a11yProps(2)} />
<Tab label="Arguments" {...a11yProps(3)} />
<Tab label="Parameters" {...a11yProps(4)} />
<Tab label="Synset" {...a11yProps(5)} />
</Tabs>

<TabPanel value={index} index={0} >
<TabPanel value={index} index={0} className={classes.tabpanel}>
<>
{Object.keys(props.model).filter((key) => !(typeof props.model[key] === 'object')).map((key) => (
<div >
<TextField
id={key}
label={key}
key={props.model[key]}
fullWidth={true}
defaultValue={props.model[key]}
InputProps={{
readOnly: true,
Expand All @@ -214,6 +212,7 @@ export default function ModelView(props) {
id={'dataset1'}
label={'dataset'}
key={props.model.properties.dataset}
fullWidth={true}
defaultValue={props.model.properties.dataset}
InputProps={{
readOnly: true,
Expand All @@ -225,22 +224,27 @@ export default function ModelView(props) {

</>
</TabPanel>
<TabPanel value={index} index={1}>
<TabPanel value={index} index={1} className={classes.tabpanel}>
<DynForm data={props.model.metadata} />
<h4>Licenses</h4>
<DynForm data={props.model.metadata.licenses}/>
</TabPanel>
<TabPanel value={index} index={2} className={classes.tabpanel}>
<DynForm data={props.model.properties} />
</TabPanel>
<TabPanel value={index} index={2}>
<TabPanel value={index} index={3} className={classes.tabpanel}>
{props.model.arguments
? <DynForm data={props.model.arguments} />
: <DynForm data={noArguments}/>
}
</TabPanel>
<TabPanel value={index} index={3}>
<TabPanel value={index} index={4} className={classes.tabpanel}>
{props.model.files.parameters
? <DynForm data={props.model.files.parameters}/>
: <DynForm data={noParameters}/>
}
</TabPanel>
<TabPanel value={index} index={4}>
<TabPanel value={index} index={5} className={classes.tabpanel}>
{props.model.files.synset
? <DynForm data={props.model.files.synset}/>
: <DynForm data={noSynset}/>
Expand Down