Spaces:
Running
Running
File size: 8,440 Bytes
2018b94 |
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 |
import gradio as gr
from datasets import disable_caching, load_dataset
from transformer_ranker import TransformerRanker
from demo.config import SAMPLE_SIZE, MAX_SAMPLE_SIZE, ALL_LMS, PRESELECTED_LMS, GRADIO_THEME
from demo.utils import (
BANNER, FOOTER, CSS, UNSET,
EmbeddingProgressTracker, compute_ratio,
validate_dataset, preprocess_dataset, ensure_dataset_is_loaded
)
disable_caching()
with gr.Blocks(css=CSS, theme=None) as demo:
gr.Markdown(BANNER)
##### 1. Load from datasets #####
gr.Markdown("## Load Downstream Dataset")
gr.Markdown(
"Select a dataset from the Hugging Face Hub such as `trec`. "
"This defines your downstream task."
)
with gr.Group():
dataset = gr.State(None)
dataset_id = gr.Textbox(
label="Dataset name",
placeholder="try: trec, conll2003, ag_news",
max_lines=1,
)
load_dataset_button = gr.Button(value="Load data", variant="primary", interactive=True,)
# enable loading if dataset exists on hub
dataset_id.change(validate_dataset, inputs=dataset_id, outputs=load_dataset_button)
gr.Markdown(
"Settings auto-configured. "
"Adjust the downsampling ratio in Dataset Setup, "
"or use the complete dataset with the [framework](https://github.com/flairNLP/transformer-ranker)."
)
##### data preprocessing #####
with gr.Accordion("Dataset Setup", open=False) as dataset_config:
with gr.Row() as dataset_details:
dataset_id_label = gr.Label("", label="Dataset")
num_samples = gr.State(0)
num_samples_label = gr.Label("", label="Dataset size")
num_samples.change(
lambda x: str(x), inputs=[num_samples], outputs=[num_samples_label]
)
with gr.Row():
text_column = gr.Dropdown("", label="Text Column")
text_pair_column = gr.Dropdown("", label="Text Pair")
with gr.Row():
label_column = gr.Dropdown("", label="Labels")
task_category = gr.Dropdown("", label="Downstream Task")
with gr.Group():
downsample_ratio = gr.State(0.0)
sampling_rate = gr.Slider(
20, MAX_SAMPLE_SIZE, label="Sampling rate", value=SAMPLE_SIZE, step=1
)
downsample_ratio_label = gr.Label("", label="Sampling rate")
downsample_ratio.change(
lambda x: f"{x:.1%}",
inputs=[downsample_ratio],
outputs=[downsample_ratio_label],
)
sampling_rate.change(
compute_ratio,
inputs=[sampling_rate, num_samples],
outputs=downsample_ratio,
)
num_samples.change(
compute_ratio,
inputs=[sampling_rate, num_samples],
outputs=downsample_ratio,
)
# load and show details
def load_hf_dataset(dataset_id):
try:
dataset = load_dataset(dataset_id, trust_remote_code=True)
dataset_details = preprocess_dataset(dataset)
except ValueError as e:
gr.Warning("Collections not supported. Load one dataset only.")
return (
gr.update(value="Loaded"),
dataset_id,
dataset,
*dataset_details
)
load_dataset_button.click(
load_hf_dataset,
inputs=[dataset_id],
outputs=[
load_dataset_button,
dataset_id_label,
dataset,
task_category,
text_column,
text_pair_column,
label_column,
num_samples,
],
scroll_to_output=True,
)
########## 2. Select LMs ##########
gr.Markdown("## Select Language Models")
gr.Markdown(
"Add two or more pretrained models for ranking. "
"Go with small models since this demo runs on CPU."
)
with gr.Group():
model_options = [
(model_handle.split("/")[-1], model_handle)
for model_handle in ALL_LMS
]
models = gr.CheckboxGroup(
choices=model_options, label="Model List", value=PRESELECTED_LMS
)
########## 3. Run ranking ##########
gr.Markdown("## Rank Language Models")
gr.Markdown(
"Rank models by transferability to your downstream task. "
"Adjust the metric and layer aggregation in Advanced Settings."
)
with gr.Group():
submit_button = gr.Button("Run ranking", variant="primary", interactive=False)
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
estimator = gr.Dropdown(
choices=["hscore", "logme", "knn"],
label="Transferability metric",
value="hscore",
)
layer_aggregator = gr.Dropdown(
choices=["lastlayer", "layermean", "bestlayer"],
label="Layer aggregation",
value="layermean",
)
# ranking button works after dataset loads
dataset.change(
ensure_dataset_is_loaded,
inputs=[dataset, text_column, label_column, task_category],
outputs=submit_button
)
label_column.change(
ensure_dataset_is_loaded,
inputs=[dataset, text_column, label_column, task_category],
outputs=submit_button
)
text_column.change(
ensure_dataset_is_loaded,
inputs=[dataset, text_column, label_column, task_category],
outputs=submit_button
)
def rank_models(
dataset,
downsample_ratio,
selected_models,
layer_aggregator,
estimator,
text_column,
text_pair_column,
label_column,
task_category,
progress=gr.Progress(),
):
if text_column == UNSET:
raise gr.Error("Text column is not set.")
if label_column == UNSET:
raise gr.Error("Label column is not set.")
if task_category == UNSET:
raise gr.Error(
"Task category not set. Dataset must support classification or regression."
)
if text_pair_column == UNSET:
text_pair_column = None
progress(0.0, "Starting")
with EmbeddingProgressTracker(progress=progress, model_names=selected_models) as tracker:
try:
ranker = TransformerRanker(
dataset,
dataset_downsample=downsample_ratio,
text_column=text_column,
text_pair_column=text_pair_column,
label_column=label_column,
task_category=task_category,
)
results = ranker.run(
models=selected_models,
layer_aggregator=layer_aggregator,
estimator=estimator,
batch_size=64,
tracker=tracker,
)
sorted_results = sorted(
results._results.items(), key=lambda item: item[1], reverse=True
)
return [
(i + 1, model, score) for i, (model, score) in enumerate(sorted_results)
]
except Exception as e:
print(e)
gr.Warning(f"Ranking issue: {e}")
return []
gr.Markdown("Ranking table → higher scores indicate better downstream performance.")
ranking_results = gr.Dataframe(
headers=["Rank", "Model", "Score"],
datatype=["number", "str", "number"],
value=[["-", "-", "-"]]
)
submit_button.click(
rank_models,
inputs=[
dataset,
downsample_ratio,
models,
layer_aggregator,
estimator,
text_column,
text_pair_column,
label_column,
task_category,
],
outputs=ranking_results,
scroll_to_output=True,
)
gr.Markdown(FOOTER)
if __name__ == "__main__":
# run up to 3 requests at once
demo.queue(default_concurrency_limit=3)
# run with 6 workers
demo.launch(max_threads=6)
|