### Install @huggingface/blake3-jit Source: https://github.com/huggingface/huggingface.js/blob/main/packages/blake3-jit/README.md Install the package using npm. ```bash npm install @huggingface/blake3-jit ``` -------------------------------- ### Install Jinja and Hub Packages Source: https://github.com/huggingface/huggingface.js/blob/main/packages/jinja/README.md Install the necessary packages for using Jinja with Hugging Face Hub. ```sh npm i @huggingface/jinja npm i @huggingface/hub ``` -------------------------------- ### Install Dependencies Source: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/README.md Run this command from the root of the repository to install dependencies for all packages, including the MCP client. ```bash pnpm install ``` -------------------------------- ### Install @huggingface/tiny-agents Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tiny-agents/README.md Install the tiny-agents package using npm or pnpm. ```bash npm install @huggingface/tiny-agents # or pnpm add @huggingface/tiny-agents ``` -------------------------------- ### Install Hugging Face Blobs Source: https://github.com/huggingface/huggingface.js/blob/main/packages/blob/README.md Install the package using your preferred package manager. ```console pnpm add @huggingface/blob npm add @huggingface/blob yarn add @huggingface/blob ``` -------------------------------- ### Install @huggingface/gguf Source: https://github.com/huggingface/huggingface.js/blob/main/packages/gguf/README.md Install the package using npm. ```bash npm install @huggingface/gguf ``` -------------------------------- ### Install Transformers.js Package Source: https://github.com/huggingface/huggingface.js/blob/main/packages/jinja/README.md Install the Transformers.js package for chat template rendering. ```sh npm i @huggingface/transformers ``` -------------------------------- ### Install sentence-transformers Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/feature-extraction/about.md Installs the sentence-transformers library, which is useful for training similarity and search models. ```bash pip install -U sentence-transformers ``` -------------------------------- ### Build Command Example Source: https://github.com/huggingface/huggingface.js/blob/main/CONTRIBUTING.md A common build command for new packages, using tsup for bundling and TypeScript for declarations. ```bash tsup src/index.ts --format cjs,esm --clean && tsc --emitDeclarationOnly --declaration ``` -------------------------------- ### Install Hugging Face Agents.js Source: https://github.com/huggingface/huggingface.js/blob/main/packages/agents/README.md Install the @huggingface/agents package using pnpm, npm, or yarn. ```bash pnpm add @huggingface/agents ``` ```bash npm add @huggingface/agents ``` ```bash yarn add @huggingface/agents ``` -------------------------------- ### Install Hugging Face Inference Client (Deno) Source: https://github.com/huggingface/huggingface.js/blob/main/packages/inference/README.md Import the client library in Deno using esm.sh or npm specifiers. ```typescript // esm.sh import { InferenceClient } from "https://esm.sh/@huggingface/inference"; // or npm: import { InferenceClient } from "npm:@huggingface/inference"; ``` -------------------------------- ### Install @huggingface/hub with pnpm, npm, or yarn Source: https://github.com/huggingface/huggingface.js/blob/main/packages/hub/README.md Install the @huggingface/hub package using your preferred Node.js package manager. ```bash pnpm add @huggingface/hub ``` ```bash npm add @huggingface/hub ``` ```bash yarn add @huggingface/hub ``` -------------------------------- ### Install and Use gguf-view Globally Source: https://github.com/huggingface/huggingface.js/blob/main/packages/gguf/README.md Install the @huggingface/gguf package globally to access the `gguf-view` command-line tool for inspecting local GGUF files. ```bash npm i -g @huggingface/gguf gguf-view my_model.gguf ``` -------------------------------- ### Image-to-Image Inference with Flux2Pipeline Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-image/about.md Example of using Flux2Pipeline for image generation based on a text prompt. Ensure you have the diffusers library installed and a compatible CUDA device for optimal performance. ```python import torch from diffusers import Flux2Pipeline from diffusers.utils import load_image repo_id = "black-forest-labs/FLUX.2-dev" device = "cuda:0" torch_dtype = torch.bfloat16 pipe = Flux2Pipeline.from_pretrained( repo_id, torch_dtype=torch_dtype ) pipe.enable_model_cpu_offload() #no need to do cpu offload for >80G VRAM carts like H200, B200, etc. and do a `pipe.to(device)` instead prompt = "Realistic macro photograph of a hermit crab using a soda can as its shell, partially emerging from the can, captured with sharp detail and natural colors, on a sunlit beach with soft shadows and a shallow depth of field, with blurred ocean waves in the background. The can has the text `BFL Diffusers` on it and it has a color gradient that start with #FF5733 at the top and transitions to #33FF57 at the bottom." #cat_image = load_image("https://huggingface.co/spaces/zerogpu-aoti/FLUX.1-Kontext-Dev-fp8-dynamic/resolve/main/cat.png") image = pipe( prompt=prompt, #image=[cat_image] #multi-image input generator=torch.Generator(device=device).manual_seed(42), num_inference_steps=50, guidance_scale=4, ).images[0] image.save("flux2_output.png") ``` -------------------------------- ### Install Hugging Face Inference Client (yarn) Source: https://github.com/huggingface/huggingface.js/blob/main/packages/inference/README.md Install the client library using yarn. yarn is another popular package manager for Node.js. ```bash yarn add @huggingface/inference ``` -------------------------------- ### Start Svelte Development Server Source: https://github.com/huggingface/huggingface.js/blob/main/e2e/svelte/README.md Run the development server to see your Svelte application in action. The --open flag will automatically open the app in your browser. ```bash npm run dev # or start the server and open the app in a new browser tab npm run dev -- --open ``` -------------------------------- ### Install Languages.js with npm Source: https://github.com/huggingface/huggingface.js/blob/main/packages/languages/README.md Install the Languages.js package using the npm package manager. ```console npm add @huggingface/languages ``` -------------------------------- ### Zero-Shot Prompt Example Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/zero-shot-classification/about.md An example of a prompt for zero-shot text classification, demonstrating how to instruct the model to categorize input text. ```text Classify the following input text into one of the following three categories: [positive, negative, neutral] Input Text: Hugging Face is awesome for making all of these state of the art models available! Sentiment: positive ``` -------------------------------- ### Install @huggingface/hub via NPM Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Install the @huggingface/hub library using NPM. This command is used to add the library to your project's dependencies. ```bash npm install @huggingface/hub ``` -------------------------------- ### Install Space Header with npm, pnpm, or yarn Source: https://github.com/huggingface/huggingface.js/blob/main/packages/space-header/README.md Install the @huggingface/space-header package using your preferred package manager. ```bash pnpm add @huggingface/space-header ``` ```bash npm add @huggingface/space-header ``` ```bash yarn add @huggingface/space-header ``` -------------------------------- ### Install Hugging Face Inference Client (pnpm) Source: https://github.com/huggingface/huggingface.js/blob/main/packages/inference/README.md Install the client library using pnpm. pnpm is an alternative package manager for Node.js. ```bash pnpm add @huggingface/inference ``` -------------------------------- ### Install Languages.js with yarn Source: https://github.com/huggingface/huggingface.js/blob/main/packages/languages/README.md Install the Languages.js package using the yarn package manager. ```console yarn add @huggingface/languages ``` -------------------------------- ### Install @huggingface/mcp-client via NPM Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Install the @huggingface/mcp-client library using NPM. This command is used to add the library to your project's dependencies. ```bash npm install @huggingface/mcp-client ``` -------------------------------- ### Example Feature Output Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-feature-extraction/about.md This is an example of the numerical output representing extracted image features. ```text '[[[0.21236686408519745, 1.0919708013534546, 0.8512550592422485, ...]]}' ``` -------------------------------- ### Install Languages.js with pnpm Source: https://github.com/huggingface/huggingface.js/blob/main/packages/languages/README.md Install the Languages.js package using the pnpm package manager. ```console pnpm add @huggingface/languages ``` -------------------------------- ### Install Dependencies with pnpm Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Install project dependencies using pnpm, including enabling corepack for package manager management. ```console sudo corepack enable pnpm install ``` -------------------------------- ### Install DDUF with pnpm, npm, or yarn Source: https://github.com/huggingface/huggingface.js/blob/main/packages/dduf/README.md Install the @huggingface/dduf package using your preferred Node.js package manager. ```console pnpm add @huggingface/dduf ``` ```console npm add @huggingface/dduf ``` ```console yarn add @huggingface/dduf ``` -------------------------------- ### Image-Text-to-Video Generation with LTXImageToVideoPipeline Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-video/about.md Inference example using LTXImageToVideoPipeline to generate a video from an image and a text prompt. Requires the Diffusers library and a CUDA-enabled GPU. ```python import torch from diffusers import LTXImageToVideoPipeline from diffusers.utils import export_to_video, load_image pipe = LTXImageToVideoPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16) pipe.to("cuda") image = load_image( "https://huggingface.co/datasets/a-r-r-o-w/tiny-meme-dataset-captioned/resolve/main/images/8.png" ) prompt = "A young girl stands calmly in the foreground, looking directly at the camera, as a house fire rages in the background. Flames engulf the structure, with smoke billowing into the air. Firefighters in protective gear rush to the scene, a fire truck labeled '38' visible behind them. The girl's neutral expression contrasts sharply with the chaos of the fire, creating a poignant and emotionally charged scene." negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted" video = pipe( image=image, prompt=prompt, negative_prompt=negative_prompt, width=704, height=480, num_frames=161, num_inference_steps=50, ).frames[0] export_to_video(video, "output.mp4", fps=24) ``` -------------------------------- ### Style Transfer with Text-to-Image Pipeline and IP-Adapter Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-to-image/about.md Utilize AutoPipelineForText2Image with IP-Adapter for style transfer. This example loads a base model, an IP-adapter, sets adapter scales, and then generates an image based on a text prompt and a style reference image. ```python from diffusers import AutoPipelineForText2Image from diffusers.utils import load_image import torch # load pipeline pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda") pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin") # set the adapter and scales - this is a component that lets us add the style control from an image to the text-to-image model scale = { "down": {"block_2": [0.0, 1.0]}, "up": {"block_0": [0.0, 1.0, 0.0]}, } pipeline.set_ip_adapter_scale(scale) style_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg") generator = torch.Generator(device="cpu").manual_seed(26) image = pipeline( prompt="a cat, masterpiece, best quality, high quality", ip_adapter_image=style_image, negative_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry", guidance_scale=5, num_inference_steps=30, generator=generator, ).images[0] image ``` -------------------------------- ### Install @huggingface/inference via NPM Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Install the @huggingface/inference library using NPM. This command is used to add the library to your project's dependencies. ```bash npm install @huggingface/inference ``` -------------------------------- ### Video-to-Text Inference Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/video-text-to-text/about.md This snippet shows how to load a video-language model (LLaVA-NeXT-Video-7B-hf), sample frames from a video URL, format a conversation prompt, process the video and text inputs, and generate a text response. Ensure OpenCV is installed (`pip install opencv-python`). ```python import uuid import requests import cv2 import torch from transformers import LlavaNextVideoProcessor, LlavaNextVideoForConditionalGeneration from PIL import Image device = "cuda" if torch.cuda.is_available() else "cpu" model_id = "llava-hf/LLaVA-NeXT-Video-7B-hf" model = LlavaNextVideoForConditionalGeneration.from_pretrained( model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True, ).to(device) processor = LlavaNextVideoProcess ``` -------------------------------- ### Audio Question Answering with Qwen2-Audio-Instruct Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/audio-text-to-text/about.md This snippet shows how to set up and use the Qwen2-Audio-7B-Instruct model for a conversational task involving both audio and text inputs. Ensure you have the necessary libraries installed and the model is accessible. ```python from io import BytesIO from urllib.request import urlopen import librosa from transformers import Qwen2AudioForConditionalGeneration, AutoProcessor processor = AutoProcessor.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct") model = Qwen2AudioForConditionalGeneration.from_pretrained("Qwen/Qwen2-Audio-7B-Instruct", device_map="auto") conversation = [ {'role': 'system', 'content': 'You are a helpful assistant.'}, {"role": "user", "content": [ {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/glass-breaking-151256.mp3"}, {"type": "text", "text": "What's that sound?"}, ]}, {"role": "assistant", "content": "It is the sound of glass shattering."}, {"role": "user", "content": [ {"type": "text", "text": "What can you do when you hear that?"}, ]}, {"role": "assistant", "content": "Stay alert and cautious, and check if anyone is hurt or if there is any damage to property."}, {"role": "user", "content": [ {"type": "audio", "audio_url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/1272-128104-0000.flac"}, {"type": "text", "text": "What does the person say?"}, ]}, ] text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False) audios = [] for message in conversation: if isinstance(message["content"], list): for ele in message["content"]: if ele["type"] == "audio": audios.append( librosa.load( BytesIO(urlopen(ele['audio_url']).read()), sr=processor.feature_extractor.sampling_rate)[0] ) inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True) inputs.input_ids = inputs.input_ids.to("cuda") generate_ids = model.generate(**inputs, max_length=256) generate_ids = generate_ids[:, inputs.input_ids.size(1):] response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] ``` -------------------------------- ### Inference with Qwen2.5-Omni-7B Model Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/any-to-any/about.md This snippet shows how to load the Qwen2.5-Omni-7B model and processor, prepare a multimodal conversation with video and text inputs, and perform inference to generate both text and audio responses. The generated audio is saved to an output file. Ensure you have the necessary libraries like `soundfile` and `transformers` installed. ```python import soundfile as sf from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor model = Qwen2_5OmniForConditionalGeneration.from_pretrained( "Qwen/Qwen2.5-Omni-7B", torch_dtype="auto", device_map="auto", attn_implementation="flash_attention_2", ) processor = Qwen2_5OmniProcessor.from_pretrained("Qwen/Qwen2.5-Omni-7B") conversation = [ { "role": "system", "content": [ {"type": "text", "text": "You are Qwen, a virtual human developed by the Qwen Team, Alibaba Group, capable of perceiving auditory and visual inputs, as well as generating text and speech."} ], }, { "role": "user", "content": [ {"type": "video", "video": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-Omni/draw.mp4"}, {"type": "text", "text": "What can you hear and see in this video?"}, ], }, ] inputs = processor.apply_chat_template( conversation, load_audio_from_video=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", video_fps=2, # kwargs to be passed to `Qwen2-5-OmniProcessor` padding=True, use_audio_in_video=True, ) # Inference: Generation of the output text and audio text_ids, audio = model.generate(**inputs, use_audio_in_video=True) text = processor.batch_decode(text_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) print(text) sf.write( "output.wav", audio.reshape(-1).detach().cpu().numpy(), samplerate=24000, ) ``` -------------------------------- ### Audio Classification with huggingface.js Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/audio-classification/about.md Perform audio classification using the huggingface.js library. This example demonstrates how to use the InferenceClient to classify an audio file. ```javascript import { InferenceClient } from "@huggingface/inference"; const inference = new InferenceClient(HF_TOKEN); await inference.audioClassification({ data: await (await fetch("sample.flac")).blob(), model: "facebook/mms-lid-126", }); ``` -------------------------------- ### Video Costume Editing with Lucy-Edit-Dev Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/video-to-video/about.md Demonstrates how to use the Lucy-Edit-Dev pipeline for video costume editing. Requires installation of torch and diffusers. The video is loaded, processed, and then edited based on a text prompt. ```python !pip install torch diffusers import torch from PIL import Image from diffusers import AutoencoderKLWan, LucyEditPipeline from diffusers.utils import export_to_video, load_video url = "https://d2drjpuinn46lb.cloudfront.net/painter_original_edit.mp4" prompt = "Change the apron and blouse to a classic clown costume: satin polka-dot jumpsuit in bright primary colors, ruffled white collar, oversized pom-pom buttons, white gloves, oversized red shoes, red foam nose; soft window light from left, eye-level medium shot, natural folds and fabric highlights." negative_prompt = "" num_frames = 81 height = 480 width = 832 def convert_video(video: List[Image.Image]) -> List[Image.Image]: video = load_video(url)[:num_frames] video = [video[i].resize((width, height)) for i in range(num_frames)] return video video = load_video(url, convert_method=convert_video) model_id = "decart-ai/Lucy-Edit-Dev" vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32) pipe = LucyEditPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) pipe.to("cuda") output = pipe( prompt=prompt, video=video, negative_prompt=negative_prompt, height=480, width=832, num_frames=81, guidance_scale=5.0 ).frames[0] export_to_video(output, "output.mp4", fps=24) ``` -------------------------------- ### Configure Agent with Custom Model Source: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/README.md Example of setting environment variables to use a specific model ID for the agent's inference. Ensure HF_TOKEN is also set. ```bash export HF_TOKEN="hf_..." export MODEL_ID="Qwen/Qwen2.5-72B-Instruct" pnpm agent ``` -------------------------------- ### Text-to-Image Generation with Inference API Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Generate an image from a text prompt using the @huggingface/inference library. This example demonstrates using the 'replicate' provider for text-to-image models. ```typescript await inference.textToImage({ model: "black-forest-labs/FLUX.1-dev", provider: "replicate", inputs: "a picture of a green bird", }); ``` -------------------------------- ### Text Classification Inference Example Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/text-classification/about.md Demonstrates how to use the text-classification pipeline from the transformers library to assess grammatical acceptability. This snippet requires the transformers library to be installed. ```python from transformers import pipeline classifier = pipeline("text-classification", model = "textattack/distilbert-base-uncased-CoLA") classifier("I will walk to home when I went through the bus.") ## [{'label': 'unacceptable', 'score': 0.95}] ``` -------------------------------- ### Image-to-Video Inference with Wan 2.1 T2V 1.3B Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-to-video/about.md Run inference using the Wan 2.1 T2V 1.3B model to generate a video from a text prompt. Ensure you have the diffusers library installed and a CUDA-enabled GPU for optimal performance. The output video is saved as 'output.mp4'. ```python import torch from diffusers import AutoencoderKLWan, WanPipeline from diffusers.utils import export_to_video model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers" vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32) pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) pipe.to("cuda") prompt = "A cat walks on the grass, realistic" negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards" output = pipe( prompt=prompt, negative_prompt=negative_prompt, height=480, width=832, num_frames=81, guidance_scale=5.0 ).frames[0] export_to_video(output, "output.mp4", fps=15) ``` -------------------------------- ### Text Generation with a Specific Provider Source: https://github.com/huggingface/huggingface.js/blob/main/packages/inference/README.md Example of performing text generation using a specified provider (e.g., 'together'). This snippet shows how to configure the model, provider, and input prompt. ```typescript await hf.textGeneration({ model: 'mistralai/Mixtral-8x7B-v0.1', provider: "together", inputs: 'The answer to the universe is' }) ``` -------------------------------- ### Feature Extraction with Transformers Pipeline Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/feature-extraction/about.md Infers feature extraction models using the `pipeline` from the transformers library. This example shows how to get embeddings for a given text. ```python from transformers import pipeline checkpoint = "facebook/bart-base" feature_extractor = pipeline("feature-extraction", framework="pt", model=checkpoint) text = "Transformers is an awesome library!" #Reducing along the first dimension to get a 768 dimensional array feature_extractor(text,return_tensors = "pt")[0].numpy().mean(axis=0) ``` -------------------------------- ### Create a local agent configuration directory Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tiny-agents/README.md Set up a new directory and an agent.json file for a custom agent. ```bash mkdir my-agent touch my-agent/agent.json ``` -------------------------------- ### Document Question Answering Inference Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/document-question-answering/about.md Perform inference with a Document Question Answering pipeline. This snippet shows how to load the pipeline, provide an image and a question, and get the extracted answer. Ensure you have the 'transformers' and 'Pillow' libraries installed. ```python from transformers import pipeline from PIL import Image pipe = pipeline("document-question-answering", model="naver-clova-ix/donut-base-finetuned-docvqa") question = "What is the purchase amount?" image = Image.open("your-document.png") pipe(image=image, question=question) ## [{'answer': '20,000$'}] ``` -------------------------------- ### Strictly Typed Metadata Example Source: https://github.com/huggingface/huggingface.js/blob/main/packages/gguf/README.md Demonstrates runtime type checking for known metadata fields based on model architecture. This helps ensure correct access to architecture-specific parameters. ```typescript const { metadata, tensorInfos } = await gguf(URL_MODEL); // Type check for model architecture at runtime if (metadata["general.architecture"] === "llama") { // "llama.attention.head_count" is a valid key for llama architecture, this is typed as a number console.log(model["llama.attention.head_count"]); // "mamba.ssm.conv_kernel" is an invalid key, because it requires model architecture to be mamba console.log(model["mamba.ssm.conv_kernel"]); // error } ``` -------------------------------- ### Programmatic Agent Creation and Usage Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tiny-agents/README.md Demonstrates how to programmatically create an Agent instance with specific configurations and run a query. ```typescript import { Agent } from '@huggingface/tiny-agents'; const HF_TOKEN = "hf_..."; // Create an Agent const agent = new Agent({ provider: "auto", model: "Qwen/Qwen2.5-72B-Instruct", apiKey: HF_TOKEN, servers: [ { // Playwright MCP command: "npx", args: ["@playwright/mcp@latest"], }, ], }); await agent.loadTools(); // Use the Agent for await (const chunk of agent.run("What are the top 5 trending models on Hugging Face?")) { if ("choices" in chunk) { const delta = chunk.choices[0]?.delta; if (delta.content) { console.log(delta.content); } } } ``` -------------------------------- ### PoS Tagging with spaCy Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/token-classification/about.md Inference for Part-of-Speech tagging using a spaCy model. This example demonstrates loading a spaCy model and iterating through tokens to print their text, part of speech, dependency, and entity type. ```python !pip install https://huggingface.co/spacy/en_core_web_sm/resolve/main/en_core_web_sm-any-py3-none-any.whl import en_core_web_sm lp = en_core_web_sm.load() doc = nlp("I'm Omar and I live in Zürich.") for token in doc: print(token.text, token.pos_, token.dep_, token.ent_type_) ``` -------------------------------- ### Image-to-Image Inference with Stable Diffusion XL Refiner Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-to-image/about.md Use StableDiffusionImg2ImgPipeline for image-to-image tasks. This example demonstrates preparing an initial image and passing it to the pipeline with a text prompt for generation. Model CPU offloading is enabled to reduce memory usage. ```python import torch from diffusers import AutoPipelineForImage2Image from diffusers.utils import make_image_grid, load_image pipeline = AutoPipelineForImage2Image.from_pretrained( "stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) # this helps us to reduce memory usage- since SDXL is a bit heavy, this could help by # offloading the model to CPU w/o hurting performance. pipeline.enable_model_cpu_offload() # prepare image url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-sdxl-init.png" init_image = load_image(url) prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" # pass prompt and image to pipeline image = pipeline(prompt, image=init_image, strength=0.5).images[0] make_image_grid([init_image, image], rows=1, cols=2) ``` -------------------------------- ### Tiny Agents CLI Usage Overview Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tiny-agents/README.md Provides an overview of available commands for the tiny-agents CLI, including 'run' and 'serve'. ```bash Usage: tiny-agents [flags] tiny-agents run "agent/id" tiny-agents serve "agent/id" Available Commands: run Run the Agent in command-line serve Run the Agent as an OpenAI-compatible HTTP server ``` -------------------------------- ### Format Codebase Source: https://github.com/huggingface/huggingface.js/blob/main/CONTRIBUTING.md Run this command from the root of the project to format the entire codebase. ```bash pnpm -r format ``` -------------------------------- ### Generate All Docs Source: https://github.com/huggingface/huggingface.js/blob/main/packages/doc-internal/README.md Run this command to generate all markdown documentation files for the huggingface.js library. ```console pnpm run start ``` -------------------------------- ### Run Gymnasium Environment with Random Actions Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/reinforcement-learning/about.md This snippet demonstrates how to initialize a Gymnasium environment and run a specified number of episodes, taking random actions. ```python import gymnasium as gym env = gym.make("CartPole-v1") observation, info = env.reset() for _ in range(20): action = env.action_space.sample() # samples random action from action sample space # the agent takes the action observation, reward, terminated, truncated, info = env.step(action) # if the agent reaches terminal state, we reset the environment if terminated or truncated: print("Environment is reset") observation = env.reset() env.close() ``` -------------------------------- ### Build Package Source: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/README.md Build the MCP client package using the pnpm build script. ```bash pnpm build ``` -------------------------------- ### Example Output of Top Classification Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/zero-shot-image-classification/about.md This is an example of the printed output showing the highest classification score and its corresponding label. ```text The highest probability is 0.995 for the label cat and dog ``` -------------------------------- ### Initialize and Push Model Repository Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/tabular-classification/about.md Initialize a local repository with a trained model and push it to the Hugging Face Hub using skops. ```python from skops import hub_utils # initialize a repository with a trained model local_repo = "/path_to_new_repo" hub_utils.init(model, dst=local_repo) # push to Hub! hub_utils.push("username/my-awesome-model", source=local_repo) ``` -------------------------------- ### Preview HTML Docs Source: https://github.com/huggingface/huggingface.js/blob/main/packages/doc-internal/README.md Use this command within the cloned doc-builder repository to preview the generated HTML documentation for huggingface.js. ```console # Inside the doc-builder folder doc-builder preview huggingface.js ../huggingface.js/docs --not_python_module ``` -------------------------------- ### Semantic Textual Similarity with Sentence Transformers Library Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/sentence-similarity/about.md Compute cosine similarity between sentence embeddings using the Sentence Transformers library. Install with `pip install -U sentence-transformers`. ```python pip install -U sentence-transformers from sentence_transformers import SentenceTransformer, util sentences = ["I'm happy", "I'm full of happiness"] model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') # Compute embedding for both lists embedding_1 = model.encode(sentences[0], convert_to_tensor=True) embedding_2 = model.encode(sentences[1], convert_to_tensor=True) util.pytorch_cos_sim(embedding_1, embedding_2) ``` -------------------------------- ### Interacting with Gymnasium Environment Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/reinforcement-learning/about.md This snippet demonstrates how to interact with a reinforcement learning environment using the gymnasium library. It covers resetting the environment, taking actions, and observing the results. ```python import gymnasium as gym env = gym.make("CartPole-v1", render_mode="rgb_array") observation, info = env.reset() for _ in range(1000): action = env.action_space.sample() # agent policy and action selection logic observation, reward, terminated, truncated, info = env.step(action) if terminated or truncated: observation, info = env.reset() env.close() ``` -------------------------------- ### Perform Visual Question Answering Inference Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/visual-question-answering/about.md Inference with Visual Question Answering models using the `transformers` library. Requires PIL for image processing. Install with `pip install pillow`. ```python from PIL import Image from transformers import pipeline vqa_pipeline = pipeline("visual-question-answering") image = Image.open("elephant.jpeg") question = "Is there an elephant?" vqa_pipeline(image, question, top_k=1) #[{'score': 0.9998154044151306, 'answer': 'yes'}] ``` -------------------------------- ### Run Tests Source: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/README.md Execute the test suite using Vitest. ```bash pnpm test ``` -------------------------------- ### Table Question Answering Inference Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/table-question-answering/about.md Infers an answer to a question based on a pandas DataFrame. Ensure you have installed torch-scatter. ```python from transformers import pipeline import pandas as pd # prepare table + question data = {"Actors": ["Brad Pitt", "Leonardo Di Caprio", "George Clooney"], "Number of movies": ["87", "53", "69"]} table = pd.DataFrame.from_dict(data) question = "how many movies does Leonardo Di Caprio have?" # pipeline model # Note: you must to install torch-scatter first. tqa = pipeline(task="table-question-answering", model="google/tapas-large-finetuned-wtq") # result print(tqa(table=table, query=question)['cells'][0]) #53 ``` -------------------------------- ### Run MCP Agent (Package Directory) Source: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/README.md Navigate to the 'packages/mcp-client' directory and execute the agent script using pnpm. ```bash cd packages/mcp-client pnpm agent ``` -------------------------------- ### Run an agent from the Hugging Face Hub Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tiny-agents/README.md Execute an agent hosted on the Hugging Face Hub by providing its agent ID. ```bash npx @huggingface/tiny-agents run "julien-c/flux-schnell-generator" ``` -------------------------------- ### resetHash Method Source: https://github.com/huggingface/huggingface.js/blob/main/packages/gearhash-jit/README.md Resets the rolling hash state to zero. This method should be called when starting to process a new chunk of data. ```APIDOC ## `hasher.resetHash(): void` ### Description Reset the rolling hash to zero (call when starting a new chunk). ``` -------------------------------- ### Synthesize Speech with Transformers Pipeline Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/text-to-speech/about.md This code synthesizes speech directly using the Transformers library. It requires the 'transformers' library to be installed. ```python from transformers import pipeline synthesizer = pipeline("text-to-speech", "suno/bark") synthesizer("Look I am generating speech in three lines of code!") ``` -------------------------------- ### Build Project with pnpm Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Build all packages in the project using pnpm, generating ESM and CJS output files. ```console pnpm -r build ``` -------------------------------- ### Format Code Source: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/README.md Format the codebase using Prettier. ```bash pnpm format ``` -------------------------------- ### Python Image Segmentation Inference Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-segmentation/about.md Perform image segmentation using the 'image-segmentation' pipeline in Python. Requires the 'timm' library to be installed. ```python !pip install timm model = pipeline("image-segmentation") model("cat.png") #[{'label': 'cat', # 'mask': mask_code, # 'score': 0.999} # ...] ``` -------------------------------- ### Command Recognition with Speechbrain Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/audio-classification/about.md Classify audio files into predefined commands using a Speechbrain model. Ensure you have the Speechbrain library installed. ```python from speechbrain.pretrained import EncoderClassifier model = EncoderClassifier.from_hparams( "speechbrain/google_speech_command_xvector" ) model.classify_file("file.wav") ``` -------------------------------- ### Initialize Image-to-Text Pipeline Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-text/about.md Initialize the `image-text-to-text` pipeline with a specific model. This is the first step for performing inference. ```python from transformers import pipeline pipe = pipeline("image-text-to-text", model="llava-hf/llava-interleave-qwen-0.5b-hf") ``` -------------------------------- ### CLI Usage for Uploading Files and Branches Source: https://github.com/huggingface/huggingface.js/blob/main/packages/hub/README.md Utilize the command-line interface to upload files and manage branches for repositories. Supports specifying repository type and revision. ```bash npx @huggingface/hub upload coyotte508/test-model . npx @huggingface/hub upload datasets/coyotte508/test-dataset . # Same thing npx @huggingface/hub upload --repo-type dataset coyotte508/test-dataset . # Upload new data with 0 history in a separate branch npx @huggingface/hub branch create coyotte508/test-model release --empty npx @huggingface/hub upload coyotte508/test-model . --revision release npx @huggingface/hub --help npx @huggingface/hub upload --help ``` ```bash hfjs upload coyotte508/test-model . hfjs branch create --repo-type dataset coyotte508/test-dataset release --empty hfjs upload --repo-type dataset coyotte508/test-dataset . --revision release hfjs --help hfjs upload --help hfjs help jobs ``` -------------------------------- ### Sentence Embeddings with Sentence-Transformers Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/feature-extraction/about.md Infers sentence embeddings using the `sentence-transformers` library. This example demonstrates encoding sentences and calculating their similarity. ```python from sentence_transformers import SentenceTransformer model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium.", ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities) # tensor([[1.0000, 0.6660, 0.1046], # [0.6660, 1.0000, 0.1411], # [0.1046, 0.1411, 1.0000]]) ``` -------------------------------- ### Perform Image-to-Text Inference Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-text/about.md Pass formatted messages to the pipeline for inference. Set `return_full_text=False` to get only the generated text, excluding the prompt. ```python outputs = pipe(text=messages, max_new_tokens=60, return_full_text=False) outputs[0]["generated_text"] ``` -------------------------------- ### Train PPO Model with Stable-Baselines3 Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/reinforcement-learning/about.md This snippet shows how to initialize a PPO model from Stable-Baselines3, train it on the LunarLander-v2 environment for a specified number of timesteps, and save the trained model. ```python from stable_baselines3 import PPO # initialize the environment env = gym.make("LunarLander-v2") # initialize the model model = PPO(policy = "MlpPolicy", env = env, n_steps = 1024, batch_size = 64, n_epochs = 4, verbose = 1) # train the model for 1000 time steps model.learn(total_timesteps = 1000) # Saving the model in desired directory model_name = "PPO-LunarLander-v2" model.save(model_name) ``` -------------------------------- ### Load Zero-Shot Image Classification Pipeline Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/zero-shot-image-classification/about.md Load the zero-shot image classification pipeline with a specified model. Ensure you have the 'transformers' library installed. ```python from transformers import pipeline # More models in the model hub. model_name = "openai/clip-vit-large-patch14-336" classifier = pipeline("zero-shot-image-classification", model = model_name) ``` -------------------------------- ### Speech Enhancement with Speechbrain Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/audio-to-audio/about.md Use this snippet to enhance audio quality by removing noise using the Speechbrain library. Ensure you have Speechbrain installed. ```python from speechbrain.pretrained import SpectralMaskEnhancement model = SpectralMaskEnhancement.from_hparams( "speechbrain/mtl-mimic-voicebank" ) model.enhance_file("file.wav") ``` -------------------------------- ### Create and Upload to Hugging Face Hub Repository Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Programmatically create a new repository and upload a file to it using the @huggingface/hub library. Requires an access token and specifies the repository type and name. ```typescript await createRepo({ repo: { type: "model", name: "my-user/nlp-model" }, accessToken: HF_TOKEN }); await uploadFile({ repo: "my-user/nlp-model", accessToken: HF_TOKEN, // Can work with native File in browsers file: { path: "pytorch_model.bin", content: new Blob(...) } }); ``` -------------------------------- ### Run a local agent from a directory Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tiny-agents/README.md Execute a locally defined agent using the tiny-agents CLI, pointing to the agent's directory. ```bash npx @huggingface/tiny-agents run ./my-agent ``` -------------------------------- ### Streaming Data with Hasher Source: https://github.com/huggingface/huggingface.js/blob/main/packages/gearhash-jit/README.md Scan data in pieces by carrying over the hash state between `nextMatch` calls. Reset the hasher when starting a new chunk. ```typescript const hasher = new Hasher(mask); for (const chunk of dataSource) { const pos = hasher.nextMatch(chunk); if (pos !== -1) { // Found boundary at `pos` within this chunk hasher.resetHash(); } } ``` -------------------------------- ### Generate Image Caption Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-to-text/about.md Use the `image-to-text` pipeline from the Transformers library to generate a caption for a given image URL. Requires the `transformers` library to be installed. ```python from transformers import pipeline captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") captioner("https://huggingface.co/datasets/Narsil/image_dummy/resolve/main/parrots.png") ## [{\"generated_text\": \"two birds are standing next to each other \"}] ``` -------------------------------- ### Launch Gradio UI for Model Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/tabular-classification/about.md Launch a user interface for a Hugging Face Hub model with a single line of code using Gradio. ```python import gradio as gr gr.Interface.load("huggingface/username/my-awesome-model").launch() ``` -------------------------------- ### Tabular Classification Example Source: https://github.com/huggingface/huggingface.js/blob/main/packages/inference/README.md Performs tabular classification using a specified model and input data. Ensure the model is suitable for tabular classification tasks. ```typescript await hf.tabularClassification({ model: "vvmnnnkv/wine-quality", inputs: { data: { "fixed_acidity": ["7.4", "7.8", "10.3"], "volatile_acidity": ["0.7", "0.88", "0.32"], "citric_acid": ["0", "0", "0.45"], "residual_sugar": ["1.9", "2.6", "6.4"], "chlorides": ["0.076", "0.098", "0.073"], "free_sulfur_dioxide": ["11", "25", "5"], "total_sulfur_dioxide": ["34", "67", "13"], "density": ["0.9978", "0.9968", "0.9976"], "pH": ["3.51", "3.2", "3.23"], "sulphates": ["0.56", "0.68", "0.82"], "alcohol": ["9.4", "9.8", "12.6"] }, }, }) ``` -------------------------------- ### Sending Text-to-Image Request to Replicate Provider Source: https://github.com/huggingface/huggingface.js/blob/main/packages/inference/README.md Example of sending a text-to-image request to the Replicate provider using the InferenceClient. Ensure you have the correct access token for the provider. ```typescript const accessToken = "hf_..."; // Either a HF access token, or an API key from the third-party provider (Replicate in this example) const client = new InferenceClient(accessToken); await client.textToImage({ provider: "replicate", model:"black-forest-labs/Flux.1-dev", inputs: "A black forest cake" }) ``` -------------------------------- ### Run Formatting and Linting Checks Source: https://github.com/huggingface/huggingface.js/blob/main/README.md Execute formatting and linting checks across all packages using pnpm. ```console pnpm -r format:check pnpm -r lint:check ``` -------------------------------- ### One-Shot Chunking and Hashing Source: https://github.com/huggingface/huggingface.js/blob/main/packages/xetchunk-wasm/README.md Process an entire Uint8Array at once to get all chunks, their XORB hash, and file hash. Useful for smaller data buffers where streaming is not necessary. ```typescript import { createChunker, nextBlock, finalize, getChunks, hashToHex, xorbHash, fileHash } from '@huggingface/xetchunk-wasm'; // One-shot: chunk all data at once const data = new Uint8Array(1_000_000); const chunks = getChunks(data); console.log(`${chunks.length} chunks`); console.log('xorb hash:', hashToHex(xorbHash(chunks))); console.log('file hash:', hashToHex(fileHash(chunks))); ``` -------------------------------- ### Inference API Example with Curl Source: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-text/about.md Use the Hugging Face Inference API to test image-text-to-text models via curl. Requires a Hugging Face token for authentication. ```bash curl https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct \ -X POST \ -d '{"messages": [{"role": "user","content": [{"type": "image"}, {"type": "text", "text": "Can you describe the image?"}]}]}' \ -H "Content-Type: application/json" \ -H "Authorization: Bearer hf_***" ```