lollms-webui/static/js/settings.js

392 lines
13 KiB
JavaScript
Raw Normal View History

2023-04-23 19:05:39 +00:00
function selectOptionByText(selectElement, optionText) {
for (let i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].text === optionText) {
selectElement.selectedIndex = i;
break;
}
}
}
2023-04-13 10:31:48 +00:00
fetch('/settings')
.then(response => response.text())
.then(html => {
document.getElementById('settings').innerHTML = html;
2023-05-25 21:24:14 +00:00
bindingInput = document.getElementById('binding');
2023-04-13 10:31:48 +00:00
modelInput = document.getElementById('model');
2023-04-20 20:33:10 +00:00
personalityLanguageInput = document.getElementById('personalities_language');
personalityCategoryInput = document.getElementById('personalities_category');
2023-04-14 00:10:22 +00:00
personalityInput = document.getElementById('personalities');
languageInput = document.getElementById('language');
2023-04-14 08:48:14 +00:00
voiceInput = document.getElementById('voice');
2023-04-13 10:31:48 +00:00
seedInput = document.getElementById('seed');
tempInput = document.getElementById('temperature');
2023-04-13 10:31:48 +00:00
nPredictInput = document.getElementById('n-predict');
topKInput = document.getElementById('top-k');
topPInput = document.getElementById('top-p');
repeatPenaltyInput = document.getElementById('repeat-penalty');
repeatLastNInput = document.getElementById('repeat-last-n');
temperatureValue = document.getElementById('temperature-value');
n_predictValue = document.getElementById('n-predict-value');
topkValue = document.getElementById('top-k-value');
toppValue = document.getElementById('top-p-value');
repeatPenaltyValue = document.getElementById('repeat-penalty-value');
repeatLastNValue = document.getElementById('repeat-last-n');
2023-04-23 19:05:39 +00:00
function update_config(){
fetch('/get_config')
.then((response) => response.json())
.then((data) => {
console.log("Received config")
console.log(data);
2023-05-25 21:24:14 +00:00
selectOptionByText(bindingInput, data["binding"])
2023-04-23 19:05:39 +00:00
selectOptionByText(modelInput, data["model"])
selectOptionByText(personalityLanguageInput, data["personality_language"])
selectOptionByText(personalityCategoryInput, data["personality_category"])
selectOptionByText(personalityInput, data["personality"])
languageInput.value = data["language"]
voiceInput.value = data["voice"]
seedInput.value = data["seed"]
tempInput.value = data["temperature"]
2023-04-23 19:05:39 +00:00
nPredictInput.value = data["n_predict"]
topKInput.value = data["top_k"]
topPInput.value = data["top_p"]
repeatPenaltyInput.textContent = data["repeat_penalty"]
repeatLastNInput.textContent = data["repeat_last_n"]
temperatureValue.textContent =`Temperature(${data["temperature"]})`
2023-04-23 19:05:39 +00:00
n_predictValue.textContent =`N Predict(${data["n_predict"]})`
topkValue.textContent =`Top-K(${data["top_k"]})`
toppValue.textContent =`Top-P(${data["top_p"]})`
repeatPenaltyValue.textContent =`Repeat penalty(${data["repeat_penalty"]})`
repeatLastNValue.textContent =`Repeat last N(${data["repeat_last_n"]})`
})
.catch((error) => {
console.error('Error:', error);
});
}
2023-04-13 10:31:48 +00:00
2023-05-25 21:24:14 +00:00
bindingInput.addEventListener('input',() => {
console.log(`Binding (${bindingInput.value})`)
// Use fetch to send form values to Flask endpoint
2023-05-25 21:24:14 +00:00
fetch('/set_binding', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
2023-05-25 21:24:14 +00:00
body: JSON.stringify({"binding":bindingInput.value}),
})
.then((response) => response.json())
.then((data) => {
console.log(data);
2023-04-23 22:19:15 +00:00
if(data["status"]==="no_models_found"){
2023-05-25 21:24:14 +00:00
alert("No models found for this binding. Make sure you select a binding that you have models for or download models from links in our repository")
2023-04-23 22:19:15 +00:00
}
else{
populate_settings();
2023-05-25 21:24:14 +00:00
alert("Binding set successfully")
2023-04-23 22:19:15 +00:00
}
2023-04-23 19:05:39 +00:00
})
.catch((error) => {
console.error('Error:', error);
alert("Error setting configuration")
});
})
modelInput.addEventListener('input',() => {
console.log(`Model (${modelInput.value})`)
// Use fetch to send form values to Flask endpoint
fetch('/set_model', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({"model":modelInput.value}),
})
.then((response) => response.json())
.then((data) => {
console.log(data);
populate_settings();
2023-05-25 21:24:14 +00:00
alert("Binding set successfully")
})
.catch((error) => {
console.error('Error:', error);
alert("Error setting configuration")
});
})
2023-04-23 19:05:39 +00:00
2023-04-13 10:31:48 +00:00
tempInput.addEventListener('input',() => {
temperatureValue.textContent =`Temperature(${tempInput.value})`
})
nPredictInput.addEventListener('input',() => {
n_predictValue.textContent =`N Predict(${nPredictInput.value})`
})
topKInput.addEventListener('input',() => {
topkValue.textContent =`Top-K(${topKInput.value})`
})
topPInput.addEventListener('input',() => {
toppValue.textContent =`Top-P(${topPInput.value})`
})
repeatPenaltyInput.addEventListener('input',() => {
repeatPenaltyValue.textContent =`Repeat penalty(${repeatPenaltyInput.value})`
})
repeatLastNInput.addEventListener('input',() => {
repeatLastNValue.textContent =`Repeat last N(${repeatLastNInput.value})`
})
2023-04-13 10:31:48 +00:00
const submitButton = document.getElementById('submit-model-params');
submitButton.addEventListener('click', (event) => {
// Prevent default form submission
event.preventDefault();
// Get form values and put them in an object
const formValues = {
seed: seedInput.value,
2023-05-25 21:24:14 +00:00
binding: bindingInput.value,
2023-04-14 08:48:14 +00:00
model: modelInput.value,
2023-04-20 20:30:42 +00:00
personality_language:personalityLanguageInput.value,
personality_category:personalityCategoryInput.value,
2023-04-14 08:48:14 +00:00
personality: personalityInput.value,
language: languageInput.value,
voice: voiceInput.value,
2023-05-06 12:42:50 +00:00
temperature: tempInput.value,
2023-04-13 10:31:48 +00:00
nPredict: nPredictInput.value,
topK: topKInput.value,
topP: topPInput.value,
repeatPenalty: repeatPenaltyInput.value,
repeatLastN: repeatLastNInput.value
};
console.log(formValues);
// Use fetch to send form values to Flask endpoint
fetch('/update_model_params', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formValues),
})
.then((response) => response.json())
.then((data) => {
console.log(data);
alert("Configuration set successfully")
2023-04-13 10:31:48 +00:00
})
.catch((error) => {
console.error('Error:', error);
alert("Error setting configuration")
2023-04-13 10:31:48 +00:00
});
});
2023-04-23 19:05:39 +00:00
populate_settings();
2023-04-13 10:31:48 +00:00
2023-04-23 19:05:39 +00:00
function populate_settings(){
// Get a reference to the <select> element
2023-05-25 21:24:14 +00:00
const selectBinding = document.getElementById('binding');
2023-04-23 19:05:39 +00:00
const selectModel = document.getElementById('model');
const selectPersonalityLanguage = document.getElementById('personalities_language');
2023-04-23 19:05:39 +00:00
const selectPersonalityCategory = document.getElementById('personalities_category');
const selectPersonality = document.getElementById('personalities');
2023-05-25 21:24:14 +00:00
function populate_bindings(){
selectBinding.innerHTML = "";
2023-04-23 19:05:39 +00:00
// Fetch the list of .bin files from the models subfolder
2023-05-25 21:24:14 +00:00
fetch('/list_bindings')
2023-04-23 19:05:39 +00:00
.then(response => response.json())
.then(data => {
if (Array.isArray(data)) {
// data is an array
data.forEach(filename => {
const optionElement = document.createElement('option');
optionElement.value = filename;
optionElement.textContent = filename;
2023-05-25 21:24:14 +00:00
selectBinding.appendChild(optionElement);
2023-04-23 19:05:39 +00:00
});
// fetch('/get_args')
// .then(response=> response.json())
// .then(data=>{
// })
} else {
console.error('Expected an array, but received:', data);
}
});
}
function populate_models(){
2023-04-24 21:11:32 +00:00
selectModel.innerHTML=""
2023-04-23 19:05:39 +00:00
// Fetch the list of .bin files from the models subfolder
fetch('/list_models')
.then(response => response.json())
.then(data => {
if (Array.isArray(data)) {
// data is an array
data.forEach(filename => {
const optionElement = document.createElement('option');
optionElement.value = filename;
optionElement.textContent = filename;
selectModel.appendChild(optionElement);
});
// fetch('/get_args')
// .then(response=> response.json())
// .then(data=>{
// })
} else {
console.error('Expected an array, but received:', data);
}
});
}
function populate_personalities_languages(){
const selectPersonalityLanguage = document.getElementById('personalities_language');
selectPersonalityLanguage.innerHTML=""
// Fetch the list of .yaml files from the models subfolder
fetch('/list_personalities_languages')
.then(response => response.json())
.then(data => {
if (Array.isArray(data)) {
// data is an array
data.forEach(filename => {
const optionElement = document.createElement('option');
optionElement.value = filename;
optionElement.textContent = filename;
selectPersonalityLanguage.appendChild(optionElement);
});
// fetch('/get_args')
// .then(response=> response.json())
// .then(data=>{
// })
} else {
console.error('Expected an array, but received:', data);
}
});
}
function populate_personalities_categories(){
const selectPersonalityCategory = document.getElementById('personalities_category');
selectPersonalityCategory.innerHTML=""
// Fetch the list of .yaml files from the models subfolder
fetch('/list_personalities_categories')
.then(response => response.json())
.then(data => {
if (Array.isArray(data)) {
// data is an array
data.forEach(filename => {
const optionElement = document.createElement('option');
optionElement.value = filename;
optionElement.textContent = filename;
selectPersonalityCategory.appendChild(optionElement);
});
} else {
console.error('Expected an array, but received:', data);
}
});
}
function populate_personalities(){
const selectPersonalityLanguage = document.getElementById('personalities_language');
selectPersonality.innerHTML=""
// Fetch the list of .yaml files from the models subfolder
fetch('/list_personalities')
.then(response => response.json())
.then(data => {
if (Array.isArray(data)) {
// data is an array
data.forEach(filename => {
const optionElement = document.createElement('option');
optionElement.value = filename;
optionElement.textContent = filename;
selectPersonality.appendChild(optionElement);
});
} else {
console.error('Expected an array, but received:', data);
}
});
}
function set_personality_language(lang, callback) {
fetch(`/set_personality_language?language=${lang}`)
.then(response => response.json())
.then(data => {
callback(data);
2023-04-20 17:30:03 +00:00
});
2023-04-23 19:05:39 +00:00
}
// Example usage: call another function after set_personality_language returns
selectPersonalityLanguage.addEventListener('click', function() {
set_personality_language(selectPersonalityLanguage.value, function(data) {
console.log('Response received:', data);
populate_personalities_categories();
});
2023-04-20 17:30:03 +00:00
});
2023-04-23 19:05:39 +00:00
function set_personality_category(category, callback) {
fetch(`/set_personality_category?category=${category}`)
.then(response => response.json())
.then(data => {
callback()
});
2023-04-23 19:05:39 +00:00
}
// Example usage: call another function after set_personality_category returns
selectPersonalityCategory.addEventListener('click', function() {
set_personality_category(selectPersonalityCategory.value, function(data) {
console.log('Response received:', data);
populate_personalities();
});
});
2023-04-23 19:05:39 +00:00
2023-05-25 21:24:14 +00:00
populate_bindings()
2023-04-23 19:05:39 +00:00
populate_models()
populate_personalities_languages()
populate_personalities_categories()
populate_personalities()
setTimeout(update_config,100);
2023-04-23 14:59:00 +00:00
// Fetch the list of .yaml files from the models subfolder
2023-04-23 19:05:39 +00:00
fetch('/list_languages')
2023-04-23 14:59:00 +00:00
.then(response => response.json())
.then(data => {
if (Array.isArray(data)) {
// data is an array
2023-04-23 19:05:39 +00:00
const selectLanguage = document.getElementById('language');
data.forEach(row => {
2023-04-23 14:59:00 +00:00
const optionElement = document.createElement('option');
2023-04-23 19:05:39 +00:00
optionElement.value = row.value;
optionElement.innerHTML = row.label;
selectLanguage.appendChild(optionElement);
2023-04-23 14:59:00 +00:00
});
} else {
console.error('Expected an array, but received:', data);
}
});
2023-04-23 19:05:39 +00:00
}
2023-04-20 17:30:03 +00:00
2023-04-23 19:05:39 +00:00
})
.catch(error => {
console.error('Error loading settings page:', error);
});
2023-04-20 17:30:03 +00:00
2023-04-14 00:10:22 +00:00