You can download pre-trained machine learning (ML) models into your Jupyter environment from the Hugging Face® Hub for translation, semantic analysis and more.
The following approved ML models are available for download from Hugging Face to your Jupyter environment:
Each model has its own repository on Hugging Face. You can download any of the above approved repositories or any file within these repositories using our custom utility. The following example shows downloading a complete repository:
from fbri.package_managers.huggingface import hf_download_repo hf_download_repo(repo="facebook/mbart-large-50-many-to-many-mmt", revision="main", output_dir=None)
This example shows downloading a specific file:
from fbri.package_managers.huggingface import hf_download_file repo = "facebook/mbart-large-50-many-to-many-mmt" filename = ".gitattributes" hf_download_file(filename, repo, revision="main", output_dir=None)
The default revision is main
but you can specify any revision available in the Hugging Face repository. The output_dir
parameter allows you to specify the target location for file download. The default location is ~/huggingface/<REPO_NAME>/<REVISION>
.
Once you have downloaded a model, you can use the Hugging Face transformers library to perform model inference. The example below shows a usage of the facebook/mbart-large-50-many-to-many-mmt model to translate to German:
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast import torch device = ("cuda" if torch.cuda.is_available() else "cpu") article_en = "The house is wonderful" model_name = "facebook/mbart-large-50-many-to-many-mmt" model_path = f"/home/jovyan/huggingface/{model_name}/main" model = MBartForConditionalGeneration.from_pretrained(model_path).to(device) tokenizer = MBart50TokenizerFast.from_pretrained(model_path) # translate English to German tokenizer.src_lang = "en_XX" encoded_en = tokenizer(article_en, return_tensors="pt").to(device) generated_tokens = model.generate( **encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id["de_DE"] ) tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
Note: You will see faster runtimes if you use a GPU machine because machine learning models are optimized for GPU.
To use any of the other available models, refer to these Hugging Face usage guides:
Pass the path of the model as shown in the facebook/mbart-large-50-many-to-many-mmt example above.
If you want to use an ML model in the Hugging Face Hub that is not currently on our approved list, you can submit a request for review by creating a support ticket. See Get help for instructions. Provide a link to the requested ML model and describe your use case as it relates to your approved research agenda. This will allow us to review the requested model, assess the implementation scope, and determine whether it is compliant with Meta policies and safeguards. Please note that submitted requests will be reviewed accordingly, and immediate implementation is not guaranteed.
Any models that are approved through this process will then be made available to all researchers, not exclusively to the researcher who requested it.
Note: The only models that can be considered at this time are text-based models in the Hugging Face Hub under an open source license.