()
| 44 | return parser.parse_args() |
| 45 | |
| 46 | def main(): |
| 47 | args = parse_args() |
| 48 | model_file = args.model |
| 49 | |
| 50 | if not model_file: |
| 51 | print("No model file specified.") |
| 52 | return |
| 53 | |
| 54 | if not os.path.isfile(model_file): |
| 55 | print(f"The file {model_file} does not exist.") |
| 56 | sys.exit(1) |
| 57 | |
| 58 | # Read the source code from the model file |
| 59 | with open(model_file, 'r') as f: |
| 60 | model_code = f.read() |
| 61 | |
| 62 | # Custom layer name |
| 63 | custom_layer = 'MyDense' |
| 64 | |
| 65 | # Parse model source code to AST |
| 66 | tree = ast.parse(model_code) |
| 67 | |
| 68 | # Transformer to replace Dense with MyDense |
| 69 | class LayerReplacer(ast.NodeTransformer): |
| 70 | def visit_Call(self, node): |
| 71 | if isinstance(node.func, ast.Name) and node.func.id == 'Dense': |
| 72 | node.func.id = custom_layer |
| 73 | return self.generic_visit(node) |
| 74 | |
| 75 | transformer = LayerReplacer() |
| 76 | modified_tree = transformer.visit(tree) |
| 77 | |
| 78 | # Generate modified source code |
| 79 | modified_code = ast.unparse(modified_tree) |
| 80 | |
| 81 | print("Modified Model:\n", modified_code) |
| 82 | |
| 83 | output_file = f'encrypt_{model_file}' |
| 84 | |
| 85 | # Write the modified model code to a new file |
| 86 | try: |
| 87 | with open(output_file, 'w') as file: |
| 88 | file.write(modified_code) |
| 89 | file.write("\n\n" + MyDense_complate) |
| 90 | |
| 91 | print(f"File {output_file} has been created with the modified code.") |
| 92 | except IOError as e: |
| 93 | print(f"Failed to write to file {output_file}: {e}") |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | main() |
no test coverage detected