File size: 7,012 Bytes
50e6fbc
e0be252
82935d8
4c20fbb
 
cf5eed6
3e4a220
50e6fbc
4b039b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82935d8
4b039b3
 
 
 
 
 
4671e61
4b039b3
 
 
 
 
a2dd03e
e0be252
 
a2dd03e
 
37ed6eb
3e4a220
 
2c09868
4b039b3
e0be252
cf5eed6
2c09868
4c20fbb
 
 
 
cf5eed6
 
4c20fbb
 
cf5eed6
 
 
4c20fbb
 
cf5eed6
 
4c20fbb
 
2c09868
e0be252
 
2c09868
 
 
e0be252
 
 
 
 
 
50e6fbc
4c20fbb
4b039b3
4c20fbb
cfeaf7d
 
 
 
 
 
 
 
 
 
 
 
a2dd03e
 
 
8952db1
a2dd03e
 
 
 
 
 
 
 
e0be252
4c20fbb
 
4b039b3
4c20fbb
50e6fbc
e0be252
 
cf5eed6
e0be252
 
4c20fbb
a2dd03e
4c20fbb
 
 
e0be252
 
a2dd03e
cf5eed6
a2dd03e
e0be252
4c20fbb
a2dd03e
4c20fbb
 
 
e0be252
 
 
cf5eed6
e0be252
 
82935d8
a2dd03e
 
8952db1
a2dd03e
 
 
 
 
 
 
 
37ed6eb
a2dd03e
 
 
 
 
2c09868
 
 
247598f
2c09868
a2dd03e
2c09868
 
 
247598f
2c09868
a2dd03e
 
82935d8
4b039b3
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import random
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
from functools import partial
from datasets import load_dataset

dataset_names = [
    "AI4Code",
    "AMPS",
    "ASFPublicMail",
    "CPDataset",
    "DMMath",
    "Discourse",
    "Enwiki",
    "EuroParliamentProceedings",
    "FreeLaw_Options",
    "GithubDiff",
    "GithubIssues",
    "Gutenberg",
    "LeetCode",
    "PileOfLaw",
    "PubMed",
    "S2ORC",
    "StackExchange",
    "USENET",
    "USPTO",
    "UbuntuIRC",
    "arXiv",
]

dataset_data = {}
for name in dataset_names:
    path = f"data/{name}/data.json"
    ds = load_dataset(
        "CarperAI/pilev2_smol_metadata",
        data_files=path,
        use_auth_token=os.environ["HF_TOKEN"],
        split="train",
        # download_mode="force_redownload",
    )
    dataset_data[name] = {
        "ds": ds,
        "check_word_number_criteria": np.array(ds["check_word_number_criteria"]),
        "check_char_repetition_criteria": np.array(ds["check_char_repetition_criteria"]),
        "check_flagged_words_criteria": np.array(ds["check_flagged_words_criteria"]),
        "check_stop_word_ratio_criteria": np.array(ds["check_stop_word_ratio_criteria"]),
        "check_perplexity_criteria": np.array(ds["check_perplexity_criteria"]),
        "check_compression_ratio_criteria": np.array(ds["check_compression_ratio_criteria"]),
    }

def plt_plot(criteria, dataset, threshold, greater_than=True):
    plt.close("all")
    x = dataset_data[dataset][criteria]
    # calculate percentage of data that will be removed given threshold
    perc = np.sum(x > threshold if greater_than else x < threshold) / len(x)
    # create a figure
    fig = plt.figure()
    # add a subplot
    ax = fig.add_subplot(111)
    # plot some data using black
    ax.hist(x, bins=50, color="black")
    # plot red dashed line at threshold
    ax.axvline(threshold, color='r', linestyle='dashed', linewidth=2)
    # set title
    # add percentage of data removed
    ax.set_title(f"{dataset} (removed {perc:.2%})")
    plt.xlabel("Value")
    plt.ylabel("Frequency")
    # make it look nice
    plt.tight_layout()
    return fig

def check_filtered(criteria, dataset, threshold, greater_than=True):
    ds = dataset_data[dataset]["ds"]

    filtered_ds = ds.filter(
        lambda x: x[criteria] > threshold if greater_than else x[criteria] < threshold
    )
    if len(filtered_ds) == 0:
        return "No examples found"
    # get random sample of 1
    sample = filtered_ds.select([random.randint(0, len(filtered_ds) - 1)])["text"][0]

    return sample

with gr.Blocks() as demo:
    dataset = gr.Radio(dataset_names, label="Dataset", value="arXiv")

    with gr.Tab("Character Repetition Criteria"):
        # plot some random data
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=1, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(plt_plot, "check_char_repetition_criteria")
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(check_filtered, "check_char_repetition_criteria")
        check.click(check_fn, [dataset, threshold], filtered_data)

    with gr.Tab("Number of Words Criteria"):
        # plot some random data
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=50_000, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(plt_plot, "check_word_number_criteria")
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(check_filtered, "check_word_number_criteria")
        check.click(check_fn, [dataset, threshold], filtered_data)

    with gr.Tab("Character Repetition Criteria"):
        # plot some random data
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=1, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(plt_plot, "check_char_repetition_criteria")
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(check_filtered, "check_char_repetition_criteria")
        check.click(check_fn, [dataset, threshold], filtered_data)
    
    with gr.Tab("Stop Word Ratio Criteria"):
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=1, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(plt_plot, "check_stop_word_ratio_criteria")
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(check_filtered, "check_stop_word_ratio_criteria")
        check.click(check_fn, [dataset, threshold], filtered_data)
    
    with gr.Tab("Flagged Word Criteria"):
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=1, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(plt_plot, "check_flagged_words_criteria")
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(check_filtered, "check_flagged_words_criteria")
        check.click(check_fn, [dataset, threshold], filtered_data)

    with gr.Tab("Perplexity Criteria"):
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=50_000, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(plt_plot, "check_perplexity_criteria")
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(check_filtered, "check_perplexity_criteria")
        check.click(check_fn, [dataset, threshold], filtered_data)
    
    with gr.Tab("Compression Ratio Criteria"):
        plot = gr.Plot()
        threshold = gr.Slider(minimum=0, maximum=1, label="Threshold")
        calculate = gr.Button("Calculate")
        check = gr.Button("Check Filtered Data")
        filtered_data = gr.Textbox(lines=5, label="Filtered Data")
        plot_fn = partial(
            plt_plot,
            "check_compression_ratio_criteria",
            greater_than=False
        )
        calculate.click(plot_fn, [dataset, threshold], plot)
        check_fn = partial(
            check_filtered,
            "check_compression_ratio_criteria",
            greater_than=False
        )
        check.click(check_fn, [dataset, threshold], filtered_data)

if __name__ == "__main__":
    demo.launch()