refactor: in cli/server extract helpers, flatten nested functions, use CB context manager
Refactors `src/transformers/cli/serve.py` to reduce nesting depth, eliminate code duplication,
and improve maintainability. No behavioral changes and the public API is unchanged.
This change is motivated by discussion in https://github.com/huggingface/transformers/pull/44566
where type checking was made a bit complex due to the current code architecture.