|
import gradio as gr |
|
from src.services.util import ( |
|
REPORT_STATUS_OPTIONS, CONFIDENTIALITY_LEVELS, DATA_USAGE_OPTIONS, DATA_FORMAT, |
|
DATA_TYPES, DATA_SOURCE, |
|
ACCURACY_LEVELS, INFRA_TYPES, |
|
POWER_SUPPLIER_TYPES, POWER_SOURCES, QUALITY_LEVELS |
|
) |
|
|
|
|
|
def create_dynamic_section(section_name, fields_config, initial_count=1, layout="row"): |
|
""" |
|
Creates a dynamic section in a Gradio interface where users can add or remove rows of input fields. |
|
|
|
Args: |
|
section_name (str): The name of the section (e.g., "Algorithms", "Components"). |
|
fields_config (list): A list of dictionaries defining the configuration for each field in the section. |
|
Each dictionary should include: |
|
- "type": The Gradio component type (e.g., gr.Textbox, gr.Number). |
|
- "label": The label for the field. |
|
- "info": Additional information or tooltip for the field. |
|
- "value" (optional): The default value for the field. |
|
- "kwargs" (optional): Additional keyword arguments for the component. |
|
- "elem_classes" (optional): CSS classes for styling the field. |
|
initial_count (int): The initial number of rows to render in the section. |
|
layout (str): The layout of the fields in each row ("row" or "column"). |
|
|
|
Returns: |
|
tuple: A tuple containing: |
|
- count_state: A Gradio state object tracking the number of rows. |
|
- field_states: A list of Gradio state objects, one for each field, to store the values of the fields. |
|
- add_btn: The "Add" button component for adding new rows. |
|
""" |
|
|
|
|
|
count_state = gr.State(value=initial_count+1) |
|
|
|
field_states = [gr.State([]) for _ in fields_config] |
|
|
|
all_components = [] |
|
|
|
def update_fields(*states_and_values): |
|
""" |
|
Updates the state of the fields when a value changes. |
|
|
|
Args: |
|
*states_and_values: A combination of the current states and the new values for the fields. |
|
|
|
Returns: |
|
tuple: Updated states for all fields. |
|
""" |
|
|
|
|
|
states = list(states_and_values[:len(fields_config)]) |
|
|
|
current_values = states_and_values[len(fields_config):-1] |
|
index = states_and_values[-1] |
|
|
|
|
|
for field_idx, (state, value) in enumerate(zip(states, current_values)): |
|
|
|
while len(state) <= index: |
|
state.append("") |
|
|
|
state[index] = value if value is not None else "" |
|
|
|
return tuple(states) |
|
|
|
@gr.render(inputs=count_state) |
|
def render_dynamic_section(count): |
|
""" |
|
Renders the dynamic section with the current number of rows and their states. |
|
|
|
Args: |
|
count (int): The number of rows to render. |
|
|
|
Returns: |
|
list: A list of dynamically generated components for the section. |
|
""" |
|
nonlocal all_components |
|
all_components = [] |
|
|
|
for i in range(count): |
|
|
|
with (gr.Row() if layout == "row" else gr.Column()): |
|
row_components = [] |
|
field_refs = [] |
|
|
|
for field_idx, config in enumerate(fields_config): |
|
|
|
component = config["type"]( |
|
label=f"{config['label']} ({section_name}{i + 1})", |
|
info=config.get("info", ""), |
|
value=config.get("value", ""), |
|
**config.get("kwargs", {}), |
|
elem_classes=config.get("elem_classes", "") |
|
) |
|
row_components.append(component) |
|
field_refs.append(component) |
|
|
|
|
|
component.change( |
|
fn=update_fields, |
|
inputs=[*field_states, *field_refs, gr.State(i)], |
|
outputs=field_states |
|
) |
|
|
|
|
|
remove_btn = gr.Button("❌", variant="secondary") |
|
remove_btn.click( |
|
lambda x, idx=i, fs=field_states: ( |
|
max(0, x - 1), |
|
|
|
*[fs[i].value[:idx] + fs[i].value[idx + 1:] for i in range(len(fs))] |
|
), |
|
inputs=count_state, |
|
outputs=[count_state, *field_states] |
|
) |
|
row_components.append(remove_btn) |
|
|
|
|
|
all_components.extend(row_components) |
|
return all_components |
|
|
|
|
|
render_dynamic_section(count=initial_count) |
|
|
|
|
|
add_btn = gr.Button(f"Add {section_name}") |
|
add_btn.click(lambda x: x + 1, count_state, count_state) |
|
|
|
return (count_state, *field_states, add_btn) |
|
|
|
|
|
def create_header_tab(): |
|
"""Create the header tab components.""" |
|
with gr.Tab("Header"): |
|
licensing = gr.Textbox( |
|
label="Licensing", info="(the type of licensing applicable for the sharing of the report)") |
|
formatVersion = gr.Textbox( |
|
label="Format Version", info="(the version of the specification of this set of schemas defining the report's fields)") |
|
formatVersionSpecificationUri = gr.Textbox( |
|
label="Format Version Specification URI", info="(the URI of the present specification of this set of schemas)") |
|
reportId = gr.Textbox( |
|
label="Report ID", info="(the unique identifier of this report, preferably as a uuid4 string)") |
|
reportDatetime = gr.Textbox( |
|
label="Report Datetime", info="Required field<br>(the publishing date of this report in format YYYY-MM-DD HH:MM:SS)", elem_classes="mandatory_field") |
|
reportStatus = gr.Dropdown(value=None, |
|
label="Report Status", |
|
choices=REPORT_STATUS_OPTIONS, |
|
info="(the status of this report)" |
|
) |
|
|
|
with gr.Accordion("Publisher"): |
|
publisher_name = gr.Textbox( |
|
label="Name", info="(name of the organization)") |
|
publisher_division = gr.Textbox( |
|
label="Division", info="(name of the publishing department within the organization)") |
|
publisher_projectName = gr.Textbox( |
|
label="Project Name", info="(name of the publishing project within the organization)") |
|
publisher_confidentialityLevel = gr.Dropdown(value=None, |
|
label="Confidentiality Level", |
|
choices=CONFIDENTIALITY_LEVELS, |
|
info="Required field<br>(the confidentiality of the report)", |
|
elem_classes="mandatory_field" |
|
) |
|
publisher_publicKey = gr.Textbox( |
|
label="Public Key", info="(the cryptographic public key to check the identity of the publishing organization)") |
|
|
|
return [ |
|
licensing, formatVersion, formatVersionSpecificationUri, reportId, |
|
reportDatetime, reportStatus, publisher_name, publisher_division, |
|
publisher_projectName, publisher_confidentialityLevel, publisher_publicKey |
|
] |
|
|
|
|
|
def create_task_tab(): |
|
"""Create the task tab components.""" |
|
with gr.Tab("Task", elem_id="mandatory_part"): |
|
taskStage = gr.Textbox( |
|
label="Task Stage", info="Required field<br>(stage of the task, example: datacreation, preprocessing, training, finetuning, inference, retraining..., add a + between stages if several but we do recommand to measure each step independantly)", elem_classes="mandatory_field") |
|
taskFamily = gr.Textbox( |
|
label="Task Family", info="Required field<br>(the family of task you are running, e.g. text classification, image generation, speech recognition, robotics navigation...)", elem_classes="mandatory_field") |
|
nbRequest = gr.Number( |
|
label="Number of Requests", info="(if inference stage, the number of requests the measure corresponds to, 0 or empty if you're not measuring the inference stage)", |
|
value=lambda: None, minimum=0) |
|
|
|
with gr.Accordion("Algorithms", elem_id="mandatory_part"): |
|
_, trainingType, algorithmType, algorithmName, algorithmUri, foundationModelName, foundationModelUri, parametersNumber, framework, frameworkVersion, classPath, layersNumber, epochsNumber, optimizer, quantization, add_algorithm_btn = create_dynamic_section( |
|
section_name="algorithm", |
|
fields_config=[ |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Type of training", |
|
"info": "(if applicable, type of training (if the stage corresponds to a training) : supervisedLearning, unsupervisedLearning, semiSupervisedLearning, reinforcementLearning, transferLearning ...)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Type of algorithm", |
|
"info": "(the type of algorithm used, example : embeddings creation, rag, nlp, neural network, llm...)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Algorithm Name", |
|
"info": "(the case-sensitive common name of the algorithm, example: randomForest, naive bayes, cnn, rnn, transformers, if you are directly using a foundation model, let it empty and fill the field foundationModelName...)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Algorithm Uri", |
|
"info": "(the URI of the model, if publicly available)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Foundation Model Name", |
|
"info": "(if a foundation model is used, its case-sensitive common name, example: llama3.1-8b, gpt4-o...)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Foundation Model Uri", |
|
"info": "(the URI of the foundation model, if publicly available)", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Number of parameters", |
|
"info": "(if applicable, number of billions of total parameters of your model, e.g. 8 for llama3.1-8b)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Framework", |
|
"info": "(the common name of the software framework implementing the algorithm, if any)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "frameworkVersion", |
|
"info": "(the version of the software framework implementing the algorithm, if any)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "classPath", |
|
"info": "(the full class path of the algorithm within the framework, with elements separated by dots)", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Number of layers in the network", |
|
"info": "(if deep learning, precise the number of layers in your network)", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Number of epochs", |
|
"info": "(if training, the number of complete passes through the training dataset)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "optimizer", |
|
"info": "(the algorithm used to optimize the models weights, e.g. gridSearch, lora, adam)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "quantization", |
|
"info": "(the type of quantization used : fp32, fp16, b16, int8 ...)", |
|
} |
|
], |
|
initial_count=0, |
|
layout="column" |
|
) |
|
|
|
with gr.Accordion("Dataset", elem_id="mandatory_part"): |
|
_, dataUsage, dataType, dataFormat, dataSize, dataQuantity, shape, source, sourceUri, owner, add_dataset_btn = create_dynamic_section( |
|
section_name="dataset", |
|
fields_config=[ |
|
{ |
|
"type": gr.Dropdown, |
|
"label": "Data Usage", |
|
"info": "Required field<br>(the use of the dataset: is it used as model input or output ?)", |
|
"value": None, |
|
"kwargs": {"choices": DATA_USAGE_OPTIONS}, |
|
"elem_classes": "mandatory_field", |
|
}, |
|
{ |
|
"type": gr.Dropdown, |
|
"label": "Data Type", |
|
"info": "Required field<br>(the nature of the data used)", |
|
"value": None, |
|
"kwargs": {"choices": DATA_TYPES}, |
|
"elem_classes": "mandatory_field", |
|
}, |
|
{ |
|
"type": gr.Dropdown, |
|
"label": "Data Format", |
|
"info": "(if the data is passed in the form of a file, what format is the data in?)", |
|
"value": None, |
|
"kwargs": {"choices": DATA_FORMAT} |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Data Size", |
|
"info": "(the size of the dataset (in Go), if small quantity just fill the field quantity)", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Data Quantity", |
|
"info": "(the number of data in the dataset, e.g. 3 (images, audio or tokens))", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Data shape", |
|
"info": "(the shape of your dataset, can be found with X.shape with dataframes, e.g. (12, 1000) for a 2D table with 12 columns and 1000 rows)", |
|
}, |
|
{ |
|
"type": gr.Dropdown, |
|
"label": "Data source", |
|
"info": "(the kind of source of the dataset)", |
|
"value": None, |
|
"kwargs": {"choices": DATA_SOURCE} |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Source Uri", |
|
"info": "(the URI of the dataset if available)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Owner", |
|
"info": "(the owner of the dataset if available)", |
|
} |
|
], |
|
initial_count=0, |
|
layout="column" |
|
) |
|
|
|
with gr.Row(): |
|
measuredAccuracy = gr.Number(value=lambda: None, |
|
label="Measured Accuracy", info="(the measured accuracy of your model (between 0 and 1))") |
|
estimatedAccuracy = gr.Dropdown(value=None, |
|
label="Estimated Accuracy", |
|
choices=ACCURACY_LEVELS, |
|
info="(estimated accuracy assessment)" |
|
) |
|
with gr.Row(): |
|
taskDescription = gr.Textbox( |
|
label="Task description", info="(free field, to be fillied in if you have more details to share about your task)") |
|
|
|
return [ |
|
taskFamily, taskStage, nbRequest, |
|
trainingType, algorithmType, algorithmName, algorithmUri, foundationModelName, foundationModelUri, parametersNumber, framework, frameworkVersion, classPath, layersNumber, epochsNumber, optimizer, quantization, |
|
dataUsage, dataType, dataFormat, dataSize, dataQuantity, shape, source, sourceUri, owner, |
|
measuredAccuracy, estimatedAccuracy, taskDescription |
|
] |
|
|
|
|
|
def create_measures_tab(): |
|
"""Create the measures tab components.""" |
|
with gr.Tab("Measures", elem_id="mandatory_part"): |
|
with gr.Accordion("Measures"): |
|
_, measurementMethod, manufacturer, version, cpuTrackingMode, gpuTrackingMode, averageUtilizationCpu, averageUtilizationGpu, powerCalibrationMeasurement, durationCalibrationMeasurement, powerConsumption, measurementDuration, measurementDateTime, add_measurement_btn = create_dynamic_section( |
|
section_name="measure", |
|
fields_config=[ |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Method of measurement", |
|
"info": "Required field<br>(the method used to perform the energy measure, example: codecarbon, carbonai, flops-compute, wattmeter...)", |
|
"elem_classes": "mandatory_field", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Manufacturer", |
|
"info": "(the builder of the measuring tool, if the measurement method is wattmeter)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Version of the measurement tool", |
|
"info": "(the version of the measuring tool, if any)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "CPU tracking mode", |
|
"info": "(the method used to track the consumption of the CPU, example: constant, rapl...)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "GPU tracking mode", |
|
"info": "(the method used to track the consumption of the GPU, example: constant, nvml...)", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Average CPU Utilization", |
|
"info": "(the average percentage of use of the CPU during the task, for example: 0.5 if your CPU load was 50% on average)", |
|
"minimum": 0, |
|
"maximum": 1 |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Average GPU Utilization", |
|
"info": "(the average percentage of use of the GPU during the task, for example: 0.8 if your GPU load was 80% on average)", |
|
"minimum": 0, |
|
"maximum": 1 |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Power calibration measurement", |
|
"info": "(the power consumed (in kWh) during the calibration measure if any (to isolate the initial consumption of the hardware))", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Duration calibration measurement", |
|
"info": "(the duration of the calibration if any (in seconds))", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Power consumption", |
|
"info": "Required field<br>(the power consumption measure of the computing task (in kWh))", |
|
"elem_classes": "mandatory_field", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Measurement Duration", |
|
"info": "(the duration of the measurement (in seconds))", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Measurement date time", |
|
"info": "(the date when the measurement began, in format YYYY-MM-DD HH:MM:SS)", |
|
} |
|
], |
|
initial_count=0, |
|
layout="column" |
|
) |
|
|
|
return [ |
|
measurementMethod, manufacturer, version, cpuTrackingMode, gpuTrackingMode, |
|
averageUtilizationCpu, averageUtilizationGpu, powerCalibrationMeasurement, |
|
durationCalibrationMeasurement, powerConsumption, |
|
measurementDuration, measurementDateTime |
|
] |
|
|
|
|
|
def create_system_tab(): |
|
"""Create the system tab components.""" |
|
with gr.Tab("System"): |
|
os = gr.Textbox( |
|
label="OS", info="Required field<br>(name of the operating system)", elem_classes="mandatory_field") |
|
distribution = gr.Textbox( |
|
label="Distribution", info="(distribution of the operating system)") |
|
distributionVersion = gr.Textbox( |
|
label="Distribution Version", info="(distribution version)") |
|
|
|
return [os, distribution, distributionVersion] |
|
|
|
|
|
def create_software_tab(): |
|
"""Create the software tab components.""" |
|
with gr.Tab("Software"): |
|
language = gr.Textbox( |
|
label="Language", info="Required field<br>(name of the programming language used, example : c, java, julia, python...)", elem_classes="mandatory_field") |
|
version_software = gr.Textbox( |
|
label="Version", info="(version of the programming language used)") |
|
|
|
return [language, version_software] |
|
|
|
|
|
def create_infrastructure_tab(): |
|
"""Create the infrastructure tab components.""" |
|
with gr.Tab("Infrastructure", elem_id="mandatory_part"): |
|
infraType = gr.Dropdown(value=None, |
|
label="Infrastructure Type", |
|
choices=INFRA_TYPES, |
|
info="Required field<br>(the type of infrastructure used)", |
|
elem_classes="mandatory_field" |
|
) |
|
cloudProvider = gr.Textbox( |
|
label="Cloud Provider", info="(If you are on the cloud, the name of your cloud provider, for example : aws, azure, google, ovh...)") |
|
cloudInstance = gr.Textbox( |
|
label="Cloud Instance", info="(If you are on a cloud vm, the name of your cloud instance, for example : a1.large, dasv4-type2...)") |
|
cloudService = gr.Textbox( |
|
label="Cloud Service", info="(If you are using an AI cloud service, the name of your cloud service, for example : openAI service...)") |
|
with gr.Accordion("Components", elem_id="mandatory_part"): |
|
_, componentName, componentType, nbComponent, memorySize, manufacturer_infra, family, series, share, add_component_btn = create_dynamic_section( |
|
section_name="component", |
|
fields_config=[ |
|
|
|
{ |
|
"type": gr.Textbox, |
|
"label": "Component Name", |
|
"info": "(the name of this subsystem part of your infrastructure, example returned by codecarbon: 1 x NVIDIA GeForce GTX 1080 Ti)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Component Type", |
|
"info": "Required field<br>(the type of this subsystem part of your infrastructure, example: cpu, gpu, ram, hdd, sdd...)", |
|
"elem_classes": "mandatory_field", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Number of Components", |
|
"info": "Required field<br>(the number of items of this component in your infrastructure, if you have 1 RAM of 32Go, fill 1 here and 32 inside memorySize)", |
|
"elem_classes": "mandatory_field", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Memory Size", |
|
"info": "(the size of the memory of the component in Gbytes, useful to detail the memory associated to ONE of your gpus for example (if we want the total memory, we will multiply the memorySize by nbComponent). If the component is CPU do not fill the RAM size here, create another component for RAM, this field is for the embeded memory of a component.)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Manufacturer", |
|
"info": "(the name of the manufacturer, example: nvidia)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Family", |
|
"info": "(the family of this component, example: geforce)", |
|
}, |
|
{ |
|
"type": gr.Textbox, |
|
"label": "Series", |
|
"info": "(the series of this component, example: gtx1080)", |
|
}, |
|
{ |
|
"type": gr.Number, |
|
"label": "Share", |
|
"info": "(the percentage of the physical equipment used by the task, this sharing property should be set to 1 by default (if no share) and otherwise to the correct percentage, e.g. 0.5 if you share half-time.)", |
|
} |
|
], |
|
initial_count=0, |
|
layout="column" |
|
) |
|
|
|
return [ |
|
infraType, cloudProvider, cloudInstance, cloudService, componentName, componentType, |
|
nbComponent, memorySize, manufacturer_infra, family, |
|
series, share |
|
] |
|
|
|
|
|
def create_environment_tab(): |
|
"""Create the environment tab components.""" |
|
with gr.Tab("Environment"): |
|
country = gr.Textbox( |
|
label="Country", info="Required field", elem_classes="mandatory_field") |
|
latitude = gr.Number(label="Latitude", value=lambda: None) |
|
longitude = gr.Number(label="Longitude", value=lambda: None) |
|
location = gr.Textbox( |
|
label="Location", info="(more precise location like city, region or datacenter name)") |
|
powerSupplierType = gr.Dropdown(value=lambda: None, |
|
label="Power Supplier Type", |
|
choices=POWER_SUPPLIER_TYPES, |
|
info="(the type of power supplier)" |
|
) |
|
powerSource = gr.Dropdown(value=None, |
|
label="Power Source", |
|
choices=POWER_SOURCES, |
|
info="(the source of power)" |
|
) |
|
powerSourceCarbonIntensity = gr.Number(value=lambda: None, |
|
label="Power Source Carbon Intensity") |
|
|
|
return [ |
|
country, latitude, longitude, location, |
|
powerSupplierType, powerSource, powerSourceCarbonIntensity |
|
] |
|
|
|
|
|
def create_quality_tab(): |
|
"""Create the quality tab components.""" |
|
with gr.Tab("Quality"): |
|
quality = gr.Dropdown(value=None, |
|
label="Quality", |
|
choices=QUALITY_LEVELS, |
|
info="(the quality of the information you provided, 3 possibilities : high (percentage error +/-10%), medium (percentage error +/-25%), low (percentage error +/-50%))" |
|
) |
|
|
|
return [quality] |
|
|