Spaces:
Runtime error
Runtime error
File size: 6,456 Bytes
8773ff3 4b83e74 8773ff3 4b83e74 8773ff3 4b83e74 8773ff3 4b83e74 8773ff3 4b83e74 8773ff3 4b83e74 8773ff3 4b83e74 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 |
import subprocess
import sys
import time
from typing import List
from distilabel.steps.generators.data import LoadDataFromDicts
from distilabel.steps.expand import ExpandColumns
from distilabel.steps.keep import KeepColumns
from distilabel.steps.tasks.self_instruct import SelfInstruct
from distilabel.steps.tasks.evol_instruct.base import EvolInstruct
from distilabel.llms.huggingface import InferenceEndpointsLLM
from distilabel.pipeline import Pipeline
from distilabel.steps import TextGenerationToArgilla
from dotenv import load_dotenv
from domain import (
DomainExpert,
CleanNumberedList,
create_topics,
create_examples_template,
APPLICATION_DESCRIPTION,
)
load_dotenv()
def define_pipeline(
argilla_api_key: str,
argilla_api_url: str,
argilla_dataset_name: str,
topics: List[str],
perspectives: List[str],
domain_expert_prompt: str,
examples: List[dict],
hub_token: str,
endpoint_base_url: str,
):
"""Define the pipeline for the specific domain."""
terms = create_topics(topics, perspectives)
template = create_examples_template(examples)
with Pipeline("farming") as pipeline:
load_data = LoadDataFromDicts(
name="load_data",
data=[{"input": term} for term in terms],
batch_size=64,
)
llm = InferenceEndpointsLLM(
base_url=endpoint_base_url,
api_key=hub_token,
)
self_instruct = SelfInstruct(
name="self-instruct",
application_description=APPLICATION_DESCRIPTION,
num_instructions=5,
input_batch_size=8,
llm=llm,
)
evol_instruction_complexity = EvolInstruct(
name="evol_instruction_complexity",
llm=llm,
num_evolutions=2,
store_evolutions=True,
input_batch_size=8,
include_original_instruction=True,
input_mappings={"instruction": "question"},
)
expand_instructions = ExpandColumns(
name="expand_columns", columns={"instructions": "question"}
)
cleaner = CleanNumberedList(name="clean_numbered_list")
expand_evolutions = ExpandColumns(
name="expand_columns_evolved",
columns={"evolved_instructions": "evolved_questions"},
)
domain_expert = DomainExpert(
name="domain_expert",
llm=llm,
input_batch_size=8,
input_mappings={"instruction": "evolved_questions"},
output_mappings={"generation": "domain_expert_answer"},
)
domain_expert._system_prompt = domain_expert_prompt
domain_expert._template = template
keep_columns = KeepColumns(
name="keep_columns",
columns=["model_name", "evolved_questions", "domain_expert_answer"],
)
to_argilla = TextGenerationToArgilla(
name="text_generation_to_argilla",
dataset_name=argilla_dataset_name,
dataset_workspace="admin",
api_url=argilla_api_url,
api_key=argilla_api_key,
input_mappings={
"instruction": "evolved_questions",
"generation": "domain_expert_answer",
},
)
load_data.connect(self_instruct)
self_instruct.connect(expand_instructions)
expand_instructions.connect(cleaner)
cleaner.connect(evol_instruction_complexity)
evol_instruction_complexity.connect(expand_evolutions)
expand_evolutions.connect(domain_expert)
domain_expert.connect(keep_columns)
keep_columns.connect(to_argilla)
return pipeline
def serialize_pipeline(
argilla_api_key: str,
argilla_api_url: str,
argilla_dataset_name: str,
topics: List[str],
perspectives: List[str],
domain_expert_prompt: str,
hub_token: str,
endpoint_base_url: str,
pipeline_config_path: str = "pipeline.yaml",
examples: List[dict] = [],
):
"""Serialize the pipeline to a yaml file."""
pipeline = define_pipeline(
argilla_api_key=argilla_api_key,
argilla_api_url=argilla_api_url,
argilla_dataset_name=argilla_dataset_name,
topics=topics,
perspectives=perspectives,
domain_expert_prompt=domain_expert_prompt,
hub_token=hub_token,
endpoint_base_url=endpoint_base_url,
examples=examples,
)
pipeline.save(path=pipeline_config_path, overwrite=True, format="yaml")
def create_pipelines_run_command(
hub_token: str,
argilla_api_key: str,
argilla_api_url: str,
pipeline_config_path: str = "pipeline.yaml",
argilla_dataset_name: str = "domain_specific_datasets",
):
"""Create the command to run the pipeline."""
command_to_run = [
sys.executable,
"-m",
"distilabel",
"pipeline",
"run",
"--config",
pipeline_config_path,
"--param",
f"text_generation_to_argilla.dataset_name={argilla_dataset_name}",
"--param",
f"text_generation_to_argilla.api_key={argilla_api_key}",
"--param",
f"text_generation_to_argilla.api_url={argilla_api_url}",
"--param",
f"self-instruct.llm.api_key={hub_token}",
"--param",
f"evol_instruction_complexity.llm.api_key={hub_token}",
"--param",
f"domain_expert.llm.api_key={hub_token}",
"--ignore-cache",
]
return command_to_run
def run_pipeline(
hub_token: str,
argilla_api_key: str,
argilla_api_url: str,
pipeline_config_path: str = "pipeline.yaml",
argilla_dataset_name: str = "domain_specific_datasets",
):
"""Run the pipeline and yield the output as a generator of logs."""
command_to_run = create_pipelines_run_command(
hub_token=hub_token,
pipeline_config_path=pipeline_config_path,
argilla_dataset_name=argilla_dataset_name,
argilla_api_key=argilla_api_key,
argilla_api_url=argilla_api_url,
)
# Run the script file
process = subprocess.Popen(
args=command_to_run,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env={"HF_TOKEN": hub_token},
)
while process.stdout and process.stdout.readable():
time.sleep(0.2)
line = process.stdout.readline()
if not line:
break
yield line.decode("utf-8")
|