Ben Burtenshaw
commited on
Commit
Β·
3c9d064
1
Parent(s):
0ac0929
transfer pipeline
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from hub import (
|
|
| 4 |
setup_dataset_on_hub,
|
| 5 |
duplicate_space_on_hub,
|
| 6 |
add_project_config_to_space_repo,
|
|
|
|
| 7 |
)
|
| 8 |
|
| 9 |
import streamlit as st
|
|
@@ -107,6 +108,13 @@ if st.button("π€ Setup Project Resources"):
|
|
| 107 |
argilla_space_repo_id=f"{hub_username}/{argilla_name}",
|
| 108 |
project_space_repo_id=f"{hub_username}/{space_name}",
|
| 109 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
st.subheader("π’ Next Steps")
|
| 112 |
|
|
|
|
| 4 |
setup_dataset_on_hub,
|
| 5 |
duplicate_space_on_hub,
|
| 6 |
add_project_config_to_space_repo,
|
| 7 |
+
push_pipeline_to_hub,
|
| 8 |
)
|
| 9 |
|
| 10 |
import streamlit as st
|
|
|
|
| 108 |
argilla_space_repo_id=f"{hub_username}/{argilla_name}",
|
| 109 |
project_space_repo_id=f"{hub_username}/{space_name}",
|
| 110 |
)
|
| 111 |
+
|
| 112 |
+
push_pipeline_to_hub(
|
| 113 |
+
pipeline_path="pipeline.py",
|
| 114 |
+
hub_username=hub_username,
|
| 115 |
+
hub_token=hub_token,
|
| 116 |
+
project_name=project_name,
|
| 117 |
+
)
|
| 118 |
|
| 119 |
st.subheader("π’ Next Steps")
|
| 120 |
|
hub.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import json
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from huggingface_hub import duplicate_space, HfApi
|
| 4 |
|
|
@@ -61,3 +63,32 @@ def add_project_config_to_space_repo(
|
|
| 61 |
repo_id=project_space_repo_id,
|
| 62 |
repo_type="space",
|
| 63 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
from tempfile import mktemp
|
| 3 |
+
|
| 4 |
|
| 5 |
from huggingface_hub import duplicate_space, HfApi
|
| 6 |
|
|
|
|
| 63 |
repo_id=project_space_repo_id,
|
| 64 |
repo_type="space",
|
| 65 |
)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def pull_seed_data_from_repo(repo_id, hub_token):
|
| 69 |
+
tempfile_path = mktemp()
|
| 70 |
+
# pull the dataset repo from the hub
|
| 71 |
+
hf_api.hf_hub_download(
|
| 72 |
+
repo_id=repo_id, token=hub_token, repo_type="dataset", filename=tempfile_path
|
| 73 |
+
)
|
| 74 |
+
return json.load(open(tempfile_path))
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def push_pipeline_to_hub(
|
| 78 |
+
pipeline_path,
|
| 79 |
+
hub_username,
|
| 80 |
+
hub_token: str,
|
| 81 |
+
project_name,
|
| 82 |
+
):
|
| 83 |
+
repo_id = f"{hub_username}/{project_name}"
|
| 84 |
+
|
| 85 |
+
# upload the pipeline to the hub
|
| 86 |
+
hf_api.upload_file(
|
| 87 |
+
path_or_fileobj=pipeline_path,
|
| 88 |
+
path_in_repo="pipeline.py",
|
| 89 |
+
token=hub_token,
|
| 90 |
+
repo_id=repo_id,
|
| 91 |
+
repo_type="dataset",
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
print(f"pipeline.py uploaded to {repo_id}")
|
pipeline.py
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
|
| 5 |
+
from distilabel.llms.huggingface import InferenceEndpointsLLM
|
| 6 |
+
from distilabel.pipeline import Pipeline
|
| 7 |
+
from distilabel.steps import TextGenerationToArgilla
|
| 8 |
+
from distilabel.steps.expand import ExpandColumns
|
| 9 |
+
from distilabel.steps.generators.data import LoadDataFromDicts
|
| 10 |
+
from distilabel.steps.tasks.self_instruct import SelfInstruct
|
| 11 |
+
from distilabel.steps.tasks.text_generation import TextGeneration
|
| 12 |
+
from distilabel.steps.tasks.typing import ChatType
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
################################################################################
|
| 16 |
+
# Functions to create task prompts
|
| 17 |
+
################################################################################
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def create_application_instruction(domain: str, examples: List[Dict[str, str]]):
|
| 21 |
+
"""Create the instruction for Self-Instruct task."""
|
| 22 |
+
system_prompt = dedent(
|
| 23 |
+
f"""You are an AI assistant than generates queries around the domain of {domain}.
|
| 24 |
+
Your should not expect basic but profound questions from your users.
|
| 25 |
+
The queries should reflect a diversxamity of vision and economic positions and political positions.
|
| 26 |
+
The queries may know about different methods of {domain}.
|
| 27 |
+
The queries can be positioned politically, economically, socially, or practically.
|
| 28 |
+
Also take into account the impact of diverse causes on diverse domains."""
|
| 29 |
+
)
|
| 30 |
+
for example in examples:
|
| 31 |
+
question = example["question"]
|
| 32 |
+
answer = example["answer"]
|
| 33 |
+
system_prompt += f"""\n- Question: {question}\n- Answer: {answer}\n"""
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def create_seed_terms(topics: List[str], perspectives: List[str]) -> List[str]:
|
| 37 |
+
"""Create seed terms for self intruct to start from."""
|
| 38 |
+
|
| 39 |
+
return [
|
| 40 |
+
f"{topic} from a {perspective} perspective"
|
| 41 |
+
for topic in topics
|
| 42 |
+
for perspective in perspectives
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
################################################################################
|
| 47 |
+
# Define out custom step for the domain expert
|
| 48 |
+
################################################################################
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class DomainExpert(TextGeneration):
|
| 52 |
+
"""A customized task to generate text as a domain expert in the domain of farming and agriculture."""
|
| 53 |
+
|
| 54 |
+
system_prompt: str
|
| 55 |
+
template: str = """This is the the instruction: {instruction}"""
|
| 56 |
+
|
| 57 |
+
def format_input(self, input: Dict[str, Any]) -> "ChatType":
|
| 58 |
+
return [
|
| 59 |
+
{
|
| 60 |
+
"role": "system",
|
| 61 |
+
"content": self.system_prompt,
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"role": "user",
|
| 65 |
+
"content": self.template.format(**input),
|
| 66 |
+
},
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
################################################################################
|
| 71 |
+
# Main script to run the pipeline
|
| 72 |
+
################################################################################
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
import argparse
|
| 77 |
+
import json
|
| 78 |
+
|
| 79 |
+
parser = argparse.ArgumentParser(
|
| 80 |
+
description="Run the pipeline to generate domain-specific datasets."
|
| 81 |
+
)
|
| 82 |
+
parser.add_argument("--hub-token", type=str, help="The Hugging Face API token.")
|
| 83 |
+
parser.add_argument("--argilla-api-key", type=str, help="The Argilla API key.")
|
| 84 |
+
parser.add_argument("--argilla-api-url", type=str, help="The Argilla API URL.")
|
| 85 |
+
parser.add_argument(
|
| 86 |
+
"--argilla-dataset-name", type=str, help="The name of the dataset in Argilla."
|
| 87 |
+
)
|
| 88 |
+
parser.add_argument(
|
| 89 |
+
"--seed_data_path",
|
| 90 |
+
type=str,
|
| 91 |
+
help="The path to the seed data.",
|
| 92 |
+
default="seed_data.json",
|
| 93 |
+
)
|
| 94 |
+
parser.add_argument(
|
| 95 |
+
"--endpoint-base-url", type=str, help="The base URL of the inference endpoint."
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
args = parser.parse_args()
|
| 99 |
+
|
| 100 |
+
# collect our seed data
|
| 101 |
+
|
| 102 |
+
with open(args.seed_data_path, "r") as f:
|
| 103 |
+
seed_data = json.load(f)
|
| 104 |
+
|
| 105 |
+
topics = seed_data.get("topics", [])
|
| 106 |
+
perspectives = seed_data.get("perspectives", [])
|
| 107 |
+
domain_expert_prompt = seed_data.get("domain_expert_prompt", "")
|
| 108 |
+
examples = seed_data.get("examples", [])
|
| 109 |
+
domain_name = seed_data.get("domain_name", "domain")
|
| 110 |
+
|
| 111 |
+
# Define the task prompts
|
| 112 |
+
|
| 113 |
+
terms = create_seed_terms(topics=topics, perspectives=perspectives)
|
| 114 |
+
application_instruction = create_application_instruction(
|
| 115 |
+
domain=domain_name, examples=examples
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Define the distilabel pipeline
|
| 119 |
+
|
| 120 |
+
with Pipeline(domain_name) as pipeline:
|
| 121 |
+
load_data = LoadDataFromDicts(
|
| 122 |
+
name="load_data",
|
| 123 |
+
data=[{"input": term} for term in terms],
|
| 124 |
+
batch_size=64,
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
self_instruct = SelfInstruct(
|
| 128 |
+
name="self_instruct",
|
| 129 |
+
num_instructions=5,
|
| 130 |
+
input_batch_size=8,
|
| 131 |
+
llm=InferenceEndpointsLLM(
|
| 132 |
+
base_url=args.endpoint_base_url,
|
| 133 |
+
api_key=args.hub_token,
|
| 134 |
+
),
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
expand_instructions = ExpandColumns(
|
| 138 |
+
name="expand_columns", columns={"instructions": "instruction"}
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
domain_expert = DomainExpert(
|
| 142 |
+
name="domain_expert",
|
| 143 |
+
llm=InferenceEndpointsLLM(
|
| 144 |
+
base_url=args.endpoint_base_url,
|
| 145 |
+
api_key=args.hub_token,
|
| 146 |
+
),
|
| 147 |
+
input_batch_size=8,
|
| 148 |
+
system_prompt=domain_expert_prompt,
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
to_argilla = TextGenerationToArgilla(
|
| 152 |
+
name="text_generation_to_argilla",
|
| 153 |
+
dataset_name=args.argilla_dataset_name,
|
| 154 |
+
dataset_workspace="admin",
|
| 155 |
+
api_url=args.argilla_api_url,
|
| 156 |
+
api_key=args.argilla_api_key,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Connect up the pipeline
|
| 160 |
+
|
| 161 |
+
load_data.connect(self_instruct)
|
| 162 |
+
self_instruct.connect(expand_instructions)
|
| 163 |
+
expand_instructions.connect(domain_expert)
|
| 164 |
+
domain_expert.connect(to_argilla)
|
| 165 |
+
|
| 166 |
+
# Run the pipeline
|
| 167 |
+
|
| 168 |
+
pipeline.run(
|
| 169 |
+
parameters={
|
| 170 |
+
"self_instruct": {
|
| 171 |
+
"llm": {"api_key": args.hub_token, "base_url": args.endpoint_base_url}
|
| 172 |
+
},
|
| 173 |
+
"domain_expert": {
|
| 174 |
+
"llm": {"api_key": args.hub_token, "base_url": args.endpoint_base_url}
|
| 175 |
+
},
|
| 176 |
+
"text_generation_to_argilla": {
|
| 177 |
+
"dataset_name": args.argilla_dataset_name,
|
| 178 |
+
"api_key": args.argilla_api_key,
|
| 179 |
+
"api_url": args.argilla_api_url,
|
| 180 |
+
},
|
| 181 |
+
},
|
| 182 |
+
use_cache=False,
|
| 183 |
+
)
|
utils.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from defaults import (
|
| 4 |
-
ARGILLA_SPACE_REPO_ID,
|
| 5 |
-
PROJECT_NAME,
|
| 6 |
-
ARGILLA_URL,
|
| 7 |
-
DIBT_PARENT_APP_URL,
|
| 8 |
-
DATASET_URL,
|
| 9 |
-
DATASET_REPO_ID,
|
| 10 |
-
ARGILLA_SPACE_REPO_ID,
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def project_sidebar():
|
| 15 |
-
if PROJECT_NAME == "DEFAULT_DOMAIN":
|
| 16 |
-
st.warning(
|
| 17 |
-
"Please set up the project configuration in the parent app before proceeding."
|
| 18 |
-
)
|
| 19 |
-
st.stop()
|
| 20 |
-
|
| 21 |
-
st.sidebar.subheader(f"A Data Growing Project in the domain of {PROJECT_NAME}")
|
| 22 |
-
st.sidebar.markdown(
|
| 23 |
-
"""
|
| 24 |
-
This space helps you create a dataset seed for building diverse domain-specific datasets for aligning models.
|
| 25 |
-
"""
|
| 26 |
-
)
|
| 27 |
-
st.sidebar.link_button(f"π Dataset Repo", DATASET_URL)
|
| 28 |
-
st.sidebar.link_button(f"π€ Argilla Space", ARGILLA_URL)
|
| 29 |
-
st.sidebar.divider()
|
| 30 |
-
st.sidebar.link_button("π§βπΎ New Project", DIBT_PARENT_APP_URL)
|
| 31 |
-
st.sidebar.link_button(
|
| 32 |
-
"π€ Get your Hub Token", "https://huggingface.co/settings/tokens"
|
| 33 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|