Spaces:
Runtime error
Runtime error
File size: 5,250 Bytes
ea476a5 35536db e04439f ea476a5 19d3041 1215f5e 19d3041 4f05465 19d3041 ea476a5 19d3041 ea476a5 19d3041 ea476a5 8c446e9 e477ce0 8c446e9 9135f86 8c446e9 9135f86 8c446e9 1215f5e 8c446e9 fb910bc 8c446e9 9135f86 ea476a5 4f05465 ea476a5 9135f86 ea476a5 fb910bc 1215f5e ea476a5 1215f5e 35536db ea476a5 9135f86 ea476a5 9135f86 ea476a5 9135f86 ea476a5 8a8b805 ea476a5 fb910bc eda1740 fb910bc eda1740 ea476a5 35536db 1215f5e fb910bc 1215f5e fb910bc 1215f5e 6784f8f ea476a5 8c446e9 35536db ea476a5 1215f5e ea476a5 8c446e9 35536db 8c446e9 ea476a5 35536db 8c446e9 ea476a5 8c446e9 1215f5e ea476a5 b26b6c5 fb910bc |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import gradio as gr
import sys
sys.path.append(".")
sys.path.append("..")
from model_loader import Model
from PIL import Image
import cv2
import io
from huggingface_hub import snapshot_download
import json
# disable if running on another environment
RESIZE = True
models_path = snapshot_download(repo_id="radames/UserControllableLT", repo_type="model")
# models fron pretrained/latent_transformer folder
models_files = {
"anime": "anime.pt",
"car": "car.pt",
"cat": "cat.pt",
"church": "church.pt",
"ffhq": "ffhq.pt",
}
models = {name: Model(models_path + "/" + path) for name, path in models_files.items()}
canvas_html = """<draggan-canvas id="canvas-root" style='display:flex;max-width: 500px;margin: 0 auto;'></draggan-canvas>"""
load_js = """
async () => {
const script = document.createElement('script');
script.type = "module"
script.src = "file=custom_component.js"
document.head.appendChild(script);
}
"""
image_change = """
async (base64img) => {
const canvasEl = document.getElementById("canvas-root");
canvasEl.loadBase64Image(base64img);
}
"""
reset_stop_points = """
async () => {
const canvasEl = document.getElementById("canvas-root");
canvasEl.resetStopPoints();
}
"""
default_dxdysxsy = json.dumps(
{"dx": 1, "dy": 0, "sx": 128, "sy": 128, "stopPoints": []}
)
def cv_to_pil(img):
img = Image.fromarray(cv2.cvtColor(img.astype("uint8"), cv2.COLOR_BGR2RGB))
if RESIZE:
img = img.resize((128, 128))
return img
def random_sample(model_name: str):
model = models[model_name]
img, latents = model.random_sample()
img_pil = cv_to_pil(img)
print(img_pil)
return img_pil, model_name, latents
def transform(model_state, latents_state, dxdysxsy=default_dxdysxsy, dz=0):
if "w1" not in latents_state or "w1_initial" not in latents_state:
raise gr.Error("Generate a random sample first")
data = json.loads(dxdysxsy)
model = models[model_state]
dx = int(data["dx"])
dy = int(data["dy"])
sx = int(data["sx"])
sy = int(data["sy"])
stop_points = [[int(x), int(y)] for x, y in data["stopPoints"]]
img, latents_state = model.transform(
latents_state, dz, dxy=[dx, dy], sxsy=[sx, sy], stop_points=stop_points
)
img_pil = cv_to_pil(img)
return img_pil, latents_state
def change_style(image: Image.Image, model_state, latents_state):
model = models[model_state]
img, latents_state = model.change_style(latents_state)
img_pil = cv_to_pil(img)
return img_pil, latents_state
def reset(model_state, latents_state):
model = models[model_state]
img, latents_state = model.reset(latents_state)
img_pil = cv_to_pil(img)
return img_pil, latents_state
def image_click(evt: gr.SelectData):
click_pos = evt.index
return click_pos
with gr.Blocks() as block:
model_state = gr.State(value="cat")
latents_state = gr.State({})
gr.Markdown(
"""# UserControllableLT: User Controllable Latent Transformer
Unofficial Gradio Demo
**Author**: Yuki Endo\\
**Paper**: [2208.12408](http://arxiv.org/abs/2208.12408)\\
**Code**: [UserControllableLT](https://github.com/endo-yuki-t/UserControllableLT)
<small>
Double click to add or remove stop points.
<small>
"""
)
with gr.Row():
with gr.Column():
model_name = gr.Dropdown(
choices=list(models_files.keys()),
label="Select Pretrained Model",
value="cat",
)
with gr.Row():
button = gr.Button("Random sample")
reset_btn = gr.Button("Reset")
change_style_bt = gr.Button("Change style")
dxdysxsy = gr.Textbox(
label="dxdysxsy",
value=default_dxdysxsy,
elem_id="dxdysxsy",
visible=False,
)
dz = gr.Slider(
minimum=-15, maximum=15, step_size=0.01, label="zoom", value=0.0
)
image = gr.Image(type="pil", visible=False, preprocess=False)
with gr.Column():
html = gr.HTML(canvas_html, label="output")
button.click(
random_sample, inputs=[model_name], outputs=[image, model_state, latents_state]
)
reset_btn.click(
reset,
inputs=[model_state, latents_state],
outputs=[image, latents_state],
queue=False,
).then(None, None, None, _js=reset_stop_points, queue=False)
change_style_bt.click(
change_style,
inputs=[image, model_state, latents_state],
outputs=[image, latents_state],
)
dxdysxsy.change(
transform,
inputs=[model_state, latents_state, dxdysxsy, dz],
outputs=[image, latents_state],
show_progress=False,
)
dz.change(
transform,
inputs=[model_state, latents_state, dxdysxsy, dz],
outputs=[image, latents_state],
show_progress=False,
)
image.change(None, inputs=[image], outputs=None, _js=image_change)
block.load(None, None, None, _js=load_js)
block.load(
random_sample, inputs=[model_name], outputs=[image, model_state, latents_state]
)
block.queue(api_open=False)
block.launch(show_api=False)
|