TF version compatibility fixes
This PR makes several changes to core methods to support the upcoming 2.13 release of TF and generally futureproof against any other upcoming changes that might happen.

The core source of all of these problems is that Keras has been very mobile inside TensorFlow: Initially we used `tf.python.keras` until they told us this was a deprecated copy of Keras that shouldn't be there, and it got removed. We then switched to `tf.keras`, but now Keras has fully moved into its own library and namespace again. Although this is still mirrored at `tf.keras`, for the newest version of TF we'll just `import keras`.

There are several other related problems where our code assumed that parts of Keras that weren't really part of the public API would stay where they were. Not all of these have caused problems yet (that I know of) but they look very risky to me, and so I made some general fixes. This might surface some hidden bugs!

Fixes #23352
_The documentation is not available anymore as the PR was closed or merged._<|||||>@sgugger I've added a proper framework inference function, put it in `utils/generic` and moved everything over to that. That should catch any edge cases in future, unless one of the frameworks gets renamed entirely :sweat_smile: 