Spaces:
Runtime error
Runtime error
Update model.py
Browse files2 unmodified lines34567892 unmodified lines12131415161724 unmodified lines4243444546474849502 unmodified lines5354555657585960612 unmodified lines646566676869707172737475767778792 unmodified linesimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom typing import List, Dict, Anyfrom collections import defaultdictfrom accelerate import Accelerator2 unmodified linessuper(DynamicModel, self).__init__()self.sections = nn.ModuleDict()for section_name, layers in sections.items():self.sections[section_name] = nn.ModuleList()for layer_params in layers:24 unmodified lineslayers = []for prov in root.findall('.//prov'):layer_params = {'input_size': 128, # Example: fixed input size'output_size': 256, # Example: fixed output size'activation': 'relu' # Default activation}layers.append(layer_params)2 unmodified linesdef create_model_from_folder(folder_path: str) -> DynamicModel:sections = defaultdict(list)for root, dirs, files in os.walk(folder_path):for file in files:if file.endswith('.xml'):file_path = os.path.join(root, file)try:layers = parse_xml_file(file_path)2 unmodified linesexcept Exception as e:print(f"Error processing {file_path}: {str(e)}")return DynamicModel(sections)def main():folder_path = 'app/data'model = create_model_from_folder(folder_path)print(f"Created dynamic PyTorch model with sections: {list(model.sections.keys())}")# Get first section's first layer's input size dynamicallyfirst_section = list(model.sections.keys())[0]first_layer = model.sections[first_section][0]input_features = first_layer[0].in_features2 unmodified lines34567892 unmodified lines1213141516171819202122232425262724 unmodified lines5253545556575859602 unmodified lines6364656667686970717273747576772 unmodified lines80818283848586878889909192939495969798992 unmodified linesimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom typing import List, Dict, Any, Optionalfrom collections import defaultdictfrom accelerate import Accelerator2 unmodified linessuper(DynamicModel, self).__init__()self.sections = nn.ModuleDict()# Default section if none providedif not sections:sections = {'default': [{'input_size': 128,'output_size': 256,'activation': 'relu'}]}for section_name, layers in sections.items():self.sections[section_name] = nn.ModuleList()for layer_params in layers:24 unmodified lineslayers = []for prov in root.findall('.//prov'):layer_params = {'input_size': 128,'output_size': 256,'activation': 'relu'}layers.append(layer_params)2 unmodified linesdef create_model_from_folder(folder_path: str) -> DynamicModel:sections = defaultdict(list)if not os.path.exists(folder_path):print(f"Warning: Folder {folder_path} does not exist. Creating model with default configuration.")return DynamicModel({})xml_files_found = Falsefor root, dirs, files in os.walk(folder_path):for file in files:if file.endswith('.xml'):xml_files_found = Truefile_path = os.path.join(root, file)try:layers = parse_xml_file(file_path)2 unmodified linesexcept Exception as e:print(f"Error processing {file_path}: {str(e)}")if not xml_files_found:print("Warning: No XML files found. Creating model with default configuration.")return DynamicModel({})return DynamicModel(dict(sections))def main():folder_path = 'Xml_Data'model = create_model_from_folder(folder_path)print(f"Created dynamic PyTorch model with sections: {list(model.sections.keys())}")# Get first section's first layer's input size dynamicallyfirst_section = next(iter(model.sections.keys()))first_layer = model.sections[first_section][0]input_features = first_layer[0].in_features
model.py+27-7