I have guinea pigs and I gave them some pineapple about 2 wks. ago. I started getting gnats and was using a flying pest spray on them. I would spray the entire room after removing my piggies and let it sit closed for an hour or so. Well, no more pineapple since my piggies don't eat it but my gnat problem has increased about 10 times more than before.
I was having a problem with fruit flies and my friend's grandmother told me of an old trick they use to do. My husband has Asthma and I couldn't use spray.
You take a piece of ripe banana peel and put it in the very back of an empty bread sack. Make sure there is no food out and lay it out flat on the counter or table with the end open for the flies to get in there. Shut the room up and even put a towel up to the door. Wait a few hours and check. When the bag is full of the little buggers, just knot up the sack and toss in the outside trash.
You might want to take the piggies and their food and bedding out of the room so the gnats wont have a choice in where they feed and have to go to the sack for some grub.
Hope this helps, at least you wont be out any money and your piggies will be safe.
Now that the Gnats are already established in that area you will have to fully clean the cages the piggies occupy. The juice an fluids from the pineapple have permeated the bedding materials at the bottom of the cage. Clean it soon and you may have to limit the food to an amount that the guinea pigs will eat almost completely.
Use sticky fly strips... we used them in our barn for our horses and they worked perfectly! I also have guinea pigs and I found that I had the same problem as you do. So I just placed about three fly strips on the top outside area of each guinea pigs cage and some on the side as long your guinea pig can't reach them and it worked like a charm! Within a few days the gnat problem was gone. And it didn't harm my guinea pigs either in fact I still do it to this day but we rarely have gnats any more.
Use red cedar wood chips as bedding. Some pet stores will say that it is not good or safe for your little piggies, but it helps repel ants, some gnats an other pests that you and I don't want near our piggies. Also you can put your piggies in a room that has a air conditioner in it so the pests will think the weather is to cold and leave your house. I hope this helped you out!
I am a expert when it come to talking about piggies. I have 3 of my own guinea pigs and they are very happy especially my 1st piggie Cynna Bunn, he loves me so much and he never stops climbing on me and jumping on and off of tall objects.