-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiModelClient.js
More file actions
70 lines (62 loc) · 2.64 KB
/
miniModelClient.js
File metadata and controls
70 lines (62 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This file contains the JavaScript code to interact with the
// MiniModelController API endpoints.
// 1. trainModel(): Sends a POST request to the /api/minilm/train endpoint
// to train the mini-LM with the text entered in the trainingText textarea.
// It updates the trainStatus div with the training status.
// 2. sampleModel(): Sends a GET request to the /api/minilm/sample endpoint
// with the prefix and maxTokens parameters.
// It updates the completionResult div with the generated completion and
// the tokenResult div with the list of tokens.
// 3. Error handling: Displays graceful error messages in the
// completionResult and trainStatus divs if the API requests fail.
function trainModel() {
const trainingText = document.getElementById("trainingText").value;
const trainStatusDiv = document.getElementById("trainStatus");
trainStatusDiv.innerHTML = "Training..."
fetch('/api/minilm/train', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(trainingText) //Sending the trainingText as the body
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return response.json();
})
.then(data => {
if (data.ok) {
trainStatusDiv.innerHTML = "Training complete!";
} else {
trainStatusDiv.innerHTML = `Training failed: ${data.error}`;
}
})
.catch(error => {
trainStatusDiv.innerHTML = `Error: ${error}`
})
}
function sampleModel() {
const prefix = document.getElementById("prefix").value;
const maxTokens = document.getElementById("maxTokens").value;
const completionResultDiv = document.getElementById("completionResult");
const tokenResultDiv = document.getElementById("tokenResult");
completionResultDiv.innerHTML = "Generating completion...";
tokenResultDiv.innerHTML = "";
fetch(`/api/minilm/sample?prefix=${prefix}&maxTokens=${maxTokens}`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
completionResultDiv.innerHTML = data.completion;
tokenResultDiv.innerHTML = data.tokens.join(", ");
})
.catch(error => {
completionResultDiv.innerHTML = `Error: ${error}`;
tokenResultDiv.innerHTML = "";
});
}