Predicting spectra and related properties such as the dielectric function of crystalline materials based on machine learning has a huge, hitherto unexplored, technological potential. For this reason, we create an database of 9915 dielectric tensors of semiconductors and insulators calculated in the independent-particle approximation (IPA). In addition, we present the family of machine learning models, a series of graph attention neural networks (GAT) trained to predict the dielectric function and refractive index. yields accurate prediction of spectra of semiconductors using only their crystal structure. Smooth, artifact-free curves are obtained without these properties being enforced by penalties. Published by the American Physical Society 2024