huggingface / transformers.js

State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
https://huggingface.co/docs/transformers.js
Apache License 2.0
11.9k stars 750 forks source link

Method to send `HF_TOKEN` from the client side #777

Open AnmolBansalDEV opened 5 months ago

AnmolBansalDEV commented 5 months ago

Feature request

For using private/gated models, we should add functionality to pass the HF_TOKEN from the client side as well. Currently Transformer.js only provides ways to access private/gated models from the node js servers only i.e. backend (check here). But there are relevant use cases where passing the HF_TOKEN from the Frontend also makes sense.

Motivation

Usecases

One usecase I can think of is when the application developer wants the user to enter their own HF_TOKEN from the Frontend to access the model and use the application locally in their browser. This a a valid usecase that is being implemented in many AI applications coming up that expect the user to input the API key to use the application.

Security Considerations

I understand that it is a dangerous feature as it makes it possible for a developer to expose their token to the web. But I think not having functionality to support this is not the optimum way to handle the situation, instead, we should add an explicit warning regarding the pitfalls of accidentally exposing the token.

Your contribution

I can work on this, but I'm not very confident as I haven't gone through the codebase. But happy to have a stab at it if okay with maintainers.

monomy001 commented 3 months ago

+1 on this.. i am developing a react application so that the app can run translation AI offline.. there is no internet connection, so cant connect to server, bt react-scripts can only set env variable using REACTAPP prefix. this is a simple issue bt a pain in the neck to work around. @xenova

You just need to change this file: @xenova/transformers/src/utils/hub.js line 194 from const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN; to const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN ?? process.env?.REACT_APP_HF_TOKEN;

wouldve solve the problem really easily. pls help consider acommodate.

xenova commented 3 months ago

One workaround is to define the process object in your application as follows:

self.process = { env: { HF_TOKEN: 'TOKEN GOES HERE' } };