mirror of
https://github.com/KoboldAI/KoboldAI-Client.git
synced 2025-06-05 21:59:24 +02:00
Simplified Actions Sync to UI
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
import os, re, time, threading, json, pickle, base64, copy, tqdm, datetime
|
||||
from io import BytesIO
|
||||
from flask import has_request_context
|
||||
import socketio as socketio_client
|
||||
from flask_socketio import SocketIO
|
||||
from collections import OrderedDict
|
||||
import requests
|
||||
|
||||
rely_clients = {}
|
||||
serverstarted = False
|
||||
@@ -36,12 +34,10 @@ def process_variable_changes(socketio, classname, name, value, old_value, debug_
|
||||
#Special Case for KoboldStoryRegister
|
||||
if isinstance(value, KoboldStoryRegister):
|
||||
socketio.emit("var_changed", {"classname": "actions", "name": "Action Count", "old_value": None, "value":value.action_count}, broadcast=True, room="UI_2")
|
||||
|
||||
for i in range(len(value.actions)):
|
||||
socketio.emit("var_changed", {"classname": "actions", "name": "Selected Text", "old_value": None, "value": {"id": i, "text": value[i]}}, include_self=True, broadcast=True, room="UI_2")
|
||||
socketio.emit("var_changed", {"classname": "actions", "name": "Options", "old_value": None, "value": {"id": i, "options": value.actions[i]['Options']}}, include_self=True, broadcast=True, room="UI_2")
|
||||
socketio.emit("var_changed", {"classname": "actions", "name": "Selected Text Length", "old_value": None, "value": {"id": i, 'length': value.actions[i]['Selected Text Length']}}, include_self=True, broadcast=True, room="UI_2")
|
||||
if 'In AI Input' in value.actions[i]:
|
||||
socketio.emit("var_changed", {"classname": "actions", "name": "In AI Input", "old_value": None, "value": {"id": i, 'In AI Input': value.actions[i]['In AI Input']}}, include_self=True, broadcast=True, room="UI_2")
|
||||
print(value.actions[i])
|
||||
socketio.emit("var_changed", {"classname": "story", "name": "actions", "old_value": None, "value":{"id": i, "action": value.actions[i]}}, broadcast=True, room="UI_2")
|
||||
elif isinstance(value, KoboldWorldInfo):
|
||||
value.send_to_ui()
|
||||
else:
|
||||
@@ -747,6 +743,7 @@ class KoboldStoryRegister(object):
|
||||
|
||||
def __setitem__(self, i, text):
|
||||
if i in self.actions:
|
||||
old = self.actions[i]
|
||||
old_text = self.actions[i]["Selected Text"]
|
||||
old_length = self.actions[i]["Selected Text Length"]
|
||||
if self.actions[i]["Selected Text"] != text:
|
||||
@@ -761,6 +758,7 @@ class KoboldStoryRegister(object):
|
||||
else:
|
||||
old_text = None
|
||||
old_length = None
|
||||
old = None
|
||||
self.actions[i] = {"Selected Text": text, "Probabilities": [], "Options": []}
|
||||
|
||||
if self.tokenizer is not None:
|
||||
@@ -768,10 +766,7 @@ class KoboldStoryRegister(object):
|
||||
else:
|
||||
self.actions[i]['Selected Text Length'] = None
|
||||
self.actions[i]["In AI Input"] = False
|
||||
process_variable_changes(self.socketio, "actions", "Selected Text", {"id": i, "text": text}, {"id": i, "text": old_text})
|
||||
process_variable_changes(self.socketio, "actions", 'Selected Text Length', {"id": i, 'length': self.actions[i]['Selected Text Length']}, {"id": i, 'length': old_length})
|
||||
process_variable_changes(self.socketio, "actions", 'Probabilities', {"id": i, 'Probabilities': self.actions[i]['Probabilities']}, None)
|
||||
process_variable_changes(self.socketio, "actions", 'In AI Input', {"id": i, 'In AI Input': self.actions[i]["In AI Input"]}, None)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": i, 'action': self.actions[i]}, old)
|
||||
ignore = self.koboldai_vars.calc_ai_text()
|
||||
self.set_game_saved()
|
||||
|
||||
@@ -801,9 +796,7 @@ class KoboldStoryRegister(object):
|
||||
temp = {}
|
||||
for item in json_data['actions']:
|
||||
temp[int(item)] = json_data['actions'][item]
|
||||
process_variable_changes(self.socketio, "actions", "Selected Text", {"id": int(item), "text": json_data['actions'][item]["Selected Text"]}, None)
|
||||
if "Options" in json_data['actions'][item]:
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": int(item), "options": json_data['actions'][item]["Options"]}, None)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": item, 'action': temp[int(item)]}, None)
|
||||
|
||||
self.action_count = json_data['action_count']
|
||||
self.actions = temp
|
||||
@@ -827,7 +820,6 @@ class KoboldStoryRegister(object):
|
||||
if item['text'] == text:
|
||||
old_options = self.actions[self.action_count]["Options"]
|
||||
del item
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": self.action_count, "options": self.actions[self.action_count]["Options"]}, {"id": self.action_count, "options": old_options})
|
||||
|
||||
else:
|
||||
if self.tokenizer is not None:
|
||||
@@ -837,10 +829,7 @@ class KoboldStoryRegister(object):
|
||||
|
||||
self.actions[self.action_count] = {"Selected Text": text, "Selected Text Length": selected_text_length, "In AI Input": False, "Options": [], "Probabilities": []}
|
||||
|
||||
process_variable_changes(self.socketio, "actions", "Selected Text", {"id": self.action_count, "text": text}, None)
|
||||
process_variable_changes(self.socketio, "actions", 'Selected Text Length', {"id": self.action_count, 'length': self.actions[self.action_count]['Selected Text Length']}, {"id": self.action_count, 'length': 0})
|
||||
process_variable_changes(self.socketio, "actions", 'In AI Input', {"id": self.action_count, 'In AI Input': self.actions[self.action_count]["In AI Input"]}, None)
|
||||
process_variable_changes(self.socketio, "actions", 'Probabilities', {"id": self.action_count, 'Probabilities': self.actions[self.action_count]['Probabilities']}, None)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": self.action_count, 'action': self.actions[self.action_count]}, None)
|
||||
ignore = self.koboldai_vars.calc_ai_text()
|
||||
self.set_game_saved()
|
||||
|
||||
@@ -854,7 +843,7 @@ class KoboldStoryRegister(object):
|
||||
else:
|
||||
old_options = None
|
||||
self.actions[self.action_count+1] = {"Selected Text": "", "Selected Text Length": 0, "In AI Input": False, "Options": [{"text": x, "Pinned": False, "Previous Selection": False, "Edited": False, "Probabilities": []} for x in option_list]}
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": self.action_count+1, "options": self.actions[self.action_count+1]["Options"]}, {"id": self.action_count+1, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": self.action_count+1, 'action': self.actions[self.action_count+1]}, None)
|
||||
self.set_game_saved()
|
||||
|
||||
def set_options(self, option_list, action_id):
|
||||
@@ -870,7 +859,7 @@ class KoboldStoryRegister(object):
|
||||
#We already have this option, so we need to save the probabilities
|
||||
item['Probabilities'] = old_item['Probabilities']
|
||||
self.actions[action_id]["Options"].append(item)
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_id, "options": self.actions[action_id]["Options"]}, {"id": action_id, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None)
|
||||
|
||||
def clear_unused_options(self, pointer=None):
|
||||
new_options = []
|
||||
@@ -881,7 +870,7 @@ class KoboldStoryRegister(object):
|
||||
old_options = copy.deepcopy(self.actions[pointer]["Options"])
|
||||
self.actions[pointer]["Options"] = [x for x in self.actions[pointer]["Options"] if x["Pinned"] or x["Previous Selection"] or x["Edited"]]
|
||||
new_options = self.actions[pointer]["Options"]
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": pointer, "options": new_options}, {"id": pointer, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": pointer, 'action': self.actions[pointer]}, None)
|
||||
self.set_game_saved()
|
||||
|
||||
def set_action_in_ai(self, action_id, used=True):
|
||||
@@ -891,14 +880,14 @@ class KoboldStoryRegister(object):
|
||||
old = None
|
||||
self.actions[action_id]['In AI Input'] = used
|
||||
if old != used:
|
||||
process_variable_changes(self.socketio, "actions", 'In AI Input', {"id": action_id, 'In AI Input': self.actions[action_id]["In AI Input"]}, None)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None)
|
||||
|
||||
def set_pin(self, action_step, option_number):
|
||||
if action_step in self.actions:
|
||||
if option_number < len(self.actions[action_step]['Options']):
|
||||
old_options = copy.deepcopy(self.actions[action_step]["Options"])
|
||||
self.actions[action_step]['Options'][option_number]['Pinned'] = True
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_step, "options": self.actions[action_step]["Options"]}, {"id": action_step, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_step, 'action': self.actions[action_step]}, None)
|
||||
self.set_game_saved()
|
||||
|
||||
def unset_pin(self, action_step, option_number):
|
||||
@@ -906,7 +895,7 @@ class KoboldStoryRegister(object):
|
||||
old_options = copy.deepcopy(self.actions[action_step]["Options"])
|
||||
if option_number < len(self.actions[action_step]['Options']):
|
||||
self.actions[action_step]['Options'][option_number]['Pinned'] = False
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_step, "options": self.actions[action_step]["Options"]}, {"id": action_step, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_step, 'action': self.actions[action_step]}, None)
|
||||
self.set_game_saved()
|
||||
|
||||
def toggle_pin(self, action_step, option_number):
|
||||
@@ -914,7 +903,7 @@ class KoboldStoryRegister(object):
|
||||
old_options = copy.deepcopy(self.actions[action_step]["Options"])
|
||||
if option_number < len(self.actions[action_step]['Options']):
|
||||
self.actions[action_step]['Options'][option_number]['Pinned'] = not self.actions[action_step]['Options'][option_number]['Pinned']
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_step, "options": self.actions[action_step]["Options"]}, {"id": action_step, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_step, 'action': self.actions[action_step]}, None)
|
||||
self.set_game_saved()
|
||||
|
||||
def use_option(self, option_number, action_step=None):
|
||||
@@ -936,9 +925,7 @@ class KoboldStoryRegister(object):
|
||||
if action_step-1 == self.action_count:
|
||||
self.action_count+=1
|
||||
self.socketio.emit("var_changed", {"classname": "actions", "name": "Action Count", "old_value": None, "value":self.action_count}, broadcast=True, room="UI_2")
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_step, "options": self.actions[action_step]["Options"]}, {"id": action_step, "options": old_options})
|
||||
process_variable_changes(self.socketio, "actions", "Selected Text", {"id": action_step, "text": self.actions[action_step]["Selected Text"]}, {"id": action_step, "Selected Text": old_text})
|
||||
process_variable_changes(self.socketio, "actions", 'Selected Text Length', {"id": action_step, 'length': self.actions[action_step]["Selected Text Length"]}, {"id": action_step, 'length': old_length})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_step, 'action': self.actions[action_step]}, None)
|
||||
ignore = self.koboldai_vars.calc_ai_text()
|
||||
self.set_game_saved()
|
||||
|
||||
@@ -951,9 +938,7 @@ class KoboldStoryRegister(object):
|
||||
self.actions[action_id]["Selected Text"] = ""
|
||||
self.actions[action_id]['Selected Text Length'] = 0
|
||||
self.action_count -= 1
|
||||
process_variable_changes(self.socketio, "actions", "Selected Text", {"id": action_id, "text": None}, {"id": action_id, "text": old_text})
|
||||
process_variable_changes(self.socketio, "actions", 'Selected Text Length', {"id": action_id, 'length': 0}, {"id": action_id, 'length': old_length})
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_id, "options": self.actions[action_id]["Options"]}, {"id": action_id, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None)
|
||||
self.set_game_saved()
|
||||
|
||||
def pop(self):
|
||||
@@ -1000,10 +985,11 @@ class KoboldStoryRegister(object):
|
||||
if self.tokenizer is not None:
|
||||
for key in self.actions:
|
||||
self.actions[key]['Selected Text Length'] = len(self.tokenizer.encode(self.actions[key]['Selected Text']))
|
||||
process_variable_changes(self.socketio, "actions", 'Selected Text Length', {"id": key, 'length': self.actions[key]['Selected Text Length']}, None)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": key, 'action': self.actions[key]}, None)
|
||||
else:
|
||||
for key in self.actions:
|
||||
self.actions[key]['Selected Text Length'] = None
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": key, 'action': self.actions[key]}, None)
|
||||
ignore = self.koboldai_vars.calc_ai_text()
|
||||
|
||||
def stream_tokens(self, text_list):
|
||||
@@ -1024,6 +1010,7 @@ class KoboldStoryRegister(object):
|
||||
self.actions[self.action_count+1]['Options'].append({"text": text_list[i], "Pinned": False, "Previous Selection": False, "Edited": False, "Probabilities": [], "stream_id": i})
|
||||
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": self.action_count+1, "options": self.actions[self.action_count+1]["Options"]}, {"id": self.action_count+1, "options": None})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": self.action_count+1, 'action': self.actions[self.action_count+1]}, None)
|
||||
else:
|
||||
#We're streaming single options so our output is our selected
|
||||
if self.tokenizer is not None:
|
||||
@@ -1037,6 +1024,7 @@ class KoboldStoryRegister(object):
|
||||
|
||||
process_variable_changes(self.socketio, "actions", "Selected Text", {"id": self.action_count+1, "text": self.actions[self.action_count+1]['Selected Text']}, None)
|
||||
process_variable_changes(self.socketio, "actions", 'Selected Text Length', {"id": self.action_count+1, 'length': self.actions[self.action_count+1]['Selected Text Length']}, {"id": self.action_count, 'length': 0})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": self.action_count+1, 'action': self.actions[self.action_count+1]}, None)
|
||||
|
||||
def set_probabilites(self, probabilities, action_id=None):
|
||||
print(probabilities)
|
||||
@@ -1044,7 +1032,7 @@ class KoboldStoryRegister(object):
|
||||
action_id = self.action_count
|
||||
if action_id in self.actions:
|
||||
self.actions[action_id]['Probabilities'].append(probabilities)
|
||||
process_variable_changes(self.socketio, "actions", 'Probabilities', {"id": action_id, 'Probabilities': self.actions[action_id]['Probabilities']}, None)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None)
|
||||
|
||||
def set_option_probabilities(self, probabilities, option_number, action_id=None):
|
||||
if action_id is None:
|
||||
@@ -1053,7 +1041,7 @@ class KoboldStoryRegister(object):
|
||||
old_options = self.actions[action_id]["Options"]
|
||||
if option_number < len(self.actions[action_id]["Options"]):
|
||||
self.actions[action_id]["Options"][option_number]['Probabilities'].append(probabilities)
|
||||
process_variable_changes(self.socketio, "actions", "Options", {"id": action_id, "options": self.actions[action_id]["Options"]}, {"id": action_id, "options": old_options})
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
new_variable = name not in self.__dict__
|
||||
|
@@ -12,8 +12,6 @@ bleach==4.1.0
|
||||
sentencepiece
|
||||
protobuf
|
||||
accelerate
|
||||
python-socketio[client]
|
||||
flask_session
|
||||
marshmallow>=3.13
|
||||
apispec-webframeworks
|
||||
gevent
|
||||
apispec-webframeworks
|
@@ -17,8 +17,6 @@ eventlet
|
||||
lupa==1.10
|
||||
markdown
|
||||
bleach==4.1.0
|
||||
python-socketio[client]
|
||||
flask-session
|
||||
marshmallow>=3.13
|
||||
apispec-webframeworks
|
||||
gevent
|
||||
apispec-webframeworks
|
@@ -133,7 +133,7 @@ function create_options(data) {
|
||||
table.classList.add("sequences");
|
||||
//Add Redo options
|
||||
i=0;
|
||||
for (item of data.value.options) {
|
||||
for (item of data.value.action.Options) {
|
||||
if ((item['Previous Selection'])) {
|
||||
var row = document.createElement("div");
|
||||
row.classList.add("sequence_row");
|
||||
@@ -162,7 +162,7 @@ function create_options(data) {
|
||||
}
|
||||
//Add general options
|
||||
i=0;
|
||||
for (item of data.value.options) {
|
||||
for (item of data.value.action.Options) {
|
||||
if (!(item.Edited) && !(item['Previous Selection'])) {
|
||||
var row = document.createElement("div");
|
||||
row.classList.add("sequence_row");
|
||||
@@ -211,10 +211,10 @@ function do_story_text_updates(data) {
|
||||
while (item.firstChild) {
|
||||
item.removeChild(item.firstChild);
|
||||
}
|
||||
if (data.value.text == null) {
|
||||
if (data.value.action['Selected Text'] == null) {
|
||||
var text_array = [];
|
||||
} else {
|
||||
var text_array = data.value.text.split(" ");
|
||||
var text_array = data.value.action['Selected Text'].split(" ");
|
||||
}
|
||||
text_array.forEach(function (text, i) {
|
||||
if (text != "") {
|
||||
@@ -229,7 +229,7 @@ function do_story_text_updates(data) {
|
||||
}
|
||||
|
||||
});
|
||||
item.original_text = data.value.text;
|
||||
item.original_text = data.value.action['Selected Text'];
|
||||
item.setAttribute("world_info_uids", "");
|
||||
item.classList.remove("pulse")
|
||||
item.scrollIntoView();
|
||||
@@ -239,7 +239,7 @@ function do_story_text_updates(data) {
|
||||
span.id = 'Selected Text Chunk '+data.value.id;
|
||||
span.classList.add("rawtext");
|
||||
span.chunk = data.value.id;
|
||||
span.original_text = data.value.text;
|
||||
span.original_text = data.value.action['Selected Text'];
|
||||
span.setAttribute("contenteditable", true);
|
||||
span.onblur = function () {
|
||||
if (this.textContent != this.original_text) {
|
||||
@@ -249,7 +249,7 @@ function do_story_text_updates(data) {
|
||||
}
|
||||
}
|
||||
span.onkeydown = detect_enter_text;
|
||||
var text_array = data.value.text.split(" ");
|
||||
var text_array = data.value.action['Selected Text'].split(" ");
|
||||
text_array.forEach(function (text, i) {
|
||||
if (text != "") {
|
||||
var word = document.createElement("span");
|
||||
@@ -311,7 +311,7 @@ function do_prompt(data) {
|
||||
}
|
||||
|
||||
function do_story_text_length_updates(data) {
|
||||
document.getElementById('Selected Text Chunk '+data.value.id).setAttribute("token_length", data.value.length);
|
||||
document.getElementById('Selected Text Chunk '+data.value.id).setAttribute("token_length", data.value.action["Selected Text Length"]);
|
||||
|
||||
}
|
||||
|
||||
@@ -392,23 +392,34 @@ function var_changed(data) {
|
||||
console.log(data);
|
||||
}
|
||||
//console.log({"name": data.name, "data": data});
|
||||
//Special Case for Story Text
|
||||
if ((data.classname == "actions") && (data.name == "Selected Text")) {
|
||||
//Special Case for Actions
|
||||
if ((data.classname == "story") && (data.name == "actions")) {
|
||||
console.log(data);
|
||||
do_story_text_updates(data);
|
||||
//Special Case for Story Options
|
||||
} else if ((data.classname == "actions") && (data.name == "Options")) {
|
||||
create_options(data);
|
||||
//Special Case for Story Text Length
|
||||
} else if ((data.classname == "actions") && (data.name == "Selected Text Length")) {
|
||||
do_story_text_length_updates(data);
|
||||
//Special Case for Story Text Length
|
||||
} else if ((data.classname == "actions") && (data.name == "In AI Input")) {
|
||||
//console.log(data.value);
|
||||
if (data.value['In AI Input']) {
|
||||
if (data.value.action['In AI Input']) {
|
||||
document.getElementById('Selected Text Chunk '+data.value.id).classList.add("within_max_length");
|
||||
} else {
|
||||
document.getElementById('Selected Text Chunk '+data.value.id).classList.remove("within_max_length");
|
||||
}
|
||||
//Special Case for Story Text
|
||||
} else if ((data.classname == "actions") && (data.name == "Selected Text")) {
|
||||
//do_story_text_updates(data);
|
||||
//Special Case for Story Options
|
||||
} else if ((data.classname == "actions") && (data.name == "Options")) {
|
||||
//create_options(data);
|
||||
//Special Case for Story Text Length
|
||||
} else if ((data.classname == "actions") && (data.name == "Selected Text Length")) {
|
||||
//do_story_text_length_updates(data);
|
||||
//Special Case for Story Text Length
|
||||
} else if ((data.classname == "actions") && (data.name == "In AI Input")) {
|
||||
//console.log(data.value);
|
||||
//if (data.value['In AI Input']) {
|
||||
// document.getElementById('Selected Text Chunk '+data.value.id).classList.add("within_max_length");
|
||||
//} else {
|
||||
// document.getElementById('Selected Text Chunk '+data.value.id).classList.remove("within_max_length");
|
||||
//}
|
||||
//Special Case for Presets
|
||||
} else if ((data.classname == 'model') && (data.name == 'presets')) {
|
||||
do_presets(data);
|
||||
|
Reference in New Issue
Block a user