From: Max Value Date: Mon, 21 Jul 2025 16:04:00 +0000 (+0100) Subject: Started implementing Deepseek system X-Git-Url: https://git.ozva.co.uk/?a=commitdiff_plain;h=HEAD;p=form Started implementing Deepseek system + added recursion for generating new questions if there are no unanswered questions at get time. --- diff --git a/form.py b/form.py index 4a512f2..b989504 100755 --- a/form.py +++ b/form.py @@ -16,9 +16,13 @@ import os FORM_COUNT = 5 GENERATE_COUNT = 10 GENERATE_THRESHHOLD = 10 -BACKUP_TIME = 2 + +BACKUP_TIME = 2#h BACKUP_COUNT = 5 +RECCUR_WAIT = 10#s +RECCUR_DEPTH = 5 + app = Flask(__name__) auth = HTTPBasicAuth() @@ -182,10 +186,12 @@ def answer_question(key: str, rowid: int, answer: int): f"UPDATE master SET counter = counter + 1 WHERE key = 'u{key}'" ) + connection.commit() + return key # get FORM_COUNT questions from the database and increment the tracker -def get_questions(key: str): +def get_questions(key: str, depth = 1): with sqlite3.connect(f"{app.root_path}/database.db") as connection: cursor = connection.cursor() """ @@ -197,12 +203,35 @@ def get_questions(key: str): SELECT * FROM u{key} WHERE answer IS NULL ORDER BY - created DESC; + created ASC; """ ) names = [description[0] for description in cursor.description] - result = [dict(zip(names, selection.fetchone())) for _ in range(FORM_COUNT)] + result = [] + for _ in range(FORM_COUNT): + row = selection.fetchone() + if row is not None: + question = dict(zip(names, row)) + result.append(question) + elif depth < RECCUR_DEPTH: + logger.error(f"User {key} ran out of questions. Waiting and reccuring") + + dispatch = True + for th in threading.enumerate(): + if th.name == f"g{key}": + dispatch = False + logger.warning(f"Generator {key} attempted to dispatch, but was already running") + + if dispatch: + logger.info(f"Generator {key} re-dispatched within recursion (depth {depth})") + threading.Thread(target=generate_questions, args=(key,), name=f"g{key}").start() + + time.sleep(RECCUR_WAIT) + result = get_questions(key, depth+1) + else: + logger.error(f"User {key} maximum depth reached") + quit() return result @@ -227,7 +256,7 @@ def check_generate(key: str): """ for th in threading.enumerate(): if th.name == f"g{key}": - logger.warn(f"Generator {key} attempted to dispatch, but was already running") + logger.warning(f"Generator {key} attempted to dispatch, but was already running") return logger.info(f"Generator {key} dispatched") @@ -236,7 +265,26 @@ def check_generate(key: str): # add GENERATE_COUNT questions to the database for key x based off the previous questions def generate_questions(key: str): start = time.time() - time.sleep(10) + + with sqlite3.connect(f"{app.root_path}/database.db") as connection: + cursor = connection.cursor() + selection = cursor.execute(f""" + SELECT * FROM u{key} + WHERE answer IS NOT NULL + ORDER BY created DESC; + """) + + names = [description[0] for description in cursor.description] + data = [dict(zip(names, selection.fetchone())) for _ in range(FORM_COUNT)] + learn = [{"question":x["question"], "answer":x[f"option_{x['answer']}"]} for x in data] + + prompt = f""" + + Here are a selection of questions and their respecive aswers from a participant answering a survey. + Please produce {GENERATE_COUNT} more questions that lead off of the given answers to produce a more + personal experiance. + + """ # get the generic pre-written questions with open("generic.json","r") as f: @@ -366,6 +414,7 @@ def reset(): if table[0][0] == "u": cursor.execute(f"DROP TABLE {table[0]};") + connection.commit() return "", 200 scheduler = BackgroundScheduler()