in src/setfit/model_card.py [0:0]
def to_dict(self) -> Dict[str, Any]:
super_dict = {field.name: getattr(self, field.name) for field in fields(self)}
# Compute required formats from the raw data
if self.eval_results_dict:
dataset_split = list(self.eval_results_dict.keys())[0].split("_")[0]
dataset_id = self.dataset_id or "unknown"
dataset_name = self.dataset_name or self.dataset_id or "Unknown"
eval_results = [
EvalResult(
task_type="text-classification",
dataset_type=dataset_id,
dataset_name=dataset_name,
dataset_split=dataset_split,
dataset_revision=self.dataset_revision,
metric_type=metric_key.split("_", maxsplit=1)[1],
metric_value=metric_value,
task_name="Text Classification",
metric_name=metric_key.split("_", maxsplit=1)[1].title(),
)
for metric_key, metric_value in self.eval_results_dict.items()
]
super_dict["metrics"] = [metric_key.split("_", maxsplit=1)[1] for metric_key in self.eval_results_dict]
super_dict["model-index"] = eval_results_to_model_index(self.model_name, eval_results)
eval_lines_list = [
{
key: f"**{self._maybe_round(value)}**" if line["Step"] == self.best_model_step else value
for key, value in line.items()
}
for line in self.eval_lines_list
]
super_dict["eval_lines"] = make_markdown_table(eval_lines_list)
super_dict["explain_bold_in_eval"] = "**" in super_dict["eval_lines"]
# Replace |:---:| with |:---| for left alignment
super_dict["label_examples"] = make_markdown_table(self.label_example_list).replace("-:|", "--|")
super_dict["train_set_metrics"] = make_markdown_table(self.train_set_metrics_list).replace("-:|", "--|")
super_dict["train_set_sentences_per_label_list"] = make_markdown_table(
self.train_set_sentences_per_label_list
).replace("-:|", "--|")
super_dict["metrics_table"] = make_markdown_table(self.metric_lines).replace("-:|", "--|")
if self.code_carbon_callback and self.code_carbon_callback.tracker:
emissions_data = self.code_carbon_callback.tracker._prepare_emissions_data()
super_dict["co2_eq_emissions"] = {
# * 1000 to convert kg to g
"emissions": float(emissions_data.emissions) * 1000,
"source": "codecarbon",
"training_type": "fine-tuning",
"on_cloud": emissions_data.on_cloud == "Y",
"cpu_model": emissions_data.cpu_model,
"ram_total_size": emissions_data.ram_total_size,
"hours_used": round(emissions_data.duration / 3600, 3),
}
if emissions_data.gpu_model:
super_dict["co2_eq_emissions"]["hardware_used"] = emissions_data.gpu_model
if self.dataset_id:
super_dict["datasets"] = [self.dataset_id]
if self.st_id:
super_dict["base_model"] = self.st_id
super_dict["model_max_length"] = self.model.model_body.get_max_seq_length()
if super_dict["num_classes"] is None:
if self.model.labels:
super_dict["num_classes"] = len(self.model.labels)
if super_dict["absa"]:
super_dict.update(super_dict.pop("absa"))
for key in IGNORED_FIELDS:
super_dict.pop(key, None)
return super_dict