### Install Example-Specific Requirements Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt After setting up the correct library version, install the dependencies required for a specific example script by running pip install with a requirements.txt file. ```bash pip install -r requirements.txt ``` -------------------------------- ### Environment Setup for NLP Tasks Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the necessary libraries, transformers and accelerate, using pip. The `-q` flag ensures quiet installation. ```bash pip install -q transformers accelerate ``` -------------------------------- ### Install PEFT Library Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the PEFT library using pip. This is the standard way to get started with PEFT. ```bash pip install peft ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the required libraries for running IDEFICS examples. This includes bitsandbytes for quantization, sentencepiece for tokenization, accelerate for distributed training/inference, and transformers for model access. ```bash pip install -q bitsandbytes sentencepiece accelerate transformers ``` -------------------------------- ### Launch Training Script with Accelerate (FSDP Example) Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Command to launch a training script using `accelerate launch` with parameters specified in a configuration file, demonstrated with an FSDP setup. ```bash accelerate launch \ ./examples/pytorch/text-classification/run_glue.py \ --model_name_or_path google-bert/bert-base-cased \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --max_seq_length 128 \ --per_device_train_batch_size 16 \ --learning_rate 5e-5 \ --num_train_epochs 3 \ --output_dir /tmp/$TASK_NAME/ \ --overwrite_output_dir ``` -------------------------------- ### Install Transformers with PyTorch Backend Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs ๐Ÿค— Transformers along with PyTorch support in a single command. This is useful for CPU-only setups. ```bash pip install 'transformers[torch]' ``` -------------------------------- ### Install EETQ from source Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Clone the EETQ repository and install it from the source code. This method requires initializing submodules and installing dependencies. ```bash git clone https://github.com/NetEase-FuXi/EETQ.git cd EETQ/ git submodule update --init --recursive pip install . ``` -------------------------------- ### Install Quanto and bitsandbytes Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the necessary libraries for quantization. Ensure you have the latest versions for optimal compatibility. ```bash pip install -U quanto bitsandbytes ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the required libraries for finetuning and inference. Ensure you have transformers, datasets, evaluate, and accelerate. ```bash pip install transformers datasets evaluate accelerate ``` -------------------------------- ### Install necessary libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Ensure you have the required libraries installed for finetuning and evaluation. ```bash pip install transformers datasets evaluate ``` -------------------------------- ### Few-Shot Prompting Example in Python Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates one-shot prompting by providing a single example to guide the model's output format. Useful for tasks requiring specific output patterns. ```python torch.manual_seed(0) prompt = """Text: The first human went into space and orbited the Earth on April 12, 1961. Date: 04/12/1961 Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. Date:""" sequences = pipe( prompt, max_new_tokens=8, do_sample=True, top_k=10, ) for seq in sequences: print(f"Result: {seq['generated_text']}") ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the required libraries for finetuning and evaluating ASR models. Ensure you have transformers, datasets, evaluate, and jiwer installed. ```bash pip install transformers datasets evaluate jiwer ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the required libraries for datasets, transformers, acceleration, and image augmentation. ```bash pip install -q datasets transformers accelerate timm pip install -q -U albumentations>=1.4.5 torchmetrics pycocotools ``` -------------------------------- ### Run GLUE Example with MPS Device Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Execute the run_glue.py script using the MPS backend. The Trainer automatically uses the MPS device if available, simplifying setup. ```bash export TASK_NAME=mrpc python examples/pytorch/text-classification/run_glue.py \ --model_name_or_path google-bert/bert-base-cased \ --task_name $TASK_NAME \ - --use_mps_device \ --do_train \ --do_eval \ --max_seq_length 128 \ --per_device_train_batch_size 32 \ --learning_rate 2e-5 \ --num_train_epochs 3 \ --output_dir /tmp/$TASK_NAME/ \ --overwrite_output_dir ``` -------------------------------- ### Install ๐Ÿค— Datasets Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the ๐Ÿค— Datasets library to load and experiment with datasets for model training. ```bash pip install datasets ``` -------------------------------- ### Install Quanto, Accelerate, and Transformers Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Before using Quanto with Transformers, ensure these essential libraries are installed in your environment. ```bash pip install quanto accelerate transformers ``` -------------------------------- ### Install Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the necessary libraries for running image-text-to-text models. Use this command before initializing any models. ```bash pip install -q transformers accelerate flash_attn ``` -------------------------------- ### Install Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs necessary libraries for running video language models with Transformers. ```bash pip install -q transformers accelerate flash_attn ``` -------------------------------- ### Install Libraries for Distillation Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs necessary libraries for knowledge distillation, model evaluation, and acceleration. Ensure these are installed before proceeding. ```bash pip install transformers datasets accelerate tensorboard evaluate --upgrade ``` -------------------------------- ### Install Required Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the necessary libraries for GPTQ quantization, including auto-gptq, accelerate, optimum, and transformers. ```bash pip install auto-gptq pip install --upgrade accelerate optimum transformers ``` -------------------------------- ### Install bitsandbytes Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the bitsandbytes library required for quantization. ```bash !pip install bitsandbytes ``` -------------------------------- ### Install Datasets Library Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the ๐Ÿค— Datasets library with audio support, required for loading audio files. ```bash pip install --upgrade pip pip install datasets[audio] ``` -------------------------------- ### Install Optional Development Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt If the initial installation fails due to missing dependencies, install the 'quality' extras after ensuring your deep learning framework (PyTorch, TensorFlow, Flax) is installed. This covers most common development needs. ```bash pip install -e ".[quality]" ``` -------------------------------- ### Install Transformers and g2p-en Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the required libraries for using the FastSpeech2 Conformer model. Ensure pip is up-to-date before installing. ```bash pip install --upgrade pip pip install --upgrade transformers g2p-en ``` -------------------------------- ### Installing GaLore Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the GaLore library using pip before using it for memory-efficient training. ```bash pip install galore-torch ``` -------------------------------- ### Install Accelerate Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the Accelerate library to manage distributed training environments. Ensure PyTorch 2.1.0 or newer is installed. ```bash pip install accelerate ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the required libraries for working with datasets, transformers, evaluation metrics, and acceleration. ```python # uncomment to install the necessary libraries !pip install -q datasets transformers evaluate accelerate ``` -------------------------------- ### Install Gradio Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the Gradio library using pip. This is a prerequisite for creating web demos. ```bash pip install gradio ``` -------------------------------- ### Install compressed-tensors from Source Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the compressed-tensors library from its source code for development purposes. This involves cloning the repository and performing an editable installation. ```bash git clone https://github.com/neuralmagic/compressed-tensors cd compressed-tensors pip install -e . ``` -------------------------------- ### Install HQQ Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the latest version of HQQ and build its CUDA kernels. ```bash pip install hqq ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the required libraries for video classification tasks, including PyTorchVideo, Transformers, and Evaluate. ```bash pip install -q pytorchvideo transformers evaluate ``` -------------------------------- ### Install Transformers Agents Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the agents extras for the Transformers library to get all default dependencies. ```bash pip install transformers[agents] ``` -------------------------------- ### Inspect a Dataset Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Retrieves and displays the first example from the loaded dataset to understand its structure and content. ```python dataset[0] ``` -------------------------------- ### Install Transformers with Conda Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the Transformers library from the conda-forge channel. This is a straightforward way to get the library set up. ```bash conda install conda-forge::transformers ``` -------------------------------- ### Run Pytest for Example Tests Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Execute tests for a specific example subfolder using pytest. Ensure requirements are installed first if needed. ```bash pip install -r examples/xxx/requirements.txt # only needed the first time python -m pytest -n auto --dist=loadfile -s -v ./examples/pytorch/text-classification ``` -------------------------------- ### Test ๐Ÿค— Accelerate Setup Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Verifies that the ๐Ÿค— Accelerate environment is configured correctly. Run this after `accelerate config` to ensure proper setup. ```bash accelerate test ``` -------------------------------- ### Install Transformers from Source Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Clone the Hugging Face Transformers repository and install it using pip. This ensures you have the latest version for running example scripts. ```bash git clone https://github.com/huggingface/transformers cd transformers pip install . ``` -------------------------------- ### Set Up Development Environment Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the library in editable mode with development dependencies. This command ensures that changes to the source code are immediately reflected without needing to reinstall. ```bash pip install -e ".[dev]" ``` -------------------------------- ### ChatML Template Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt This is an example of the ChatML template format used for structuring chat messages. It iterates through messages and wraps each with start and end tokens. ```jinja {%- for message in messages %} {{- '<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n' }} {%- endfor %} ``` -------------------------------- ### Create and Train a Dummy Tokenizer Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt This snippet demonstrates how to initialize a tokenizer with BPE, set up a trainer, define pre-tokenization, and train the tokenizer on specified files. Ensure the `files` variable is populated with actual file paths. ```python from tokenizers import Tokenizer from tokenizers.models import BPE from tokenizers.trainers import BpeTrainer from tokenizers.pre_tokenizers import Whitespace tokenizer = Tokenizer(BPE(unk_token="[UNK]")) trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]) tokenizer.pre_tokenizer = Whitespace() files = [...] tokenizer.train(files, trainer) ``` -------------------------------- ### Install Transformers with Development Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the Transformers library with all development dependencies required for running checks and contributing to the project. Use this for a full development setup. ```bash pip install transformers[dev] ``` -------------------------------- ### Install Hyperparameter Search Backends Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the necessary libraries for your chosen hyperparameter search backend (Optuna, SigOpt, Wandb, or Ray Tune). ```bash pip install optuna/sigopt/wandb/ray[tune] ``` -------------------------------- ### Loading SQuAD v2 Processor and Examples Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates how to load the SquadV2Processor and retrieve development set examples from a specified directory. ```python # Loading a V2 processor processor = SquadV2Processor() examples = processor.get_dev_examples(squad_v2_data_dir) ``` -------------------------------- ### Inference with Pipeline Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Uses the `pipeline` function for straightforward inference with a finetuned automatic speech recognition model. This is the simplest way to get started. ```python from transformers import pipeline transcriber = pipeline("automatic-speech-recognition", model="stevhliu/my_awesome_asr_minds_model") transcriber(audio_file) ``` -------------------------------- ### Build DeepSpeed Binary Wheel Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Generate a binary wheel for DeepSpeed, suitable for installation on multiple machines with the same setup. This involves building extensions and packaging them. ```bash git clone https://github.com/microsoft/DeepSpeed/ cd DeepSpeed rm -rf build TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 python setup.py build_ext -j8 bdist_wheel ``` -------------------------------- ### Access First Training Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Retrieves and displays the first example from the training split, showing the 'audio' and 'intent_class' fields. ```python print(minds["train"][0]) ``` -------------------------------- ### Generate a response with Blenderbot Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt This example demonstrates how to use the Blenderbot model and tokenizer to generate a conversational response to a given utterance. Ensure you have the 'transformers' library installed. ```python from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration mname = "facebook/blenderbot-400M-distill" model = BlenderbotForConditionalGeneration.from_pretrained(mname) tokenizer = BlenderbotTokenizer.from_pretrained(mname) UTTERANCE = "My friends are cool but they eat too many carbs." inputs = tokenizer([UTTERANCE], return_tensors="pt") reply_ids = model.generate(**inputs) print(tokenizer.batch_decode(reply_ids)) ``` -------------------------------- ### Loading SQuAD v1 Processor and Examples Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Shows how to load the SquadV1Processor and obtain development set examples from a given directory. ```python # Loading a V1 processor processor = SquadV1Processor() examples = processor.get_dev_examples(squad_v1_data_dir) ``` -------------------------------- ### Initialize ASR Pipeline with Default Model Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Create a pipeline for automatic speech recognition using the default model. This is the simplest way to get started with ASR. ```python >>> from transformers import pipeline >>> transcriber = pipeline(task="automatic-speech-recognition") ``` -------------------------------- ### CodeGen Model Usage Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates how to load a CodeGen model and tokenizer, generate code completions for a given prompt, and decode the output. Ensure you have the 'transformers' library installed. ```python from transformers import AutoModelForCausalLM, AutoTokenizer checkpoint = "Salesforce/codegen-350M-mono" model = AutoModelForCausalLM.from_pretrained(checkpoint) tokenizer = AutoTokenizer.from_pretrained(checkpoint) text = "def hello_world():" completion = model.generate(**tokenizer(text, return_tensors="pt")) print(tokenizer.decode(completion[0])) ``` -------------------------------- ### KOSMOS-2 Image Grounding and Captioning Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates how to use the KOSMOS-2 model to generate captions and identify object bounding boxes from an image. Ensure you have Pillow and requests installed. ```python from PIL import Image import requests from transformers import AutoProcessor, Kosmos2ForConditionalGeneration model = Kosmos2ForConditionalGeneration.from_pretrained("microsoft/kosmos-2-patch14-224") processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224") url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg" image = Image.open(requests.get(url, stream=True).raw) prompt = " An image of" inputs = processor(text=prompt, images=image, return_tensors="pt") generated_ids = model.generate( pixel_values=inputs["pixel_values"], input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], image_embeds=None, image_embeds_position_mask=inputs["image_embeds_position_mask"], use_cache=True, max_new_tokens=64, ) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] processed_text = processor.post_process_generation(generated_text, cleanup_and_extract=False) print(processed_text) caption, entities = processor.post_process_generation(generated_text) print(caption) print(entities) ``` -------------------------------- ### MGP-STR Inference Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates how to perform Optical Character Recognition (OCR) using the MgpstrProcessor and MgpstrForSceneTextRecognition. Load an image, preprocess it, get model outputs, and decode the predictions. ```python >>> from transformers import MgpstrProcessor, MgpstrForSceneTextRecognition >>> import requests >>> from PIL import Image >>> processor = MgpstrProcessor.from_pretrained('alibaba-damo/mgp-str-base') >>> model = MgpstrForSceneTextRecognition.from_pretrained('alibaba-damo/mgp-str-base') >>> # load image from the IIIT-5k dataset >>> url = "https://i.postimg.cc/ZKwLg2Gw/367-14.png" >>> image = Image.open(requests.get(url, stream=True).raw).convert("RGB") >>> pixel_values = processor(images=image, return_tensors="pt").pixel_values >>> outputs = model(pixel_values) >>> generated_text = processor.batch_decode(outputs.logits)['generated_text'] ``` -------------------------------- ### Install PEFT from Source Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the PEFT library directly from its GitHub repository to access the latest features. This is useful for trying out brand new functionalities. ```bash pip install git+https://github.com/huggingface/peft.git ``` -------------------------------- ### Initialize a Chat Conversation Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Build the initial conversation history for a chat model. This includes a system message to guide the model's behavior and a user message to start the interaction. ```python chat = [ {"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."}, {"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"} ] ``` -------------------------------- ### Install ๐Ÿค— Accelerate from Git Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the latest development version of the ๐Ÿค— Accelerate library from its GitHub repository. This is recommended due to rapid development. ```bash pip install git+https://github.com/huggingface/accelerate ``` -------------------------------- ### Few-Shot Prompting for Image Captioning Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Employ few-shot prompting to guide the model's output format and content by providing examples within the prompt. This enables in-context learning for more complex tasks. ```python >>> prompt = ["User:", ... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", ... "Describe this image.\nAssistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building.\n", ... "User:", ... "https://images.unsplash.com/photo-1524099163253-32b7f0256868?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3387&q=80", ... "Describe this image.\nAssistant:" ... ] >>> inputs = processor(prompt, return_tensors="pt").to("cuda") >>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids >>> generated_ids = model.generate(**inputs, max_new_tokens=30, bad_words_ids=bad_words_ids) >>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) >>> print(generated_text[0]) User: Describe this image. Assistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building. User: Describe this image. Assistant: An image of the Statue of Liberty. Fun fact: the Statue of Liberty is 151 feet tall. ``` -------------------------------- ### M2M100 Supervised Training Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates how to load the M2M100 model and tokenizer, prepare input text with target text, and calculate the loss during a forward pass for supervised training. Ensure 'sentencepiece' is installed. ```python from transformers import M2M100Config, M2M100ForConditionalGeneration, M2M100Tokenizer model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M") tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="en", tgt_lang="fr") src_text = "Life is like a box of chocolates." tgt_text = "La vie est comme une boรฎte de chocolat." model_inputs = tokenizer(src_text, text_target=tgt_text, return_tensors="pt") loss = model(**model_inputs).loss # forward pass ``` -------------------------------- ### Define Tools for Model Interaction Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Define functions that the model can call, including type hints and detailed docstrings for arguments and return values. This example includes functions for getting temperature and wind speed. ```python def get_current_temperature(location: str, unit: str) -> float: """ Get the current temperature at a location. Args: location: The location to get the temperature for, in the format "City, Country" unit: The unit to return the temperature in. (choices: ["celsius", "fahrenheit"]) Returns: The current temperature at the specified location in the specified units, as a float. """ return 22. # A real function should probably actually get the temperature! def get_current_wind_speed(location: str) -> float: """ Get the current wind speed in km/h at a given location. Args: location: The location to get the temperature for, in the format "City, Country" Returns: The current wind speed at the given location in km/h, as a float. """ return 6. # A real function should probably actually get the wind speed! tools = [get_current_temperature, get_current_wind_speed] ``` -------------------------------- ### Create a Web Demo from an Image Classification Pipeline Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Create and launch a web demo for an image classification pipeline using Gradio's Interface.from_pipeline. The demo runs locally by default. ```python from transformers import pipeline import gradio as gr pipe = pipeline("image-classification", model="google/vit-base-patch16-224") gr.Interface.from_pipeline(pipe).launch() ``` -------------------------------- ### Pegasus Summarization Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt This snippet shows how to perform text summarization using the Pegasus model and tokenizer. Ensure you have the 'transformers' and 'torch' libraries installed. The code automatically selects CUDA if available, otherwise defaults to CPU. ```python >>> from transformers import PegasusForConditionalGeneration, PegasusTokenizer >>> import torch >>> src_text = [ ... """ PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.""" ... ] ... model_name = "google/pegasus-xsum" >>> device = "cuda" if torch.cuda.is_available() else "cpu" >>> tokenizer = PegasusTokenizer.from_pretrained(model_name) >>> model = PegasusForConditionalGeneration.from_pretrained(model_name).to(device) >>> batch = tokenizer(src_text, truncation=True, padding="longest", return_tensors="pt").to(device) >>> translated = model.generate(**batch) >>> tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True) >>> assert ( ... tgt_text[0] ... == "California's largest electricity provider has turned off power to hundreds of thousands of customers." ... ) ``` -------------------------------- ### Encode Dataset for LLM Input Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt This Python function encodes a batch of text examples, including questions, words, bounding boxes, and answers, for LLM processing. It handles tokenization, determines start and end positions of answers within the encoded text, and adds image information. ```python def encode_dataset(examples, max_length=512): questions = examples["question"] words = examples["words"] boxes = examples["boxes"] answers = examples["answer"] # encode the batch of examples and initialize the start_positions and end_positions encoding = tokenizer(questions, words, boxes, max_length=max_length, padding="max_length", truncation=True) start_positions = [] end_positions = [] # loop through the examples in the batch for i in range(len(questions)): cls_index = encoding["input_ids"][i].index(tokenizer.cls_token_id) # find the position of the answer in example's words words_example = [word.lower() for word in words[i]] answer = answers[i] match, word_idx_start, word_idx_end = subfinder(words_example, answer.lower().split()) if match: # if match is found, use `token_type_ids` to find where words start in the encoding token_type_ids = encoding["token_type_ids"][i] token_start_index = 0 while token_type_ids[token_start_index] != 1: token_start_index += 1 token_end_index = len(encoding["input_ids"][i]) - 1 while token_type_ids[token_end_index] != 1: token_end_index -= 1 word_ids = encoding.word_ids(i)[token_start_index : token_end_index + 1] start_position = cls_index end_position = cls_index # loop over word_ids and increase `token_start_index` until it matches the answer position in words # once it matches, save the `token_start_index` as the `start_position` of the answer in the encoding for id in word_ids: if id == word_idx_start: start_position = token_start_index else: token_start_index += 1 # similarly loop over `word_ids` starting from the end to find the `end_position` of the answer for id in word_ids[::-1]: if id == word_idx_end: end_position = token_end_index else: token_end_index -= 1 start_positions.append(start_position) end_positions.append(end_position) else: start_positions.append(cls_index) end_positions.append(cls_index) encoding["image"] = examples["image"] encoding["start_positions"] = start_positions encoding["end_positions"] = end_positions return encoding ``` -------------------------------- ### Install necessary libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the required libraries for loading and running transformer models, including accelerate and bitsandbytes for optimization. ```bash !pip install transformers accelerate bitsandbytes optimum ``` -------------------------------- ### Install DeepSpeed with Transformers Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install DeepSpeed as an extra dependency when installing the Transformers library. ```bash pip install transformers[deepspeed] ``` -------------------------------- ### Encode and Decode Audio with EnCodec Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt This example demonstrates how to load the EnCodec model and processor, prepare audio data, encode it, and then decode it back to audio values. It also shows the equivalent direct forward pass. ```python from datasets import load_dataset, Audio from transformers import EncodecModel, AutoProcessor librispeech_dummy = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") model = EncodecModel.from_pretrained("facebook/encodec_24khz") processor = AutoProcessor.from_pretrained("facebook/encodec_24khz") librispeech_dummy = librispeech_dummy.cast_column("audio", Audio(sampling_rate=processor.sampling_rate)) audio_sample = librispeech_dummy[-1]["audio"]["array"] inputs = processor(raw_audio=audio_sample, sampling_rate=processor.sampling_rate, return_tensors="pt") encoder_outputs = model.encode(inputs["input_values"], inputs["padding_mask"]) audio_values = model.decode(encoder_outputs.audio_codes, encoder_outputs.audio_scales, inputs["padding_mask"])[0] # or the equivalent with a forward pass audio_values = model(inputs["input_values"], inputs["padding_mask"]).audio_values ``` -------------------------------- ### Initialize and Train the Model Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Instantiate the Trainer with the model, training arguments, datasets, and other necessary components, then start the training process. ```python from transformers import Trainer trainer = Trainer( model=model, args=training_args, train_dataset=dataset["train"], eval_dataset=dataset["test"], processing_class=tokenizer, data_collator=data_collator, compute_metrics=compute_metrics, ) trainer.train() ``` -------------------------------- ### Install Transformers Library Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the transformers library to use SAM for mask generation. This command installs the library quietly. ```bash pip install -q transformers ``` -------------------------------- ### Start Training Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Initiates the training process using the configured Trainer instance. This method should be called after the Trainer has been fully initialized. ```python trainer.train() ``` -------------------------------- ### Configure and Run Training Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Sets up training hyperparameters using Seq2SeqTrainingArguments and initializes the Seq2SeqTrainer. The model is then finetuned by calling trainer.train(). ```python >>> training_args = Seq2SeqTrainingArguments( ... output_dir="my_awesome_billsum_model", ... eval_strategy="epoch", ... learning_rate=2e-5, ... per_device_train_batch_size=16, ... per_device_eval_batch_size=16, ... weight_decay=0.01, ... save_total_limit=3, ... num_train_epochs=4, ... predict_with_generate=True, ... fp16=True, #change to bf16=True for XPU ... push_to_hub=True, ... ) >>> trainer = Seq2SeqTrainer( ... model=model, ... args=training_args, ... train_dataset=tokenized_billsum["train"], ... eval_dataset=tokenized_billsum["test"], ... processing_class=tokenizer, ... data_collator=data_collator, ... compute_metrics=compute_metrics, ... ) >>> trainer.train() ``` -------------------------------- ### Pop2Piano: Local Audio File Example Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Shows how to load a local audio file using librosa and convert it to a MIDI file using the Pop2Piano model. ```python >>> import librosa >>> from transformers import Pop2PianoForConditionalGeneration, Pop2PianoProcessor >>> audio, sr = librosa.load("", sr=44100) # feel free to change the sr to a suitable value. >>> model = Pop2PianoForConditionalGeneration.from_pretrained("sweetcocoa/pop2piano") >>> processor = Pop2PianoProcessor.from_pretrained("sweetcocoa/pop2piano") >>> inputs = processor(audio=audio, sampling_rate=sr, return_tensors="pt") >>> model_output = model.generate(input_features=inputs["input_features"], composer="composer1") >>> tokenizer_output = processor.batch_decode( ... token_ids=model_output, feature_extractor_output=inputs ... )["pretty_midi_objects"][0] >>> tokenizer_output.write("./Outputs/midi_output.mid") ``` -------------------------------- ### Install LayoutLMv2 Dependencies Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Installs the required libraries for LayoutLMv2, including detectron2, torchvision, and tesseract. Ensure these are installed before using the model. ```bash python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' python -m pip install torchvision tesseract ``` -------------------------------- ### Example Usage of Custom Pipeline Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Demonstrates how to instantiate and use a custom pipeline for a new task, including passing arguments for custom behavior. ```python >>> pipe = pipeline("my-new-task") >>> pipe("This is a test") [{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}, {"label": "3-star", "score": 0.05} {"label": "4-star", "score": 0.025}, {"label": "5-star", "score": 0.025}] >>> pipe("This is a test", top_k=2) [{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}] ``` -------------------------------- ### Install Necessary Libraries Source: https://huggingface-projects-docs-llms-txt.hf.space/transformers/llms.txt Install the required libraries for summarization tasks, including transformers, datasets, evaluate, and rouge_score. ```bash pip install transformers datasets evaluate rouge_score ```