Upload files to "/"
This commit is contained in:
parent
3d5abcfaca
commit
65471d519b
132
api.py
132
api.py
|
@ -34,6 +34,38 @@ import piexif
|
||||||
import piexif.helper
|
import piexif.helper
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
|
|
||||||
|
'''
|
||||||
|
Eris Bot API modifier
|
||||||
|
Eris Promptlog:
|
||||||
|
Every Prompt that is send from through the API will be saved in a Textfile, with the current date as name, in a folder named "logs"
|
||||||
|
|
||||||
|
Eris Imagelog:
|
||||||
|
Every generated Images will be saved in the default "outputs" folder
|
||||||
|
|
||||||
|
Eris Consolelog:
|
||||||
|
Every Prompt that is send from through the API will be displayed in the console/terminal window
|
||||||
|
|
||||||
|
Eris TRTpatch:
|
||||||
|
Necessary modification for the TRT Plugin to work.
|
||||||
|
Please refer the Readme on https://nextcloud.hitmare.me/index.php/s/D3BmNDDHRXLdxfL for the full TRTpatch instructions
|
||||||
|
|
||||||
|
Eris Imagelimit:
|
||||||
|
Will resize every request down to 1024px max on height and width, keeping the aspect ratio. Also reduces Steps down to 35 if the set Steps exceeds 35
|
||||||
|
Compatible with the TRTpatch. Please refer the Readme on https://nextcloud.hitmare.me/index.php/s/D3BmNDDHRXLdxfL for the full TRTpatch instructions
|
||||||
|
|
||||||
|
|
||||||
|
To activate any modifications, change the "False" to "True". The capital "T" is important
|
||||||
|
for example:
|
||||||
|
eris_imagelog = True
|
||||||
|
'''
|
||||||
|
# Eris modifier switches
|
||||||
|
|
||||||
|
eris_promtlog = False
|
||||||
|
eris_imagelog = False
|
||||||
|
eris_consolelog = False
|
||||||
|
eris_TRTpatch = False
|
||||||
|
eris_imagelimit = False
|
||||||
|
|
||||||
|
|
||||||
def script_name_to_index(name, scripts):
|
def script_name_to_index(name, scripts):
|
||||||
try:
|
try:
|
||||||
|
@ -337,6 +369,40 @@ class Api:
|
||||||
return script_args
|
return script_args
|
||||||
|
|
||||||
def text2imgapi(self, txt2imgreq: models.StableDiffusionTxt2ImgProcessingAPI):
|
def text2imgapi(self, txt2imgreq: models.StableDiffusionTxt2ImgProcessingAPI):
|
||||||
|
|
||||||
|
# Eris TRTpacht
|
||||||
|
if eris_TRTpatch:
|
||||||
|
txt2imgreq.width = round(txt2imgreq.width / 64) * 64
|
||||||
|
txt2imgreq.height = round(txt2imgreq.height / 64) * 64
|
||||||
|
# Eris ______
|
||||||
|
|
||||||
|
|
||||||
|
# Eris imagelimit 1024x1024 35 Steps
|
||||||
|
if eris_imagelimit:
|
||||||
|
# Add a check to only resize if the dimensions are larger than 1024
|
||||||
|
if txt2imgreq.width > 1024 or txt2imgreq.height > 1024:
|
||||||
|
# Calculate the aspect ratio
|
||||||
|
aspect_ratio = txt2imgreq.width / txt2imgreq.height
|
||||||
|
# Scale down to the maximum allowed dimensions while retaining the aspect ratio
|
||||||
|
if aspect_ratio > 1: # If the image is wider than it is tall
|
||||||
|
txt2imgreq.width = 1024
|
||||||
|
txt2imgreq.height = round(1024 / aspect_ratio)
|
||||||
|
else: # If the image is as wide as it is tall or taller
|
||||||
|
txt2imgreq.height = 1024
|
||||||
|
txt2imgreq.width = round(1024 * aspect_ratio)
|
||||||
|
# Ensure both dimensions are a multiple of 64 (if this is a requirement for your use case)
|
||||||
|
txt2imgreq.width = round(txt2imgreq.width // 64) * 64
|
||||||
|
txt2imgreq.height = round(txt2imgreq.height // 64) * 64
|
||||||
|
# Reduce maximum steps
|
||||||
|
txt2imgreq.steps = min(txt2imgreq.steps, 35)
|
||||||
|
# Eris ______
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Eris console prompt log -> writes promts into the console/terminal window
|
||||||
|
if eris_consolelog:
|
||||||
|
print('[t2i]', txt2imgreq.width, 'x', txt2imgreq.height, '|', txt2imgreq.prompt)
|
||||||
|
# Eris ______
|
||||||
script_runner = scripts.scripts_txt2img
|
script_runner = scripts.scripts_txt2img
|
||||||
if not script_runner.scripts:
|
if not script_runner.scripts:
|
||||||
script_runner.initialize_scripts(False)
|
script_runner.initialize_scripts(False)
|
||||||
|
@ -344,7 +410,10 @@ class Api:
|
||||||
if not self.default_script_arg_txt2img:
|
if not self.default_script_arg_txt2img:
|
||||||
self.default_script_arg_txt2img = self.init_default_script_args(script_runner)
|
self.default_script_arg_txt2img = self.init_default_script_args(script_runner)
|
||||||
selectable_scripts, selectable_script_idx = self.get_selectable_script(txt2imgreq.script_name, script_runner)
|
selectable_scripts, selectable_script_idx = self.get_selectable_script(txt2imgreq.script_name, script_runner)
|
||||||
|
# Eris save generated images -> will be saved in default outputs folder
|
||||||
|
if eris_imagelog:
|
||||||
|
txt2imgreq.save_images = True
|
||||||
|
# Eris ______
|
||||||
populate = txt2imgreq.copy(update={ # Override __init__ params
|
populate = txt2imgreq.copy(update={ # Override __init__ params
|
||||||
"sampler_name": validate_sampler_name(txt2imgreq.sampler_name or txt2imgreq.sampler_index),
|
"sampler_name": validate_sampler_name(txt2imgreq.sampler_name or txt2imgreq.sampler_index),
|
||||||
"do_not_save_samples": not txt2imgreq.save_images,
|
"do_not_save_samples": not txt2imgreq.save_images,
|
||||||
|
@ -362,7 +431,17 @@ class Api:
|
||||||
|
|
||||||
send_images = args.pop('send_images', True)
|
send_images = args.pop('send_images', True)
|
||||||
args.pop('save_images', None)
|
args.pop('save_images', None)
|
||||||
|
# Eris Promtlog -> writing daily log file for txt2img into logs folder
|
||||||
|
if eris_imagelog:
|
||||||
|
logfolder = "logs"
|
||||||
|
if not os.path.exists(logfolder):
|
||||||
|
os.makedirs(logfolder)
|
||||||
|
apilogtxt2imgfile = open(f"{logfolder}/{datetime.date.today()}-txt2img.txt", "a")
|
||||||
|
apilogtxt2imgtext = f"[{datetime.datetime.now()}] Prompt: {txt2imgreq.prompt} | Negative prompt: {txt2imgreq.negative_prompt} | Steps: {txt2imgreq.steps} | Size: {txt2imgreq.width}x{txt2imgreq.height} | CFG: {txt2imgreq.cfg_scale}"
|
||||||
|
#replace newlines and returns to keep the prompt in one line
|
||||||
|
apilogtxt2imgtext.replace("\n", " ").replace("\r", " ")
|
||||||
|
apilogtxt2imgfile.write(f"{apilogtxt2imgtext}\n")
|
||||||
|
# Eris ______
|
||||||
with self.queue_lock:
|
with self.queue_lock:
|
||||||
with closing(StableDiffusionProcessingTxt2Img(sd_model=shared.sd_model, **args)) as p:
|
with closing(StableDiffusionProcessingTxt2Img(sd_model=shared.sd_model, **args)) as p:
|
||||||
p.is_api = True
|
p.is_api = True
|
||||||
|
@ -387,6 +466,37 @@ class Api:
|
||||||
return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js())
|
return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js())
|
||||||
|
|
||||||
def img2imgapi(self, img2imgreq: models.StableDiffusionImg2ImgProcessingAPI):
|
def img2imgapi(self, img2imgreq: models.StableDiffusionImg2ImgProcessingAPI):
|
||||||
|
# Eris TRTpatch
|
||||||
|
if eris_TRTpatch:
|
||||||
|
img2imgreq.width = round(img2imgreq.width / 64) * 64
|
||||||
|
img2imgreq.height = round(img2imgreq.height / 64) * 64
|
||||||
|
# Eris ______
|
||||||
|
|
||||||
|
# Eris imagelimit 1024x1024 35 Steps
|
||||||
|
if eris_imagelimit:
|
||||||
|
# Add a check to only resize if the dimensions are larger than 1024
|
||||||
|
if img2imgreq.width > 1024 or img2imgreq.height > 1024:
|
||||||
|
# Calculate the aspect ratio of the input image
|
||||||
|
aspect_ratio = img2imgreq.width / img2imgreq.height
|
||||||
|
# Scale down to the maximum allowed dimensions while retaining the aspect ratio
|
||||||
|
if aspect_ratio > 1: # If the image is wider than it is tall
|
||||||
|
img2imgreq.width = 1024
|
||||||
|
img2imgreq.height = round(1024 / aspect_ratio)
|
||||||
|
else: # If the image is as wide as it is tall or taller
|
||||||
|
img2imgreq.height = 1024
|
||||||
|
img2imgreq.width = round(1024 * aspect_ratio)
|
||||||
|
# Ensure both dimensions are a multiple of 64 (if this is a requirement for your use case)
|
||||||
|
img2imgreq.width = round(img2imgreq.width / 64) * 64
|
||||||
|
img2imgreq.height = round(img2imgreq.height / 64) * 64
|
||||||
|
# Reduce maximum steps
|
||||||
|
img2imgreq.steps = min(img2imgreq.steps, 35)
|
||||||
|
# Eris ______
|
||||||
|
|
||||||
|
# Eris console prompt log -> writes promts into the console/terminal window
|
||||||
|
if eris_consolelog:
|
||||||
|
print('[i2i]', img2imgreq.width, 'x', img2imgreq.height, '|', img2imgreq.prompt)
|
||||||
|
# Eris ______
|
||||||
|
|
||||||
init_images = img2imgreq.init_images
|
init_images = img2imgreq.init_images
|
||||||
if init_images is None:
|
if init_images is None:
|
||||||
raise HTTPException(status_code=404, detail="Init image not found")
|
raise HTTPException(status_code=404, detail="Init image not found")
|
||||||
|
@ -402,7 +512,10 @@ class Api:
|
||||||
if not self.default_script_arg_img2img:
|
if not self.default_script_arg_img2img:
|
||||||
self.default_script_arg_img2img = self.init_default_script_args(script_runner)
|
self.default_script_arg_img2img = self.init_default_script_args(script_runner)
|
||||||
selectable_scripts, selectable_script_idx = self.get_selectable_script(img2imgreq.script_name, script_runner)
|
selectable_scripts, selectable_script_idx = self.get_selectable_script(img2imgreq.script_name, script_runner)
|
||||||
|
# Eris save generated images -> will be saved in default outputs folder
|
||||||
|
if eris_imagelog:
|
||||||
|
img2imgreq.save_images = True
|
||||||
|
# Eris ______
|
||||||
populate = img2imgreq.copy(update={ # Override __init__ params
|
populate = img2imgreq.copy(update={ # Override __init__ params
|
||||||
"sampler_name": validate_sampler_name(img2imgreq.sampler_name or img2imgreq.sampler_index),
|
"sampler_name": validate_sampler_name(img2imgreq.sampler_name or img2imgreq.sampler_index),
|
||||||
"do_not_save_samples": not img2imgreq.save_images,
|
"do_not_save_samples": not img2imgreq.save_images,
|
||||||
|
@ -422,7 +535,18 @@ class Api:
|
||||||
|
|
||||||
send_images = args.pop('send_images', True)
|
send_images = args.pop('send_images', True)
|
||||||
args.pop('save_images', None)
|
args.pop('save_images', None)
|
||||||
|
# Eris Promtlog -> writing daily log file for txt2img into logs folder
|
||||||
|
if eris_promtlog:
|
||||||
|
logfolder = "logs"
|
||||||
|
if not os.path.exists(logfolder):
|
||||||
|
os.makedirs(logfolder)
|
||||||
|
apilogimg2imgfile = open(f"{logfolder}/{datetime.date.today()}-img2img.txt", "a")
|
||||||
|
apilogimg2imgtext = f"[{datetime.datetime.now()}] Prompt: {img2imgreq.prompt} | Negative prompt: {img2imgreq.negative_prompt} | Steps: {img2imgreq.steps} | Size: {img2imgreq.width}x{img2imgreq.height} | CFG: {img2imgreq.cfg_scale} | Denoising: {img2imgreq.denoising_strength}"
|
||||||
|
#replace newlines and returns to keep the prompt in one line
|
||||||
|
apilogimg2imgtext.replace("\n", " ").replace("\r", " ")
|
||||||
|
apilogimg2imgfile.write(f"{apilogimg2imgtext}\n")
|
||||||
|
# Eris ______
|
||||||
|
|
||||||
with self.queue_lock:
|
with self.queue_lock:
|
||||||
with closing(StableDiffusionProcessingImg2Img(sd_model=shared.sd_model, **args)) as p:
|
with closing(StableDiffusionProcessingImg2Img(sd_model=shared.sd_model, **args)) as p:
|
||||||
p.init_images = [decode_base64_to_image(x) for x in init_images]
|
p.init_images = [decode_base64_to_image(x) for x in init_images]
|
||||||
|
|
Loading…
Reference in New Issue