]> OzVa Git service - form/commitdiff
Started implementing Deepseek system main
authorMax Value <greenwoodw50@gmail.com>
Mon, 21 Jul 2025 16:04:00 +0000 (17:04 +0100)
committerMax Value <greenwoodw50@gmail.com>
Mon, 21 Jul 2025 16:04:00 +0000 (17:04 +0100)
+ added recursion for generating new questions if there are no
unanswered questions at get time.

form.py

diff --git a/form.py b/form.py
index 4a512f2b9d70d041ae2f77104c24777c70a008e2..b9895040d4dc0816982c85258d2a6c5ae8945f4d 100755 (executable)
--- a/form.py
+++ b/form.py
@@ -16,9 +16,13 @@ import os
 FORM_COUNT = 5
 GENERATE_COUNT = 10
 GENERATE_THRESHHOLD = 10
 FORM_COUNT = 5
 GENERATE_COUNT = 10
 GENERATE_THRESHHOLD = 10
-BACKUP_TIME = 2
+
+BACKUP_TIME = 2#h
 BACKUP_COUNT = 5
 
 BACKUP_COUNT = 5
 
+RECCUR_WAIT = 10#s
+RECCUR_DEPTH = 5
+
 app = Flask(__name__)
 auth = HTTPBasicAuth()
 
 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}'"
                )
 
                        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
        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()
                """
        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
                        SELECT * FROM u{key}
                                WHERE answer IS NULL
                                ORDER BY
-                                       created DESC;
+                                       created ASC;
                        """
                )
 
        names = [description[0] for description in cursor.description]
                        """
                )
 
        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
 
 
        return result
 
@@ -227,7 +256,7 @@ def check_generate(key: str):
                """
                for th in threading.enumerate():
                        if th.name == f"g{key}":
                """
                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")
                                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()
 # 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"""
+       <system>
+               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.
+       </system>
+       """
 
        # get the generic pre-written questions
        with open("generic.json","r") as f:
 
        # 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]};")
 
                        if table[0][0] == "u":
                                cursor.execute(f"DROP TABLE {table[0]};")
 
+               connection.commit()
                return "", 200
 
 scheduler = BackgroundScheduler()
                return "", 200
 
 scheduler = BackgroundScheduler()