Spaces:
Runtime error
Runtime error
File size: 8,848 Bytes
ff1e8e9 e6dfc7b ff1e8e9 b8105d0 ff1e8e9 df7075a ff1e8e9 df7075a ff1e8e9 df7075a 2455b73 df7075a ff1e8e9 b8105d0 2455b73 b8105d0 c07c981 ff1e8e9 df7075a ff1e8e9 cdea98e ff1e8e9 df7075a ff1e8e9 c15f9cf cdea98e e4d8566 599e14e cdea98e 2455b73 df7075a ff1e8e9 df7075a 2455b73 c07c981 2455b73 e43f7ba ff1e8e9 df7075a ff1e8e9 f49fd2a e43f7ba ff1e8e9 df7075a f49fd2a ff1e8e9 df7075a ff1e8e9 2453ef4 df7075a f49fd2a e43f7ba f49fd2a b8105d0 5ff67bd f49fd2a b8105d0 e43f7ba df7075a f49fd2a df7075a e43f7ba df7075a f49fd2a e43f7ba df7075a e43f7ba 2453ef4 df7075a f49fd2a ff1e8e9 e43f7ba ff1e8e9 2453ef4 2455b73 |
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
import os
os.system('pip uninstall -y gradio_fake3d')
os.system('pip install gradio_fake3d-0.0.3-py3-none-any.whl')
import gradio as gr
import re
from gradio_fake3d import Fake3D
from PIL import Image
from Rodin import Generator, crop_image, log
from constant import *
generator = Generator(USER, PASSWORD, TOKEN)
change_button_name = """
function updateButton(input) {
var buttonGenerate = document.getElementById('button_generate');
buttonGenerate.innerText = 'Redo';
return '';
}
"""
change_button_name_to_generating = """
function updateButton(input) {
var buttonGenerate = document.getElementById('button_generate');
buttonGenerate.innerText = 'Generating...';
return '';
}
"""
reset_button_name = """
function updateButton(input) {
var buttonGenerate = document.getElementById('button_generate');
buttonGenerate.innerText = 'Generate';
return '';
}
"""
jump_to_rodin = """
function redirectToGithub(input) {
if (input.includes('OpenClay')) {
window.open("https://github.com/CLAY-3D/OpenCLAY", "_blank");
}
return "Rodin Gen-1(0525)";
}
"""
html_content = """
<div style="text-align: center;">
<h1>Rodin Gen-1</h1>
<div style="display: flex; justify-content: space-around;">
<p><strong>Rodin Gen-1:</strong> <a href="https://hyperhuman.top/rodin" target="_blank">https://hyperhuman.top/rodin</a></p>
<p><strong>Github:</strong> <a href="https://github.com/CLAY-3D/OpenCLAY" target="_blank">https://github.com/CLAY-3D/OpenCLAY</a></p>
</div>
</div>
"""
options = [
"Rodin Gen-1(0525)",
"OpenClay(600M) - Coming soon",
"OpenClay(200M) - Coming soon"
]
example = [
["assets/00.png"],
["assets/08.png"],
["assets/13.png"],
["assets/24.PNG"],
["assets/30.png"],
["assets/42.png"],
["assets/46.png"]
]
def do_nothing(text):
return ""
def handle_selection(selection):
return "Rodin Gen-1(0525)"
def hint_in_prompt(hint, prompt):
return re.search(fr"{hint[:-1]}", prompt) is not None
def prompt_remove_hint(prompt, hint):
return re.sub(fr"\s*{hint[:-1]}[\.,]*", "", prompt)
def handle_hint_change(prompt: str, prompt_hint):
prompt = prompt.strip()
if prompt != "" and not prompt.endswith("."):
prompt = prompt + "."
for _, hint in PROMPT_HINT_LIST:
if hint in prompt_hint:
if not hint_in_prompt(hint, prompt):
prompt = prompt + " " + hint
else:
prompt = prompt_remove_hint(prompt, hint)
prompt = prompt.strip()
return prompt
def handle_prompt_change(prompt):
hint_list = []
for _, hint in PROMPT_HINT_LIST:
if hint_in_prompt(hint, prompt):
hint_list.append(hint)
return hint_list
def clear_task(task_input=None):
"""_summary_
[cache_task_uuid, block_prompt, block_prompt_hint, fake3d]
"""
log("INFO", "Clearing task...")
return "", "", "", [], "assets/white_image.png"
def clear_task_id():
return ""
def return_render(image):
image = Image.fromarray(image)
return image, crop_image(image, DEFAULT)
def crop_image_default(image):
return crop_image(image, DEFAULT)
def crop_image_metal(image):
return crop_image(image, METAL)
def crop_image_contrast(image):
return crop_image(image, CONTRAST)
def crop_image_normal(image):
return crop_image(image, NORMAL)
with gr.Blocks() as demo:
gr.HTML(html_content)
with gr.Row():
with gr.Column():
block_image = gr.Image(height=256, image_mode="RGB", sources="upload", elem_classes="elem_imageupload", type="filepath")
block_model_card = gr.Dropdown(choices=options, label="Model Card", value="Rodin Gen-1(0525)", interactive=True)
with gr.Group():
block_prompt = gr.Textbox(
value="",
placeholder="Auto generated description of Image",
lines=1,
show_label=True,
label="Prompt",
)
block_prompt_hint = gr.CheckboxGroup(value="Labels", choices=PROMPT_HINT_LIST, show_label=False)
with gr.Column():
with gr.Group():
fake3d = Fake3D(interactive=False, label="3D Preview")
with gr.Row():
button_generate = gr.Button(value="Generate", variant="primary", elem_id="button_generate")
with gr.Column(min_width=200, scale=20):
with gr.Row():
block_default = gr.Button("Default", min_width=0)
block_metal = gr.Button("Metal", min_width=0)
with gr.Row():
block_contrast = gr.Button("Contrast", min_width=0)
block_normal = gr.Button("Normal", min_width=0)
button_more = gr.Button(value="Download from Rodin", variant="primary", link=rodin_url)
gr.Markdown("""
**TIPS**:
1. Upload an image to generate 3D geometry.
2. Click Redo to regenerate the model.
3. 4 buttons to switch the view.
4. Swipe to rotate the model.
""")
cache_task_uuid = gr.Text(value="", visible=False)
cache_raw_image = gr.Image(visible=False, type="pil")
cacha_empty = gr.Text(visible=False)
cache_image_base64 = gr.Text(visible=False)
block_example = gr.Examples(
examples=example,
fn=clear_task,
inputs=[block_image],
outputs=[cache_image_base64, cache_task_uuid, block_prompt, block_prompt_hint, fake3d],
run_on_click=True,
cache_examples=True,
label="Examples"
)
block_image.upload(
fn=do_nothing,
js=change_button_name_to_generating,
inputs=[cacha_empty],
outputs=[cacha_empty],
queue=False
).success(
fn=generator.preprocess,
inputs=[block_prompt, block_image, cache_image_base64, cache_task_uuid],
outputs=[block_prompt, cache_image_base64],
show_progress="minimal",
queue=False
).success(
fn=generator.generate_mesh,
inputs=[block_prompt, cache_image_base64, cache_task_uuid],
outputs=[cache_raw_image, cache_task_uuid, fake3d],
queue=True
).success(
fn=do_nothing,
js=change_button_name,
inputs=[cacha_empty],
outputs=[cacha_empty],
queue=False
)
block_image.clear(
fn=do_nothing,
js=reset_button_name,
inputs=[cacha_empty],
outputs=[cacha_empty],
queue=False
).then(
fn=clear_task,
outputs=[cache_image_base64, cache_task_uuid, block_prompt, block_prompt_hint, fake3d],
show_progress="hidden",
queue=False
)
button_generate.click(
fn=do_nothing,
js=change_button_name_to_generating,
inputs=[cacha_empty],
outputs=[cacha_empty],
queue=False
).success(
fn=generator.preprocess,
inputs=[block_prompt, block_image, cache_image_base64, cache_task_uuid],
outputs=[block_prompt, cache_image_base64],
show_progress="minimal",
queue=False
).success(
fn=generator.generate_mesh,
inputs=[block_prompt, cache_image_base64, cache_task_uuid],
outputs=[cache_raw_image, cache_task_uuid, fake3d],
queue=True
).then(
fn=do_nothing,
js=change_button_name,
inputs=[cacha_empty],
outputs=[cacha_empty],
queue=False
)
block_default.click(fn=crop_image_default, inputs=[cache_raw_image], outputs=fake3d, show_progress="minimal")
block_metal.click(fn=crop_image_metal, inputs=[cache_raw_image], outputs=fake3d, show_progress="minimal")
block_contrast.click(fn=crop_image_contrast, inputs=[cache_raw_image], outputs=fake3d, show_progress="minimal")
block_normal.click(fn=crop_image_normal, inputs=[cache_raw_image], outputs=fake3d, show_progress="minimal")
button_more.click()
block_prompt_hint.input(
fn=handle_hint_change, inputs=[block_prompt, block_prompt_hint], outputs=[block_prompt],
show_progress="hidden",
queue=False,
)
block_prompt.change(
fn=handle_prompt_change,
inputs=[block_prompt],
outputs=[block_prompt_hint],
trigger_mode="always_last",
show_progress="hidden",
)
block_model_card.change(fn=handle_selection, inputs=[block_model_card], outputs=[block_model_card], show_progress="hidden", js=jump_to_rodin)
if __name__ == "__main__":
demo.queue()
demo.launch(show_api=False, show_error=True)
|