Jingyi77 commited on
Commit
3ececdd
·
verified ·
1 Parent(s): ae50c81

Upload viewer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. viewer.py +112 -0
viewer.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ from PIL import Image
4
+ import gradio as gr
5
+ import pandas as pd
6
+
7
+ def decode_image(base64_str):
8
+ try:
9
+ img_data = base64.b64decode(base64_str)
10
+ img = Image.open(io.BytesIO(img_data))
11
+ return img
12
+ except:
13
+ return None
14
+
15
+ def display_sample(df, idx):
16
+ sample = df.iloc[idx]
17
+
18
+ title = sample['title']
19
+ description = sample['description']
20
+ label = "广告" if sample['label'] == 1 else "非广告"
21
+ date = sample['date']
22
+ comments = sample['comments'] if 'comments' in sample else []
23
+
24
+ images = []
25
+ for img_b64 in sample['images']:
26
+ img = decode_image(img_b64)
27
+ if img:
28
+ images.append(img)
29
+
30
+ return title, description, label, date, comments, images
31
+
32
+ def create_demo(df):
33
+ with gr.Blocks() as demo:
34
+ gr.Markdown("# RedNote Covert Advertisement Detection Dataset Viewer")
35
+
36
+ with gr.Row():
37
+ with gr.Column(scale=1):
38
+ idx_slider = gr.Slider(minimum=0, maximum=len(df)-1, step=1, value=0, label="Sample Index")
39
+ label_filter = gr.Radio(["全部", "仅广告", "仅非广告"], value="全部", label="筛选")
40
+
41
+ def update_slider(choice):
42
+ if choice == "仅广告":
43
+ ad_indices = df[df['label'] == 1].index.tolist()
44
+ return gr.Slider(minimum=0, maximum=len(ad_indices)-1, step=1, value=0)
45
+ elif choice == "仅非广告":
46
+ non_ad_indices = df[df['label'] == 0].index.tolist()
47
+ return gr.Slider(minimum=0, maximum=len(non_ad_indices)-1, step=1, value=0)
48
+ else:
49
+ return gr.Slider(minimum=0, maximum=len(df)-1, step=1, value=0)
50
+
51
+ label_filter.change(update_slider, inputs=[label_filter], outputs=[idx_slider])
52
+
53
+ with gr.Column(scale=3):
54
+ title_text = gr.Textbox(label="标题")
55
+ desc_text = gr.Textbox(label="描述", lines=5)
56
+ label_text = gr.Textbox(label="标签")
57
+ date_text = gr.Textbox(label="日期")
58
+ comments_text = gr.Textbox(label="评论", lines=5)
59
+ image_gallery = gr.Gallery(label="图片", columns=3, height=400)
60
+
61
+ def get_filtered_index(idx, filter_choice):
62
+ if filter_choice == "仅广告":
63
+ ad_indices = df[df['label'] == 1].index.tolist()
64
+ return ad_indices[idx]
65
+ elif filter_choice == "仅非广告":
66
+ non_ad_indices = df[df['label'] == 0].index.tolist()
67
+ return non_ad_indices[idx]
68
+ else:
69
+ return idx
70
+
71
+ def update_display(idx, filter_choice):
72
+ real_idx = get_filtered_index(idx, filter_choice)
73
+ return display_sample(df, real_idx)
74
+
75
+ idx_slider.change(
76
+ update_display,
77
+ inputs=[idx_slider, label_filter],
78
+ outputs=[title_text, desc_text, label_text, date_text, comments_text, image_gallery]
79
+ )
80
+
81
+ # 初始显示第一个样本
82
+ title, desc, label, date, comments, images = display_sample(df, 0)
83
+ title_text.value = title
84
+ desc_text.value = desc
85
+ label_text.value = label
86
+ date_text.value = date
87
+ comments_text.value = "\n".join(comments) if comments else ""
88
+ image_gallery.value = images
89
+
90
+ return demo
91
+
92
+ # 加载数据集
93
+ def load_dataset():
94
+ try:
95
+ train_df = pd.read_parquet("train.parquet")
96
+ val_df = pd.read_parquet("validation.parquet")
97
+ test_df = pd.read_parquet("test.parquet")
98
+ return pd.concat([train_df, val_df, test_df])
99
+ except:
100
+ # 如果加载失败,返回一个示例数据框
101
+ return pd.DataFrame({
102
+ 'title': ['示例标题'],
103
+ 'description': ['这是一个示例描述'],
104
+ 'label': [0],
105
+ 'date': ['01-01'],
106
+ 'comments': [['评论1', '评论2']],
107
+ 'images': [['']] # 空图片
108
+ })
109
+
110
+ # 创建演示
111
+ df = load_dataset()
112
+ demo = create_demo(df)