From 274935082060685cff03febb67180b89f0cac4de Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 5 Apr 2023 18:41:02 +0200 Subject: [PATCH] image generation --- matrix_pygmalion_bot/ai/runpod_pygmalion.py | 87 ++++++++++++++------- matrix_pygmalion_bot/core.py | 4 +- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/matrix_pygmalion_bot/ai/runpod_pygmalion.py b/matrix_pygmalion_bot/ai/runpod_pygmalion.py index 527750f..66a6bf7 100644 --- a/matrix_pygmalion_bot/ai/runpod_pygmalion.py +++ b/matrix_pygmalion_bot/ai/runpod_pygmalion.py @@ -2,6 +2,7 @@ import asyncio import os, tempfile import logging +import json import requests from transformers import AutoTokenizer, AutoConfig @@ -185,7 +186,7 @@ async def generate_image(input_prompt: str, negative_prompt: str, api_url: str, "prompt": input_prompt, "negative_prompt": negative_prompt, "width": 512, - "height": 512, + "height": 768, # "nsfw": True }, } @@ -247,13 +248,12 @@ async def generate_image4(input_prompt: str, negative_prompt: str, api_key: str) return await generate_image_automatic(input_prompt, negative_prompt, "https://api.runpod.ai/v1/lxdhmiccp3vdsf/", api_key) async def generate_image5(input_prompt: str, negative_prompt: str, api_key: str): - return await generate_image_automatic(input_prompt, negative_prompt, "https://api.runpod.ai/v1/p63t8o9si30nkh/", api_key) + return await generate_image_automatic(input_prompt, negative_prompt, "https://api.runpod.ai/v1/13rrs00l7yxikf/", api_key) async def generate_image6(input_prompt: str, negative_prompt: str, api_key: str): return await generate_image_automatic(input_prompt, negative_prompt, "https://api.runpod.ai/v1/5j1xzlsyw84vk5/", api_key) -async def generate_image_automatic(input_prompt: str, negative_prompt: str, api_url: str, api_key: str): - +async def serverless_automatic_request(payload, cmd, api_url: str, api_key: str): # Set the API endpoint URL endpoint = api_url + "run" @@ -264,18 +264,10 @@ async def generate_image_automatic(input_prompt: str, negative_prompt: str, api_ } # Define your inputs + payload.update({"api_endpoint": cmd}) input_data = { - "input": { - "prompt": input_prompt, - "steps": 20, -# "restore_faces": True, -# "gfpgan_visibility": 0.5, -# "codeformer_visibility": 0.5, -# "codeformer_weight": 0.5, -# "override_settings": { -# "filter_nsfw": False, -# }, - }, + "input": payload, + "cmd": cmd, } logger.info(f"sending request to runpod.io") @@ -310,24 +302,61 @@ async def generate_image_automatic(input_prompt: str, negative_prompt: str, api_ raise ValueError(f"RETURN CODE {status}") if not output: - raise ValueError(f"") + raise ValueError(f" {status}") - os.makedirs("./images", exist_ok=True) - files = [] - for i in output['images']: - temp_name = next(tempfile._get_candidate_names()) - filename = "./images/" + temp_name + ".png" - image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) - info = output['info'] - parameters = output['parameters'] - pnginfo = PngImagePlugin.PngInfo() - pnginfo.add_text("parameters", info) - image.save(filename, pnginfo=pnginfo) + return output - files.append(filename) - return files +async def generate_image_automatic(input_prompt: str, negative_prompt: str, api_url: str, api_key: str): + payload = { + "prompt": input_prompt, + "nagative_prompt": negative_prompt, + "steps": 20, + "seed": -1, + "width": 512, + "height": 768, + "restore_faces": True, + "gfpgan_visibility": 0.5, + "codeformer_visibility": 0.5, + "codeformer_weight": 0.5, +# "override_settings": { +# "filter_nsfw": False, +# }, + } + output = await serverless_automatic_request(payload, "txt2img", api_url, api_key) + upscale = False + if upscale: + count = 0 + for i in output['images']: + payload = { + "images": [i], + "prompt": input_prompt, + "nagative_prompt": negative_prompt, + "steps": 20, + "seed": -1, +# tile_width, tile_height, mask_blur, padding, seams_fix_width, seams_fix_denoise, seams_fix_padding, upscaler_index, save_upscaled_image, redraw_mode, save_seams_fix_image, seams_fix_mask_blur, seams_fix_type, target_size_type, custom_width, custom_height, custom_scale +# "script_args": ["",512,0,8,32,64,0.275,32,3,False,0,True,8,3,2,1080,1440,1.875], +# "script_name": "Ultimate SD upscale", + } + upscaled_output = await serverless_automatic_request(payload, "img2img", api_url, api_key) + output['images'][count] = upscaled_output['images'][count] + + os.makedirs("./images", exist_ok=True) + files = [] + for i in output['images']: + temp_name = next(tempfile._get_candidate_names()) + filename = "./images/" + temp_name + ".png" + image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) + info = output['info'] + parameters = output['parameters'] + pnginfo = PngImagePlugin.PngInfo() + pnginfo.add_text("parameters", info) + image.save(filename, pnginfo=pnginfo) + + files.append(filename) + + return files diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index 7f1537d..027f27e 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -101,10 +101,12 @@ class Callbacks(object): prompt = "a beautiful woman" if num == 1: negative_prompt = "out of frame, (ugly:1.3), (fused fingers), (too many fingers), (bad anatomy:1.5), (watermark:1.5), (words), letters, untracked eyes, asymmetric eyes, floating head, (logo:1.5), (bad hands:1.3), (mangled hands:1.2), (missing hands), (missing arms), backward hands, floating jewelry, unattached jewelry, floating head, doubled head, unattached head, doubled head, head in body, (misshapen body:1.1), (badly fitted headwear:1.2), floating arms, (too many arms:1.5), limbs fused with body, (facial blemish:1.5), badly fitted clothes, imperfect eyes, untracked eyes, crossed eyes, hair growing from clothes, partial faces, hair not attached to head" + elif num == 5: + negative_prompt = "anime, cartoon, penis, fake, drawing, illustration, boring, 3d render, long neck, out of frame, extra fingers, mutated hands, monochrome, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), ((ugly)), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, glitchy, bokeh, (((long neck))), 3D, 3DCG, cgstation, red eyes, multiple subjects, extra heads, close up, watermarks, logo" else: negative_prompt = "ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, signature, cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face" # else: -# negative_prompt = "ugly, out of frame" +# negative_prompt = "ugly, deformed, out of frame" if self.bot.service == "runpod": if num == 1: output = await ai.generate_image1(prompt, negative_prompt, self.bot.runpod_api_key)