I just keep a bottle of isoprople alcohol in a spray bottle nearby when I water. I found that by only filling my trays to water the gnats live all the way at the bottom because the soil up top is too dry. When I water I sometimes see my trays have baby gnats swimming around. I simply spray some alcohol on the top of the water and kill them all. Also I keep sticky cards in the trays because when you water this way the gnats try to climb up and out. If it gets real bad you could use azamax as a soil drench, but not too late in flower. I like azamax because it shuts off the feeding mechanism and the sex drive. It also suffocates them. But alcohol kills on contact and will not harm your plants! Also turn down your lights and shut off your fans before you spray anything, and never spray anything on flowering plants!!! Just the soil and pots. Don't forget under the pots, those little fuckers will hide there.
Gnats are sometimes a sign of overwatering too.