@webwriter/deep-learning-model 0.0.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/.github/workflows/main.yml +8 -0
  2. package/LICENSE +7 -0
  3. package/README.md +290 -0
  4. package/components/dataset-input/dataset-input.styles.ts +20 -0
  5. package/components/dataset-input/dataset-input.ts +196 -0
  6. package/components/inference-fields/class-input.ts +83 -0
  7. package/components/inference-fields/class-output.ts +62 -0
  8. package/components/inference-fields/image-input.ts +182 -0
  9. package/components/inference-fields/image.ts +82 -0
  10. package/components/inference-fields/inference-interfaces.ts +15 -0
  11. package/components/inference-fields/number-input.ts +62 -0
  12. package/components/inference-fields/number-output.ts +56 -0
  13. package/components/model-editor.ts +273 -0
  14. package/components/model-tests/bad-samples.ts +152 -0
  15. package/components/model-tests/loss-distribution.ts +174 -0
  16. package/components/reference-element-select.ts +105 -0
  17. package/custom-elements.json +1446 -0
  18. package/custom.d.ts +180 -0
  19. package/dist/widgets/webwriter-feature-engineering.css +444 -0
  20. package/dist/widgets/webwriter-feature-engineering.js +112241 -0
  21. package/dist/widgets/webwriter-model-designing.css +444 -0
  22. package/dist/widgets/webwriter-model-designing.js +113967 -0
  23. package/dist/widgets/webwriter-model-evaluation.css +444 -0
  24. package/dist/widgets/webwriter-model-evaluation.js +119019 -0
  25. package/dist/widgets/webwriter-model-prediction.css +444 -0
  26. package/dist/widgets/webwriter-model-prediction.js +109260 -0
  27. package/dist/widgets/webwriter-model-training.css +444 -0
  28. package/dist/widgets/webwriter-model-training.js +121391 -0
  29. package/editing-config.json +73 -0
  30. package/icon.svg +1 -0
  31. package/lib/dataset/class.ts +155 -0
  32. package/lib/dataset/dataset.ts +293 -0
  33. package/lib/dataset/feature.ts +110 -0
  34. package/lib/dataset/image.ts +312 -0
  35. package/lib/dataset/linear.ts +120 -0
  36. package/lib/layers/activation.ts +48 -0
  37. package/lib/layers/add.ts +50 -0
  38. package/lib/layers/attention.ts +74 -0
  39. package/lib/layers/concat.ts +50 -0
  40. package/lib/layers/conv.ts +83 -0
  41. package/lib/layers/dropout.ts +44 -0
  42. package/lib/layers/fcl.ts +66 -0
  43. package/lib/layers/flatten.ts +48 -0
  44. package/lib/layers/index.ts +708 -0
  45. package/lib/layers/input.ts +55 -0
  46. package/lib/layers/noise.ts +43 -0
  47. package/lib/layers/output.ts +64 -0
  48. package/lib/layers/reshape.ts +76 -0
  49. package/lib/layers/transposeconv.ts +90 -0
  50. package/lib/tflib.ts +6 -0
  51. package/lit-localize.json +47 -0
  52. package/localization/ar.xlf +532 -0
  53. package/localization/bg.xlf +531 -0
  54. package/localization/cs.xlf +532 -0
  55. package/localization/da.xlf +531 -0
  56. package/localization/de.xlf +531 -0
  57. package/localization/el.xlf +533 -0
  58. package/localization/es.xlf +532 -0
  59. package/localization/et.xlf +532 -0
  60. package/localization/fi.xlf +531 -0
  61. package/localization/fr.xlf +532 -0
  62. package/localization/generated/ar.ts +158 -0
  63. package/localization/generated/bg.ts +157 -0
  64. package/localization/generated/cs.ts +158 -0
  65. package/localization/generated/da.ts +157 -0
  66. package/localization/generated/de.ts +157 -0
  67. package/localization/generated/el.ts +159 -0
  68. package/localization/generated/es.ts +158 -0
  69. package/localization/generated/et.ts +158 -0
  70. package/localization/generated/fi.ts +157 -0
  71. package/localization/generated/fr.ts +158 -0
  72. package/localization/generated/hu.ts +157 -0
  73. package/localization/generated/id.ts +158 -0
  74. package/localization/generated/index.js +3 -0
  75. package/localization/generated/it.ts +158 -0
  76. package/localization/generated/ja.ts +157 -0
  77. package/localization/generated/ko.ts +157 -0
  78. package/localization/generated/locale-codes.js +83 -0
  79. package/localization/generated/lt.ts +157 -0
  80. package/localization/generated/lv.ts +157 -0
  81. package/localization/generated/nb.ts +157 -0
  82. package/localization/generated/nl.ts +157 -0
  83. package/localization/generated/pl.ts +158 -0
  84. package/localization/generated/pt-BR.ts +158 -0
  85. package/localization/generated/pt-PT.ts +158 -0
  86. package/localization/generated/ro.ts +158 -0
  87. package/localization/generated/ru.ts +158 -0
  88. package/localization/generated/sk.ts +158 -0
  89. package/localization/generated/sl.ts +158 -0
  90. package/localization/generated/sv.ts +157 -0
  91. package/localization/generated/tr.ts +157 -0
  92. package/localization/generated/uk.ts +157 -0
  93. package/localization/generated/zh-hans.ts +140 -0
  94. package/localization/generated/zh-hant.ts +140 -0
  95. package/localization/hu.xlf +531 -0
  96. package/localization/id.xlf +532 -0
  97. package/localization/it.xlf +532 -0
  98. package/localization/ja.xlf +531 -0
  99. package/localization/ko.xlf +531 -0
  100. package/localization/lt.xlf +531 -0
  101. package/localization/lv.xlf +531 -0
  102. package/localization/nb.xlf +531 -0
  103. package/localization/nl.xlf +531 -0
  104. package/localization/pkg/ar.xliff +7 -0
  105. package/localization/pkg/bg.xliff +7 -0
  106. package/localization/pkg/cs.xliff +7 -0
  107. package/localization/pkg/da.xliff +7 -0
  108. package/localization/pkg/de.xliff +7 -0
  109. package/localization/pkg/el.xliff +7 -0
  110. package/localization/pkg/es.xliff +7 -0
  111. package/localization/pkg/et.xliff +7 -0
  112. package/localization/pkg/fi.xliff +7 -0
  113. package/localization/pkg/fr.xliff +7 -0
  114. package/localization/pkg/hu.xliff +7 -0
  115. package/localization/pkg/id.xliff +7 -0
  116. package/localization/pkg/it.xliff +7 -0
  117. package/localization/pkg/ja.xliff +7 -0
  118. package/localization/pkg/ko.xliff +7 -0
  119. package/localization/pkg/lt.xliff +7 -0
  120. package/localization/pkg/lv.xliff +7 -0
  121. package/localization/pkg/nb.xliff +7 -0
  122. package/localization/pkg/nl.xliff +7 -0
  123. package/localization/pkg/pl.xliff +7 -0
  124. package/localization/pkg/pt-BR.xliff +7 -0
  125. package/localization/pkg/pt-PT.xliff +7 -0
  126. package/localization/pkg/ro.xliff +7 -0
  127. package/localization/pkg/ru.xliff +7 -0
  128. package/localization/pkg/sk.xliff +7 -0
  129. package/localization/pkg/sl.xliff +7 -0
  130. package/localization/pkg/sv.xliff +7 -0
  131. package/localization/pkg/tr.xliff +7 -0
  132. package/localization/pkg/uk.xliff +7 -0
  133. package/localization/pkg/zh-hans.xliff +7 -0
  134. package/localization/pkg/zh-hant.xliff +7 -0
  135. package/localization/pl.xlf +532 -0
  136. package/localization/pt-BR.xlf +532 -0
  137. package/localization/pt-PT.xlf +532 -0
  138. package/localization/ro.xlf +532 -0
  139. package/localization/ru.xlf +532 -0
  140. package/localization/sk.xlf +532 -0
  141. package/localization/sl.xlf +532 -0
  142. package/localization/snippets/ar.xliff +16 -0
  143. package/localization/snippets/bg.xliff +16 -0
  144. package/localization/snippets/cs.xliff +16 -0
  145. package/localization/snippets/da.xliff +16 -0
  146. package/localization/snippets/de.xliff +16 -0
  147. package/localization/snippets/el.xliff +16 -0
  148. package/localization/snippets/es.xliff +16 -0
  149. package/localization/snippets/et.xliff +16 -0
  150. package/localization/snippets/fi.xliff +16 -0
  151. package/localization/snippets/fr.xliff +16 -0
  152. package/localization/snippets/hu.xliff +16 -0
  153. package/localization/snippets/id.xliff +16 -0
  154. package/localization/snippets/it.xliff +16 -0
  155. package/localization/snippets/ja.xliff +16 -0
  156. package/localization/snippets/ko.xliff +16 -0
  157. package/localization/snippets/lt.xliff +16 -0
  158. package/localization/snippets/lv.xliff +16 -0
  159. package/localization/snippets/nb.xliff +16 -0
  160. package/localization/snippets/nl.xliff +16 -0
  161. package/localization/snippets/pl.xliff +16 -0
  162. package/localization/snippets/pt-BR.xliff +16 -0
  163. package/localization/snippets/pt-PT.xliff +16 -0
  164. package/localization/snippets/ro.xliff +16 -0
  165. package/localization/snippets/ru.xliff +16 -0
  166. package/localization/snippets/sk.xliff +16 -0
  167. package/localization/snippets/sl.xliff +16 -0
  168. package/localization/snippets/sv.xliff +16 -0
  169. package/localization/snippets/tr.xliff +16 -0
  170. package/localization/snippets/uk.xliff +16 -0
  171. package/localization/snippets/zh-hans.xliff +16 -0
  172. package/localization/snippets/zh-hant.xliff +16 -0
  173. package/localization/sv.xlf +531 -0
  174. package/localization/tr.xlf +531 -0
  175. package/localization/uk.xlf +531 -0
  176. package/localization/zh-hans.xlf +531 -0
  177. package/localization/zh-hant.xlf +531 -0
  178. package/package.json +104 -3
  179. package/snippets/MNIST.html +21 -0
  180. package/test.json +63 -0
  181. package/tsconfig.json +8 -0
  182. package/widgets/webwriter-feature-engineering.ts +421 -0
  183. package/widgets/webwriter-model-designing.ts +300 -0
  184. package/widgets/webwriter-model-evaluation.ts +215 -0
  185. package/widgets/webwriter-model-prediction.ts +307 -0
  186. package/widgets/webwriter-model-training.ts +684 -0
@@ -0,0 +1,300 @@
1
+ import { msg } from "@lit/localize";
2
+ import { SlButton, SlButtonGroup, SlIcon, SlIconButton, SlInput, SlOption, SlSelect } from "@shoelace-style/shoelace";
3
+ import { LitElementWw } from "@webwriter/lit";
4
+ import { css, html, TemplateResult } from "lit";
5
+ import { customElement, property, query } from "lit/decorators.js";
6
+ import { ModelEditor } from "../components/model-editor";
7
+ import { AiWidgetsReferenceSelect } from "../components/reference-element-select";
8
+ import LOCALIZE from "../localization/generated";
9
+ import { AiWidgetsFeatures } from "./webwriter-feature-engineering";
10
+
11
+ /**
12
+ * A widget to design AI models using a visual editor.
13
+ */
14
+ @customElement("webwriter-model-designing")
15
+ export class AiWidgetsDesigns extends LitElementWw {
16
+ /** @internal */
17
+ static scopedElements = {
18
+ "sl-button": SlButton,
19
+ "sl-button-group": SlButtonGroup,
20
+ "model-editor": ModelEditor,
21
+ "sl-option": SlOption,
22
+ "sl-select": SlSelect,
23
+ "sl-icon": SlIcon,
24
+ "sl-input": SlInput,
25
+ "sl-icon-button": SlIconButton,
26
+ "ref-select": AiWidgetsReferenceSelect
27
+ }
28
+
29
+ protected localize = LOCALIZE;
30
+
31
+ static styles = css`
32
+ :host {
33
+ display:block;
34
+ font-size: 0.9rem;
35
+ }
36
+ :host([contenteditable][editmode="hidden"]) {
37
+ min-height: 30px;
38
+ background:lightgray;
39
+ font-size: inherit;
40
+ }
41
+ :host div[part='options'] {
42
+ display:none;
43
+ }
44
+ :host([contenteditable]) div[part='options'] {
45
+ display:block;
46
+ }
47
+
48
+ #stats {
49
+ display: flex;
50
+ flex-direction: row;
51
+ flex-wrap: nowrap;
52
+ justify-content: flex-start;
53
+ align-items: center;
54
+ gap: 0px 20px;
55
+ margin-bottom: 10px;
56
+ }
57
+ #content {
58
+ position:relative;
59
+ }
60
+ #stats p {
61
+ margin: 0px;
62
+ }
63
+ #add {
64
+ background-color:rgba(255,255,255,0.8);
65
+ position: absolute;
66
+ bottom: 25px;
67
+ left:3px;
68
+ width: calc(100% - 24px);
69
+ display: flex;
70
+ flex-direction: row;
71
+ flex-wrap: nowrap;
72
+ justify-content: flex-start;
73
+ align-items: center;
74
+ align-content: flex-end;
75
+ gap: 5px 15px;
76
+ overflow: auto;
77
+ padding: 5px 10px;
78
+ }
79
+ #add > * {
80
+ flex-grow:1;
81
+ }
82
+ #add * > *{
83
+ width: 100%;
84
+ }
85
+ .group {
86
+ padding-top: 18px;
87
+ position:relative;
88
+ display: flex;
89
+ flex-direction: row;
90
+ align-items: flex-end;
91
+ gap:5px;
92
+ }
93
+ .group p {
94
+ position:absolute;
95
+ margin:0px;
96
+ font-size: 15px;
97
+ top: -2px;
98
+ }
99
+ #controls {
100
+ position:absolute;
101
+ right: 5px;
102
+ top: 5px;
103
+ z-index: 10;
104
+ }`
105
+
106
+ /**
107
+ * Stores the state of the model editor as a json string
108
+ */
109
+ @property({ type: String, attribute: true, reflect: true })
110
+ accessor state: string;
111
+
112
+ /**
113
+ * Stores the reference id of the widget where the dataset is used from
114
+ */
115
+ @property({ type: String, attribute: true, reflect: true })
116
+ accessor featureWidgetId: string;
117
+
118
+ @query("#feature-select")
119
+ private accessor refSelect: AiWidgetsReferenceSelect;
120
+
121
+ /**
122
+ * What students will be able to edit in the explorable. Ranging from "edit" = everything, "layer" = only layer settings, "read" = read only, to "hidden" = invisible to the students.
123
+ */
124
+ @property({ type: String, attribute: true, reflect: true })
125
+ accessor editMode: "edit"|"layer"|"read"|"hidden" = "edit";
126
+
127
+ /**
128
+ * The maximum number of total parameters allowed for training the model. Only checked by the training widget.
129
+ */
130
+ @property({ type: Number, attribute: true, reflect: true })
131
+ accessor maxModelParameters: number = 100000;
132
+
133
+ /**
134
+ * The maximum number of parameters allowed per layer to allow training the model.
135
+ */
136
+ @property({ type: Number, attribute: true, reflect: true })
137
+ accessor maxLayerParameters: number = 10000;
138
+
139
+ constructor() {
140
+ super();
141
+ let that = this;
142
+
143
+ setInterval(function () {
144
+ let nodes = that.currentCanvas.controler.getNumberOfOperations();
145
+ that.shadowRoot.getElementById("numberNodes").innerText = String(nodes);
146
+
147
+ let params = that.currentCanvas.controler.getNumberOfParameters();
148
+ that.shadowRoot.getElementById("params").innerText = String(params);
149
+ }, 100);
150
+
151
+ // Prevents the del hotkey from removing the widget
152
+ this.addEventListener("keydown", function (e) {
153
+ if (e.key == "Delete") {
154
+ e.preventDefault();
155
+ return false;
156
+ }
157
+ })
158
+ }
159
+
160
+ /**
161
+ * Called when the feature dataset source widget is changed.
162
+ * @param e Contains e.detail.id for the new widget providing the dataset
163
+ */
164
+ private _handleRefChange(e: CustomEvent) {
165
+ let that = this;
166
+ let featuresElem = that.refSelect.getRefElement() as AiWidgetsFeatures;
167
+ let datasetChangeListener = function () {
168
+ that.currentCanvas.setFields(featuresElem.getDataset().tensorDataset)
169
+ }
170
+
171
+ if (e.detail.lastRef != null)
172
+ e.detail.lastRef.removeEventListener("change", datasetChangeListener);
173
+
174
+ that.featureWidgetId = e.detail.id;
175
+ that.requestUpdate();
176
+ featuresElem = (that.refSelect.getRefElement() as AiWidgetsFeatures)
177
+ featuresElem.addEventListener("change", datasetChangeListener);
178
+ if (featuresElem.isReady && featuresElem.isReady()) {
179
+ datasetChangeListener()
180
+ }
181
+ if (this.state && this.state != "")
182
+ this.currentCanvas.setState(JSON.parse(this.state));
183
+ }
184
+
185
+ /**
186
+ * Reference to the current model editor canvas
187
+ */
188
+ @query("#model-editor")
189
+ accessor currentCanvas: ModelEditor;
190
+
191
+ /**
192
+ * Called when the model design inside the model editor was changed
193
+ * @param e
194
+ */
195
+ private _editorChange(e: CustomEvent) {
196
+ this.state = JSON.stringify(e.detail.state)
197
+ }
198
+
199
+ public render(): TemplateResult {
200
+ let that = this;
201
+ function editModeChanged(e: Event) {
202
+ const select = e.target as HTMLSelectElement;
203
+ this.editMode = select.value;
204
+ that.currentCanvas.editmode = that.editMode;
205
+ that.currentCanvas.requestUpdate();
206
+ }
207
+ function modelParametersChanged(e: Event) {
208
+ const input = e.target as SlInput;
209
+ that.maxModelParameters = parseInt(input.value);
210
+ }
211
+ function layerParametersChanged(e: Event) {
212
+ const input = e.target as SlInput;
213
+ that.maxLayerParameters = parseInt(input.value);
214
+ }
215
+
216
+ return html`
217
+ <div id="content" style=${this.editMode == "hidden" ? "display:none" : ""}>
218
+ <model-editor id="model-editor" @modelchange=${this._editorChange} editmode=${this.editMode} modelparams=${this.maxModelParameters} layerparams=${this.maxLayerParameters}></model-editor>
219
+ <div id="stats">
220
+ <p>${msg("Number of Operations:")} <span id="numberNodes"></span></p>
221
+ <p>${msg("Total Parameters:")} <span id="params"></span></p>
222
+ </div>
223
+ <div id="add" style="display:${this.editMode == "edit" ? "" : "none"}">
224
+ <div class="group">
225
+ <p>${msg("Add I/O Layer")}</p>
226
+ <sl-button @click=${() => this.currentCanvas.createLayer("input")}>${msg("Input")}</sl-button>
227
+ <sl-button @click=${() => this.currentCanvas.createLayer("output")}>${msg("Output")}</sl-button>
228
+ </div>
229
+ <div class="group">
230
+ <p>${msg("Add Trainable Layer")}</p>
231
+ <sl-button @click=${() => this.currentCanvas.createLayer("fcl")}>${msg("Linear")}</sl-button>
232
+ <sl-button @click=${() => this.currentCanvas.createLayer("conv")}>${msg("Convolution")}</sl-button>
233
+ <sl-button @click=${() => this.currentCanvas.createLayer("transposeconv")}>${msg("Transpose Convolution")}</sl-button>
234
+ </div>
235
+ <div class="group">
236
+ <p>${msg("Add misc. Layer")}</p>
237
+ <sl-button @click=${() => this.currentCanvas.createLayer("activation")}>${msg("Activation")}</sl-button>
238
+ <sl-button @click=${() => this.currentCanvas.createLayer("dropout")}>${msg("Dropout")}</sl-button>
239
+ <sl-button @click=${() => this.currentCanvas.createLayer("noise")}>${msg("Noise")}</sl-button>
240
+ </div>
241
+ <div class="group">
242
+ <p>${msg("Add Shaping Layer")}</p>
243
+ <sl-button @click=${() => this.currentCanvas.createLayer("flatten")}>${msg("Flatten")}</sl-button>
244
+ <sl-button @click=${() => this.currentCanvas.createLayer("concat")}>${msg("Concatenation")}</sl-button>
245
+ <sl-button @click=${() => this.currentCanvas.createLayer("reshape")}>${msg("Reshape")}</sl-button>
246
+ </div>
247
+ <div class="group">
248
+ <p>${msg("Add Combinational Layer")}</p>
249
+ <sl-button @click=${() => this.currentCanvas.createLayer("add")}>${msg("Addition")}</sl-button>
250
+ <sl-button @click=${() => this.currentCanvas.createLayer("attention")}>${msg("Attention")}</sl-button>
251
+ </div>
252
+ </div>
253
+ <div id="controls">
254
+ <sl-button @click=${() => this.zoomReset()}>${msg("Center View")}</sl-button>
255
+ <sl-tooltip content=${msg("Zoom In")}><sl-button @click=${() => this.zoomIn()}>+</sl-button></sl-tooltip>
256
+ <sl-tooltip content=${msg("Zoom Out")}><sl-button @click=${() => this.zoomOut()}>-</sl-button></sl-tooltip>
257
+ </div>
258
+ </div>
259
+ <p style=${this.editMode != "hidden" ? "display:none" : ""}>${msg("Model Design Widget hidden from students.")}</p>
260
+ <div part="options">
261
+ <p>${msg("Feature Source")}</p>
262
+ <ref-select id="feature-select" elementType="webwriter-feature-engineering" elemId=${this.featureWidgetId} typeName="Features" @refchange=${this._handleRefChange} ownerId=${this.id}></ref-select>
263
+ <sl-select label=${msg("Edit Permissions")} value=${this.editMode} @sl-change=${editModeChanged}>
264
+ <sl-option value="hidden">${msg("hide widget")}</sl-option>
265
+ <sl-option value="read">${msg("read only")}</sl-option>
266
+ <sl-option value="layer">${msg("properties only")}</sl-option>
267
+ <sl-option value="edit">${msg("edit everything")}</sl-option>
268
+ </sl-select>
269
+ <sl-input label=${msg("Max Model Parameters")} type="number" step="100" value=${this.maxModelParameters} min="1" max="1000000" @sl-change=${modelParametersChanged}></sl-input>
270
+ <sl-input label=${msg("Max Layer Parameters")} type="number" step="10" value=${this.maxLayerParameters} min="1" max="100000" @sl-change=${layerParametersChanged}></sl-input>
271
+ </div>`
272
+ }
273
+
274
+ /**
275
+ * Zoom the canvas in
276
+ */
277
+ public zoomIn() {
278
+ this.currentCanvas.controler.zoomIn();
279
+ }
280
+ /**
281
+ * Zoom the canvas out
282
+ */
283
+ public zoomOut() {
284
+ this.currentCanvas.controler.zoomOut();
285
+ }
286
+ /**
287
+ * Reset the zoom of the canvas
288
+ */
289
+ public zoomReset() {
290
+ this.currentCanvas.controler.zoomReset();
291
+ }
292
+
293
+ /**
294
+ * Exports the current model design
295
+ * @returns The model design
296
+ */
297
+ getModel() {
298
+ return this.currentCanvas.controler.export();
299
+ }
300
+ }
@@ -0,0 +1,215 @@
1
+ import { SlButton, SlButtonGroup, SlIcon, SlInput, SlOption, SlSelect } from "@shoelace-style/shoelace";
2
+ import { LitElementWw } from "@webwriter/lit";
3
+ import { css, html, TemplateResult } from "lit";
4
+ import { msg } from "@lit/localize";
5
+ import { customElement, property, query } from "lit/decorators.js";
6
+ import { AiWidgetsReferenceSelect } from "../components/reference-element-select";
7
+ import { AiWidgetsFeatures } from "./webwriter-feature-engineering";
8
+ //import {setWasmPaths, setThreadsCount} from '@tensorflow/tfjs-backend-wasm';
9
+ import { until } from 'lit-html/directives/until.js';
10
+ import { BadSamples } from '../components/model-tests/bad-samples';
11
+ import { LossDistribution } from '../components/model-tests/loss-distribution';
12
+ import LOCALIZE from "../localization/generated";
13
+ import { AiWidgetsTraining } from './webwriter-model-training';
14
+ //setWasmPaths('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@4.22.0/dist/');
15
+ //setThreadsCount(4);
16
+
17
+ import type * as TF from "@tensorflow/tfjs";
18
+ import getTF from "../lib/tflib";
19
+ let tf: typeof TF;
20
+ (async () => {tf = await getTF()})()
21
+
22
+ async function waitFor(condition: Function, interval = 10) {
23
+ if (condition())
24
+ return;
25
+ return new Promise(async function(res, rej) {
26
+ let inter = setInterval(function() {
27
+ if (condition()) {
28
+ clearInterval(inter)
29
+ res(null);
30
+ }
31
+ }, interval)
32
+ });
33
+ }
34
+
35
+ /**
36
+ * A widget that allows evaluating a trained model using various tests.
37
+ */
38
+ @customElement("webwriter-model-evaluation")
39
+ export class AiWidgetsTest extends LitElementWw {
40
+ /** @internal */
41
+ static scopedElements = {
42
+ "sl-button": SlButton,
43
+ "sl-button-group": SlButtonGroup,
44
+ "sl-option": SlOption,
45
+ "sl-select": SlSelect,
46
+ "sl-input":SlInput,
47
+ "sl-icon": SlIcon,
48
+ "ref-select": AiWidgetsReferenceSelect,
49
+ "loss-dist": LossDistribution,
50
+ "bad-samples": BadSamples
51
+ }
52
+
53
+ protected localize = LOCALIZE;
54
+
55
+ static styles = css`
56
+ #container {
57
+ display: flex;
58
+ flex-direction: row;
59
+ flex-wrap: wrap;
60
+ align-content: flex-start;
61
+ justify-content: center;
62
+ align-items: flex-start;
63
+ gap: 5px;
64
+ }
65
+ #container * {
66
+ flex-grow: 1;
67
+ }
68
+ :host div[part='options'] {
69
+ display:none;
70
+ }
71
+ :host([contenteditable]) div[part='options'] {
72
+ display:block;
73
+ }
74
+ #top {
75
+ width: 100%;
76
+ display: flex;
77
+ align-items: flex-end;
78
+ }
79
+ .input, .output {
80
+ padding: 5px 10px;
81
+ position: relative;
82
+ border: 1px solid lightgrey;
83
+ border-radius: 5px;
84
+ }
85
+ .input > *, .output > * {
86
+ width: 100%;
87
+ }
88
+ .input > p, .output > p {
89
+ margin: 0px;
90
+ font-size: 16px;
91
+ }
92
+ `
93
+
94
+ /**
95
+ * The id of the feature engineering widget to use as feature source.
96
+ */
97
+ @property({ type: String, attribute: true, reflect: true })
98
+ accessor featureWidgetId: string;
99
+
100
+ @query("#feature-select")
101
+ private accessor featureSelect: AiWidgetsReferenceSelect;
102
+
103
+ /**
104
+ * The id of the model training widget to use as model source.
105
+ */
106
+ @property({ type: String, attribute: true, reflect: true })
107
+ accessor modelWidgetId: string;
108
+
109
+ @query("#model-select")
110
+ private accessor modelSelect: AiWidgetsReferenceSelect;
111
+
112
+ @query("#epoch-select")
113
+ private accessor modelName: SlSelect;
114
+
115
+ @query("#test-select")
116
+ private accessor testSelect: SlSelect;
117
+
118
+ @query("#test-container")
119
+ private accessor testContainer: HTMLDivElement;
120
+
121
+ private testElem: TemplateResult;
122
+
123
+ @property({ type: String, attribute: true, reflect: true })
124
+ private accessor selectedModelName: string;
125
+
126
+ private model:TF.LayersModel;
127
+
128
+ constructor() {
129
+ super();
130
+ let that = this;
131
+ setTimeout(function() {
132
+ that.requestUpdate();
133
+ }, 3000)
134
+ }
135
+
136
+ /**
137
+ * Renders the model select field allowing the user to change the model checkpoint that is used for testing.
138
+ */
139
+ async getModelSelect() {
140
+ let that = this;
141
+ await waitFor(function() {return that.modelSelect != null && that.modelSelect.getRefElement != null && (that.modelSelect.getRefElement() as AiWidgetsTraining).getAvailableModels != null});
142
+ let models = (this.modelSelect.getRefElement() as AiWidgetsTraining).getAvailableModels();
143
+ return html`<sl-select label=${msg("Model version")} id="epoch-select" value=${this.selectedModelName} @sl-change=${this.selectedModelChange}>${models.map(model => html`<sl-option value=${model}>${model}</sl-option>`)}</sl-select>`;
144
+ }
145
+
146
+ public render(): TemplateResult {
147
+ if (this.selectedModelName == null || this.selectedModelName == "")
148
+ this.selectedModelName = "best";
149
+
150
+ return html`
151
+ ${until(this.getModelSelect(), html`${msg("Loading models...")}`)}
152
+ <sl-select label=${msg("Test / Evaluation")} id="test-select" @sl-change=${this.testChange}>
153
+ <sl-option value="loss-dist">${msg("Loss Distribution")}</sl-option>
154
+ <sl-option value="bad-samples">${msg("Worst Samples")}</sl-option>
155
+ </sl-select>
156
+ <div id="test-container">
157
+ ${this.testElem}
158
+ </div>
159
+ <div part="options">
160
+ <p>${msg("Feature Source")}</p>
161
+ <ref-select id="feature-select" typeName="Features" elemId=${this.featureWidgetId} elementType="webwriter-feature-engineering" @refchange=${this.featureRefChange} ownerId=${this.id}></ref-select>
162
+ <p>${msg("Model Source")}</p>
163
+ <ref-select id="model-select" typeName="Model" elemId=${this.modelWidgetId} elementType="webwriter-model-training" @refchange=${this.modelRefChange} ownerId=${this.id}></ref-select>
164
+ </div>`
165
+ }
166
+
167
+ private modelRefChange(e:CustomEvent) {
168
+ this.modelWidgetId = e.detail.id;
169
+
170
+ let that = this;
171
+ function modelUpdate() {
172
+ that.requestUpdate();
173
+ }
174
+
175
+ if (e.detail.lastRef != null)
176
+ e.detail.lastRef.removeEventListener("finished", modelUpdate);
177
+ this.modelSelect.getRefElement().addEventListener("finished", modelUpdate);
178
+ }
179
+
180
+ private featureRefChange(e:CustomEvent) {
181
+ this.featureWidgetId = e.detail.id;
182
+
183
+ let that = this;
184
+ function featuresUpdate() {
185
+ that.requestUpdate();
186
+ }
187
+
188
+ if (e.detail.lastRef != null)
189
+ e.detail.lastRef.removeEventListener("change", featuresUpdate);
190
+ this.modelSelect.getRefElement().addEventListener("change", featuresUpdate);
191
+ }
192
+
193
+ private async selectedModelChange() {
194
+ let name = this.modelName.value as string;
195
+ this.selectedModelName = name;
196
+ try {
197
+ this.model.dispose();
198
+ } catch(e) {}
199
+ this.model = await (this.modelSelect.getRefElement() as AiWidgetsTraining).getModel(name);
200
+ }
201
+
202
+ /** Renders the test results when the test changes. */
203
+ private async testChange() {
204
+ let test = this.testSelect.value as string;
205
+ let model = await (this.modelSelect.getRefElement() as AiWidgetsTraining).getModel(this.selectedModelName);
206
+ let {rawDataset, tensorDataset} = (this.featureSelect.getRefElement() as AiWidgetsFeatures).getDataset()
207
+ if (test == "loss-dist") {
208
+ this.testElem = html`<loss-dist .model=${model} .rawDataset=${rawDataset} .tensorDataset=${tensorDataset}></loss-dist>`
209
+ }
210
+ if (test == "bad-samples") {
211
+ this.testElem = html`<bad-samples .model=${model} .rawDataset=${rawDataset} .tensorDataset=${tensorDataset}></bad-samples>`
212
+ }
213
+ this.requestUpdate();
214
+ }
215
+ }