File size: 10,308 Bytes
099e99c 14f85b1 099e99c c2fbbc3 099e99c 14f85b1 099e99c 14f85b1 099e99c 14f85b1 099e99c ab58a29 099e99c ab58a29 099e99c 14f85b1 099e99c |
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 |
import json
import gradio as gr
import pandas as pd
from datasets import load_dataset
from gradio_huggingfacehub_search import HuggingfaceHubSearch
from src.distilabel_dataset_generator.utils import get_org_dropdown
def get_iframe(hub_repo_id) -> str:
if not hub_repo_id:
raise gr.Error("Hub repo id is required")
url = f"https://huggingface.co/datasets/{hub_repo_id}/embed/viewer"
iframe = f"""
<iframe
src="{url}"
frameborder="0"
width="100%"
height="600px"
></iframe>
"""
return iframe
def get_valid_columns(df: pd.DataFrame):
valid_columns = []
for col in df.columns:
sample_val = df[col].iloc[0]
if isinstance(sample_val, str) or (
isinstance(sample_val, list)
and all(isinstance(item, dict) for item in sample_val)
):
valid_columns.append(col)
return valid_columns
def load_dataset_from_hub(hub_repo_id: str, n_rows: int = 10):
gr.Info(message="Loading dataset ...")
if not hub_repo_id:
raise gr.Error("Hub repo id is required")
ds_dict = load_dataset(hub_repo_id)
splits = list(ds_dict.keys())
ds = ds_dict[splits[0]]
if n_rows:
ds = ds.select(range(n_rows))
df = ds.to_pandas()
# Get columns that contain either strings or lists of dictionaries
valid_columns = get_valid_columns(df)
return (
df,
gr.Dropdown(choices=valid_columns, label="Instruction Column"),
gr.Dropdown(choices=valid_columns, label="Instruction Column"),
gr.Dropdown(choices=valid_columns, label="Response Column"),
)
def define_evaluation_aspects(task_type: str):
if task_type == "instruction":
return gr.Dropdown(
value=["overall-rating"],
choices=["complexity", "quality"],
label="Evaluation Aspects",
multiselect=True,
interactive=True,
)
elif task_type == "instruction-response":
return gr.Dropdown(
value=["overall-rating"],
choices=["helpfulness", "truthfulness", "overall-rating", "honesty"],
label="Evaluation Aspects",
multiselect=True,
interactive=True,
)
else:
return gr.Dropdown(interactive=False, visible=False)
def evaluate_instruction(df: pd.DataFrame, aspects: list[str], instruction_column: str):
pass
def evaluate_instruction_response(
df: pd.DataFrame, aspects: list[str], instruction_column: str, response_column: str
):
pass
def evaluate_custom(
df: pd.DataFrame, aspects: list[str], prompt_template: str, structured_output: dict
):
pass
def _apply_to_dataset(
df: pd.DataFrame,
eval_type: str,
aspects_instruction: list[str],
instruction_column: str,
aspects_instruction_response: list[str],
instruction_column_response: str,
response_column_response: str,
aspects_custom: list[str],
prompt_template: str,
structured_output: dict,
):
if eval_type == "instruction":
df = evaluate_instruction(df, aspects_instruction, instruction_column)
elif eval_type == "instruction-response":
df = evaluate_instruction_response(
df,
aspects_instruction_response,
instruction_column_response,
response_column_response,
)
elif eval_type == "custom":
df = evaluate_custom(df, aspects_custom, prompt_template, structured_output)
return df
def apply_to_sample_dataset(
repo_id: str,
eval_type: str,
aspects_instruction: list[str],
aspects_instruction_response: list[str],
aspects_custom: list[str],
instruction_instruction: str,
instruction_instruction_response: str,
response_instruction_response: str,
prompt_template: str,
structured_output: dict,
):
df, _, _, _ = load_dataset_from_hub(repo_id, n_rows=10)
df = _apply_to_dataset(
df,
eval_type,
aspects_instruction,
instruction_instruction,
aspects_instruction_response,
instruction_instruction_response,
response_instruction_response,
aspects_custom,
prompt_template,
structured_output,
)
return df
def push_to_hub(
org_name: str,
repo_name: str,
private: bool,
n_rows: int,
original_repo_id: str,
eval_type: str,
aspects_instruction: list[str],
aspects_instruction_response: list[str],
aspects_custom: list[str],
instruction_instruction: str,
instruction_instruction_response: str,
response_instruction_response: str,
prompt_template: str,
structured_output: dict,
):
df, _, _, _ = load_dataset_from_hub(original_repo_id, n_rows=n_rows)
df = _apply_to_dataset(
df,
eval_type,
aspects_instruction,
instruction_instruction,
aspects_instruction_response,
instruction_instruction_response,
response_instruction_response,
aspects_custom,
prompt_template,
structured_output,
)
new_repo_id = f"{org_name}/{repo_name}"
######################
# Gradio UI
######################
with gr.Blocks() as app:
gr.Markdown("## 1. Select your input dataset")
with gr.Row():
with gr.Column(scale=1):
search_in = HuggingfaceHubSearch(
label="Search",
placeholder="Search for a Dataset",
search_type="dataset",
sumbit_on_select=True,
)
load_btn = gr.Button("Load dataset")
with gr.Column(scale=3):
search_out = gr.HTML(label="Dataset Preview")
gr.HTML("<hr>")
gr.Markdown("## 2. Configure your task")
with gr.Row():
with gr.Column(scale=1):
eval_type = gr.Dropdown(
label="Evaluation Type",
choices=["instruction", "instruction-response", "custom-template"],
visible=False,
)
with gr.Tab("instruction") as tab_instruction:
aspects_instruction = define_evaluation_aspects("instruction")
instruction_instruction = gr.Dropdown(
label="Instruction Column", interactive=True
)
tab_instruction.select(
lambda: "instruction",
inputs=[],
outputs=[eval_type],
)
with gr.Tab("instruction-response") as tab_instruction_response:
aspects_instruction_response = define_evaluation_aspects(
"instruction-response"
)
instruction_instruction_response = gr.Dropdown(
label="Instruction Column", interactive=True
)
response_instruction_response = gr.Dropdown(
label="Response Column", interactive=True
)
tab_instruction_response.select(
lambda: "instruction-response",
inputs=[],
outputs=[eval_type],
)
with gr.Tab("custom") as tab_custom:
aspects_custom = define_evaluation_aspects("custom")
prompt_template = gr.Code(
label="Prompt Template",
value="{{column_1}} based on {{column_2}}",
language="markdown",
interactive=True,
)
structured_output = gr.Code(
label="Structured Output",
value=json.dumps({"eval_aspect": "str"}),
language="json",
interactive=True,
)
tab_custom.select(
lambda: "custom-template",
inputs=[],
outputs=[eval_type],
)
btn_apply_to_sample_dataset = gr.Button("Refresh dataset")
with gr.Column(scale=3):
dataframe = gr.Dataframe()
gr.HTML("<hr>")
gr.Markdown("## 3. Generate your dataset")
with gr.Row():
with gr.Column(scale=1):
org_name = get_org_dropdown()
repo_name = gr.Textbox(
label="Repo name",
placeholder="dataset_name",
value="my-distiset",
interactive=True,
)
n_rows = gr.Number(
label="Number of rows",
value=10,
interactive=True,
scale=1,
)
private = gr.Checkbox(
label="Private dataset",
value=False,
interactive=True,
scale=1,
)
btn_push_to_hub = gr.Button("Push to Hub", variant="primary", scale=2)
with gr.Column(scale=3):
success_message = gr.Markdown(visible=False)
search_in.submit(get_iframe, inputs=search_in, outputs=search_out)
load_btn.click(
load_dataset_from_hub,
inputs=[search_in],
outputs=[
dataframe,
instruction_instruction,
instruction_instruction_response,
response_instruction_response,
],
)
btn_apply_to_sample_dataset.click(
apply_to_sample_dataset,
inputs=[
search_in,
eval_type,
aspects_instruction,
aspects_instruction_response,
aspects_custom,
instruction_instruction,
instruction_instruction_response,
response_instruction_response,
prompt_template,
structured_output,
],
outputs=dataframe,
)
btn_push_to_hub.click(
push_to_hub,
inputs=[
org_name,
repo_name,
private,
n_rows,
search_in,
eval_type,
aspects_instruction,
aspects_instruction_response,
aspects_custom,
instruction_instruction,
instruction_instruction_response,
response_instruction_response,
prompt_template,
structured_output,
],
outputs=success_message,
)
app.load(fn=get_org_dropdown, outputs=[org_name])
|