File size: 31,057 Bytes
9917d34
 
476f41e
 
9917d34
476f41e
9917d34
 
 
 
 
 
06594f2
76d1b05
06594f2
 
 
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d1b05
9917d34
76d1b05
9917d34
76d1b05
 
 
 
 
9917d34
76d1b05
9917d34
76d1b05
 
 
 
9917d34
76d1b05
9917d34
76d1b05
 
 
 
9917d34
76d1b05
9917d34
76d1b05
 
9917d34
76d1b05
9917d34
 
 
76d1b05
9917d34
 
 
 
 
2106945
f402348
2106945
f402348
9917d34
 
 
 
 
 
 
685b5bb
9917d34
 
685b5bb
9917d34
 
 
06594f2
 
 
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faef657
 
 
 
9917d34
faef657
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faef657
9917d34
 
 
 
faef657
9917d34
 
 
 
 
 
faef657
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faef657
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d1b05
 
 
9917d34
 
 
 
 
 
76d1b05
 
 
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
76d1b05
 
9917d34
76d1b05
 
9917d34
76d1b05
9917d34
76d1b05
 
9917d34
76d1b05
 
 
 
 
 
 
 
 
 
 
 
9917d34
76d1b05
9917d34
 
 
 
 
76d1b05
9917d34
 
 
 
 
76d1b05
 
9917d34
 
63a19c9
 
76d1b05
9917d34
76d1b05
9917d34
 
 
 
 
 
76d1b05
 
9917d34
 
 
 
 
6a77fdf
76d1b05
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d1b05
9917d34
 
 
 
 
 
 
6a77fdf
76d1b05
 
 
9917d34
 
 
 
 
06594f2
 
 
 
76d1b05
06594f2
 
 
 
 
9917d34
 
 
 
76d1b05
9917d34
 
 
 
 
76d1b05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63a19c9
 
 
 
76d1b05
 
 
 
 
 
9917d34
 
 
 
76d1b05
9917d34
 
 
 
 
76d1b05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63a19c9
 
 
 
76d1b05
 
 
 
9917d34
097faaf
fb73a92
 
 
76d1b05
06594f2
097faaf
06594f2
 
76d1b05
faef657
06594f2
 
 
 
 
faef657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06594f2
76d1b05
 
06594f2
 
 
76d1b05
 
 
06594f2
76d1b05
06594f2
 
 
 
 
 
76d1b05
 
 
 
 
 
 
 
06594f2
 
 
 
 
 
76d1b05
06594f2
 
76d1b05
 
 
9917d34
 
 
 
06594f2
9917d34
 
 
 
 
 
 
 
 
 
 
476f41e
 
 
06594f2
 
476f41e
9917d34
06594f2
 
476f41e
 
 
 
9917d34
63a19c9
9917d34
06594f2
9917d34
 
 
 
 
 
 
 
476f41e
 
 
9917d34
 
476f41e
9917d34
476f41e
 
 
 
 
9917d34
476f41e
76d1b05
9917d34
 
 
76d1b05
 
 
9917d34
76d1b05
9917d34
 
 
 
06594f2
9917d34
76d1b05
 
 
9917d34
76d1b05
 
 
 
9917d34
06594f2
9917d34
 
 
 
06594f2
9917d34
 
 
 
 
06594f2
 
 
 
476f41e
06594f2
476f41e
9917d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faef657
476f41e
 
9917d34
06594f2
476f41e
 
 
 
 
 
 
06594f2
 
 
 
476f41e
06594f2
 
476f41e
 
06594f2
476f41e
 
 
06594f2
476f41e
06594f2
476f41e
 
 
 
 
 
 
 
06594f2
 
 
097faaf
476f41e
06594f2
 
476f41e
fb73a92
06594f2
fb73a92
 
 
06594f2
 
476f41e
 
06594f2
 
faef657
06594f2
 
476f41e
 
 
 
fb73a92
476f41e
097faaf
476f41e
 
 
9917d34
476f41e
 
 
 
 
9917d34
 
 
 
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import json
import logging
import os
import tempfile
import time
import zipfile
from io import StringIO

import pandas as pd
import streamlit as st
from datasets import load_dataset
from gretel_client import Gretel
from navigator_helpers import (
    InstructionResponseConfig,
    TrainingDataSynthesizer,
    StreamlitLogHandler,
)

# Create a StringIO buffer to capture the logging output
log_buffer = StringIO()

# Create a handler to redirect logging output to the buffer
handler = logging.StreamHandler(log_buffer)
handler.setLevel(logging.INFO)

# Set up the logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(handler)


SAMPLE_DATASET_URL = "https://gretel-public-website.s3.us-west-2.amazonaws.com/datasets/llm-training-data/dolly-examples-qa-with-context.csv"
WELCOME_MARKDOWN = """Gretel Navigator is an advanced AI system for generating high-quality, diverse synthetic data to train AI models and LLMs. It combines cutting-edge techniques from recent research with Gretel's proprietary methods to enhance your training data.

### 🌟 Key Features & Techniques

- **Evolutionary Text Generation**: Inspired by WizardLM-2's diverse knowledge generation
- **AI-Aligning-AI (AAA)**: Leveraging concepts from Self-Rewarding Language Models
- **Quality Evaluation & Ranking**: Using Gretel's proprietary scoring methods
- **Instruction-Response Generation**: Influenced by StarCoder2-Instruct's approach
- **Comprehensive Training Data**: Inspired by "Textbooks Are All You Need II"

### πŸš€ Use Cases

1. Create diverse training/evaluation data from seeds
2. Enhance limited datasets
3. Mitigate bias and toxicity
4. Improve model performance with domain-specific data

### πŸ”§ How It Works

1. Initialize with custom configuration
2. Generate and evolve text populations
3. Apply AI Align AI (AAA) for quality enhancement
4. Evaluate and output high-quality synthetic data

### πŸ“‚ Input & Output

- **Input**: Seed data (text or input/output pairs) in various formats (CSV, JSON, JSONL, Hugging Face datasets)
- **Output**: High-quality synthetic training examples

Ready to elevate your AI training data? Let's get started with Gretel Navigator! πŸš€

---

*Gretel Navigator combines techniques from recent academic research with Gretel's innovative approaches to deliver state-of-the-art synthetic data generation.*
"""


def main():
    st.set_page_config(page_title="Gretel", layout="wide")
    st.title("🎨 Gretel Navigator: Create Synthetic Data from a Prompt")
    st.write(
        "Generate diverse synthetic training data from text or existing datasets to improve or evaluate AI models."
    )

    with st.expander("Introduction", expanded=False):
        st.markdown(WELCOME_MARKDOWN)

    st.subheader("Step 1: API Key Validation")
    with st.expander("API Key Configuration", expanded=True):
        api_key = st.text_input(
            "Enter your Gretel API key (Get a free API key at: https://console.gretel.ai/users/me/key)",
            value="",
            type="password",
            help="Your Gretel API key is required to authenticate and use Gretel Navigator. If you don't have one yet, sign up for a free account at https://console.gretel.ai to get started.",
        )
        if "gretel" not in st.session_state:
            st.session_state.gretel = None
        if "synthesized_data" not in st.session_state:
            st.session_state.synthesized_data = []

        if st.button("Validate API Key"):
            if api_key:
                try:
                    st.session_state.gretel = Gretel(api_key=api_key, validate=True)
                    st.success("API key validated. Connection successful!")
                except Exception as e:
                    st.error(f"Error connecting to Gretel: {str(e)}")
            else:
                st.warning("Please enter your Gretel API key to proceed.")
        if st.session_state.gretel is None:
            st.stop()

    st.subheader("Step 2: Data Source Selection")
    with st.expander("Data Source", expanded=True):
        data_source = st.radio(
            "Select data source",
            options=[
                "Upload a file",
                "Select a dataset from Hugging Face",
                "Use a sample dataset",
            ],
            help="Choose whether to upload a file, select a dataset from Hugging Face, or use a sample dataset",
        )

        df = None
        dataset_source_type = ""
        huggingface_dataset = ""
        huggingface_split = ""
        
        if data_source == "Upload a file":
            dataset_source_type = "uploaded"
            uploaded_file = st.file_uploader(
                "Upload a CSV, JSON, or JSONL file",
                type=["csv", "json", "jsonl"],
                help="Upload the dataset file in CSV, JSON, or JSONL format",
            )

            if uploaded_file is not None:
                if uploaded_file.name.endswith(".csv"):
                    df = pd.read_csv(uploaded_file)
                elif uploaded_file.name.endswith(".json"):
                    df = pd.read_json(uploaded_file)
                elif uploaded_file.name.endswith(".jsonl"):
                    df = pd.read_json(uploaded_file, lines=True)
                st.success(f"File uploaded successfully: {uploaded_file.name}")

        elif data_source == "Select a dataset from Hugging Face":
            dataset_source_type = "huggingface"
            huggingface_dataset = st.text_input(
                "Hugging Face Dataset Repository",
                help="Enter the name of the Hugging Face dataset repository (e.g., 'squad')",
            )
            st.session_state.huggingface_dataset = huggingface_dataset

            huggingface_split = st.selectbox(
                "Dataset Split",
                options=["train", "validation", "test"],
                help="Select the dataset split to use",
            )
            st.session_state.huggingface_split = huggingface_split

            if st.button("Load Hugging Face Dataset"):
                if huggingface_dataset:
                    try:
                        with st.spinner("Loading dataset from Hugging Face..."):
                            dataset = load_dataset(
                                huggingface_dataset, split=huggingface_split
                            )
                            df = dataset.to_pandas()
                        st.success(
                            f"Dataset loaded from Hugging Face repository: {huggingface_dataset}"
                        )
                    except Exception as e:
                        st.error(f"Error loading dataset from Hugging Face: {str(e)}")
                else:
                    st.warning("Please provide a Hugging Face dataset repository name.")

        elif data_source == "Use a sample dataset":
            dataset_source_type = "sample"
            st.write("Try a sample dataset to get started quickly.")
            if st.button("Try Sample Dataset"):
                try:
                    df = pd.read_csv(SAMPLE_DATASET_URL)
                    st.success("Sample dataset loaded successfully.")
                except Exception as e:
                    st.error(f"Error downloading sample dataset: {str(e)}")

        if df is not None:
            st.session_state.df = df
            st.session_state.selected_fields = list(df.columns)
            st.write(
                f"Loaded dataset with {len(df)} rows and {len(df.columns)} columns."
            )
        else:
            df = st.session_state.get("df")

    st.subheader("Step 3: Data Preview and Configuration")
    if df is not None:
        with st.expander("Data Preview", expanded=True):
            st.dataframe(df.head())

        with st.expander("Input Fields Selection", expanded=True):
            st.write(
                "Select the context fields to provide the LLM access to for generating input/output pairs. This can include existing instructions and responses. All selected fields will be treated as ground truth data."
            )

            selected_fields = []
            for column in df.columns:
                if st.checkbox(
                    column,
                    value=column in st.session_state.get("selected_fields", []),
                    key=f"checkbox_{column}",
                ):
                    selected_fields.append(column)

            st.session_state.selected_fields = selected_fields

        with st.expander("Advanced Options", expanded=False):
            output_instruction_field = st.text_input(
                "Synthetic instruction field",
                value=st.session_state.get(
                    "output_instruction_field", "synthetic_instruction"
                ),
                help="Specify the name of the output field for generated instructions",
            )
            st.session_state.output_instruction_field = output_instruction_field

            output_response_field = st.text_input(
                "Synthetic response field",
                value=st.session_state.get(
                    "output_response_field", "synthetic_response"
                ),
                help="Specify the name of the output field for generated responses",
            )
            st.session_state.output_response_field = output_response_field

            num_records = st.number_input(
                "Max number of records from input data to process",
                min_value=1,
                max_value=len(df),
                value=len(df),
                help="Specify the number of records to process",
            )
            st.session_state.num_records = num_records

            num_generations = st.number_input(
                "Number of generations",
                min_value=1,
                value=st.session_state.get("num_generations", 3),
                help="Specify the number of generations for the evolutionary algorithm",
            )
            st.session_state.num_generations = num_generations

            population_size = st.number_input(
                "Population size",
                min_value=1,
                value=st.session_state.get("population_size", 5),
                help="Specify the population size for the evolutionary algorithm",
            )
            st.session_state.population_size = population_size

            mutation_rate = st.slider(
                "Mutation rate",
                min_value=0.0,
                max_value=1.0,
                value=st.session_state.get("mutation_rate", 0.5),
                step=0.1,
                help="Adjust the mutation rate for the evolutionary algorithm",
            )
            st.session_state.mutation_rate = mutation_rate

            temperature = st.slider(
                "Temperature",
                min_value=0.0,
                max_value=1.0,
                value=st.session_state.get("temperature", 0.7),
                step=0.1,
                help="Adjust the temperature for response generation",
            )
            st.session_state.temperature = temperature

            max_tokens = st.slider(
                "Max tokens",
                min_value=1,
                max_value=1024,
                step=64,
                value=st.session_state.get("max_tokens", 192),
                help="Specify the maximum number of tokens for generated text",
            )
            st.session_state.max_tokens = max_tokens

        with st.expander("Model Configuration", expanded=True):
            st.markdown("### Primary Navigator Models")

            navigator_tabular = st.selectbox(
                "Navigator Tabular",
                options=["gretelai/auto"],
                index=0,
                help="Select the primary Navigator tabular model",
            )

            navigator_llm = st.selectbox(
                "Navigator LLM",
                options=["gretelai/gpt-auto", "gretelai/gpt-llama3-1-8b"],
                index=0,
                help="Select the primary Navigator LLM",
            )

            st.markdown("---")
            st.markdown("### AI Align AI (AAA)")
            st.write(
                "AI Align AI (AAA) is a technique that iteratively improves the quality and coherence of generated outputs by using multiple LLMs for co-teaching and self-teaching. Enabling AAA will enhance the overall quality of the synthetic data, but it may slow down the generation process."
            )

            use_aaa = st.checkbox(
                "Use AI Align AI (AAA)",
                value=st.session_state.get("use_aaa", True),
                help="Enable or disable the use of AI Align AI.",
            )
            st.session_state.use_aaa = use_aaa

            co_teach_llms = []

            if use_aaa:
                st.markdown("#### Navigator Co-teaching LLMs")
                st.write(
                    "Select additional Navigator LLMs for co-teaching in AAA. It is recommended to use different LLMs than the primary Navigator LLM for this step."
                )

                co_teach_options = ["gretelai/gpt-llama3-1-8b", "gretelai/gpt-mistral-nemo-2407"]
                for model in co_teach_options:
                    if st.checkbox(model, value=True, key=f"checkbox_{model}"):
                        co_teach_llms.append(model)
                st.session_state.co_teach_llms = co_teach_llms

            st.markdown("---")
            st.markdown("### Format Prompts")

            system_prompt = st.text_area(
                "System Prompt",
                value=st.session_state.get(
                    "system_prompt",
                    "You are an expert in generating balanced, context-rich questions and comprehensive answers based on given contexts. Your goal is to create question-answer pairs that are informative, detailed when necessary, and understandable without prior knowledge, while not revealing the answer in the question.",
                ),
                help="Specify the system prompt for the LLM",
            )
            st.session_state.system_prompt = system_prompt

            instruction_format_prompt = st.text_area(
                "Instruction Format Prompt",
                value=st.session_state.get(
                    "instruction_format_prompt",
                    "Generate a specific and clear question directly related to a key point in the given context. The question should include enough background information to be understood without prior knowledge, while being answerable using only the information provided. Do not reveal the answer in the question. Ensure the question is focused and can be answered concisely if the information allows, but also accommodate for more detailed responses when appropriate.",
                ),
                help="Specify the format prompt for instructions",
            )
            st.session_state.instruction_format_prompt = instruction_format_prompt

            instruction_mutation_prompt = st.text_area(
                "Instruction Mutation Prompt",
                value=st.session_state.get(
                    "instruction_mutation_prompt",
                    "Refine this question to include necessary context for understanding, without revealing the answer. Ensure it remains clear and can be comprehensively answered using only the information in the given context. Adjust the question to allow for a concise answer if possible, but also consider if a more detailed response is warranted based on the complexity of the topic.",
                ),
                help="Specify the mutation prompt for instructions",
            )
            st.session_state.instruction_mutation_prompt = instruction_mutation_prompt

            instruction_quality_prompt = st.text_area(
                "Instruction Quality Prompt",
                value=st.session_state.get(
                    "instruction_quality_prompt",
                    "Evaluate the quality of this question based on its specificity, inclusion of necessary context, relevance to the original context, clarity for someone unfamiliar with the topic, and ability to be answered appropriately (either concisely or in detail) without revealing the answer:",
                ),
                help="Specify the quality evaluation prompt for instructions",
            )
            st.session_state.instruction_quality_prompt = instruction_quality_prompt

            instruction_complexity_target = st.slider(
                "Instruction Complexity Target",
                min_value=1,
                max_value=5,
                value=st.session_state.get("instruction_complexity_target", 3),
                step=1,
                help="Specify the target complexity for instructions",
            )
            st.session_state.instruction_complexity_target = (
                instruction_complexity_target
            )

            response_format_prompt = st.text_area(
                "Response Format Prompt",
                value=st.session_state.get(
                    "response_format_prompt",
                    "Generate an informative answer to the given question. Use only the information provided in the original context. The response should be as concise as possible while fully addressing the question, including relevant context and explanations where necessary. For complex topics, provide a more detailed response. Ensure the answer provides enough background information to be understood by someone unfamiliar with the topic.",
                ),
                help="Specify the format prompt for responses",
            )
            st.session_state.response_format_prompt = response_format_prompt

            response_mutation_prompt = st.text_area(
                "Response Mutation Prompt",
                value=st.session_state.get(
                    "response_mutation_prompt",
                    "Refine this answer to balance conciseness with comprehensiveness. For straightforward questions, aim for brevity while ensuring accuracy. For complex topics, provide more detail and context. Add relevant information from the context as needed. Verify factual accuracy and correct any inaccuracies or missing key information. Ensure the answer can be understood without prior knowledge of the topic.",
                ),
                help="Specify the mutation prompt for responses",
            )
            st.session_state.response_mutation_prompt = response_mutation_prompt

            response_quality_prompt = st.text_area(
                "Response Quality Prompt",
                value=st.session_state.get(
                    "response_quality_prompt",
                    "Evaluate the quality of this answer based on its accuracy, appropriate level of detail (concise for simple questions, comprehensive for complex ones), relevance to the question, clarity for someone unfamiliar with the topic, inclusion of necessary background information, and whether it provides a satisfactory response using only the information from the given context:",
                ),
                help="Specify the quality evaluation prompt for responses",
            )
            st.session_state.response_quality_prompt = response_quality_prompt

            response_complexity_target = st.slider(
                "Response Complexity Target",
                min_value=1,
                max_value=5,
                value=st.session_state.get("response_complexity_target", 3),
                step=1,
                help="Specify the target complexity for responses",
            )
            st.session_state.response_complexity_target = response_complexity_target

        with st.expander("Download SDK Code", expanded=False):
            st.markdown("### Ready to generate data at scale?")
            st.write(
                "Get started with your current configuration using the SDK code below:"
            )

            config_text = f"""#!pip install -Uqq git+https://github.com/gretelai/navigator-helpers.git

import logging
import pandas as pd
from navigator_helpers import InstructionResponseConfig, TrainingDataSynthesizer
from datasets import load_dataset

# Configure the logger
logging.basicConfig(level=logging.INFO, format="%(message)s")

API_KEY = "YOUR_API_KEY"
DATASET_SOURCE = "{dataset_source_type}"
HUGGINGFACE_DATASET = "{huggingface_dataset}"
HUGGINGFACE_SPLIT = "{huggingface_split}"
SAMPLE_DATASET_URL = "{SAMPLE_DATASET_URL}"

# Load dataset
if DATASET_SOURCE == 'uploaded':
    df = pd.read_csv("YOUR_UPLOADED_FILE_PATH")  # Replace with the actual file path
elif DATASET_SOURCE == 'huggingface':
    dataset = load_dataset(HUGGINGFACE_DATASET, split=HUGGINGFACE_SPLIT)
    df = dataset.to_pandas()
elif DATASET_SOURCE == 'sample':
    df = pd.read_csv(SAMPLE_DATASET_URL)
else:
    raise ValueError("Invalid DATASET_SOURCE specified")

# Create the instruction response configuration
config = InstructionResponseConfig(
    input_fields={st.session_state.selected_fields},
    output_instruction_field="{output_instruction_field}",
    output_response_field="{output_response_field}",
    num_generations={num_generations},
    population_size={population_size},
    mutation_rate={mutation_rate},
    temperature={temperature},
    max_tokens={max_tokens},
    api_key=API_KEY,
    navigator_tabular="{navigator_tabular}",
    navigator_llm="{navigator_llm}",
    co_teach_llms={co_teach_llms},
    system_prompt='''{system_prompt}''',
    instruction_format_prompt='''{instruction_format_prompt}''',
    instruction_mutation_prompt='''{instruction_mutation_prompt}''',
    instruction_quality_prompt='''{instruction_quality_prompt}''',
    instruction_complexity_target={instruction_complexity_target},
    response_format_prompt='''{response_format_prompt}''',
    response_mutation_prompt='''{response_mutation_prompt}''',
    response_quality_prompt='''{response_quality_prompt}''',
    response_complexity_target={response_complexity_target},
    use_aaa={use_aaa}
)

# Create the training data synthesizer and perform synthesis
synthesizer = TrainingDataSynthesizer(
    df,
    config,
    output_file="results.jsonl",
    verbose=True,
)
new_df = synthesizer.generate()
"""

            st.code(config_text, language="python")
            st.download_button(
                label="Download SDK Code",
                data=config_text,
                file_name="data_synthesis_code.py",
                mime="text/plain",
            )

        start_stop_container = st.empty()

        col1, col2 = st.columns(2)
        with col1:
            start_button = st.button("πŸš€ Start")
        with col2:
            stop_button = st.button("πŸ›‘ Stop")

        if "logs" not in st.session_state:
            st.session_state.logs = []

        if "synthetic_data" not in st.session_state:
            st.session_state.synthetic_data = []

        if start_button:
            # Clear the synthetic data and logs before starting a new generation
            st.session_state.synthetic_data = []
            st.session_state.logs = []

            with st.expander("Synthetic Data", expanded=True):
                st.subheader("Synthetic Data Generation")
                progress_bar = st.progress(0)
                tab1, tab2 = st.tabs(["Synthetic Data", "Logs"])
                with tab1:
                    synthetic_data_placeholder = st.empty()
                    st.info(
                        "Click on the 'Logs' tab to see and debug real-time logging for each record as it is generated by the agents."
                    )
                with tab2:
                    log_container = st.empty()
                    max_log_lines = 50

                def custom_log_handler(msg):
                    st.session_state.logs.append(msg)
                    displayed_logs = st.session_state.logs[-max_log_lines:]
                    log_text = "\n".join(displayed_logs)
                    log_container.text(log_text)

                # Remove the previous log handler if it exists
                logger = logging.getLogger("navigator_helpers")
                for handler in logger.handlers:
                    if isinstance(handler, StreamlitLogHandler):
                        logger.removeHandler(handler)

                handler = StreamlitLogHandler(custom_log_handler)
                logger.addHandler(handler)

                config = InstructionResponseConfig(
                    input_fields=selected_fields,
                    output_instruction_field=output_instruction_field,
                    output_response_field=output_response_field,
                    num_generations=num_generations,
                    population_size=population_size,
                    mutation_rate=mutation_rate,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    api_key=api_key,
                    navigator_tabular=navigator_tabular,
                    navigator_llm=navigator_llm,
                    co_teach_llms=co_teach_llms,
                    system_prompt=system_prompt,
                    instruction_format_prompt=instruction_format_prompt,
                    instruction_mutation_prompt=instruction_mutation_prompt,
                    instruction_quality_prompt=instruction_quality_prompt,
                    instruction_complexity_target=instruction_complexity_target,
                    response_format_prompt=response_format_prompt,
                    response_mutation_prompt=response_mutation_prompt,
                    response_quality_prompt=response_quality_prompt,
                    response_complexity_target=response_complexity_target,
                    use_aaa=use_aaa,
                )

                start_time = time.time()
                with st.spinner("Generating synthetic data..."):
                    for index in range(num_records):
                        row = df.iloc[index]
                        synthesizer = TrainingDataSynthesizer(
                            pd.DataFrame([row]),
                            config,
                            output_file="results.csv",
                            verbose=True,
                        )
                        new_df = synthesizer.generate()
                        st.session_state.synthetic_data.append(new_df)
                        synthetic_data_placeholder.subheader("Synthetic Data")
                        synthetic_data_placeholder.dataframe(
                            pd.concat(
                                st.session_state.synthetic_data, ignore_index=True
                            )
                        )
                        progress = (index + 1) / num_records
                        progress_bar.progress(progress)

                        elapsed_time = time.time() - start_time
                        records_processed = index + 1
                        records_remaining = num_records - records_processed
                        est_time_per_record = (
                            elapsed_time / records_processed
                            if records_processed > 0
                            else 0
                        )
                        est_time_remaining = est_time_per_record * records_remaining

                        progress_text = f"Progress: {progress:.2%} | Records Processed: {records_processed} | Records Remaining: {records_remaining} | Est. Time per Record: {est_time_per_record:.2f}s | Est. Time Remaining: {est_time_remaining:.2f}s"
                        progress_bar.text(progress_text)

                        time.sleep(0.1)
                logger.removeHandler(handler)
                st.success("Data synthesis completed!")
            st.stop()

        if stop_button:
            st.warning("Synthesis stopped by the user.")

            # Get the complete logs from the session state
            complete_logs = st.session_state.logs

            # Convert complete logs to JSONL format
            log_jsonl = "\n".join([json.dumps({"log": log}) for log in complete_logs])

            # Convert synthesized data to JSONL format if it exists
            if st.session_state.synthesized_data:
                synthesized_df = pd.concat(
                    st.session_state.synthesized_data, ignore_index=True
                )
                if not synthesized_df.empty:
                    synthesized_data_jsonl = "\n".join(
                        [
                            json.dumps(row.to_dict())
                            for _, row in synthesized_df.iterrows()
                        ]
                    )
                else:
                    synthesized_data_jsonl = None
            else:
                synthesized_data_jsonl = None

            # Create a temporary directory to store the files
            with tempfile.TemporaryDirectory() as temp_dir:
                # Write the complete logs to a file
                log_file_path = os.path.join(temp_dir, "complete_logs.jsonl")
                with open(log_file_path, "w") as log_file:
                    log_file.write(log_jsonl)

                # Write the synthesized data to a file if it exists
                if synthesized_data_jsonl:
                    synthesized_data_file_path = os.path.join(
                        temp_dir, "synthetic_data.jsonl"
                    )
                    with open(synthesized_data_file_path, "w") as synthesized_data_file:
                        synthesized_data_file.write(synthesized_data_jsonl)

                # Write the SDK code to a file
                sdk_file_path = os.path.join(temp_dir, "data_synthesis_code.py")
                with open(sdk_file_path, "w") as sdk_file:
                    sdk_file.write(config_text)

                # Create a ZIP file containing the logs, synthesized data, and SDK code
                zip_file_path = os.path.join(temp_dir, "synthesis_results.zip")
                with zipfile.ZipFile(zip_file_path, "w") as zip_file:
                    zip_file.write(log_file_path, "complete_logs.jsonl")
                    if synthesized_data_jsonl:
                        zip_file.write(
                            synthesized_data_file_path, "synthetic_data.jsonl"
                        )
                    zip_file.write(sdk_file_path, "data_synthesis_code.py")

                # Download the ZIP file
                with open(zip_file_path, "rb") as zip_file:
                    st.download_button(
                        label="πŸ’Ύ Download Synthetic Data, Logs, and SDK Code",
                        data=zip_file.read(),
                        file_name="gretel_synthetic_data.zip",
                        mime="application/zip",
                    )

            st.stop()

    else:
        st.info(
            "Please upload a file or select a dataset from Hugging Face to proceed."
        )


if __name__ == "__main__":
    main()