File size: 13,520 Bytes
421b068
 
 
 
ca1279e
f327376
810f00f
44cdaf2
810f00f
44cdaf2
777edd0
88d7725
 
806dbf3
 
 
d51b1c2
88d7725
ae0bcb8
 
7d529e0
734af25
 
44cdaf2
810f00f
 
 
 
 
 
 
 
 
 
 
ca1279e
810f00f
734af25
 
f327376
ae0bcb8
 
 
 
 
 
 
 
 
 
 
ca1279e
f327376
777edd0
f327376
810f00f
f327376
 
ca1279e
 
 
45f97ba
 
 
 
 
 
 
 
 
734af25
810f00f
ca1279e
421b068
44cdaf2
806dbf3
 
 
 
 
45f97ba
 
 
44cdaf2
421b068
7d529e0
421b068
f327376
7d529e0
 
45f97ba
f327376
45f97ba
 
f327376
45f97ba
 
 
 
 
 
 
 
 
 
 
6d0709a
44cdaf2
45f97ba
fb98b30
 
 
 
 
810f00f
 
45f97ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
777edd0
 
810f00f
44cdaf2
810f00f
 
44cdaf2
810f00f
44cdaf2
777edd0
 
 
 
 
 
810f00f
777edd0
810f00f
7d529e0
ae0bcb8
806dbf3
ae0bcb8
88d7725
45f97ba
 
 
 
88d7725
 
ae0bcb8
806dbf3
ae0bcb8
806dbf3
 
 
 
 
 
 
ae0bcb8
d51b1c2
 
 
ae0bcb8
88d7725
45f97ba
 
 
 
88d7725
 
ae0bcb8
88d7725
 
 
 
 
 
ae0bcb8
 
88d7725
ae0bcb8
88d7725
 
 
ae0bcb8
88d7725
 
ae0bcb8
88d7725
 
 
45f97ba
ae0bcb8
88d7725
 
ae0bcb8
45f97ba
 
 
88d7725
 
45f97ba
810f00f
 
 
44cdaf2
810f00f
777edd0
f327376
 
777edd0
810f00f
ca1279e
f327376
 
 
 
 
810f00f
777edd0
 
ca1279e
44cdaf2
 
 
 
810f00f
806dbf3
 
 
777edd0
 
9f3ff28
806dbf3
 
 
 
777edd0
e7688e5
 
 
 
 
777edd0
 
88d7725
777edd0
 
 
 
88d7725
777edd0
 
 
 
 
fb98b30
 
 
 
 
 
 
 
 
 
 
 
 
 
e7688e5
 
 
 
 
fb98b30
 
 
 
 
 
 
 
 
 
 
 
 
777edd0
 
45f97ba
777edd0
 
 
 
 
 
 
 
45f97ba
777edd0
 
ae0bcb8
777edd0
 
ae0bcb8
777edd0
9f3ff28
45f97ba
 
421b068
7d529e0
ae0bcb8
 
 
 
 
7af3e0d
ae0bcb8
 
 
 
9f3ff28
 
 
 
fb98b30
ae0bcb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f3ff28
421b068
9f3ff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae0bcb8
fb98b30
ae0bcb8
 
 
 
 
88d7725
ae0bcb8
 
 
 
 
 
 
 
 
 
88d7725
9f3ff28
88d7725
777edd0
ae0bcb8
777edd0
 
ae0bcb8
 
806dbf3
ae0bcb8
806dbf3
 
ae0bcb8
 
88d7725
ae0bcb8
88d7725
 
45f97ba
ae0bcb8
2d53b10
7d529e0
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import gradio as gr
from gradio_huggingfacehub_search import HuggingfaceHubSearch
import nbformat as nbf
from huggingface_hub import HfApi
from httpx import Client
import logging
from huggingface_hub import InferenceClient
import json
import re
import pandas as pd
from gradio.data_classes import FileData
from utils.prompts import (
    generate_mapping_prompt,
    generate_user_prompt,
    generate_rag_system_prompt,
    generate_eda_system_prompt,
    generate_embedding_system_prompt,
)
from dotenv import load_dotenv
import os

"""
TODOs:
- Need feedback on the output commands to validate if operations are appropiate to data types 
- Refactor
- Make the notebook generation more dynamic, add loading components to do not freeze the UI
- Fix errors:
    - When generating output
    - When parsing output
    - When pushing notebook
- Add target tasks to choose for the notebook:
    - Exploratory data analysis
    - Auto training
    - RAG
    - etc.
- Enable 'generate notebook' button only if dataset is available and supports library
- First get compatible-libraries and let user choose the library
"""

# Configuration

load_dotenv()

HF_TOKEN = os.getenv("HF_TOKEN")
NOTEBOOKS_REPOSITORY = os.getenv("NOTEBOOKS_REPOSITORY")
assert HF_TOKEN is not None, "You need to set HF_TOKEN in your environment variables"
assert (
    NOTEBOOKS_REPOSITORY is not None
), "You need to set NOTEBOOKS_REPOSITORY in your environment variables"


BASE_DATASETS_SERVER_URL = "https://datasets-server.huggingface.co"
HEADERS = {"Accept": "application/json", "Content-Type": "application/json"}

client = Client(headers=HEADERS)
inference_client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")

logging.basicConfig(level=logging.INFO)


def get_compatible_libraries(dataset: str):
    try:
        response = client.get(
            f"{BASE_DATASETS_SERVER_URL}/compatible-libraries?dataset={dataset}"
        )
        response.raise_for_status()
        return response.json()
    except Exception as e:
        logging.error(f"Error fetching compatible libraries: {e}")
        raise


def create_notebook_file(cell_commands, notebook_name):
    nb = nbf.v4.new_notebook()
    nb["cells"] = [
        nbf.v4.new_code_cell(
            cmd["source"]
            if isinstance(cmd["source"], str)
            else "\n".join(cmd["source"])
        )
        if cmd["cell_type"] == "code"
        else nbf.v4.new_markdown_cell(cmd["source"])
        for cmd in cell_commands
    ]

    with open(notebook_name, "w") as f:
        nbf.write(nb, f)
    logging.info(f"Notebook {notebook_name} created successfully")


def get_first_rows_as_df(dataset: str, config: str, split: str, limit: int):
    try:
        resp = client.get(
            f"{BASE_DATASETS_SERVER_URL}/first-rows?dataset={dataset}&config={config}&split={split}"
        )
        resp.raise_for_status()
        content = resp.json()
        rows = content["rows"]
        rows = [row["row"] for row in rows]
        first_rows_df = pd.DataFrame.from_dict(rows).sample(frac=1).head(limit)
        features = content["features"]
        features_dict = {feature["name"]: feature["type"] for feature in features}
        return features_dict, first_rows_df
    except Exception as e:
        logging.error(f"Error fetching first rows: {e}")
        raise


def get_txt_from_output(output):
    extracted_text = extract_content_from_output(output)
    logging.info("--> Extracted text between json block")
    logging.info(extracted_text)
    content = json.loads(extracted_text)
    return content


def extract_content_from_output(output):
    patterns = [r"`json(.*?)`", r"```(.*?)```"]

    for pattern in patterns:
        match = re.search(pattern, output, re.DOTALL)
        if match:
            return match.group(1)

    try:
        index = output.index("```json")
        logging.info(f"Index: {index}")
        return output[index + 7 :]
    except ValueError:
        logging.error("Unable to generate Jupyter notebook.")
        raise


def content_from_output(output):
    pattern = r"`json(.*?)`"
    match = re.search(pattern, output, re.DOTALL)
    if not match:
        pattern = r"```(.*?)```"
        match = re.search(pattern, output, re.DOTALL)
        if not match:
            try:
                index = output.index("```json")
                logging.info(f"Index: {index}")
                return output[index + 7 :]
            except:
                pass
            raise Exception("Unable to generate jupyter notebook.")
    return match.group(1)


def generate_eda_cells(dataset_id):
    for messages in generate_cells(dataset_id, generate_eda_system_prompt, "eda"):
        yield messages, None  # Keep button hidden

    yield (
        messages,
        f"{dataset_id.replace('/', '-')}-eda.ipynb",
    )


def generate_rag_cells(dataset_id):
    for messages in generate_cells(dataset_id, generate_rag_system_prompt, "rag"):
        yield messages, None  # Keep button hidden

    yield (
        messages,
        f"{dataset_id.replace('/', '-')}-rag.ipynb",
    )


def generate_embedding_cells(dataset_id):
    for messages in generate_cells(
        dataset_id, generate_embedding_system_prompt, "embedding"
    ):
        yield messages, None  # Keep button hidden

    yield (
        messages,
        f"{dataset_id.replace('/', '-')}-embedding.ipynb",
    )


def _push_to_hub(
    history,
    dataset_id,
    notebook_file,
):
    logging.info(f"Pushing notebook to hub: {dataset_id} on file {notebook_file}")

    notebook_name = notebook_file.split("/")[-1]
    api = HfApi(token=HF_TOKEN)
    try:
        logging.info(f"About to push {notebook_file} - {dataset_id}")
        api.upload_file(
            path_or_fileobj=notebook_file,
            path_in_repo=notebook_name,
            repo_id=NOTEBOOKS_REPOSITORY,
            repo_type="dataset",
        )
        link = f"https://huggingface.co/datasets/{NOTEBOOKS_REPOSITORY}/blob/main/{notebook_name}"
        logging.info(f"Notebook pushed to hub: {link}")
        yield history + [
            gr.ChatMessage(
                role="user",
                content=f"[{notebook_name}]({link})",
            )
        ]

    except Exception as e:
        logging.info("Failed to push notebook", e)
        yield history + [gr.ChatMessage(role="assistant", content=e)]


def generate_cells(dataset_id, prompt_fn, notebook_type="eda"):
    try:
        libraries = get_compatible_libraries(dataset_id)
    except Exception as err:
        gr.Error("Unable to retrieve dataset info from HF Hub.")
        logging.error(f"Failed to fetch compatible libraries: {err}")
        return []

    if not libraries:
        gr.Error("Dataset not compatible with pandas library.")
        logging.error(f"Dataset not compatible with pandas library")
        return gr.File(visible=False), gr.Row.update(visible=False)

    pandas_library = next(
        (lib for lib in libraries.get("libraries", []) if lib["library"] == "pandas"),
        None,
    )
    if not pandas_library:
        gr.Error("Dataset not compatible with pandas library.")
        return []

    first_config_loading_code = pandas_library["loading_codes"][0]
    first_code = first_config_loading_code["code"]
    first_config = first_config_loading_code["config_name"]
    first_split = list(first_config_loading_code["arguments"]["splits"].keys())[0]
    features, df = get_first_rows_as_df(dataset_id, first_config, first_split, 3)
    prompt = generate_user_prompt(
        features, df.head(5).to_dict(orient="records"), first_code
    )
    messages = [gr.ChatMessage(role="user", content=prompt)]
    yield messages + [gr.ChatMessage(role="assistant", content="⏳ _Starting task..._")]

    prompt_messages = [
        {"role": "system", "content": prompt_fn()},
        {"role": "user", "content": prompt},
    ]
    output = inference_client.chat_completion(
        messages=prompt_messages,
        stream=True,
        max_tokens=2500,
        top_p=0.8,
        seed=42,
    )

    generated_text = ""
    current_line = ""
    for chunk in output:
        current_line += chunk.choices[0].delta.content
        if current_line.endswith("\n"):
            generated_text += current_line
            messages.append(gr.ChatMessage(role="assistant", content=current_line))
            current_line = ""
        yield messages
    yield messages

    logging.info("---> Notebook markdown code output")
    logging.info(generated_text)

    retries = 0
    retry_limit = 3
    while retries < retry_limit:
        try:
            formatted_prompt = generate_mapping_prompt(generated_text)
            prompt_messages = [{"role": "user", "content": formatted_prompt}]
            yield messages + [
                gr.ChatMessage(role="assistant", content="⏳ _Generating notebook..._")
            ]

            output = inference_client.chat_completion(
                messages=prompt_messages,
                stream=False,
                max_tokens=2500,
                top_p=0.8,
                seed=42,
            )
            cells_txt = output.choices[0].message.content
            logging.info(f"---> Mapping to json output attempt {retries}")
            logging.info(cells_txt)
            commands = get_txt_from_output(cells_txt)
            break
        except Exception as e:
            logging.warn("Error when parsing output, retrying ..")
            retries += 1
            if retries == retry_limit:
                logging.error(f"Unable to parse output after {retry_limit} retries")
                gr.Error("Unable to generate notebook. Try again please")
                raise e

    html_code = f"<iframe src='https://huggingface.co/datasets/{dataset_id}/embed/viewer' width='80%' height='560px'></iframe>"

    commands.insert(
        0,
        {
            "cell_type": "code",
            "source": f'from IPython.display import HTML\n\ndisplay(HTML("{html_code}"))',
        },
    )
    commands.insert(0, {"cell_type": "markdown", "source": "# Dataset Viewer"})
    notebook_name = f"{dataset_id.replace('/', '-')}-{notebook_type}.ipynb"
    create_notebook_file(commands, notebook_name=notebook_name)
    messages.append(
        gr.ChatMessage(role="user", content="See the generated notebook on the Hub")
    )
    yield messages
    yield from _push_to_hub(messages, dataset_id, notebook_name)


def coming_soon_message():
    return gr.Info("Coming soon")


def handle_example(example, button_action):
    return button_action(example)


with gr.Blocks(fill_width=True) as demo:
    gr.Markdown("# 🤖 Dataset notebook creator 🕵️")
    with gr.Row(equal_height=True):
        with gr.Column(scale=2):
            text_input = gr.Textbox(label="Suggested notebook type", visible=False)

            dataset_name = HuggingfaceHubSearch(
                label="Hub Dataset ID",
                placeholder="Search for dataset id on Huggingface",
                search_type="dataset",
                value="",
            )

            dataset_samples = gr.Examples(
                examples=[
                    [
                        "infinite-dataset-hub/WorldPopCounts",
                        "Try this dataset for Exploratory Data Analysis",
                    ],
                    [
                        "infinite-dataset-hub/GlobaleCuisineRecipes",
                        "Try this dataset for Embeddings generation",
                    ],
                    [
                        "infinite-dataset-hub/GlobalBestSellersSummaries",
                        "Try this dataset for RAG generation",
                    ],
                ],
                inputs=[dataset_name, text_input],
                cache_examples=False,
            )

            @gr.render(inputs=dataset_name)
            def embed(name):
                if not name:
                    return gr.Markdown("### No dataset provided")
                html_code = f"""
                <iframe
                src="https://huggingface.co/datasets/{name}/embed/viewer/default/train"
                frameborder="0"
                width="100%"
                height="350px"
                ></iframe>
                """
                return gr.HTML(value=html_code)

            with gr.Row():
                generate_eda_btn = gr.Button("Exploratory Data Analysis")
                generate_embedding_btn = gr.Button("Embeddings")
                generate_rag_btn = gr.Button("RAG")
                generate_training_btn = gr.Button(
                    "Training - Coming soon", interactive=False
                )
        with gr.Column(scale=1):
            with gr.Row():
                chatbot = gr.Chatbot(
                    label="Results",
                    type="messages",
                    height=650,
                    avatar_images=(
                        None,
                        None,
                    ),
                )

    notebook_file = gr.File(visible=False)
    generate_eda_btn.click(
        generate_eda_cells,
        inputs=[dataset_name],
        outputs=[chatbot, notebook_file],
    )

    generate_embedding_btn.click(
        generate_embedding_cells,
        inputs=[dataset_name],
        outputs=[chatbot, notebook_file],
    )

    generate_rag_btn.click(
        generate_rag_cells,
        inputs=[dataset_name],
        outputs=[chatbot, notebook_file],
    )

    generate_training_btn.click(coming_soon_message, inputs=[], outputs=[])


demo.launch()