File size: 8,444 Bytes
8773ff3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798f8ba
8773ff3
798f8ba
8773ff3
798f8ba
 
 
 
 
 
 
 
8773ff3
 
798f8ba
 
 
8773ff3
 
 
 
 
 
 
 
 
 
 
 
 
798f8ba
 
8773ff3
798f8ba
 
 
 
 
8773ff3
798f8ba
8773ff3
 
798f8ba
8773ff3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b83e74
8773ff3
 
4b83e74
 
 
 
 
 
 
 
 
8773ff3
 
 
 
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
import json

import streamlit as st

from hub import push_dataset_to_hub
from infer import query
from defaults import (
    DEFAULT_DOMAIN,
    DEFAULT_PERSPECTIVES,
    DEFAULT_TOPICS,
    DEFAULT_EXAMPLES,
    DEFAULT_SYSTEM_PROMPT,
    N_PERSPECTIVES,
    N_TOPICS,
    SEED_DATA_PATH,
    PIPELINE_PATH,
    DATASET_REPO_ID,
)
from utils import project_sidebar

st.set_page_config(
    page_title="Domain Data Grower",
    page_icon="πŸ§‘β€πŸŒΎ",
)
project_sidebar()

################################################################################
# HEADER
################################################################################

st.header("πŸ§‘β€πŸŒΎ Domain Data Grower")
st.divider()
st.subheader(
    "Step 2. Define the specific domain that you want to generate synthetic data for.",
)
st.write(
    "Define the project details, including the project name, domain, and API credentials"
)

################################################################################
# Domain Expert Section
################################################################################

(
    tab_domain_expert,
    tab_domain_perspectives,
    tab_domain_topics,
    tab_examples,
) = st.tabs(
    tabs=[
        "πŸ‘©πŸΌβ€πŸ”¬ Domain Expert",
        "πŸ” Domain Perspectives",
        "πŸ•ΈοΈ Domain Topics",
        "πŸ“š Examples",
    ]
)

with tab_domain_expert:
    st.text("Define the domain expertise that you want to train a language model")
    st.info(
        "A domain expert is a person who is an expert in a particular field or area. For example, a domain expert in farming would be someone who has extensive knowledge and experience in farming and agriculture."
    )

    domain = st.text_input("Domain Name", DEFAULT_DOMAIN)

    domain_expert_prompt = st.text_area(
        label="Domain Expert Definition",
        value=DEFAULT_SYSTEM_PROMPT,
        height=200,
    )

################################################################################
# Domain Perspectives
################################################################################

with tab_domain_perspectives:
    st.text("Define the different perspectives from which the domain can be viewed")
    st.info(
        """
    Perspectives are different viewpoints or angles from which a domain can be viewed. 
    For example, the domain of farming can be viewed from the perspective of a commercial 
    farmer or an independent family farmer."""
    )

    perspectives = st.session_state.get(
        "perspectives",
        [DEFAULT_PERSPECTIVES[0]],
    )
    perspectives_container = st.container()

    perspectives = [
        perspectives_container.text_input(
            f"Domain Perspective {i + 1}", value=perspective
        )
        for i, perspective in enumerate(perspectives)
    ]

    if st.button("Add Perspective", key="add_perspective"):
        n = len(perspectives)
        value = DEFAULT_PERSPECTIVES[n] if n < N_PERSPECTIVES else ""
        perspectives.append(
            perspectives_container.text_input(f"Domain Perspective {n + 1}", value="")
        )
        st.session_state["perspectives"] = perspectives


################################################################################
# Domain Topics
################################################################################

with tab_domain_topics:
    st.text("Define the main themes or subjects that are relevant to the domain")
    st.info(
        """Topics are the main themes or subjects that are relevant to the domain. For example, the domain of farming can have topics like soil health, crop rotation, or livestock management."""
    )
    topics = st.session_state.get(
        "topics",
        [DEFAULT_TOPICS[0]],
    )
    topics_container = st.container()
    topics = [
        topics_container.text_input(f"Domain Topic {i + 1}", value=topic)
        for i, topic in enumerate(topics)
    ]

    if st.button("Add Topic", key="add_topic"):
        n = len(topics)
        value = DEFAULT_TOPICS[n] if n < N_TOPICS else ""
        topics.append(topics_container.text_input(f"Domain Topics {n + 1}", value=""))
        st.session_state["topics"] = topics


################################################################################
# Examples Section
################################################################################

with tab_examples:
    st.text(
        "Add high-quality questions and answers that can be used to generate synthetic data"
    )
    st.info(
        """
    Examples are high-quality questions and answers that can be used to generate 
    synthetic data for the domain. These examples will be used to train the language model
    to generate questions and answers.
    """
    )

    questions_answers = st.session_state.get(
        "questions_answers",
        [
            (
                st.text_area(
                    "Question", key="question_0", value=DEFAULT_EXAMPLES[0]["question"]
                ),
                st.text_area(
                    "Answer", key="answer_0", value=DEFAULT_EXAMPLES[0]["answer"]
                ),
            )
        ],
    )

    if st.button("Add New Example"):
        n = len(questions_answers)
        default_question, default_answer = DEFAULT_EXAMPLES[n].values()
        st.subheader(f"Example {n + 1}")
        if st.button("Generate New Answer", key=f"generate_{n}"):
            default_answer = query(default_question)
        _question = st.text_area(
            "Question", key=f"question_{n}", value=default_question
        )
        _answer = st.text_area("Answer", key=f"answer_{n}", value=default_answer)
        questions_answers.append((_question, _answer))
        st.session_state["questions_answers"] = questions_answers

################################################################################
# Setup Dataset on the Hub
################################################################################

st.divider()

hub_username = DATASET_REPO_ID.split("/")[0]
project_name = DATASET_REPO_ID.split("/")[1]
st.write("Define the dataset repo details on the Hub")
st.session_state["project_name"] = st.text_input("Project Name", project_name)
st.session_state["hub_username"] = st.text_input("Hub Username", hub_username)
st.session_state["hub_token"] = st.text_input("Hub Token", type="password", value=None)

if all(
    (
        st.session_state.get("project_name"),
        st.session_state.get("hub_username"),
        st.session_state.get("hub_token"),
    )
):
    st.success(f"Using the dataset repo {hub_username}/{project_name} on the Hub")


if st.button("πŸ€— Push Dataset Seed") and all(
    (
        domain,
        domain_expert_prompt,
        perspectives,
        topics,
        questions_answers,
    )
):
    if all(
        (
            st.session_state.get("project_name"),
            st.session_state.get("hub_username"),
            st.session_state.get("hub_token"),
        )
    ):
        project_name = st.session_state["project_name"]
        hub_username = st.session_state["hub_username"]
        hub_token = st.session_state["hub_token"]
    else:
        st.error(
            "Please create a dataset repo on the Hub before pushing the dataset seed"
        )
        st.stop()

    perspectives = list(filter(None, perspectives))
    topics = list(filter(None, topics))
    examples = [{"question": q, "answer": a} for q, a in questions_answers]

    domain_data = {
        "domain": domain,
        "perspectives": perspectives,
        "topics": topics,
        "examples": examples,
        "domain_expert_prompt": domain_expert_prompt,
    }

    with open(SEED_DATA_PATH, "w") as f:
        json.dump(domain_data, f, indent=2)

    push_dataset_to_hub(
        domain_seed_data_path=SEED_DATA_PATH,
        project_name=project_name,
        domain=domain,
        hub_username=hub_username,
        hub_token=hub_token,
        pipeline_path=PIPELINE_PATH,
    )

    st.success(
        f"Dataset seed created and pushed to the Hub. Check it out [here](https://huggingface.co/datasets/{hub_username}/{project_name})"
    )

    st.write("You can now move on to runnning your distilabel pipeline.")

    st.page_link(
        page="pages/3_🌱 Generate Dataset.py",
        label="Generate Dataset",
        icon="🌱",
    )

else:
    st.info(
        "Please fill in all the required domain fields to push the dataset seed to the Hub"
    )