Added settings to interface

This commit is contained in:
ParisNeo 2023-04-07 23:22:17 +02:00
parent 2dea9834a7
commit 55eb7a4f43
3 changed files with 131 additions and 26 deletions

61
app.py
View File

@ -1,10 +1,11 @@
import argparse
import json
import re
import random
import sqlite3
import traceback
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from flask import (
Flask,
@ -196,8 +197,15 @@ class Gpt4AllWebUI:
self.add_endpoint(
"/update_message", "update_message", self.update_message, methods=["GET"]
)
self.add_endpoint(
"/update_model_params", "update_model_params", self.update_model_params, methods=["POST"]
)
self.prepare_a_new_chatbot()
def prepare_a_new_chatbot(self):
# Create chatbot
self.chatbot_bindings = self.create_chatbot()
# Chatbot conditionning
@ -222,17 +230,18 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
conditionning_message,
new_text_callback=self.new_text_callback,
n_predict=len(conditionning_message),
n_predict=0,#len(conditionning_message),
temp=self.args.temp,
top_k=self.args.top_k,
top_p=self.args.top_p,
repeat_penalty=self.args.repeat_penalty,
repeat_last_n = self.args.repeat_last_n,
#seed=self.args.seed,
n_threads=8,
n_threads=8
)
print(f"Bot said:{self.bot_says}")
def prepare_query(self, message):
self.bot_says = ""
self.full_text = ""
@ -317,8 +326,7 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
self.prepare_query(self.current_message)
self.chatbot_bindings.generate(
self.current_message,
new_text_callback=self.new_text_callback,
new_text_callback=self.new_text_callback_with_yield,
n_predict=len(self.current_message)+args.n_predict,
temp=self.args.temp,
top_k=self.args.top_k,
@ -326,7 +334,7 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
repeat_penalty=self.args.repeat_penalty,
repeat_last_n = self.args.repeat_last_n,
#seed=self.args.seed,
n_threads=8,
n_threads=8
)
self.current_discussion.update_message(response_id, self.bot_says)
@ -387,26 +395,30 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
Discussion.rename(self.db_path, discussion_id, title)
return "renamed successfully"
def get_messages(self):
data = request.get_json()
discussion_id = data["id"]
self.current_discussion = Discussion(discussion_id, self.db_path)
messages = self.current_discussion.get_messages()
full_message = ""
for message in messages:
full_message += message['sender'] + ": " + message['content'] + "\n"
def restore_discussion(self, full_message):
self.chatbot_bindings.generate(
full_message,
new_text_callback=self.new_text_callback,
n_predict=len(messages),
n_predict=0,#len(full_message),
temp=self.args.temp,
top_k=self.args.top_k,
top_p=self.args.top_p,
repeat_penalty= self.args.repeat_penalty,
repeat_last_n = self.args.repeat_last_n,
n_threads=8,
n_threads=8
)
def get_messages(self):
data = request.get_json()
discussion_id = data["id"]
self.current_discussion = Discussion(discussion_id, self.db_path)
messages = self.current_discussion.get_messages()
# full_message = ""
# for message in messages:
# full_message += message['sender'] + ": " + message['content'] + "\n"
# app.config['executor'].submit(self.restore_discussion, full_message)
return jsonify(messages)
def delete_discussion(self):
@ -439,13 +451,15 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
# Get the current timestamp
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Create chatbot
self.chatbot_bindings = self.create_chatbot()
# Chatbot conditionning
self.condition_chatbot()
app.config['executor'].submit(self.prepare_a_new_chatbot)
# Return a success response
return json.dumps({"id": self.current_discussion.discussion_id, "time": timestamp})
def update_model_params(self):
data = request.get_json()
self.args.temp = data["temp"]
return jsonify({"status":"ok"})
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Start the chatbot Flask app.")
@ -462,7 +476,7 @@ if __name__ == "__main__":
parser.add_argument(
"--n_predict",
type=int,
default=256,#128,
default=256,
help="Number of tokens to predict at each step.",
)
parser.add_argument(
@ -503,6 +517,9 @@ if __name__ == "__main__":
args = parser.parse_args()
check_discussion_db(args.db_path)
executor = ThreadPoolExecutor(max_workers=2)
app.config['executor'] = executor
bot = Gpt4AllWebUI(app, args)
if args.debug:

View File

@ -540,3 +540,50 @@ function uncollapse(id){
const content = document.querySelector(`#${id}`);
content.classList.toggle('active');
}
// submitting the model
// Add event listener to form submit button
const submitButton = document.getElementById('submit-model-params');
submitButton.addEventListener('click', (event) => {
// Prevent default form submission
event.preventDefault();
modelInput = document.getElementById('model');
seedInput = document.getElementById('seed');
tempInput = document.getElementById('temp');
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');
// Get form values and put them in an object
const formValues = {
model: modelInput.value,
seed: seedInput.value,
temp: tempInput.value,
nPredict: nPredictInput.value,
topK: topKInput.value,
topP: topPInput.value,
repeatPenalty: repeatPenaltyInput.value,
repeatLastN: repeatLastNInput.value
};
// 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);
})
.catch((error) => {
console.error('Error:', error);
});
});

View File

@ -19,9 +19,50 @@
</div>
</section>
<section class="md:h-1/2 flex sm:space-x-reverse">
<p>Settings</p>
</section>
<section class="md:h-1/2 md:border-b border-accent flex flex md:flex-col">
<div>
<p>Settings</p>
</div>
<div class="h-96 overflow-y-auto">
<form id="model-params-form" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="model">Model</label>
<input class="bg-gray-700 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="model" type="text" name="model" value="gpt4all-lora-quantized.bin">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="seed">Seed</label>
<input class="bg-gray-700 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="seed" type="text" name="seed" value="0">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="temp">Temperature</label>
<input class="bg-gray-700 slider absolute top-0 w-full h-full opacity-0" id="temp" type="range" min="0" max="1" step="0.1" value="0.1" name="temp">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="n-predict">N Predict</label>
<input class="bg-gray-700 slider absolute top-0 w-full h-full opacity-0" id="n-predict" type="range" min="0" max="2048" step="1" value="256" name="n-predict">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="top-k">Top K</label>
<input class="bg-gray-700 slider absolute top-0 w-full h-full opacity-0" id="top-k" type="range" min="0" max="100" step="1" value="40" name="top-k">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="top-p">Top P</label>
<input class="bg-gray-700 slider absolute top-0 w-full h-full opacity-0" id="top-p" type="range" min="0" max="100" step="1" value="40" name="top-p">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="repeat-penalty">Repeat penalty</label>
<input class="bg-gray-700 slider absolute top-0 w-full h-full opacity-0" id="repeat-penalty" type="range" min="0" max="100" step="1" value="40" name="repeat-penalty">
</div>
<div class="mb-4">
<label class="block text-gray-700 font-bold mb-2" for="repeat-last-n">Repeat penalty</label>
<input class="bg-gray-700 slider absolute top-0 w-full h-full opacity-0" id="repeat-last-n" type="range" min="0" max="100" step="1" value="40" name="repeat-last-n">
</div>
<div class="mb-4">
<button type="submit" id="submit-model-params" class="my-1 mx-1 outline-none px-4 bg-accent text-black rounded-md hover:bg-[#7ba0ea] active:bg-[#3d73e1] transition-colors ease-in-out">Update parameters</button>
</div>
</form>
</div>
</section>
</section>
<section id="chat-window" class="w-full overflow-y-auto flex flex-col">