From 3f10b966fe2f15559adcd48d4b02d37b45e63609 Mon Sep 17 00:00:00 2001 From: HappyZ Date: Thu, 25 May 2023 11:18:06 -0700 Subject: [PATCH] [FE] Supports to look up other results --- frontend.py | 9 ++++++ manage_db.py | 4 +-- templates/index.html | 70 +++++++++++++++++++++++++++++++++++++----- utilities/constants.py | 9 ++++++ utilities/database.py | 24 +++++++++++++++ 5 files changed, 107 insertions(+), 9 deletions(-) diff --git a/frontend.py b/frontend.py index 3215a14..1992817 100644 --- a/frontend.py +++ b/frontend.py @@ -126,6 +126,15 @@ def get_jobs(): return jsonify({"jobs": jobs}) +@app.route("/random_jobs", methods=["GET"]) +def random_jobs(): + # define max number of jobs to fetch from db + job_count_limit = 20 + + jobs = database.get_random_jobs(limit_count=job_count_limit) + + return jsonify({"jobs": jobs}) + @app.route("/") def index(): diff --git a/manage_db.py b/manage_db.py index 37186c0..969cbc0 100644 --- a/manage_db.py +++ b/manage_db.py @@ -132,7 +132,7 @@ def delete_jobs(c, job_uuid="", username=""): """Delete the job with the given uuid, or ignore the operation if the uuid does not exist""" if username: c.execute( - "SELECT img, ref_img FROM history WHERE apikey=(SELECT apikey FROM users WHERE username=?)", + "SELECT img, ref_img, mask_img FROM history WHERE apikey=(SELECT apikey FROM users WHERE username=?)", (username,), ) rows = c.fetchall() @@ -152,7 +152,7 @@ def delete_jobs(c, job_uuid="", username=""): print(f"removed {c.rowcount} entries") elif job_uuid: c.execute( - "SELECT img, ref_img FROM history WHERE uuid=?", + "SELECT img, ref_img, mask_img FROM history WHERE uuid=?", (job_uuid,), ) result = c.fetchone() diff --git a/templates/index.html b/templates/index.html index 8921d60..b3a208c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -39,9 +39,9 @@ data-zh_CN="这张图..">This image is.. -
Describe your image. Example: photo of a cat, +
Describe your image. Example: photo + of a cat, cute, black and white. Use () to emphasize.
@@ -352,6 +352,8 @@ +
@@ -508,8 +510,8 @@ (response.jobs[i].img ? ("
") : "") + "
") $grid.append(element); }; - $grid.imagesLoaded().progress(function() { + $grid.imagesLoaded().progress(function () { $grid.masonry({ itemSelector: '.col', columnWidth: '.col', @@ -532,9 +534,63 @@ $('#joblist').html("found nothing"); } }); - }); + function shuffle(arr) { + var j, x, index; + for (index = arr.length - 1; index > 0; index--) { + j = Math.floor(Math.random() * (index + 1)); + x = arr[index]; + arr[index] = arr[j]; + arr[j] = x; + } + return arr; + } + $('#feelingLucky').click(function () { + $.ajax({ + type: 'GET', + url: '/random_jobs', + contentType: 'application/json; charset=utf-8', + dataType: 'json', + success: function (response) { + var jobsLength = response.jobs.length; + if (jobsLength == 0) { + $('#joblist').html("found nothing"); + return; + } + var shuffled = response.jobs.map(value => ({ value, sort: Math.random() })).sort((a, b) => a.sort - b.sort).map(({ value }) => value) + + var $joblist = $('#joblist'); + var $grid = $('
'); + $joblist.html($grid); + for (var i = 0; i < jobsLength; i++) { + var element = ("
" + + (shuffled[i].img ? ("
") : "") + + "
    " + + "
  • prompt: " + truncateText(shuffled[i].prompt, 200) + "
  • " + + "
  • neg prompt: " + truncateText(shuffled[i].neg_prompt, 200) + "
  • " + + "
  • seed: " + shuffled[i].seed + "
  • " + + "
  • w x h: " + shuffled[i].width + " x " + shuffled[i].height + "
  • " + + "
" + + "
") + $grid.append(element); + }; + $grid.imagesLoaded().progress(function () { + $grid.masonry({ + itemSelector: '.col', + columnWidth: '.col', + percentPosition: true + }); + }); + + }, + error: function (xhr, status, error) { + // Handle error response + console.log(xhr.responseText); + $('#joblist').html("found nothing"); + } + }); + }); $("#upload-img").click(function () { var input = $(""); diff --git a/utilities/constants.py b/utilities/constants.py index 587b014..2564c45 100644 --- a/utilities/constants.py +++ b/utilities/constants.py @@ -94,6 +94,15 @@ OUTPUT_ONLY_KEYS = [ KEY_JOB_STATUS, # str ] +ANONYMOUS_KEYS = [ + KEY_PROMPT, + KEY_NEG_PROMPT, + KEY_SEED, + KEY_WIDTH, + KEY_HEIGHT, + BASE64IMAGE, +] + # -- internal KEY_BASE_MODEL = "base_model" INTERNAL_KEYS = [ diff --git a/utilities/database.py b/utilities/database.py index b778ad0..2962fb2 100644 --- a/utilities/database.py +++ b/utilities/database.py @@ -17,6 +17,7 @@ from utilities.constants import VALUE_JOB_DONE from utilities.constants import LOCK_FILEPATH from utilities.constants import OUTPUT_ONLY_KEYS +from utilities.constants import ANONYMOUS_KEYS from utilities.constants import OPTIONAL_KEYS from utilities.constants import REQUIRED_KEYS from utilities.constants import INTERNAL_KEYS @@ -127,6 +128,29 @@ class Database: result = c.execute(query_string, query_args).fetchone() return result[0] + def get_random_jobs(self, limit_count=0) -> list: + query = f"SELECT {', '.join(ANONYMOUS_KEYS)} FROM {HISTORY_TABLE_NAME} WHERE {KEY_JOB_STATUS} = '{VALUE_JOB_DONE}' AND RANDOM() <= .3 LIMIT {limit_count}" + + # execute the query and return the results + c = self.get_cursor() + rows = c.execute(query).fetchall() + + jobs = [] + for row in rows: + job = { + ANONYMOUS_KEYS[i]: row[i] + for i in range(len(ANONYMOUS_KEYS)) + if row[i] is not None + } + # load image to job if has one + for key in [BASE64IMAGE, REFERENCE_IMG, MASK_IMG]: + if key in job and "base64" not in job[key]: + data = load_image(job[key], to_base64=True) + job[key] = data if data else IMAGE_NOT_FOUND_BASE64 + jobs.append(job) + + return jobs + def get_jobs(self, job_uuid="", apikey="", job_status="", limit_count=0) -> list: """ Get a list of jobs from the HISTORY_TABLE_NAME table based on optional filters.