In recent years, generative AI has made remarkable strides, enabling the creation of exceptional quality novel designs and images. This study aims to enhance the performance of a conditional autoencoder, a type of generative deep learning framework. Our primary focus lies in applying these techniques to improve the design of metagratings. By harnessing the power of generative modeling and Bayesian optimization, we can generate optimized designs for metagratings, thereby enhancing their functionality and efficiency. Additionally, through the use of transfer learning, we adapt the network originally designed for transverse-electric (TE) modes to encompass transverse-magnetic (TM) modes. This adaptation spans a wide range of deflection angles and operating wavelengths, with minimal additional training data required. This versatile black-box approach has broad applications in the inverse design of various photonic and nanophotonic devices.