Updated Dockerfiles to use `uv` for installing packages
Author: Sai-Suraj-27
Pull request: yes

# What does this PR do?
In many Dockerfiles `uv` is installed but still it is not used for installing packages at some places. I think using `uv` at all these places will increase the speed of respective Docker image builds by a considerable amount.

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?
@ydshieh @ArthurZucker 

Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers.

Hi @Sai-Suraj-27 Could you check the error logs in this run: https://github.com/huggingface/transformers/actions/runs/14060656795/job/39370346086

Maybe it's as simple as using `-- -y` but I never try this myself.

```
 > [7/8] RUN uv pip uninstall -y transformers:
0.051 error: unexpected argument '-y' found
0.051 
0.051   tip: to pass '-y' as a value, use '-- -y'
```

> Hi @Sai-Suraj-27 Could you check the error logs in this run: https://github.com/huggingface/transformers/actions/runs/14060656795/job/39370346086
> 
> Maybe it's as simple as using `-- -y` but I never try this myself.
> 
> ```
>  > [7/8] RUN uv pip uninstall -y transformers:
> 0.051 error: unexpected argument '-y' found
> 0.051 
> 0.051   tip: to pass '-y' as a value, use '-- -y'
> ```

Hi, Looks like, `uv pip uninstall` does not support the `-y` flag unlike `pip uninstall`. I tested locally, just using 
`uv pip uninstall transformers` works fine. The fix is simple, I will remove the `-y` flag wherever it is being used and update the PR.


ok thank you. I will try later today.

There is remaining one failing job 

https://github.com/huggingface/transformers/actions/runs/14063433382/job/39379784581

if you could take a look 🙏 

> There is remaining one failing job
> 
> https://github.com/huggingface/transformers/actions/runs/14063433382/job/39379784581
> 
> if you could take a look 🙏

Yes, I think this is actually because `detectron2's` setup script requires `torch` but it does not explicitly list it as a build dependency. By default `uv` performs isolated builds when building packages from source. So, here it is not able to access torch. Passing the [`--no-build-isolation`](https://docs.astral.sh/uv/reference/cli/#uv-add--no-build-isolation) flag for this installation line, fixed the issue. I tested it locally by building the docker image, the build was successful.

![image](https://github.com/user-attachments/assets/593aa5df-6036-435f-baa4-4552baee6cfc)


Nice ✅ https://github.com/huggingface/transformers/actions/runs/14064927430

> Nice ✅ https://github.com/huggingface/transformers/actions/runs/14064927430

I see that we are not using uv anywhere in the Dockerfiles for gpu tasks. Is there any particular reason for this?

> I see that we are not using uv anywhere in the Dockerfiles for gpu tasks. Is there any particular reason for this? Good speedup will come by using uv right? Would be happy to raise a PR.

No particular reason :-) 

It is built in a daily basis, and during the CI run time, we rarely install new stuff, So using `uv` or not is not is not super important here.

But open to such changes if you want to give it a try. You might first focus on `docker/transformers-all-latest-gpu/Dockerfile`, see how it goes, and we can see if we want to apply the changes to other files.