|
import base64 |
|
import io |
|
from PIL import Image |
|
import gradio as gr |
|
import pandas as pd |
|
|
|
def decode_image(base64_str): |
|
try: |
|
img_data = base64.b64decode(base64_str) |
|
img = Image.open(io.BytesIO(img_data)) |
|
return img |
|
except: |
|
return None |
|
|
|
def display_sample(df, idx): |
|
sample = df.iloc[idx] |
|
|
|
title = sample['title'] |
|
description = sample['description'] |
|
label = "广告" if sample['label'] == 1 else "非广告" |
|
date = sample['date'] |
|
comments = sample['comments'] if 'comments' in sample else [] |
|
|
|
images = [] |
|
for img_b64 in sample['images']: |
|
img = decode_image(img_b64) |
|
if img: |
|
images.append(img) |
|
|
|
return title, description, label, date, comments, images |
|
|
|
def create_demo(df): |
|
with gr.Blocks() as demo: |
|
gr.Markdown("# RedNote Covert Advertisement Detection Dataset Viewer") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
idx_slider = gr.Slider(minimum=0, maximum=len(df)-1, step=1, value=0, label="Sample Index") |
|
label_filter = gr.Radio(["全部", "仅广告", "仅非广告"], value="全部", label="筛选") |
|
|
|
def update_slider(choice): |
|
if choice == "仅广告": |
|
ad_indices = df[df['label'] == 1].index.tolist() |
|
return gr.Slider(minimum=0, maximum=len(ad_indices)-1, step=1, value=0) |
|
elif choice == "仅非广告": |
|
non_ad_indices = df[df['label'] == 0].index.tolist() |
|
return gr.Slider(minimum=0, maximum=len(non_ad_indices)-1, step=1, value=0) |
|
else: |
|
return gr.Slider(minimum=0, maximum=len(df)-1, step=1, value=0) |
|
|
|
label_filter.change(update_slider, inputs=[label_filter], outputs=[idx_slider]) |
|
|
|
with gr.Column(scale=3): |
|
title_text = gr.Textbox(label="标题") |
|
desc_text = gr.Textbox(label="描述", lines=5) |
|
label_text = gr.Textbox(label="标签") |
|
date_text = gr.Textbox(label="日期") |
|
comments_text = gr.Textbox(label="评论", lines=5) |
|
image_gallery = gr.Gallery(label="图片", columns=3, height=400) |
|
|
|
def get_filtered_index(idx, filter_choice): |
|
if filter_choice == "仅广告": |
|
ad_indices = df[df['label'] == 1].index.tolist() |
|
return ad_indices[idx] |
|
elif filter_choice == "仅非广告": |
|
non_ad_indices = df[df['label'] == 0].index.tolist() |
|
return non_ad_indices[idx] |
|
else: |
|
return idx |
|
|
|
def update_display(idx, filter_choice): |
|
real_idx = get_filtered_index(idx, filter_choice) |
|
return display_sample(df, real_idx) |
|
|
|
idx_slider.change( |
|
update_display, |
|
inputs=[idx_slider, label_filter], |
|
outputs=[title_text, desc_text, label_text, date_text, comments_text, image_gallery] |
|
) |
|
|
|
|
|
title, desc, label, date, comments, images = display_sample(df, 0) |
|
title_text.value = title |
|
desc_text.value = desc |
|
label_text.value = label |
|
date_text.value = date |
|
comments_text.value = "\n".join(comments) if comments else "" |
|
image_gallery.value = images |
|
|
|
return demo |
|
|
|
|
|
def load_dataset(): |
|
try: |
|
train_df = pd.read_parquet("train.parquet") |
|
val_df = pd.read_parquet("validation.parquet") |
|
test_df = pd.read_parquet("test.parquet") |
|
return pd.concat([train_df, val_df, test_df]) |
|
except: |
|
|
|
return pd.DataFrame({ |
|
'title': ['示例标题'], |
|
'description': ['这是一个示例描述'], |
|
'label': [0], |
|
'date': ['01-01'], |
|
'comments': [['评论1', '评论2']], |
|
'images': [['']] |
|
}) |
|
|
|
|
|
df = load_dataset() |
|
demo = create_demo(df) |
|
|