Skip to content

Commit

Permalink
add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijianma committed Mar 8, 2024
1 parent f5f8001 commit 21a9b9c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
16 changes: 8 additions & 8 deletions demos/data_visualization_op_insight/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,20 @@
width: 80%;
height: 80%;
}

.show_image {
justify-content: center;
align-items: center;
display: flex; /* 启用flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
overflow: hidden; /* 如果图像超出容器,就隐藏多余部分 */
}

.show_image img {
width: 50%;
height: 50%;
max-width: 100%; /* 图像最大宽度为容器的50%,但不超过图像原始尺寸 */
max-height: 50%; /* 图像最大高度为容器的50%,但不超过图像原始尺寸 */
height: auto; /* 高度自动调整以保持纵横比 */
}



.project_label {
font-size: 18px; /* 标题字体大小 */
color: #333; /* 字体颜色,这里使用深灰色 */
Expand Down Expand Up @@ -160,12 +162,10 @@
font-size: 0.9em;
}


.project_intro {
display: grid;
place-items: center; /* 完美居中 */
height: 100px; /* 高度 */
width:
font-size: 15px; /* 正文字体大小 */
/* text-align: center; 文字居中 */
color: #555; /* 正文字体颜色,这里使用较浅的灰色 */
Expand Down
45 changes: 32 additions & 13 deletions demos/data_visualization_op_insight/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def extract_op_desc(markdown_text, header):
local_ops_dict = {op_type:[] for op_type in op_types}
multimodal = os.getenv('MULTI_MODAL', True)
multimodal_visible = False
cache_dir = './cache'
text_key = 'text'
image_key = 'images'
audio_key = 'audios'
Expand Down Expand Up @@ -125,12 +126,26 @@ def change_visible(op_name):
image_visible = True
return gr.update(visible=text_visible), gr.update(visible=image_visible), gr.update(visible=video_visible), gr.update(visible=audio_visible), gr.update(visible=text_visible), gr.update(visible=image_visible), gr.update(visible=video_visible), gr.update(visible=audio_visible)


def clear_directory(directory=cache_dir):
for item in os.listdir(directory):
if item == '.gitkeep':
continue
item_path = os.path.join(directory, item)
if os.path.isfile(item_path) or os.path.islink(item_path):
os.remove(item_path) # 删除文件或链接
elif os.path.isdir(item_path):
shutil.rmtree(item_path) # 递归删除目录


def copy_func(file):
filename = None
if file:
filename= os.path.basename(file)
shutil.copyfile(file, filename)
return filename
cache_file = None
if file:
filename= os.path.basename(file)
cache_file = os.path.join(cache_dir, filename)
shutil.copyfile(file, cache_file)
return cache_file


def encode_sample(input_text, input_image, input_video, input_audio):
sample = dict()
Expand All @@ -140,6 +155,7 @@ def encode_sample(input_text, input_image, input_video, input_audio):
sample[audio_key]=[input_audio] if input_audio else []
return sample


def decode_sample(output_sample):
output_text = output_sample[text_key]
output_image = output_sample[image_key][0] if output_sample[image_key] else None
Expand All @@ -150,6 +166,7 @@ def decode_sample(output_sample):
audio_file = copy_func(output_audio)
return output_text, image_file, video_file, audio_file


def create_tab_layout(op_tab, op_type, run_op, has_stats=False):
with op_tab:
options = get_op_lists(op_type)
Expand All @@ -167,15 +184,15 @@ def create_tab_layout(op_tab, op_type, run_op, has_stats=False):
gr.Markdown(" **Inputs**")
with gr.Row():
input_text = gr.TextArea(label="Text",interactive=True,)
input_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible, elem_classes="show_image")
input_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible)
input_video = gr.Video(label='Video', visible=multimodal_visible)
input_audio = gr.Audio(label='Audio', type='filepath', visible=multimodal_visible)

with gr.Group('Outputs'):
gr.Markdown(" **Outputs**")
with gr.Row():
output_text = gr.TextArea(label="Text",interactive=False,)
output_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible, elem_classes="show_image")
output_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible)
output_video = gr.Video(label='Video', visible=multimodal_visible)
output_audio = gr.Audio(label='Audio', type='filepath', visible=multimodal_visible)

Expand Down Expand Up @@ -215,6 +232,7 @@ def run_func(*args):
op_selector.select(change_visible, inputs=[op_selector], outputs=outputs[:4] + inputs[:4])
op_tab.select(change_visible, inputs=[op_selector], outputs=outputs[:4] + inputs[:4])


def create_mapper_tab(op_type, op_tab):
with op_tab:
def run_op(input_text, input_image, input_video, input_audio, op_name, op_params):
Expand Down Expand Up @@ -252,6 +270,7 @@ def run_op( input_text, input_image, input_video, input_audio, op_name, op_param
return decode_sample(output_sample)
create_tab_layout(op_tab, op_type, run_op, has_stats=True)


def create_tab_double_layout(op_tab, op_type, run_op):
with op_tab:
options = get_op_lists(op_type)
Expand All @@ -270,8 +289,8 @@ def create_tab_double_layout(op_tab, op_type, run_op):
with gr.Row():
input_text = gr.TextArea(label="Text",interactive=True,)
input_text2 = gr.TextArea(label="Text",interactive=True,)
input_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible, elem_classes="show_image")
input_image2 = gr.Image(label='Image', type='filepath', visible=multimodal_visible, elem_classes="show_image")
input_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible)
input_image2 = gr.Image(label='Image', type='filepath', visible=multimodal_visible)
input_video = gr.Video(label='Video', visible=multimodal_visible)
input_video2 = gr.Video(label='Video', visible=multimodal_visible)
input_audio = gr.Audio(label='Audio', type='filepath', visible=multimodal_visible)
Expand All @@ -282,8 +301,8 @@ def create_tab_double_layout(op_tab, op_type, run_op):
with gr.Row():
output_text = gr.TextArea(label="Text",interactive=False,)
output_text2 = gr.TextArea(label="Text",interactive=False,)
output_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible, elem_classes="show_image")
output_image2 = gr.Image(label='Image', type='filepath', visible=multimodal_visible, elem_classes="show_image")
output_image = gr.Image(label='Image', type='filepath', visible=multimodal_visible)
output_image2 = gr.Image(label='Image', type='filepath', visible=multimodal_visible)
output_video = gr.Video(label='Video', visible=multimodal_visible)
output_video2 = gr.Video(label='Video', visible=multimodal_visible)
output_audio = gr.Audio(label='Audio', type='filepath', visible=multimodal_visible)
Expand Down Expand Up @@ -316,8 +335,8 @@ def run_func(*args):
run_button.click(change_visible, inputs=[op_selector], outputs=outputs[:4] + inputs[:4]).then(run_func, inputs=[op_selector], outputs=[code, op_params])
op_selector.select(change_visible, inputs=[op_selector], outputs=outputs[:4] + inputs[:4]).then(show_code, inputs=[op_selector], outputs=[code, op_params])
op_tab.select(change_visible, inputs=[op_selector], outputs=outputs[:4] + inputs[:4])
with gr.Blocks(css="./app.css") as demo:

with gr.Blocks(css="./app.css") as demo:
dj_image = os.path.join(project_path, 'docs/imgs/data-juicer.jpg')
gr.HTML(format_cover_html(dj_image))

Expand All @@ -331,5 +350,5 @@ def run_func(*args):
create_op_tab_func(op_type, op_tab)
else:
gr.Error(f'{op_type} not callable')

demo.load(clear_directory, every=10)
demo.launch()
Empty file.

0 comments on commit 21a9b9c

Please sign in to comment.