Gauge invariance of the Dicke and Hopfield models