-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgradio_ui.py
97 lines (83 loc) · 2.93 KB
/
gradio_ui.py
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
import gradio as gr
from PIL import Image
from main import create_image_from_prompt, style_transfer_image_from_prompt
from PIL import Image
# This demo needs to be run from the repo folder.
# python gradio_ui.py
def style_transfer_make_images(image_array, text, strength):
image = Image.fromarray(image_array)
augmentations = [
"",
# "awesome",
# "amazing",
# "beautiful",
# "hd",
# "4k",
# "detailed aesthetic",
# "wonderful",
# "anime hd",
]
images = []
for aug in augmentations:
print(aug)
prompt = text + " " + aug
bio = style_transfer_image_from_prompt(prompt, None, strength, False, image)
save_name = prompt.replace(" ", "-")
save_path = f"outputs/{save_name}.webp"
with open(save_path, "wb") as f:
f.write(bio)
pil_image = Image.open(save_path)
images.append(pil_image)
yield images
return images
def make_images(text, width, height, steps, guidance_scale):
augmentations = [
"",
# "awesome",
# "amazing",
# # "beautiful",
# # "hd",
# # "4k",
# "detailed aesthetic",
# "wonderful",
# "anime hd",
]
images = []
for aug in augmentations:
print(aug)
prompt = text + " " + aug
bio = create_image_from_prompt(prompt, width, height, steps, {"guidance_scale": guidance_scale})
save_name = prompt.replace(" ", "-")
save_path = f"outputs/{save_name}.webp"
with open(save_path, "wb") as f:
f.write(bio)
pil_image = Image.open(save_path)
images.append(pil_image)
yield images
return images
gallery = gr.Gallery(
label="Generated images",
show_label=False,
elem_id="gallery",
columns=[4],
rows=[4],
object_fit="contain",
height="auto",
)
width = gr.Number(value=1024, label="Width")
height = gr.Number(value=1024, label="Height")
strength = gr.Number(value=0.65, label="Strength", step=0.01, minimum=0, maximum=1)
prompt = gr.Textbox(lines=3, label="Prompt", placeholder="Anime detailed hd aesthetic best quality")
image_input = gr.Image(label="Image")
steps = gr.Number(value=8, label="steps", step=8, minimum=0, maximum=80)
guidance_scale = gr.Number(value=1, label="Guidance Scale", step=0.1, minimum=0, maximum=20)
with gr.Blocks() as _block:
interface_inputs = ["text", width, height, steps, guidance_scale]
with gr.Tab("Text To Image"):
demo = gr.Interface(fn=make_images, inputs=interface_inputs, outputs=gallery)
with gr.Tab("Style Transfer"):
style_transfer_interface_inputs = [image_input, prompt, strength]
demo_style_transfer = gr.Interface(fn=style_transfer_make_images, inputs=style_transfer_interface_inputs, outputs=gallery)
if __name__ == "__main__":
# demo.launch()
_block.launch()