File size: 4,810 Bytes
2e85345 | 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 | """
Form parser configuration and utilities for handling Gradio form inputs.
This module provides a centralized way to manage form structure and parsing.
"""
from dataclasses import dataclass
from typing import List, Any, Tuple
@dataclass
class FormSection:
"""Represents a section of the form with its field count."""
name: str
field_count: int
fields: List[str] = None
@dataclass
class DynamicSection:
"""Represents a dynamic section with multiple rows and fields."""
name: str
fields: List[str]
max_rows: int = 5
@property
def total_components(self) -> int:
return len(self.fields) * self.max_rows
# Form structure configuration
FORM_STRUCTURE = [
FormSection("header", 11, [
"licensing", "formatVersion", "formatVersionSpecificationUri", "reportId",
"reportDatetime", "reportStatus", "publisher_name", "publisher_division",
"publisher_projectName", "publisher_confidentialityLevel", "publisher_publicKey"
]),
FormSection("task_simple", 3, [
"taskFamily", "taskStage", "nbRequest"
]),
DynamicSection("algorithms", [
"trainingType", "algorithmType", "algorithmName", "algorithmUri",
"foundationModelName", "foundationModelUri", "parametersNumber", "framework",
"frameworkVersion", "classPath", "layersNumber", "epochsNumber", "optimizer", "quantization"
]),
DynamicSection("dataset", [
"dataUsage", "dataType", "dataFormat", "dataSize", "dataQuantity",
"shape", "source", "sourceUri", "owner"
]),
FormSection("task_final", 3, [
"measuredAccuracy", "estimatedAccuracy", "taskDescription"
]),
DynamicSection("measures", [
"measurementMethod", "manufacturer", "version", "cpuTrackingMode", "gpuTrackingMode",
"averageUtilizationCpu", "averageUtilizationGpu", "powerCalibrationMeasurement",
"durationCalibrationMeasurement", "powerConsumption", "measurementDuration", "measurementDateTime"
]),
FormSection("system", 3, [
"osystem", "distribution", "distributionVersion"
]),
FormSection("software", 2, [
"language", "version_software"
]),
FormSection("infrastructure_simple", 4, [
"infraType", "cloudProvider", "cloudInstance", "cloudService"
]),
DynamicSection("infrastructure_components", [
"componentName", "componentType", "nbComponent", "memorySize",
"manufacturer_infra", "family", "series", "share"
]),
FormSection("environment", 7, [
"country", "latitude", "longitude", "location",
"powerSupplierType", "powerSource", "powerSourceCarbonIntensity"
]),
FormSection("quality", 1, ["quality"])
]
class FormParser:
"""Utility class for parsing form inputs based on the form structure."""
def __init__(self):
self.structure = FORM_STRUCTURE
def parse_inputs(self, inputs: Tuple[Any, ...]) -> dict:
"""
Parse form inputs into a structured dictionary.
Args:
inputs: Tuple of all form input values
Returns:
dict: Parsed form data organized by sections
"""
parsed_data = {}
idx = 0
for section in self.structure:
if isinstance(section, FormSection):
# Simple section - extract values directly
section_data = inputs[idx:idx + section.field_count]
if section.fields:
parsed_data[section.name] = dict(
zip(section.fields, section_data))
else:
parsed_data[section.name] = section_data
idx += section.field_count
elif isinstance(section, DynamicSection):
# Dynamic section - extract and reshape data
flat_data = inputs[idx:idx + section.total_components]
idx += section.total_components
# Reshape flat data into field-organized lists
section_data = {}
for field_idx, field_name in enumerate(section.fields):
start_pos = field_idx * section.max_rows
end_pos = start_pos + section.max_rows
section_data[field_name] = flat_data[start_pos:end_pos]
parsed_data[section.name] = section_data
return parsed_data
def get_total_input_count(self) -> int:
"""Get the total number of expected inputs."""
total = 0
for section in self.structure:
if isinstance(section, FormSection):
total += section.field_count
elif isinstance(section, DynamicSection):
total += section.total_components
return total
# Global parser instance
form_parser = FormParser()
|