File size: 4,726 Bytes
0784e80
 
 
 
 
 
374f46c
 
0784e80
374f46c
0784e80
79bd211
374f46c
0784e80
 
5c9ad8f
0784e80
 
 
 
 
 
 
 
374f46c
0784e80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579a9b1
 
0784e80
 
 
 
 
5c9ad8f
0784e80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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()