stella-anime / modules /engine_anime.py
ikmalsaid's picture
added/updated app files
7695918
raw
history blame
4.37 kB
import gradio as ui; import requests; from requests.exceptions import Timeout
from io import BytesIO; from PIL import Image, ImageEnhance; import concurrent.futures, time
###################################################################################################################
# Import from modules
###################################################################################################################
from modules.service_endpoints import *
from modules.input_configs import *
from modules.styles_anime import *
from modules.models_anime import *
from modules.service_configs import *
from modules.engine_upscale_alt import *
###################################################################################################################
# Image generation process
###################################################################################################################
def gen_anime(prompt, model, style, size, quality):
prompt = stella_anime[style].format(prompt=prompt)
print(f"{receive()} -> {prompt}")
data = {
'model_version': (None, '1'),
'prompt': (None, prompt),
'style_id': (None, ckpt_anime[str(model)]),
'negative_prompt': (None, 'hands, face, eyes, legs, (nsfw, naked, nude, sexy)'),
'aspect_ratio': (None, ratio[str(size)]),
'high_res_results': (None, '1'),
'cfg': (None, '9.5'),
'priority': (None, '1'),
}
try:
response = requests.post(mode['generate'], headers=head, files=data, timeout=30)
if len(response.content) < 65 * 1024:
print(reject())
return None
print(done())
if quality == 'Enhanced':
print("better1 -> better output")
better1 = ImageEnhance.Contrast(
ImageEnhance.Color(
ImageEnhance.Brightness(
ImageEnhance.Sharpness(
Image.open(BytesIO(response.content))
).enhance(2.00)
).enhance(1.05)
).enhance(1.05)
).enhance(1.05)
return better1
if quality == 'Enhanced and Upscaled':
print("better2 -> better upscaled output")
better2 = ImageEnhance.Contrast(
ImageEnhance.Color(
ImageEnhance.Brightness(
ImageEnhance.Sharpness(
Image.open(BytesIO(response.content))
).enhance(2.00)
).enhance(1.05)
).enhance(1.05)
).enhance(1.05)
return upscale(better2)
else:
print("original -> raw output")
original = Image.open(BytesIO(response.content))
return original
except Timeout:
print(timeout())
return None
except Exception as e:
print(f"An error occurred: {e}")
ui.Warning(message=single_error)
return None
###################################################################################################################
# 4 image for each generation
###################################################################################################################
def quads_anime(a, b, c, d, e, f, progress=ui.Progress()):
quantities = int(f)
result_list = [None] * quantities
percent = 0
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for i in range(quantities):
future = executor.submit(lambda x: gen_anime(a, b, c, d, e), i)
futures.append(future)
multiplier = 0.99 / quantities
percent += multiplier
progress(percent, desc=f"Processing image {i + 1} of {quantities}")
time.sleep(0.25)
for i, future in enumerate(futures):
result = future.result()
result_list[i] = result
successful_results = [result for result in result_list if result is not None]
if len(successful_results) < quantities:
if quantities == 1:
ui.Warning(message=single_error)
else:
ui.Warning(message=quads_error)
else:
pass
return successful_results