File size: 12,586 Bytes
a69bbb8
2b4b309
2723bd3
2b4b309
90e8636
e4b6cc5
6fc91c7
a69bbb8
2b4b309
f04dfa8
9ac3da0
5afa3f9
 
f04dfa8
fd936a6
f04dfa8
 
6fc91c7
0d28c87
 
 
fd936a6
9ac3da0
0d28c87
90e8636
 
6a4ac56
cb05c15
 
2b4b309
 
 
8571d5a
5afa3f9
 
 
 
 
2723bd3
f04dfa8
2723bd3
e1fdeee
2723bd3
 
2b4b309
 
 
 
6fc91c7
2b4b309
 
 
 
2723bd3
 
 
 
 
5afa3f9
 
 
 
 
2723bd3
cb05c15
 
 
2723bd3
 
2b4b309
 
6fc91c7
fd936a6
 
 
 
 
 
 
2723bd3
6a4ac56
2b4b309
0d28c87
 
 
 
 
2eb6d1a
5829740
0d28c87
5afa3f9
0d28c87
8571d5a
e36d40b
 
f04dfa8
e36d40b
5829740
f04dfa8
5829740
e36d40b
6a4ac56
 
 
4d1c962
a13f86c
 
 
 
 
 
4d1c962
a13f86c
4d1c962
a13f86c
4d1c962
2b4b309
 
0c58a58
6a4ac56
2b4b309
8571d5a
4d1c962
 
2723bd3
 
f04dfa8
2723bd3
8571d5a
 
b000e50
8571d5a
0d28c87
4d1c962
 
 
8571d5a
2b4b309
 
2eb6d1a
2723bd3
2b4b309
6fc91c7
 
a69bbb8
a13f86c
2b4b309
8571d5a
2723bd3
 
 
 
e4b6cc5
2723bd3
 
 
 
2b4b309
90e8636
a69bbb8
 
 
 
 
 
 
 
 
 
 
 
5fca25d
 
 
f04dfa8
90e8636
5afa3f9
 
fd936a6
318e969
fd936a6
ce95000
 
 
 
a69bbb8
ce95000
a69bbb8
fd936a6
2723bd3
0d28c87
8d5d65f
69a533b
9ac3da0
8d5d65f
9ac3da0
 
 
7a3ba19
9ac3da0
c7f7750
0d28c87
 
6a4ac56
0d28c87
 
8d5d65f
6a4ac56
5afa3f9
8d5d65f
8571d5a
90e8636
0d28c87
7a3ba19
5afa3f9
6a4ac56
0d28c87
 
40e000b
75f9ac3
6a4ac56
 
 
 
 
 
 
0d28c87
 
 
 
 
 
 
 
7a3ba19
0d28c87
 
 
 
 
 
7a3ba19
0d28c87
 
75f9ac3
0d28c87
 
 
5fca25d
75f9ac3
 
0d28c87
 
 
 
 
 
 
 
b000e50
0d28c87
 
6a4ac56
0d28c87
 
6a4ac56
0d28c87
 
 
 
ff3c0c2
 
fd936a6
 
 
 
 
0d28c87
cb05c15
 
 
0d28c87
 
 
6a4ac56
 
 
 
 
 
0d28c87
6a4ac56
 
5afa3f9
6a4ac56
 
 
 
9d1a2d6
0d28c87
ed40758
f04dfa8
9d1a2d6
 
 
b000e50
f04dfa8
0d28c87
 
9d1a2d6
 
 
f04dfa8
 
 
9d1a2d6
b000e50
 
 
a69bbb8
 
 
 
 
 
 
 
 
 
 
7a3ba19
 
 
 
 
 
9d1a2d6
b000e50
 
 
9d1a2d6
0d28c87
 
 
 
 
 
 
ff3c0c2
0d28c87
6a4ac56
9d1a2d6
a69bbb8
 
 
 
9b4773a
0d28c87
 
 
 
9d1a2d6
fd936a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff3c0c2
0d28c87
ce95000
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import io
import multiprocessing
import time

import gradio as gr
import pandas as pd
from distilabel.distiset import Distiset
from huggingface_hub import upload_file

from src.distilabel_dataset_generator.pipelines.sft import (
    DEFAULT_DATASET_DESCRIPTIONS,
    DEFAULT_DATASETS,
    DEFAULT_SYSTEM_PROMPTS,
    PROMPT_CREATION_PROMPT,
    generate_pipeline_code,
    get_pipeline,
    get_prompt_generation_step,
)
from src.distilabel_dataset_generator.utils import (
    get_login_button,
    get_org_dropdown,
    get_token,
    swap_visibilty,
)


def _run_pipeline(result_queue, num_turns, num_rows, system_prompt, is_sample):
    pipeline = get_pipeline(num_turns, num_rows, system_prompt, is_sample)
    distiset: Distiset = pipeline.run(use_cache=True)
    result_queue.put(distiset)


def generate_system_prompt(dataset_description, progress=gr.Progress()):
    if dataset_description in DEFAULT_DATASET_DESCRIPTIONS:
        index = DEFAULT_DATASET_DESCRIPTIONS.index(dataset_description)
        if index < len(DEFAULT_SYSTEM_PROMPTS):
            return DEFAULT_SYSTEM_PROMPTS[index]

    progress(0.1, desc="Initializing text generation")
    generate_description = get_prompt_generation_step()
    progress(0.4, desc="Loading model")
    generate_description.load()
    progress(0.7, desc="Generating system prompt")
    result = next(
        generate_description.process(
            [
                {
                    "system_prompt": PROMPT_CREATION_PROMPT,
                    "instruction": dataset_description,
                }
            ]
        )
    )[0]["generation"]
    progress(1.0, desc="System prompt generated")
    return result


def generate_sample_dataset(system_prompt, progress=gr.Progress()):
    if system_prompt in DEFAULT_SYSTEM_PROMPTS:
        index = DEFAULT_SYSTEM_PROMPTS.index(system_prompt)
        if index < len(DEFAULT_DATASETS):
            return DEFAULT_DATASETS[index]

    progress(0.1, desc="Initializing sample dataset generation")
    result = generate_dataset(
        system_prompt, num_turns=1, num_rows=1, progress=progress, is_sample=True
    )
    progress(1.0, desc="Sample dataset generated")
    return result


def generate_dataset(
    system_prompt: str,
    num_turns: int = 1,
    num_rows: int = 5,
    private: bool = True,
    org_name: str = None,
    repo_name: str = None,
    oauth_token: str = None,
    progress=gr.Progress(),
    is_sample: bool = False,
):
    repo_id = (
        f"{org_name}/{repo_name}"
        if repo_name is not None and org_name is not None
        else None
    )
    if repo_id is not None:
        if not all([repo_id, org_name, repo_name]):
            raise gr.Error(
                "Please provide a `repo_name` and `org_name` to push the dataset to."
            )

    if num_turns > 4:
        num_turns = 4
        gr.Info("You can only generate a dataset with 4 or fewer turns. Setting to 4.")
    if num_rows > 5000:
        num_rows = 1000
        gr.Info(
            "You can only generate a dataset with 1000 or fewer rows. Setting to 1000."
        )
    if num_rows < 5:
        duration = 25
    elif num_rows < 10:
        duration = 60
    elif num_rows < 30:
        duration = 120
    elif num_rows < 100:
        duration = 240
    elif num_rows < 300:
        duration = 600
    elif num_rows < 1000:
        duration = 1200
    else:
        duration = 2400

    result_queue = multiprocessing.Queue()
    p = multiprocessing.Process(
        target=_run_pipeline,
        args=(result_queue, num_turns, num_rows, system_prompt, is_sample),
    )

    try:
        p.start()
        total_steps = 100
        for step in range(total_steps):
            if not p.is_alive() or p._popen.poll() is not None:
                break
            progress(
                (step + 1) / total_steps,
                desc=f"Generating dataset with {num_rows} rows. Don't close this window.",
            )
            time.sleep(duration / total_steps)  # Adjust this value based on your needs
        p.join()
    except Exception as e:
        raise gr.Error(f"An error occurred during dataset generation: {str(e)}")

    distiset = result_queue.get()

    if repo_id is not None:
        progress(0.95, desc="Pushing dataset to Hugging Face Hub.")
        distiset.push_to_hub(
            repo_id=repo_id,
            private=private,
            include_script=True,
            token=oauth_token,
        )

    # If not pushing to hub generate the dataset directly
    distiset = distiset["default"]["train"]
    if num_turns == 1:
        outputs = distiset.to_pandas()[["prompt", "completion"]]
    else:
        outputs = distiset.to_pandas()[["messages"]]

    progress(1.0, desc="Dataset generation completed")
    return pd.DataFrame(outputs)


def upload_pipeline_code(pipeline_code, org_name, repo_name, oauth_token):
    with io.BytesIO(pipeline_code.encode("utf-8")) as f:
        upload_file(
            path_or_fileobj=f,
            path_in_repo="pipeline.py",
            repo_id=f"{org_name}/{repo_name}",
            repo_type="dataset",
            token=oauth_token,
            commit_message="Include pipeline script",
        )


css = """
.main_ui_logged_out{opacity: 0.3; pointer-events: none}
"""

with gr.Blocks(
    title="🧶 DataCraft",
    head="🧶 DataCraft",
    css=css,
) as app:
    with gr.Row():
        gr.Markdown(
            "To push the dataset to the Hugging Face Hub you need to sign in. This will only be used for pushing the dataset not for data generation."
        )
    with gr.Row():
        gr.Column()
        get_login_button()
        gr.Column()

    gr.Markdown("## Iterate on a sample dataset")
    with gr.Column() as main_ui:
        dataset_description = gr.TextArea(
            label="Give a precise description of the assistant or tool. Don't describe the dataset",
            value=DEFAULT_DATASET_DESCRIPTIONS[0],
            lines=2,
        )
        examples = gr.Examples(
            elem_id="system_prompt_examples",
            examples=[[example] for example in DEFAULT_DATASET_DESCRIPTIONS],
            inputs=[dataset_description],
        )
        with gr.Row():
            gr.Column(scale=1)
            btn_generate_system_prompt = gr.Button(value="Generate sample")
            gr.Column(scale=1)

        system_prompt = gr.TextArea(
            label="System prompt for dataset generation. You can tune it and regenerate the sample",
            value=DEFAULT_SYSTEM_PROMPTS[0],
            lines=5,
        )

        with gr.Row():
            sample_dataset = gr.DataFrame(
                value=DEFAULT_DATASETS[0],
                label="Sample dataset. Prompts and completions truncated to 256 tokens.",
                interactive=False,
                wrap=True,
            )

        with gr.Row():
            gr.Column(scale=1)
            btn_generate_sample_dataset = gr.Button(
                value="Regenerate sample",
            )
            gr.Column(scale=1)

        result = btn_generate_system_prompt.click(
            fn=generate_system_prompt,
            inputs=[dataset_description],
            outputs=[system_prompt],
            show_progress=True,
        ).then(
            fn=generate_sample_dataset,
            inputs=[system_prompt],
            outputs=[sample_dataset],
            show_progress=True,
        )

        btn_generate_sample_dataset.click(
            fn=generate_sample_dataset,
            inputs=[system_prompt],
            outputs=[sample_dataset],
            show_progress=True,
        )

        # Add a header for the full dataset generation section
        gr.Markdown("## Generate full dataset")
        gr.Markdown(
            "Once you're satisfied with the sample, generate a larger dataset and push it to the Hub."
        )

        with gr.Column() as push_to_hub_ui:
            with gr.Row(variant="panel"):
                num_turns = gr.Number(
                    value=1,
                    label="Number of turns in the conversation",
                    minimum=1,
                    maximum=4,
                    step=1,
                    info="Choose between 1 (single turn with 'instruction-response' columns) and 2-4 (multi-turn conversation with a 'messages' column).",
                )
                num_rows = gr.Number(
                    value=10,
                    label="Number of rows in the dataset",
                    minimum=1,
                    maximum=500,
                    info="The number of rows in the dataset. Note that you are able to generate more rows at once but that this will take time.",
                )

            with gr.Row(variant="panel"):
                oauth_token = gr.Textbox(
                    value=get_token(),
                    label="Hugging Face Token",
                    placeholder="hf_...",
                    type="password",
                    visible=False,
                )
                org_name = get_org_dropdown()
                repo_name = gr.Textbox(
                    label="Repo name", placeholder="dataset_name", value="my-distiset"
                )
                private = gr.Checkbox(
                    label="Private dataset", value=True, interactive=True, scale=0.5
                )
            with gr.Row() as regenerate_row:
                gr.Column(scale=1)
                btn_generate_full_dataset = gr.Button(
                    value="Generate Full Dataset", variant="primary"
                )
                gr.Column(scale=1)
            success_message = gr.Markdown(visible=False)
            with gr.Row():
                final_dataset = gr.DataFrame(
                    value=DEFAULT_DATASETS[0],
                    label="Generated dataset",
                    interactive=False,
                    wrap=True,
                )

    def show_success_message(org_name, repo_name):
        return gr.Markdown(
            value=f"""
            <div style="padding: 1em; background-color: #e6f3e6; border-radius: 5px; margin-top: 1em;">
                <h3 style="color: #2e7d32; margin: 0;">Dataset Published Successfully!</h3>
                <p style="margin-top: 0.5em;">
                    The generated dataset is in the right format for Fine-tuning with TRL, AutoTrain or other frameworks.
                    Your dataset is now available at:
                    <a href="https://huggingface.co/datasets/{org_name}/{repo_name}" target="_blank" style="color: #1565c0; text-decoration: none;">
                        https://huggingface.co/datasets/{org_name}/{repo_name}
                    </a>
                </p>
            </div>
        """,
            visible=True,
        )

    def hide_success_message():
        return gr.Markdown(visible=False)

    gr.Markdown("## Or run this pipeline locally with distilabel")

    with gr.Accordion("Run this pipeline using distilabel", open=False):
        pipeline_code = gr.Code(
            value=generate_pipeline_code(
                system_prompt.value, num_turns.value, num_rows.value
            ),
            language="python",
            label="Distilabel Pipeline Code",
        )

    sample_dataset.change(
        fn=lambda x: x,
        inputs=[sample_dataset],
        outputs=[final_dataset],
    )

    btn_generate_full_dataset.click(
        fn=hide_success_message,
        outputs=[success_message],
    ).then(
        fn=generate_dataset,
        inputs=[
            system_prompt,
            num_turns,
            num_rows,
            private,
            org_name,
            repo_name,
            oauth_token,
        ],
        outputs=[final_dataset],
        show_progress=True,
    ).then(
        fn=upload_pipeline_code,
        inputs=[pipeline_code, org_name, repo_name, oauth_token],
        outputs=[],
    ).success(
        fn=show_success_message,
        inputs=[org_name, repo_name],
        outputs=[success_message],
    )

    system_prompt.change(
        fn=generate_pipeline_code,
        inputs=[system_prompt, num_turns, num_rows],
        outputs=[pipeline_code],
    )
    num_turns.change(
        fn=generate_pipeline_code,
        inputs=[system_prompt, num_turns, num_rows],
        outputs=[pipeline_code],
    )
    num_rows.change(
        fn=generate_pipeline_code,
        inputs=[system_prompt, num_turns, num_rows],
        outputs=[pipeline_code],
    )
    app.load(get_token, outputs=[oauth_token])
    app.load(get_org_dropdown, outputs=[org_name])
    app.load(fn=swap_visibilty, outputs=main_ui)