Exercise! Seriously. At least an hour, if not two, of walking every day, or something more strenuous if you can. Or yoga.
Or get a dog. Again, seriously. A dog will get you out in all weathers.
Or do a dance class, or take up an instrument, or an art class. Something where you engage your brain but differently than in writing your PhD.
And maybe you can do a self-taught version of CBT - read over your first post. How rational are our thoughts? How could you rephrase them?
You say you feel a failure, but are you? Or are you afraid of failure? You compare yourself with others - "Comparison is the thief of joy"
It will be HARD and you will need to be disciplined, but you need to start not letting yourself think such self-indulgent thoughts: train yourself to get out of the habit.
Can you talk to a good friend who knows you well, to develop an alternative internal script? Not a "I'm awesome" cheerleading one - that's just as silly as thinking you're a failure (Rationally, anyone finishing a PhD is patently NOT a failure
) - but a moderate non-self-harming internal script.
I think that sometimes we get into bad mental hygiene habits - it's easy to do, like reaching for crappy food when we're tired. It takes a bit of mental energy and discipline to replace those thoughts with more rational ones.
And if you're clinically depressed, then you may need the chemical help of medication to help you to do this.
Look, for a lot of people, doing this kind of work doesn't come easy - we're hard on ourselves and we have to learn certain kinds of mental discipline. YOu're obviously rational, intelligent, and organised - can you turn those characteristics to helping yourself to not sabotage yourself? And part of that is seeking help from those who can give it: GP, counsellor ...