未验证 提交 5cf3c3bb 编写于 作者: G Guoxia Wang 提交者: GitHub

fix cache directory and app style (#5705)

LGTM
上级 dfe7e97c
import gradio as gr import gradio as gr
from predictor import Predictor from predictor import Predictor
model_path = "paddlecv://models/vit/v2.4/imagenet2012-ViT-B_16-224_infer.pdmodel" model_path = "paddlecv://models/vit/v2.4/imagenet2012-ViT-B_16-224_infer.pdmodel"
params_path = "paddlecv://models/vit/v2.4/imagenet2012-ViT-B_16-224_infer.pdiparams" params_path = "paddlecv://models/vit/v2.4/imagenet2012-ViT-B_16-224_infer.pdiparams"
label_path = "paddlecv://dataset/imagenet2012_labels.txt" label_path = "paddlecv://dataset/imagenet2012_labels.txt"
predictor = None
def model_inference(image): def model_inference(image):
predictor = Predictor(model_path=model_path, params_path=params_path, label_path=label_path) global predictor
if predictor is None:
predictor = Predictor(
model_path=model_path,
params_path=params_path,
label_path=label_path)
scores, labels = predictor.predict(image) scores, labels = predictor.predict(image)
json_out = {"scores": scores.tolist(), "labels": labels.tolist()} json_out = {"scores": scores.tolist(), "labels": labels.tolist()}
return image, json_out return image, json_out
...@@ -22,13 +29,15 @@ with gr.Blocks() as demo: ...@@ -22,13 +29,15 @@ with gr.Blocks() as demo:
with gr.Column(scale=1, min_width=100): with gr.Column(scale=1, min_width=100):
img_in = gr.Image(value="https://plsc.bj.bcebos.com/dataset/test_images/cat.jpg",label="Input").style(height=200) img_in = gr.Image(
value="https://plsc.bj.bcebos.com/dataset/test_images/cat.jpg",
label="Input")
with gr.Row(): with gr.Row():
btn1 = gr.Button("Clear") btn1 = gr.Button("Clear")
btn2 = gr.Button("Submit") btn2 = gr.Button("Submit")
img_out = gr.Image(label="Output").style(height=200) img_out = gr.Image(label="Output")
json_out = gr.JSON(label="jsonOutput") json_out = gr.JSON(label="jsonOutput")
btn2.click(fn=model_inference, inputs=img_in, outputs=[img_out, json_out]) btn2.click(fn=model_inference, inputs=img_in, outputs=[img_out, json_out])
......
...@@ -33,10 +33,10 @@ __all__ = [ ...@@ -33,10 +33,10 @@ __all__ = [
'get_data_path', 'get_data_path',
] ]
WEIGHTS_HOME = osp.expanduser("~/.cache/paddlecv/models") WEIGHTS_HOME = osp.expanduser("~/.cache/paddlecv/models/plsc")
CONFIGS_HOME = osp.expanduser("~/.cache/paddlecv/configs") CONFIGS_HOME = osp.expanduser("~/.cache/paddlecv/configs/plsc")
DICTS_HOME = osp.expanduser("~/.cache/paddlecv/dicts") DICTS_HOME = osp.expanduser("~/.cache/paddlecv/dicts/plsc/")
DATA_HOME = osp.expanduser("~/.cache/paddlecv/dataset") DATA_HOME = osp.expanduser("~/.cache/paddlecv/dataset/plsc")
# dict of {dataset_name: (download_info, sub_dirs)} # dict of {dataset_name: (download_info, sub_dirs)}
# download info: [(url, md5sum)] # download info: [(url, md5sum)]
...@@ -68,7 +68,7 @@ def get_model_path(path): ...@@ -68,7 +68,7 @@ def get_model_path(path):
if not is_url(path): if not is_url(path):
return path return path
url = parse_url(path) url = parse_url(path)
path, _ = get_path(url, WEIGHTS_HOME, path_depth=2) path, _ = get_path(url, WEIGHTS_HOME, path_depth=3)
return path return path
...@@ -79,7 +79,7 @@ def get_data_path(path): ...@@ -79,7 +79,7 @@ def get_data_path(path):
if not is_url(path): if not is_url(path):
return path return path
url = parse_url(path) url = parse_url(path)
path, _ = get_path(url, DATA_HOME, path_depth=2) path, _ = get_path(url, DATA_HOME, path_depth=1)
return path return path
...@@ -162,7 +162,6 @@ def _download(url, path, md5sum=None): ...@@ -162,7 +162,6 @@ def _download(url, path, md5sum=None):
raise RuntimeError("Download from {} failed. " raise RuntimeError("Download from {} failed. "
"Retry limit reached".format(url)) "Retry limit reached".format(url))
# NOTE: windows path join may incur \, which is invalid in url # NOTE: windows path join may incur \, which is invalid in url
if sys.platform == "win32": if sys.platform == "win32":
url = url.replace('\\', '/') url = url.replace('\\', '/')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册