prompt-2-sd / app.py
daveckw's picture
Edit ngrok
5c9ad8f
raw
history blame
4.73 kB
import requests
from PIL import Image
import os
import io
import base64
from pathlib import Path
import gradio as gr
# parameters which can be customized in settings.json of webui
initial_width = 512
initial_height = 512
params = {
"enable_SD_api": True,
"address": "https://fb9f-2001-d08-e3-41de-e9bf-9904-76d2-5584.ngrok-free.app",
"save_img": True,
"SD_model": "Unused", # not actually used
"prompt_prefix": "4k, 8k, masterpiece, artstation, in dressed",
"negative_prompt": "Naked, Nude, EasyNegative, paintings, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, glans,extra fingers,fewer fingers,strange fingers,bad hand, paintings, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, (outdoor:1.6), manboobs, backlight,(ugly:1.331), (duplicate:1.331), (morbid:1.21), (mutilated:1.21), (tranny:1.331), mutated hands, (poorly drawn hands:1.331), blurry, (bad anatomy:1.21), (bad proportions:1.331), extra limbs, (disfigured:1.331), (more than 2 nipples:1.331), (missing arms:1.331), (extra legs:1.331), (fused fingers:1.61051), (too many fingers:1.61051), (unclear eyes:1.331), bad hands, missing fingers, extra digit, (futa:1.1), bad body, NG_DeepNegative_V1_75T,pubic hair, glans, paintings, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, (outdoor:1.6), manboobs, backlight,(ugly:1.331), (duplicate:1.331), (morbid:1.21), (mutilated:1.21), (tranny:1.331), mutated hands, (poorly drawn hands:1.331), blurry, (bad anatomy:1.21), (bad proportions:1.331), extra limbs, (disfigured:1.331), (more than 2 nipples:1.331), (missing arms:1.331), (extra legs:1.331), (fused fingers:1.61051), (too many fingers:1.61051), (unclear eyes:1.331), bad hands, missing fingers, extra digit, (futa:1.1), bad body, NG_DeepNegative_V1_75T,pubic hair, glans",
"width": initial_width,
"height": initial_height,
"restore_faces": False,
}
pic_id = 0
# Get and save the Stable Diffusion-generated picture
def get_SD_pictures(description):
global params, pic_id
payload = {
"prompt": params["prompt_prefix"] + description,
"seed": -1,
"sampler_name": "Euler a",
"steps": 20,
"cfg_scale": 7,
"width": params["width"],
"height": params["height"],
"restore_faces": params["restore_faces"],
"negative_prompt": params["negative_prompt"],
}
response = requests.post(
url=f'{params["address"]}/sdapi/v1/txt2img', json=payload, timeout=10
)
r = response.json()
visible_result = ""
for img_str in r["images"]:
image = Image.open(io.BytesIO(base64.b64decode(img_str.split(",", 1)[0])))
if not os.path.exists("outputs"):
os.makedirs("outputs")
if params["save_img"]:
output_file = Path(f"outputs/{pic_id:06d}.png")
image.save(output_file.as_posix())
pic_id += 1
# lower the resolution of received images for the chat
# otherwise the log size gets out of control quickly
# with all the base64 values in visible history
image.thumbnail((512, 512))
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
buffered.seek(0)
image_bytes = buffered.getvalue()
img_str = "data:image/jpeg;base64," + base64.b64encode(image_bytes).decode()
visible_result = visible_result + f'<img src="{img_str}" alt="{description}">\n'
return visible_result
def display_image(description: str, kashif: bool, daniel_ho: bool) -> gr.Image:
if kashif:
description += " <lora:Kashif_v1:0.9>"
if daniel_ho:
description += " <lora:DanielHo_v1:0.9>"
if kashif or daniel_ho:
params["width"] = 512
params["height"] = 512
else:
params["width"] = initial_width
params["height"] = initial_height
visible_result = get_SD_pictures(description)
print(description)
image_data = base64.b64decode(visible_result.split(",", 1)[1])
image = Image.open(io.BytesIO(image_data))
return image
inputs = [
gr.inputs.Textbox(lines=2, label="Enter image description"),
gr.inputs.Checkbox(label="Kashif"),
gr.inputs.Checkbox(label="Daniel Ho"),
]
outputs = gr.outputs.Image(type="pil", label="Generated Image")
interface = gr.Interface(
fn=display_image, inputs=inputs, outputs=outputs, title="DC Image Generator"
)
interface.launch()