File size: 4,201 Bytes
3ececdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)