From 4ba9b8a9a19df4b053ef4a2758708a12320c905e Mon Sep 17 00:00:00 2001 From: HappyZ Date: Sun, 30 Apr 2023 00:32:20 -0700 Subject: [PATCH] add img2img backend barebone, reorganized components --- BUILD | 1 + main.py | 31 +++++-- templates/index.html | 197 ++++++++++++++++++++++------------------- utilities/BUILD | 14 +++ utilities/config.py | 49 +++++++--- utilities/constants.py | 19 ++-- utilities/img2img.py | 88 ++++++++++++++++++ 7 files changed, 286 insertions(+), 113 deletions(-) create mode 100644 utilities/img2img.py diff --git a/BUILD b/BUILD index 8a80bc9..5093cbb 100644 --- a/BUILD +++ b/BUILD @@ -11,6 +11,7 @@ par_binary( "//utilities:logger", "//utilities:model", "//utilities:text2img", + "//utilities:img2img", "//utilities:envvar", "//utilities:times", ], diff --git a/main.py b/main.py index 6bfd75c..b69eb1a 100644 --- a/main.py +++ b/main.py @@ -14,14 +14,21 @@ from utilities.constants import API_KEY from utilities.constants import API_KEY_FOR_DEMO from utilities.constants import KEY_APP from utilities.constants import KEY_JOB_STATUS +from utilities.constants import KEY_JOB_TYPE from utilities.constants import KEY_PROMPT from utilities.constants import KEY_NEG_PROMPT from utilities.constants import LOGGER_NAME +from utilities.constants import LOGGER_NAME_IMG2IMG +from utilities.constants import LOGGER_NAME_TXT2IMG +from utilities.constants import REFERENCE_IMG from utilities.constants import MAX_JOB_NUMBER from utilities.constants import OPTIONAL_KEYS from utilities.constants import REQUIRED_KEYS from utilities.constants import UUID from utilities.constants import VALUE_APP +from utilities.constants import VALUE_JOB_TXT2IMG +from utilities.constants import VALUE_JOB_IMG2IMG +from utilities.constants import VALUE_JOB_INPAINTING from utilities.constants import VALUE_JOB_PENDING from utilities.constants import VALUE_JOB_RUNNING from utilities.constants import VALUE_JOB_DONE @@ -36,7 +43,7 @@ from utilities.text2img import Text2Img app = Flask(__name__) -fast_web_debugging = False +fast_web_debugging = True memory_lock = Lock() event_termination = Event() logger = Logger(name=LOGGER_NAME) @@ -65,6 +72,9 @@ def add_job(): if required_key not in req: return jsonify({"msg": "missing one or more required keys"}), 404 + if req[KEY_JOB_TYPE] == VALUE_JOB_IMG2IMG and REFERENCE_IMG not in req: + return jsonify({"msg": "missing reference image"}), 404 + if len(local_job_stack) > MAX_JOB_NUMBER: return jsonify({"msg": "too many jobs in queue, please wait"}), 500 @@ -204,9 +214,11 @@ def load_model(logger: Logger) -> Model: def backend(event_termination): model = load_model(logger) - text2img = Text2Img(model, logger=logger) + text2img = Text2Img(model, logger=Logger(name=LOGGER_NAME_TXT2IMG)) + img2img = Img2Img(model, logger=Logger(name=LOGGER_NAME_IMG2IMG)) text2img.breakfast() + img2img.breakfast() while not event_termination.is_set(): wait_for_seconds(1) @@ -223,9 +235,18 @@ def backend(event_termination): config = Config().set_config(next_job) try: - result_dict = text2img.lunch( - prompt=prompt, negative_prompt=negative_prompt, config=config - ) + if next_job[KEY_JOB_TYPE] == VALUE_JOB_TXT2IMG: + result_dict = text2img.lunch( + prompt=prompt, negative_prompt=negative_prompt, config=config + ) + elif next_job[KEY_JOB_TYPE] == VALUE_JOB_IMG2IMG: + ref_img = next_job[REFERENCE_IMG] + result_dict = img2img.lunch( + prompt=prompt, + negative_prompt=negative_prompt, + reference_image=ref_img, + config=config, + ) except BaseException as e: logger.error("text2img.lunch error: {}".format(e)) local_job_stack.pop(0) diff --git a/templates/index.html b/templates/index.html index fbde46b..3a62a77 100644 --- a/templates/index.html +++ b/templates/index.html @@ -14,107 +14,111 @@
-
- -
-
-
-
- - +
+
+ + +
+
+
+ +
-
-
- - +
+
+
+ + +
Less than 77 words otherwise it'll be truncated. Example: + "photo of cute cat, RAW photo, (high detailed skin:1.2), 8k uhd, dslr, soft lighting, high + quality, film grain, Fujifilm XT3"
+
+
+ + +
Less than 77 words otherwise it'll be truncated. + Example: "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, + drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg + artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn + hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad + proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, + missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long + neck"
+
+
+
+
+ + +
Leave it empty or put 0 to use a random + seed
+
+ + +
Each step is about 38s (CPU) or 0.1s + (GPU) +
+
+
+ + +
+
+ + +
+
-
- - -
Less than 77 words otherwise it'll be truncated. Example: - "photo of cute cat, RAW photo, (high detailed skin:1.2), 8k uhd, dslr, soft lighting, high - quality, film grain, Fujifilm XT3"
-
-
- - -
Less than 77 words otherwise it'll be truncated. - Example: "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, - drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg - artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn - hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad - proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, - missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long - neck"
-
-
-
-
-
- - -
Leave it empty or put 0 to use a random - seed +
+
+
+ +
+ + +
+
+
+ Result
-
-
- - -
Each step is about 38s (CPU) or 0.1s - (GPU) +
+
    +
  • +
  • +
+
-
-
- - -
-
- - -
-
- -
-
-
-
- Result -
-
-
    -
  • -
  • -
-
- -
+
- +
@@ -138,7 +142,6 @@ crossorigin="anonymous">