HuggingFaceM4/IDEFICS_Data_Measurement_Tool
2
1import gradio as gr2 3from widgets.widget_base import Widget4from data_measurements.dataset_statistics import DatasetStatisticsCacheClass as dmt_cls5import utils6 7logs = utils.prepare_logging(__file__)8 9 10class LabelDistribution(Widget):11 def __init__(self):12 self.label_dist_plot = gr.Plot(render=False, visible=False)13 self.label_dist_no_label_text = gr.Markdown(14 value="No labels were found in the dataset", render=False, visible=False15 )16 self.label_dist_accordion = gr.Accordion(render=False, label="", open=False)17 18 def render(self):19 with gr.TabItem(label="Label Distribution"):20 gr.Markdown(21 "Use this widget to see how balanced the labels in your dataset are."22 )23 self.label_dist_plot.render()24 self.label_dist_no_label_text.render()25 26 def update(self, dstats: dmt_cls):27 logs.info(f"FIGS labels: {bool(dstats.fig_labels)}")28 if dstats.fig_labels:29 output = {30 self.label_dist_plot: gr.Plot.update(31 value=dstats.fig_labels, visible=True32 ),33 self.label_dist_no_label_text: gr.Markdown.update(visible=False),34 }35 else:36 output = {37 self.label_dist_plot: gr.Plot.update(visible=False),38 self.label_dist_no_label_text: gr.Markdown.update(visible=True),39 }40 return output41 42 @property43 def output_components(self):44 return [self.label_dist_plot, self.label_dist_no_label_text]45 46 def add_events(self, state: gr.State):47 pass48 