soury's picture
push_data_pr (#2)
2e85345 verified
"""
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()