Ben Burtenshaw
commited on
Commit
·
d9e9462
1
Parent(s):
3c9d064
transfer pipeline
Browse files- app.py +0 -8
- hub.py +0 -18
- pipeline.py +0 -183
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from hub import (
|
|
| 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,13 +107,6 @@ if st.button("🤗 Setup Project Resources"):
|
|
| 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 |
|
|
|
|
| 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 |
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 |
|
hub.py
CHANGED
|
@@ -74,21 +74,3 @@ def pull_seed_data_from_repo(repo_id, hub_token):
|
|
| 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}")
|
|
|
|
| 74 |
return json.load(open(tempfile_path))
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipeline.py
DELETED
|
@@ -1,183 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
from textwrap import dedent
|
| 3 |
-
from typing import Any, Dict, List
|
| 4 |
-
|
| 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":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