Spaces:
Runtime error
Runtime error
Initialise
Browse files- .gitignore +1 -0
- .pylintrc +2 -0
- app.py +102 -4
- requirements.txt +1 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
./outputs
|
.pylintrc
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
[pylint.messages_control]
|
2 |
+
disable=W0612,W0621,C0411,W0611,C0114,C0116,W1514,C0103,W0718,W0602
|
app.py
CHANGED
@@ -1,9 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
from pathlib import Path
|
7 |
import gradio as gr
|
8 |
|
9 |
+
# parameters which can be customized in settings.json of webui
|
10 |
|
11 |
+
initial_width = 512
|
12 |
+
initial_height = 768
|
13 |
|
14 |
+
params = {
|
15 |
+
"enable_SD_api": True,
|
16 |
+
"address": "https://06a4-2001-d08-e3-41de-60e3-1037-3d62-daf9.ngrok-free.app",
|
17 |
+
"save_img": True,
|
18 |
+
"SD_model": "Unused", # not actually used
|
19 |
+
"prompt_prefix": "4k, 8k, masterpiece, artstation, in dressed",
|
20 |
+
"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",
|
21 |
+
"width": initial_width,
|
22 |
+
"height": initial_height,
|
23 |
+
"restore_faces": False,
|
24 |
+
}
|
25 |
|
26 |
+
pic_id = 0
|
27 |
+
|
28 |
+
|
29 |
+
# Get and save the Stable Diffusion-generated picture
|
30 |
+
def get_SD_pictures(description):
|
31 |
+
global params, pic_id
|
32 |
+
payload = {
|
33 |
+
"prompt": params["prompt_prefix"] + description,
|
34 |
+
"seed": -1,
|
35 |
+
"sampler_name": "Euler a",
|
36 |
+
"steps": 20,
|
37 |
+
"cfg_scale": 7,
|
38 |
+
"width": params["width"],
|
39 |
+
"height": params["height"],
|
40 |
+
"restore_faces": params["restore_faces"],
|
41 |
+
"negative_prompt": params["negative_prompt"],
|
42 |
+
}
|
43 |
+
|
44 |
+
response = requests.post(
|
45 |
+
url=f'{params["address"]}/sdapi/v1/txt2img', json=payload, timeout=10
|
46 |
+
)
|
47 |
+
|
48 |
+
r = response.json()
|
49 |
+
|
50 |
+
visible_result = ""
|
51 |
+
for img_str in r["images"]:
|
52 |
+
image = Image.open(io.BytesIO(base64.b64decode(img_str.split(",", 1)[0])))
|
53 |
+
|
54 |
+
if not os.path.exists("outputs"):
|
55 |
+
os.makedirs("outputs")
|
56 |
+
|
57 |
+
if params["save_img"]:
|
58 |
+
output_file = Path(f"outputs/{pic_id:06d}.png")
|
59 |
+
image.save(output_file.as_posix())
|
60 |
+
pic_id += 1
|
61 |
+
|
62 |
+
# lower the resolution of received images for the chat
|
63 |
+
# otherwise the log size gets out of control quickly
|
64 |
+
# with all the base64 values in visible history
|
65 |
+
image.thumbnail((512, 512))
|
66 |
+
buffered = io.BytesIO()
|
67 |
+
image.save(buffered, format="JPEG")
|
68 |
+
buffered.seek(0)
|
69 |
+
image_bytes = buffered.getvalue()
|
70 |
+
img_str = "data:image/jpeg;base64," + base64.b64encode(image_bytes).decode()
|
71 |
+
visible_result = visible_result + f'<img src="{img_str}" alt="{description}">\n'
|
72 |
+
|
73 |
+
return visible_result
|
74 |
+
|
75 |
+
|
76 |
+
def display_image(description: str, kashif: bool, daniel_ho: bool) -> gr.Image:
|
77 |
+
if kashif:
|
78 |
+
description += " <lora:Kashif_v1:0.9>"
|
79 |
+
if daniel_ho:
|
80 |
+
description += " <lora:DanielHo_v1:0.9>"
|
81 |
+
|
82 |
+
if kashif or daniel_ho:
|
83 |
+
params["width"] = 768
|
84 |
+
params["height"] = 1152
|
85 |
+
else:
|
86 |
+
params["width"] = initial_width
|
87 |
+
params["height"] = initial_height
|
88 |
+
|
89 |
+
visible_result = get_SD_pictures(description)
|
90 |
+
image_data = base64.b64decode(visible_result.split(",", 1)[1])
|
91 |
+
image = Image.open(io.BytesIO(image_data))
|
92 |
+
return image
|
93 |
+
|
94 |
+
|
95 |
+
inputs = [
|
96 |
+
gr.inputs.Textbox(lines=2, label="Enter image description"),
|
97 |
+
gr.inputs.Checkbox(label="Kashif"),
|
98 |
+
gr.inputs.Checkbox(label="Daniel Ho"),
|
99 |
+
]
|
100 |
+
|
101 |
+
outputs = gr.outputs.Image(type="pil", label="Generated Image")
|
102 |
+
|
103 |
+
interface = gr.Interface(
|
104 |
+
fn=display_image, inputs=inputs, outputs=outputs, title="DC Image Generator"
|
105 |
+
)
|
106 |
+
|
107 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
requests
|