Spaces:
Runtime error
Runtime error
eliphatfs
commited on
Commit
·
cd542fa
1
Parent(s):
471a386
Better UX: no refresh inside form.
Browse files
app.py
CHANGED
|
@@ -5,10 +5,12 @@ from huggingface_hub import HfFolder, snapshot_download
|
|
| 5 |
|
| 6 |
@st.cache_data
|
| 7 |
def load_support():
|
| 8 |
-
|
|
|
|
| 9 |
sys.path.append(snapshot_download("OpenShape/openshape-demo-support"))
|
| 10 |
|
| 11 |
|
|
|
|
| 12 |
load_support()
|
| 13 |
|
| 14 |
|
|
@@ -43,13 +45,15 @@ torch.set_grad_enabled(False)
|
|
| 43 |
|
| 44 |
from openshape.demo import misc_utils, classification, caption, sd_pc2img, retrieval
|
| 45 |
|
|
|
|
| 46 |
st.title("OpenShape Demo")
|
|
|
|
| 47 |
prog = st.progress(0.0, "Idle")
|
| 48 |
-
tab_cls,
|
| 49 |
"Classification",
|
| 50 |
-
"Retrieval
|
| 51 |
-
"Retrieval
|
| 52 |
-
"Retrieval
|
| 53 |
"Image Generation",
|
| 54 |
"Captioning",
|
| 55 |
])
|
|
@@ -62,7 +66,9 @@ def demo_classification():
|
|
| 62 |
if len(cats) > 64:
|
| 63 |
st.error('Maximum 64 custom categories supported in the demo')
|
| 64 |
return
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
pc = load_data(prog)
|
| 67 |
col2 = misc_utils.render_pc(pc)
|
| 68 |
prog.progress(0.5, "Running Classification")
|
|
@@ -72,7 +78,7 @@ def demo_classification():
|
|
| 72 |
st.text(cat)
|
| 73 |
st.caption("Similarity %.4f" % sim)
|
| 74 |
prog.progress(1.0, "Idle")
|
| 75 |
-
if
|
| 76 |
pc = load_data(prog)
|
| 77 |
col2 = misc_utils.render_pc(pc)
|
| 78 |
prog.progress(0.5, "Computing Category Embeddings")
|
|
@@ -89,40 +95,42 @@ def demo_classification():
|
|
| 89 |
|
| 90 |
|
| 91 |
def demo_captioning():
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
def demo_pc2img():
|
| 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 |
def retrieval_results(results):
|
|
@@ -144,43 +152,46 @@ def retrieval_results(results):
|
|
| 144 |
|
| 145 |
def demo_retrieval():
|
| 146 |
with tab_text:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
with tab_img:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
| 171 |
|
| 172 |
with tab_pc:
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
try:
|
|
|
|
| 5 |
|
| 6 |
@st.cache_data
|
| 7 |
def load_support():
|
| 8 |
+
if st.secrets.has_key('etoken'):
|
| 9 |
+
HfFolder().save_token(st.secrets['etoken'])
|
| 10 |
sys.path.append(snapshot_download("OpenShape/openshape-demo-support"))
|
| 11 |
|
| 12 |
|
| 13 |
+
# st.set_page_config(layout='wide')
|
| 14 |
load_support()
|
| 15 |
|
| 16 |
|
|
|
|
| 45 |
|
| 46 |
from openshape.demo import misc_utils, classification, caption, sd_pc2img, retrieval
|
| 47 |
|
| 48 |
+
|
| 49 |
st.title("OpenShape Demo")
|
| 50 |
+
st.caption("For faster inference without waiting in queue, you may clone the space and run it yourself.")
|
| 51 |
prog = st.progress(0.0, "Idle")
|
| 52 |
+
tab_cls, tab_img, tab_text, tab_pc, tab_sd, tab_cap = st.tabs([
|
| 53 |
"Classification",
|
| 54 |
+
"Retrieval w/ Image",
|
| 55 |
+
"Retrieval w/ Text",
|
| 56 |
+
"Retrieval w/ 3D",
|
| 57 |
"Image Generation",
|
| 58 |
"Captioning",
|
| 59 |
])
|
|
|
|
| 66 |
if len(cats) > 64:
|
| 67 |
st.error('Maximum 64 custom categories supported in the demo')
|
| 68 |
return
|
| 69 |
+
lvis_run = st.button("Run Classification on LVIS Categories")
|
| 70 |
+
custom_run = st.button("Run Classification on Custom Categories")
|
| 71 |
+
if lvis_run:
|
| 72 |
pc = load_data(prog)
|
| 73 |
col2 = misc_utils.render_pc(pc)
|
| 74 |
prog.progress(0.5, "Running Classification")
|
|
|
|
| 78 |
st.text(cat)
|
| 79 |
st.caption("Similarity %.4f" % sim)
|
| 80 |
prog.progress(1.0, "Idle")
|
| 81 |
+
if custom_run:
|
| 82 |
pc = load_data(prog)
|
| 83 |
col2 = misc_utils.render_pc(pc)
|
| 84 |
prog.progress(0.5, "Computing Category Embeddings")
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
def demo_captioning():
|
| 98 |
+
with st.form("capform"):
|
| 99 |
+
load_data = misc_utils.input_3d_shape('cap')
|
| 100 |
+
cond_scale = st.slider('Conditioning Scale', 0.0, 4.0, 2.0)
|
| 101 |
+
if st.form_submit_button("Generate a Caption"):
|
| 102 |
+
pc = load_data(prog)
|
| 103 |
+
col2 = misc_utils.render_pc(pc)
|
| 104 |
+
prog.progress(0.5, "Running Generation")
|
| 105 |
+
cap = caption.pc_caption(model_b32, pc, cond_scale)
|
| 106 |
+
st.text(cap)
|
| 107 |
+
prog.progress(1.0, "Idle")
|
| 108 |
|
| 109 |
|
| 110 |
def demo_pc2img():
|
| 111 |
+
with st.form("sdform"):
|
| 112 |
+
load_data = misc_utils.input_3d_shape('sd')
|
| 113 |
+
prompt = st.text_input("Prompt (Optional)")
|
| 114 |
+
noise_scale = st.slider('Variation Level', 0, 5, 1)
|
| 115 |
+
cfg_scale = st.slider('Guidance Scale', 0.0, 30.0, 10.0)
|
| 116 |
+
steps = st.slider('Diffusion Steps', 8, 50, 25)
|
| 117 |
+
width = 640 # st.slider('Width', 480, 640, step=32)
|
| 118 |
+
height = 640 # st.slider('Height', 480, 640, step=32)
|
| 119 |
+
if st.form_submit_button("Generate"):
|
| 120 |
+
pc = load_data(prog)
|
| 121 |
+
col2 = misc_utils.render_pc(pc)
|
| 122 |
+
prog.progress(0.49, "Running Generation")
|
| 123 |
+
if torch.cuda.is_available():
|
| 124 |
+
clip_model.cpu()
|
| 125 |
+
img = sd_pc2img.pc_to_image(
|
| 126 |
+
model_l14, pc, prompt, noise_scale, width, height, cfg_scale, steps,
|
| 127 |
+
lambda i, t, _: prog.progress(0.49 + i / (steps + 1) / 2, "Running Diffusion Step %d" % i)
|
| 128 |
+
)
|
| 129 |
+
if torch.cuda.is_available():
|
| 130 |
+
clip_model.cuda()
|
| 131 |
+
with col2:
|
| 132 |
+
st.image(img)
|
| 133 |
+
prog.progress(1.0, "Idle")
|
| 134 |
|
| 135 |
|
| 136 |
def retrieval_results(results):
|
|
|
|
| 152 |
|
| 153 |
def demo_retrieval():
|
| 154 |
with tab_text:
|
| 155 |
+
with st.form("rtextform"):
|
| 156 |
+
k = st.slider("# Shapes to Retrieve", 1, 100, 16, key='rtext')
|
| 157 |
+
text = st.text_input("Input Text")
|
| 158 |
+
if st.form_submit_button("Run with Text"):
|
| 159 |
+
prog.progress(0.49, "Computing Embeddings")
|
| 160 |
+
device = clip_model.device
|
| 161 |
+
tn = clip_prep(text=[text], return_tensors='pt', truncation=True, max_length=76).to(device)
|
| 162 |
+
enc = clip_model.get_text_features(**tn).float().cpu()
|
| 163 |
+
prog.progress(0.7, "Running Retrieval")
|
| 164 |
+
retrieval_results(retrieval.retrieve(enc, k))
|
| 165 |
+
prog.progress(1.0, "Idle")
|
| 166 |
|
| 167 |
with tab_img:
|
| 168 |
+
with st.form("rimgform"):
|
| 169 |
+
k = st.slider("# Shapes to Retrieve", 1, 100, 16, key='rimage')
|
| 170 |
+
pic = st.file_uploader("Upload an Image")
|
| 171 |
+
if st.form_submit_button("Run with Image"):
|
| 172 |
+
img = Image.open(pic)
|
| 173 |
+
st.image(img)
|
| 174 |
+
prog.progress(0.49, "Computing Embeddings")
|
| 175 |
+
device = clip_model.device
|
| 176 |
+
tn = clip_prep(images=[img], return_tensors="pt").to(device)
|
| 177 |
+
enc = clip_model.get_image_features(pixel_values=tn['pixel_values'].type(half)).float().cpu()
|
| 178 |
+
prog.progress(0.7, "Running Retrieval")
|
| 179 |
+
retrieval_results(retrieval.retrieve(enc, k))
|
| 180 |
+
prog.progress(1.0, "Idle")
|
| 181 |
|
| 182 |
with tab_pc:
|
| 183 |
+
with st.form("rpcform"):
|
| 184 |
+
k = st.slider("# Shapes to Retrieve", 1, 100, 16, key='rpc')
|
| 185 |
+
load_data = misc_utils.input_3d_shape('retpc')
|
| 186 |
+
if st.form_submit_button("Run with Shape"):
|
| 187 |
+
pc = load_data(prog)
|
| 188 |
+
col2 = misc_utils.render_pc(pc)
|
| 189 |
+
prog.progress(0.49, "Computing Embeddings")
|
| 190 |
+
ref_dev = next(model_g14.parameters()).device
|
| 191 |
+
enc = model_g14(torch.tensor(pc[:, [0, 2, 1, 3, 4, 5]].T[None], device=ref_dev)).cpu()
|
| 192 |
+
prog.progress(0.7, "Running Retrieval")
|
| 193 |
+
retrieval_results(retrieval.retrieve(enc, k))
|
| 194 |
+
prog.progress(1.0, "Idle")
|
| 195 |
|
| 196 |
|
| 197 |
try:
|