Update static/demo/index.html
Browse files- static/demo/index.html +39 -1
static/demo/index.html
CHANGED
@@ -76,12 +76,15 @@
|
|
76 |
const marginBottom = 30;
|
77 |
const marginLeft = 30;
|
78 |
const nodeRadius = 3;
|
|
|
79 |
const svg = d3.select("#graph").append("svg")
|
80 |
.attr("viewBox", [0, 0, width, height])
|
81 |
.attr("style", "max-width: 100%; height: auto; font: 8px sans-serif;");
|
|
|
82 |
var tooltip = d3.select("body").append("div")
|
83 |
.attr("class", "tooltip")
|
84 |
.style("opacity", 0);
|
|
|
85 |
const renderGraph = (recommendations) => {
|
86 |
if( !recommendations ){
|
87 |
return ;
|
@@ -110,6 +113,7 @@
|
|
110 |
graphData.edges.push({ source: (i-1), target: i, type: 'input' });
|
111 |
}
|
112 |
}
|
|
|
113 |
// Adding nodes & edges for inclusion recommendations
|
114 |
if( recommendations['add'].length > 0 ){
|
115 |
for( j = 0; j < recommendations['add'].length; j++ ){
|
@@ -124,6 +128,7 @@
|
|
124 |
graphData.edges.push({ source: (i-1), target: (i+j+1), type: 'add' });
|
125 |
}
|
126 |
}
|
|
|
127 |
// Adding nodes & edges for removal recommendations
|
128 |
if( recommendations['remove'].length > 0 ){
|
129 |
// Showing only the first removal recommendation
|
@@ -136,24 +141,30 @@
|
|
136 |
type: 'remove'
|
137 |
});
|
138 |
}
|
|
|
139 |
// Convert edge references to actual node objects
|
140 |
graphData.edges = graphData.edges.map(edge => ({
|
141 |
source: graphData.nodes.find(n => n.id === edge.source),
|
142 |
target: graphData.nodes.find(n => n.id === edge.target)
|
143 |
}));
|
|
|
144 |
const { nodes, edges } = graphData;
|
|
|
145 |
// Prepare the ranges of values for the axes
|
146 |
const xDomain = d3.extent(nodes, d => d.x);
|
147 |
const yDomain = d3.extent(nodes, d => d.y);
|
148 |
const xPadding = 2
|
149 |
const yPadding = 2
|
|
|
150 |
// Prepare the scales for positional encoding.
|
151 |
const x = d3.scaleLinear()
|
152 |
.domain([xDomain[0]-xPadding,xDomain[1]+xPadding]).nice()
|
153 |
.range([marginLeft, width - marginRight]);
|
|
|
154 |
const y = d3.scaleLinear()
|
155 |
.domain([yDomain[0]-yPadding,yDomain[1]+yPadding]).nice()
|
156 |
.range([height - marginBottom, marginTop]);
|
|
|
157 |
// Create the axes.
|
158 |
svg.append("g")
|
159 |
.attr("transform", `translate(0,${height - marginBottom})`)
|
@@ -165,6 +176,7 @@
|
|
165 |
.attr("fill", "currentColor")
|
166 |
.attr("text-anchor", "end")
|
167 |
.text("Semantic dimension 1"));
|
|
|
168 |
svg.append("g")
|
169 |
.attr("transform", `translate(${marginLeft},0)`)
|
170 |
.call(d3.axisLeft(y))
|
@@ -175,6 +187,7 @@
|
|
175 |
.attr("fill", "currentColor")
|
176 |
.attr("text-anchor", "start")
|
177 |
.text("Semantic dimension 2"));
|
|
|
178 |
// Create the grid.
|
179 |
svg.append("g")
|
180 |
.attr("stroke", "#cccccc")
|
@@ -195,6 +208,7 @@
|
|
195 |
.attr("y2", d => 0.5 + y(d))
|
196 |
.attr("x1", marginLeft)
|
197 |
.attr("x2", width - marginRight));
|
|
|
198 |
// Add a layer of dots.
|
199 |
svg.append("g")
|
200 |
.attr("stroke-width", 2.5)
|
@@ -207,6 +221,7 @@
|
|
207 |
.attr("cx", d => x(d.x))
|
208 |
.attr("cy", d => y(d.y))
|
209 |
.attr("r", nodeRadius);
|
|
|
210 |
// Add a layer of labels.
|
211 |
svg.append("g")
|
212 |
.attr("font-family", "sans-serif")
|
@@ -246,6 +261,7 @@
|
|
246 |
.duration(50)
|
247 |
.style("opacity", 0);
|
248 |
});
|
|
|
249 |
// Adding edges
|
250 |
svg.append("g")
|
251 |
.selectAll("line")
|
@@ -258,14 +274,18 @@
|
|
258 |
.attr("x2", d => x(d.target.x)+(d.source.x>d.target.x?1.3*nodeRadius:nodeRadius*-1.3))
|
259 |
.attr("y2", d => y(d.target.y))
|
260 |
.style("stroke-dasharray", d => d.target.type == "add" ? "3,3" : "");
|
|
|
261 |
};
|
|
|
262 |
// ------------------------------------------------
|
|
|
263 |
// Init state
|
264 |
if( $( "#prompt" ).val() == "" ){
|
265 |
$( "#generate" ).attr( "disabled", true ) ;
|
266 |
}
|
267 |
var last_request = Date.now() - 60 * 60 * 1000 ;
|
268 |
var last_prompt = $( "#prompt" ).val().trim() ;
|
|
|
269 |
// Add recommendations to the prompt
|
270 |
function add_recommendation( p ){
|
271 |
preview_add_recommendation( p, "hide" )
|
@@ -273,6 +293,7 @@
|
|
273 |
$( "#recommendation" ).html( "" ) ;
|
274 |
$( "#prompt" ).trigger( "keyup" ) ; // Looking for recommendations after accepting a recommendation
|
275 |
}
|
|
|
276 |
// Preview for add recommendation
|
277 |
function preview_add_recommendation( p, flag ){
|
278 |
if( flag == "show" ){
|
@@ -282,6 +303,7 @@
|
|
282 |
$( "#prompt" ).val( $( "#prompt" ).val().replace( " " + p, "" ) ) ;
|
283 |
}
|
284 |
}
|
|
|
285 |
// Remove adversarial sentences from prompt
|
286 |
function remove_recommendation( p ){
|
287 |
$( "#prompt" ).val( $( "#prompt" ).val().replace( p, "" ) ) ;
|
@@ -289,6 +311,7 @@
|
|
289 |
$( "#recommendation" ).html( "" ) ;
|
290 |
$( "#prompt" ).trigger( "keyup" ) ; // Looking for recommendations after accepting a recommendation
|
291 |
}
|
|
|
292 |
// Preview for add recommendation
|
293 |
function preview_remove_recommendation( p, flag ){
|
294 |
if( flag == "show" ){
|
@@ -299,8 +322,10 @@
|
|
299 |
$( "#prompt" ).val( $( "#prompt" ).data( "previous_prompt" ) ) ;
|
300 |
}
|
301 |
}
|
|
|
302 |
// Listening to changes performed on the prompt input field
|
303 |
$( "#prompt" ).on( "keyup", function( e ){
|
|
|
304 |
// Updating the generate button state based on prompt length
|
305 |
if( $( "#prompt" ).val().length > 0 ){
|
306 |
$( "#generate" ).removeAttr( "disabled" ) ;
|
@@ -308,21 +333,25 @@
|
|
308 |
else{
|
309 |
$( "#generate" ).attr( "disabled", true ) ;
|
310 |
}
|
|
|
311 |
// Minimum timeout between the requests
|
312 |
if( Date.now() - last_request > 500 && last_prompt != $( "#prompt" ).val().trim() ){
|
313 |
last_request = Date.now() ;
|
314 |
last_prompt = $( "#prompt" ).val().trim() ;
|
315 |
// Getting the last typed char
|
316 |
var last_char = $( "#prompt" ).val().trim().slice( -1 ) ;
|
|
|
317 |
// Triggering the API request when ending of a sentence is found, e.g., .?!
|
318 |
if( last_char == "." || last_char == "?" || last_char == "!" ){
|
319 |
$( "#recommendation" ).html( 'Requesting recommendations: <div class="bx--tag bx--tag--gray bx--tag--deletable">...</div>' ) ;
|
320 |
var api_url = "/recommend?prompt="
|
321 |
// var api_url = "/recommend_local?prompt="
|
322 |
var p = $( "#prompt" ).val() ;
|
|
|
323 |
// API request
|
324 |
$.getJSON( api_url + encodeURI( p ), function( data ) {
|
325 |
$( "#recommendation" ).html( "Recommendations: " ) ;
|
|
|
326 |
// Looking first for removal recommendations
|
327 |
// if( data["remove"].length > 0 && data["remove"][0].similarity > 0.5 ){
|
328 |
if( data["remove"].length > 0 ){
|
@@ -335,6 +364,7 @@
|
|
335 |
break ; // Showing only once removal recommendation at a time
|
336 |
}
|
337 |
}
|
|
|
338 |
// else if( data["add"].length > 0 ){ // After the removal recommendations are dealt with, then we show recommendations for inclusion
|
339 |
if( data["add"].length > 0 ){ // Think Demo UX
|
340 |
for( var i = 0; i < data["add"].length; i++ ){
|
@@ -347,16 +377,19 @@
|
|
347 |
}
|
348 |
}
|
349 |
}
|
|
|
350 |
// User status message about recommendations found
|
351 |
if( data["add"].length == 0 && data["remove"].length == 0 ){
|
352 |
$( "#recommendation" ).html( "No recommendations found." ) ;
|
353 |
}
|
|
|
354 |
$("#prompt").data( "recommendations", data );
|
355 |
// renderGraph(data);
|
356 |
});
|
357 |
}
|
358 |
}
|
359 |
});
|
|
|
360 |
// Generation request
|
361 |
$( "#demo" ).on( "submit", function(e){ // Hugging Face
|
362 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
@@ -372,6 +405,7 @@
|
|
372 |
}
|
373 |
setTimeout( loading_animation, 500 );
|
374 |
} )()
|
|
|
375 |
$.ajax({
|
376 |
url: encodeURI("/demo_inference?prompt=" + $("#prompt").val()),
|
377 |
dataType: 'json',
|
@@ -381,15 +415,18 @@
|
|
381 |
console.log(data)
|
382 |
// Resetting the status of the button
|
383 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
|
|
384 |
// Clearing the previous timeout
|
385 |
if( $( "#demo" ).data( "timeoutId" ) != "" ){
|
386 |
clearTimeout( $( "#demo" ).data( "timeoutId" ) );
|
387 |
$( "#demo" ).data( "timeoutId", "" ) ;
|
388 |
}
|
|
|
389 |
out = data.content.split("");
|
390 |
model_id = data.model_id;
|
391 |
temperature = data.temperature
|
392 |
max_new_tokens = data.max_new_tokens
|
|
|
393 |
$( "#outcome" ).append( "\n\n+ ------------------------------------\n| Model: " + model_id + "\n| Temperature: " + temperature + "\n| Max new tokens: " + max_new_tokens + "\n+ ------------------------------------\n\n" ) ;
|
394 |
// Animating the generated output
|
395 |
( function typing_animation(){
|
@@ -406,10 +443,11 @@
|
|
406 |
$( "#outcome" ).val(out);
|
407 |
}
|
408 |
})
|
|
|
409 |
// Returning false so the form keeps user in the same page
|
410 |
return false;
|
411 |
});
|
412 |
</script>
|
413 |
<!-- <script src="https://unpkg.com/carbon-components/scripts/carbon-components.min.js"></script> -->
|
414 |
</body>
|
415 |
-
</html>
|
|
|
76 |
const marginBottom = 30;
|
77 |
const marginLeft = 30;
|
78 |
const nodeRadius = 3;
|
79 |
+
|
80 |
const svg = d3.select("#graph").append("svg")
|
81 |
.attr("viewBox", [0, 0, width, height])
|
82 |
.attr("style", "max-width: 100%; height: auto; font: 8px sans-serif;");
|
83 |
+
|
84 |
var tooltip = d3.select("body").append("div")
|
85 |
.attr("class", "tooltip")
|
86 |
.style("opacity", 0);
|
87 |
+
|
88 |
const renderGraph = (recommendations) => {
|
89 |
if( !recommendations ){
|
90 |
return ;
|
|
|
113 |
graphData.edges.push({ source: (i-1), target: i, type: 'input' });
|
114 |
}
|
115 |
}
|
116 |
+
|
117 |
// Adding nodes & edges for inclusion recommendations
|
118 |
if( recommendations['add'].length > 0 ){
|
119 |
for( j = 0; j < recommendations['add'].length; j++ ){
|
|
|
128 |
graphData.edges.push({ source: (i-1), target: (i+j+1), type: 'add' });
|
129 |
}
|
130 |
}
|
131 |
+
|
132 |
// Adding nodes & edges for removal recommendations
|
133 |
if( recommendations['remove'].length > 0 ){
|
134 |
// Showing only the first removal recommendation
|
|
|
141 |
type: 'remove'
|
142 |
});
|
143 |
}
|
144 |
+
|
145 |
// Convert edge references to actual node objects
|
146 |
graphData.edges = graphData.edges.map(edge => ({
|
147 |
source: graphData.nodes.find(n => n.id === edge.source),
|
148 |
target: graphData.nodes.find(n => n.id === edge.target)
|
149 |
}));
|
150 |
+
|
151 |
const { nodes, edges } = graphData;
|
152 |
+
|
153 |
// Prepare the ranges of values for the axes
|
154 |
const xDomain = d3.extent(nodes, d => d.x);
|
155 |
const yDomain = d3.extent(nodes, d => d.y);
|
156 |
const xPadding = 2
|
157 |
const yPadding = 2
|
158 |
+
|
159 |
// Prepare the scales for positional encoding.
|
160 |
const x = d3.scaleLinear()
|
161 |
.domain([xDomain[0]-xPadding,xDomain[1]+xPadding]).nice()
|
162 |
.range([marginLeft, width - marginRight]);
|
163 |
+
|
164 |
const y = d3.scaleLinear()
|
165 |
.domain([yDomain[0]-yPadding,yDomain[1]+yPadding]).nice()
|
166 |
.range([height - marginBottom, marginTop]);
|
167 |
+
|
168 |
// Create the axes.
|
169 |
svg.append("g")
|
170 |
.attr("transform", `translate(0,${height - marginBottom})`)
|
|
|
176 |
.attr("fill", "currentColor")
|
177 |
.attr("text-anchor", "end")
|
178 |
.text("Semantic dimension 1"));
|
179 |
+
|
180 |
svg.append("g")
|
181 |
.attr("transform", `translate(${marginLeft},0)`)
|
182 |
.call(d3.axisLeft(y))
|
|
|
187 |
.attr("fill", "currentColor")
|
188 |
.attr("text-anchor", "start")
|
189 |
.text("Semantic dimension 2"));
|
190 |
+
|
191 |
// Create the grid.
|
192 |
svg.append("g")
|
193 |
.attr("stroke", "#cccccc")
|
|
|
208 |
.attr("y2", d => 0.5 + y(d))
|
209 |
.attr("x1", marginLeft)
|
210 |
.attr("x2", width - marginRight));
|
211 |
+
|
212 |
// Add a layer of dots.
|
213 |
svg.append("g")
|
214 |
.attr("stroke-width", 2.5)
|
|
|
221 |
.attr("cx", d => x(d.x))
|
222 |
.attr("cy", d => y(d.y))
|
223 |
.attr("r", nodeRadius);
|
224 |
+
|
225 |
// Add a layer of labels.
|
226 |
svg.append("g")
|
227 |
.attr("font-family", "sans-serif")
|
|
|
261 |
.duration(50)
|
262 |
.style("opacity", 0);
|
263 |
});
|
264 |
+
|
265 |
// Adding edges
|
266 |
svg.append("g")
|
267 |
.selectAll("line")
|
|
|
274 |
.attr("x2", d => x(d.target.x)+(d.source.x>d.target.x?1.3*nodeRadius:nodeRadius*-1.3))
|
275 |
.attr("y2", d => y(d.target.y))
|
276 |
.style("stroke-dasharray", d => d.target.type == "add" ? "3,3" : "");
|
277 |
+
|
278 |
};
|
279 |
+
|
280 |
// ------------------------------------------------
|
281 |
+
|
282 |
// Init state
|
283 |
if( $( "#prompt" ).val() == "" ){
|
284 |
$( "#generate" ).attr( "disabled", true ) ;
|
285 |
}
|
286 |
var last_request = Date.now() - 60 * 60 * 1000 ;
|
287 |
var last_prompt = $( "#prompt" ).val().trim() ;
|
288 |
+
|
289 |
// Add recommendations to the prompt
|
290 |
function add_recommendation( p ){
|
291 |
preview_add_recommendation( p, "hide" )
|
|
|
293 |
$( "#recommendation" ).html( "" ) ;
|
294 |
$( "#prompt" ).trigger( "keyup" ) ; // Looking for recommendations after accepting a recommendation
|
295 |
}
|
296 |
+
|
297 |
// Preview for add recommendation
|
298 |
function preview_add_recommendation( p, flag ){
|
299 |
if( flag == "show" ){
|
|
|
303 |
$( "#prompt" ).val( $( "#prompt" ).val().replace( " " + p, "" ) ) ;
|
304 |
}
|
305 |
}
|
306 |
+
|
307 |
// Remove adversarial sentences from prompt
|
308 |
function remove_recommendation( p ){
|
309 |
$( "#prompt" ).val( $( "#prompt" ).val().replace( p, "" ) ) ;
|
|
|
311 |
$( "#recommendation" ).html( "" ) ;
|
312 |
$( "#prompt" ).trigger( "keyup" ) ; // Looking for recommendations after accepting a recommendation
|
313 |
}
|
314 |
+
|
315 |
// Preview for add recommendation
|
316 |
function preview_remove_recommendation( p, flag ){
|
317 |
if( flag == "show" ){
|
|
|
322 |
$( "#prompt" ).val( $( "#prompt" ).data( "previous_prompt" ) ) ;
|
323 |
}
|
324 |
}
|
325 |
+
|
326 |
// Listening to changes performed on the prompt input field
|
327 |
$( "#prompt" ).on( "keyup", function( e ){
|
328 |
+
|
329 |
// Updating the generate button state based on prompt length
|
330 |
if( $( "#prompt" ).val().length > 0 ){
|
331 |
$( "#generate" ).removeAttr( "disabled" ) ;
|
|
|
333 |
else{
|
334 |
$( "#generate" ).attr( "disabled", true ) ;
|
335 |
}
|
336 |
+
|
337 |
// Minimum timeout between the requests
|
338 |
if( Date.now() - last_request > 500 && last_prompt != $( "#prompt" ).val().trim() ){
|
339 |
last_request = Date.now() ;
|
340 |
last_prompt = $( "#prompt" ).val().trim() ;
|
341 |
// Getting the last typed char
|
342 |
var last_char = $( "#prompt" ).val().trim().slice( -1 ) ;
|
343 |
+
|
344 |
// Triggering the API request when ending of a sentence is found, e.g., .?!
|
345 |
if( last_char == "." || last_char == "?" || last_char == "!" ){
|
346 |
$( "#recommendation" ).html( 'Requesting recommendations: <div class="bx--tag bx--tag--gray bx--tag--deletable">...</div>' ) ;
|
347 |
var api_url = "/recommend?prompt="
|
348 |
// var api_url = "/recommend_local?prompt="
|
349 |
var p = $( "#prompt" ).val() ;
|
350 |
+
|
351 |
// API request
|
352 |
$.getJSON( api_url + encodeURI( p ), function( data ) {
|
353 |
$( "#recommendation" ).html( "Recommendations: " ) ;
|
354 |
+
|
355 |
// Looking first for removal recommendations
|
356 |
// if( data["remove"].length > 0 && data["remove"][0].similarity > 0.5 ){
|
357 |
if( data["remove"].length > 0 ){
|
|
|
364 |
break ; // Showing only once removal recommendation at a time
|
365 |
}
|
366 |
}
|
367 |
+
|
368 |
// else if( data["add"].length > 0 ){ // After the removal recommendations are dealt with, then we show recommendations for inclusion
|
369 |
if( data["add"].length > 0 ){ // Think Demo UX
|
370 |
for( var i = 0; i < data["add"].length; i++ ){
|
|
|
377 |
}
|
378 |
}
|
379 |
}
|
380 |
+
|
381 |
// User status message about recommendations found
|
382 |
if( data["add"].length == 0 && data["remove"].length == 0 ){
|
383 |
$( "#recommendation" ).html( "No recommendations found." ) ;
|
384 |
}
|
385 |
+
|
386 |
$("#prompt").data( "recommendations", data );
|
387 |
// renderGraph(data);
|
388 |
});
|
389 |
}
|
390 |
}
|
391 |
});
|
392 |
+
|
393 |
// Generation request
|
394 |
$( "#demo" ).on( "submit", function(e){ // Hugging Face
|
395 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
|
|
405 |
}
|
406 |
setTimeout( loading_animation, 500 );
|
407 |
} )()
|
408 |
+
|
409 |
$.ajax({
|
410 |
url: encodeURI("/demo_inference?prompt=" + $("#prompt").val()),
|
411 |
dataType: 'json',
|
|
|
415 |
console.log(data)
|
416 |
// Resetting the status of the button
|
417 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
418 |
+
|
419 |
// Clearing the previous timeout
|
420 |
if( $( "#demo" ).data( "timeoutId" ) != "" ){
|
421 |
clearTimeout( $( "#demo" ).data( "timeoutId" ) );
|
422 |
$( "#demo" ).data( "timeoutId", "" ) ;
|
423 |
}
|
424 |
+
|
425 |
out = data.content.split("");
|
426 |
model_id = data.model_id;
|
427 |
temperature = data.temperature
|
428 |
max_new_tokens = data.max_new_tokens
|
429 |
+
|
430 |
$( "#outcome" ).append( "\n\n+ ------------------------------------\n| Model: " + model_id + "\n| Temperature: " + temperature + "\n| Max new tokens: " + max_new_tokens + "\n+ ------------------------------------\n\n" ) ;
|
431 |
// Animating the generated output
|
432 |
( function typing_animation(){
|
|
|
443 |
$( "#outcome" ).val(out);
|
444 |
}
|
445 |
})
|
446 |
+
|
447 |
// Returning false so the form keeps user in the same page
|
448 |
return false;
|
449 |
});
|
450 |
</script>
|
451 |
<!-- <script src="https://unpkg.com/carbon-components/scripts/carbon-components.min.js"></script> -->
|
452 |
</body>
|
453 |
+
</html>
|