### Install Pixeltable SDK Source: https://docs.pixeltable.com/ Installs the Pixeltable Python SDK using pip. This is the first step to get started with Pixeltable. ```bash pip install pixeltable ``` -------------------------------- ### Install Pixeltable and Clone Repository (Python) Source: https://github.com/pixeltable/pixeltable-mcp-server Installs the Pixeltable Python package and clones the MCP server repository for local development. It then navigates into the cloned directory and starts the services using Docker Compose. ```bash pip install pixeltable git clone https://github.com/pixeltable/mcp-server-pixeltable.git cd mcp-server-pixeltable/ servers docker-compose up --build # Run locally with docker-compose docker-compose down # Take down resources ``` -------------------------------- ### Verify PyTorch Installation and Tensor Creation Source: https://pytorch.org/get-started/locally This Python code snippet verifies a successful PyTorch installation by creating a randomly initialized tensor and printing it. It's a common first step to ensure PyTorch is working correctly. ```python import torch x = torch.rand(5, 3) print(x) ``` -------------------------------- ### Install PyTorch via pip for Python 3.x Source: https://pytorch.org/get-started/locally Installs PyTorch and TorchVision using pip for Python versions 3.x. Assumes Python and pip are already installed; use pip3 if needed. Works on macOS, Linux, or Windows without specific CUDA; no GPU acceleration. ```bash # Python 3.x pip3 install torch torchvision ``` -------------------------------- ### Installing Python on Windows with Chocolatey Source: https://pytorch.org/get-started/locally This command installs Python using the Chocolatey package manager on Windows. It requires running an administrative command prompt and supports Python 3.9-3.13. Pip is included automatically for subsequent PyTorch installation. ```powershell choco install python ``` -------------------------------- ### Install Dependencies for Pixeltable Voxel51 Integration Source: https://docs.pixeltable.com/examples/vision/voxel51 This command installs the required Python packages pixeltable, fiftyone, and transformers using pip. It sets up the environment for running Pixeltable workflows with Voxel51. Requires a Python environment with pip. No inputs or outputs during execution; it is a one-time setup. ```bash pip install pixeltable fiftyone transformers ``` -------------------------------- ### Verify PyTorch Installation in Python Source: https://pytorch.org/get-started/locally Verifies PyTorch installation by creating and printing a random tensor. Requires PyTorch to be installed; runs in Python environment. Outputs a 5x3 tensor with random float values; fails if PyTorch is not properly installed. ```python import torch x = torch.rand(5, 3) print(x) ``` -------------------------------- ### Install Label Studio SDK using Pip and Poetry Source: https://labelstud.io/sdk/project.html Installs the Label Studio SDK. Supports both pip and Poetry package managers. Ensure you have Python and pip/Poetry installed. ```bash pip install label-studio-sdk ``` ```bash poetry add label-studio-sdk ``` -------------------------------- ### Transcribe Audio with WhisperX (command line) Source: https://github.com/m-bain/whisperX Runs WhisperX on an audio file using default parameters. Requires ffmpeg and other dependencies installed. Output includes word-level timing alignment. ```bash whisperx path/to/audio.wav ``` -------------------------------- ### Install Python 3 Pip on Debian/Ubuntu Source: https://pytorch.org/get-started/locally Installs the pip package manager for Python 3 on Debian-based Linux distributions using the APT package manager. This is a prerequisite for installing PyTorch via pip. ```bash sudo apt install python3-pip ``` -------------------------------- ### Go SDK Example Source: https://ai.google.dev/gemini-api/docs/image-generation Example demonstrating image generation using the Go SDK. ```APIDOC ```go package main import ( "context" "fmt" "log" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } result, _ := client.Models.GenerateContent( ctx, "gemini-2.5-flash-image", genai.Text("Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'. The text should be in a clean, bold, sans-serif font. The design should feature a simple, stylized icon of a a coffee bean seamlessly integrated with the text. The color scheme is black and white."), ) for _, part := range result.Candidates[0].Content.Parts { if part.Text != "" { fmt.Println(part.Text) } else if part.InlineData != nil { imageBytes := part.InlineData.Data outputFilename := "logo_example.png" _ = os.WriteFile(outputFilename, imageBytes, 0644) } } } ``` ``` -------------------------------- ### Install Older Label Studio SDK Version Source: https://labelstud.io/sdk/project.html Installs a specific older version of the Label Studio SDK (e.g., less than 1.0). Use this if compatibility with older projects is required. ```bash pip install "label-studio-sdk<1" ``` -------------------------------- ### Set System Instructions in Gemini Models Source: https://ai.google.dev/gemini-api/docs/text-generation Shows how to configure system instructions for Gemini models to guide their behavior. Examples provided in multiple programming languages. ```Python from google import genai from google.genai import types client = genai.Client() response = client.models.generate_content( model="gemini-2.5-flash", config=types.GenerateContentConfig( system_instruction="You are a cat. Your name is Neko."), contents="Hello there" ) print(response.text) ``` ```JavaScript import { GoogleGenAI } from "@google/genai"; const ai = new GoogleGenAI({}); async function main() { const response = await ai.models.generateContent({ model: "gemini-2.5-flash", contents: "Hello there", config: { systemInstruction: "You are a cat. Your name is Neko.", }, }); console.log(response.text); } await main(); ``` ```Go package main import ( "context" "fmt" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } config := &genai.GenerateContentConfig{ SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", genai.RoleUser), } result, _ := client.Models.GenerateContent( ctx, "gemini-2.5-flash", genai.Text("Hello there"), config, ) fmt.Println(result.Text()) } ``` -------------------------------- ### Install YOLOX from Source (Python) Source: https://github.com/Megvii-BaseDetection/YOLOX This code snippet demonstrates how to clone the YOLOX repository and install it locally using pip. It ensures that the project is set up for development or immediate use. ```shell git clone git@github.com:Megvii-BaseDetection/YOLOX.git cd YOLOX pip3 install -v -e . # or python3 setup.py develop ``` -------------------------------- ### Python SDK Example Source: https://ai.google.dev/gemini-api/docs/image-generation Example demonstrating image generation using the Python SDK. ```APIDOC ```python from google import genai from google.genai import types from PIL import Image # Initialize the client client = genai.Client() # Generate an image from a text prompt response = client.models.generate_content( model="gemini-2.5-flash-image", contents="Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'. The text should be in a clean, bold, sans-serif font. The design should feature a simple, stylized icon of a a coffee bean seamlessly integrated with the text. The color scheme is black and white.", ) # Process the response to get image data image_parts = [part.inline_data.data for part in response.parts if part.inline_data] if image_parts: # Assuming only one image part for simplicity image = part.as_image() # Note: This might need adjustment based on actual SDK usage image.save('logo_example.png') image.show() ``` ``` -------------------------------- ### cURL Example Source: https://ai.google.dev/gemini-api/docs/image-generation Example demonstrating image generation using a cURL command. ```APIDOC ```bash curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "contents": [{"parts": [ {"text": "Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'. The text should be in a clean, bold, sans-serif font. The design should feature a simple, stylized icon of a a coffee bean seamlessly integrated with the text. The color scheme is black and white."} ] }] }' \ | grep -o '"data": "[^"]*"' \ | cut -d'"' -f4 \ | base64 --decode > logo_example.png ``` ``` -------------------------------- ### Create Fine-Tuning Job with Groq API (Node.js, Python, cURL) Source: https://console.groq.com/docs/api-reference This snippet demonstrates how to initiate a fine-tuning job using the Groq API. It requires an input file ID, a model name, a type (e.g., 'lora'), and a base model. The Node.js and Python examples use the 'groq-sdk', while the cURL example shows direct API interaction. ```cURL curl https://api.groq.com/v1/fine_tunings -s \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $GROQ_API_KEY" \ -d '{ "input_file_id": "", "name": "test-1", "type": "lora", "base_model": "llama-3.1-8b-instant" }' ``` ```Node.js import Groq from "groq-sdk"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); async function main() { const fineTunings = await groq.fine_tunings.create({ input_file_id: "", name: "test-1", type: "lora", base_model: "llama-3.1-8b-instant" }); console.log(fineTunings); } main(); ``` ```Python import os from groq import Groq client = Groq( # This is the default and can be omitted api_key=os.environ.get("GROQ_API_KEY"), ) fine_tunings = client.fine_tunings.create( input_file_id="", name="test-1", type="lora", base_model="llama-3.1-8b-instant" ) print(fine_tunings) ``` -------------------------------- ### JavaScript (Node.js) SDK Example Source: https://ai.google.dev/gemini-api/docs/image-generation Example demonstrating image generation using the JavaScript (Node.js) SDK. ```APIDOC ```javascript import { GoogleGenAI } from "@google/genai"; import * as fs from "node:fs"; async function main() { const ai = new GoogleGenAI({}); const prompt = "Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'. The text should be in a clean, bold, sans-serif font. The design should feature a simple, stylized icon of a a coffee bean seamlessly integrated with the text. The color scheme is black and white."; const response = await ai.models.generateContent({ model: "gemini-2.5-flash-image", contents: prompt, }); for (const part of response.parts) { if (part.text) { console.log(part.text); } else if (part.inlineData) { const imageData = part.inlineData.data; const buffer = Buffer.from(imageData, "base64"); fs.writeFileSync("logo_example.png", buffer); console.log("Image saved as logo_example.png"); } } } main(); ``` ``` -------------------------------- ### GET /v1/fine_tunings Source: https://console.groq.com/docs/api-reference Lists all previously created fine-tuning jobs. This endpoint is in closed beta and requires special access. Returns an array of fine-tuning configurations and their current status. ```APIDOC ## GET /v1/fine_tunings ### Description Lists all previously created fine-tuning jobs. This endpoint is in closed beta and requires contact with Groq for access. ### Method GET ### Endpoint https://api.groq.com/v1/fine_tunings ### Parameters No parameters required. ### Request Example ```json {} ``` ### Response #### Success Response (200) - **data** (array) - Array of fine-tuning objects - **object** (string) - The object type, always "list" #### Response Example ```json { "object": "list", "data": [ { "id": "string", "name": "string", "base_model": "string", "type": "string", "input_file_id": "string", "created_at": 0, "fine_tuned_model": "string" } ] } ``` ``` -------------------------------- ### Pixeltable Quick Start: Table Creation and AI Processing Source: https://github.com/pixeltable/pixeltable This example sets up a Pixeltable table with an image column, adds computed columns for object detection using Hugging Face DETR model and image description using OpenAI Vision API, inserts an image from a URL, and queries the results. It requires torch, transformers, and openai libraries for model integrations. Outputs include structured detections and unstructured vision descriptions, with automatic caching and incremental computation. ```shell pip install -qU torch transformers openai pixeltable ``` ```python import pixeltable as pxt # Table with multimodal column types (Image, Video, Audio, Document) t = pxt.create_table('images', {'input_image': pxt.Image}) # Computed columns: define transformation logic once, runs on all data from pixeltable.functions import huggingface # Object detection with automatic model management t.add_computed_column( detections=huggingface.detr_for_object_detection( t.input_image, model_id='facebook/detr-resnet-50' ) ) # Extract specific fields from detection results t.add_computed_column(detections_text=t.detections.label_text) # OpenAI Vision API integration with built-in rate limiting and async management from pixeltable.functions import openai t.add_computed_column( vision=openai.vision( prompt="Describe what's in this image.", image=t.input_image, model='gpt-4o-mini' ) ) # Insert data directly from an external URL # Automatically triggers computation of all computed columns t.insert(input_image='https://raw.github.com/pixeltable/pixeltable/release/docs/resources/images/000000000025.jpg') # Query - All data, metadata, and computed results are persistently stored # Structured and unstructured data are returned side-by-side results = t.select( t.input_image, t.detections_text, t.vision ).collect() ``` -------------------------------- ### API Parameters Configuration Example Source: https://docs.together.ai/reference/chat-completions-1 Example showing various API parameters that control text generation behavior including max tokens, temperature, stop sequences, and sampling parameters. These parameters allow fine-tuning of model output randomness and length. ```json { "model": "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", "max_tokens": 512, "temperature": 0.7, "stop": ["\n\n", "<|end|>"], "top_p": 0.9, "top_k": 50, "stream": false, "presence_penalty": 0.5, "frequency_penalty": 0.3 } ``` -------------------------------- ### Text Generation with Gemini API Source: https://ai.google.dev/gemini-api/docs/text-generation This section provides examples for generating text content using the Gemini API. It demonstrates how to send a text prompt and receive a text response. Examples are provided for Python, JavaScript, Go, Java, REST, and Apps Script. ```APIDOC ## POST /v1beta/models/gemini-2.5-flash:generateContent ### Description Generates text output from various inputs, including text, images, video, and audio, leveraging Gemini models. This example focuses on a single text input. ### Method POST ### Endpoint https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent ### Parameters #### Query Parameters - **x-goog-api-key** (string) - Required - Your Gemini API key. #### Request Body - **contents** (array) - Required - An array of content parts to send to the model. - **parts** (array) - Required - An array of content parts within a content block. - **text** (string) - Required - The text content of the part. ### Request Example (REST) ```bash curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [ { "parts": [ { "text": "How does AI work?" } ] } ] }' ``` ### Response #### Success Response (200) - **candidates** (array) - An array of candidate responses from the model. - **content** (object) - The generated content. - **parts** (array) - An array of content parts. - **text** (string) - The generated text. #### Response Example (JSON) ```json { "candidates": [ { "content": { "parts": [ { "text": "AI works by enabling computers to perform tasks that typically require human intelligence, such as learning, problem-solving, and decision-making, often through algorithms trained on large datasets." } ] } } ] } ``` ``` -------------------------------- ### Python Client Usage Source: https://docs.mistral.ai/api Example of using the Mistral AI Python client library to make chat completion requests. ```APIDOC ## Python Client Usage ### Description Using the official Mistral AI Python client library to interact with the chat completions API. ### Setup and Usage The Mistral client handles authentication and connection management automatically. ### Code Example ```python from mistralai import Mistral import os with Mistral( api_key=os.getenv("MISTRAL_API_KEY", ""), ) as mistral: res = mistral.chat.complete( model="mistral-small-latest", messages=[ { "content": "Who is the best French painter? Answer in one short sentence.", "role": "user", }, ], stream=False ) # Handle response print(res) ``` ### Response Format The response object contains the same fields as the raw API response including choices, model information, and usage statistics. ``` -------------------------------- ### Get match positions in Python regex Source: https://docs.python.org/3/library/re.html Demonstrates using start() and end() methods to get match positions, and span() to get both as a tuple. ```python >>> email = "tony@tiremove_thisger.net" >>> m = re.search("remove_this", email) >>> email[:m.start()] + email[m.end():] 'tony@tiger.net' ``` -------------------------------- ### Transcribe Audio using Groq API (Python) Source: https://console.groq.com/docs/api-reference This Python example demonstrates audio transcription via the Groq API. It opens a local audio file in binary read mode and sends it to the `client.audio.transcriptions.create` method. You can specify the model, prompt, response format, language, and temperature. The transcribed text is then printed. ```python import os from groq import Groq client = Groq() filename = os.path.dirname(__file__) + "/sample_audio.m4a" with open(filename, "rb") as file: transcription = client.audio.transcriptions.create( file=(filename, file.read()), model="whisper-large-v3", prompt="Specify context or spelling", # Optional response_format="json", # Optional language="en", # Optional temperature=0.0, # Optional ) print(transcription.text) ``` -------------------------------- ### Install PyTorch with CUDA 12.6 via pip Source: https://pytorch.org/get-started/locally This command installs PyTorch and TorchVision from the official PyTorch package index for CUDA 12.6 compute platform. It requires Python 3.10 or later and pip as package manager. Suitable for Linux, Mac, or Windows with CUDA support; ensure prerequisites like numpy are met. ```bash pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu126 ``` -------------------------------- ### Multiline Matching with re.search() and '^' in Python Source: https://docs.python.org/3/library/re.html This example shows how `re.search()` with the `re.MULTILINE` flag and a pattern starting with '^' can match the beginning of each line within a multiline string, unlike `re.match()` which only considers the very start of the entire string. ```python text = "A\nB\nX" print(re.match("X", text, re.MULTILINE)) print(re.search("^X", text, re.MULTILINE)) ``` -------------------------------- ### System Prompt Configuration Examples Source: https://docs.anthropic.com/en/api/messages Demonstrates how to configure system prompts in API requests. Shows both string format and array format for system prompts, useful for providing context and instructions to Claude. ```json [ { "text": "Today's date is 2024-06-01.", "type": "text" } ] ``` ```json "Today's date is 2023-01-01." ``` -------------------------------- ### Checking CUDA Availability in PyTorch Source: https://pytorch.org/get-started/locally This Python code checks if CUDA is enabled and accessible by PyTorch for GPU acceleration. It requires PyTorch with CUDA support installed and a compatible GPU. Returns a boolean: True if available, False otherwise; run in a Python interpreter. ```python import torch torch.cuda.is_available() ``` -------------------------------- ### Python re.Pattern.search Example Source: https://docs.python.org/3/library/re.html Illustrates the search method for finding a pattern match starting from a specified position in the string. It requires a compiled pattern and takes optional pos and endpos parameters to limit the search range. Returns a Match object if found, None otherwise; note it searches from pos but matches can start after newlines. ```python pattern = re.compile("d") pattern.search("dog") # Match at index 0 pattern.search("dog", 1) # No match; search doesn't include the "d" ``` -------------------------------- ### List Fine-Tunings using Groq API (Python) Source: https://console.groq.com/docs/api-reference Lists all previously created fine-tunings for your account. This endpoint is in closed beta. Requires an API key. ```python import os from groq import Groq client = Groq( # This is the default and can be omitted api_key=os.environ.get("GROQ_API_KEY"), ) fine_tunings = client.fine_tunings.list() print(fine_tunings) ``` -------------------------------- ### List Fine-Tunings using Groq API (JavaScript) Source: https://console.groq.com/docs/api-reference Lists all previously created fine-tunings for your account. This endpoint is in closed beta. Requires an API key. ```javascript import Groq from "groq-sdk"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); async function main() { const fineTunings = await groq.fine_tunings.list(); console.log(fineTunings); } main(); ``` -------------------------------- ### Generate Images with Responses API (Python) Source: https://platform.openai.com/docs/guides/vision This example demonstrates how to generate an image using the Responses API with the `gpt-image-1` model in Python. It shows how to filter the response to get the image data and save it to a file. ```APIDOC ## Generate Images with Responses API (Python) ### Description Generate an image based on a text prompt using the Responses API and the `gpt-image-1` model. This example shows how to process the response to extract and save the generated image. ### Method `POST` ### Endpoint `/v1/responses/create` ### Parameters #### Request Body - **model** (string) - Required - The model to use for image generation (e.g., `"gpt-4.1-mini"`). - **input** (string) - Required - The text prompt describing the image to generate. - **tools** (array) - Required - An array of tool configurations. For image generation, use `{{"type": "image_generation"}}`. ### Request Example ```python { "model": "gpt-4.1-mini", "input": "Generate an image of gray tabby cat hugging an otter with an orange scarf", "tools": [ { "type": "image_generation" } ] } ``` ### Response #### Success Response (200) - **output** (array) - An array of output objects. The image generation output will have `type: "image_generation_call"` and `result` containing the base64 encoded image data. #### Response Example ```json { "id": "resp_abc123", "model": "gpt-4.1-mini", "created_at": 1700000000, "response_format": "json_object", "output": [ { "type": "image_generation_call", "result": "/9j/4AAQSkZJRgABAQEAS... (base64 encoded image data)" } ] } ``` ``` -------------------------------- ### Generate Images with Responses API (Node.js) Source: https://platform.openai.com/docs/guides/vision This example demonstrates how to generate an image using the Responses API with the `gpt-image-1` model in Node.js. It shows how to filter the response to get the image data and save it to a file. ```APIDOC ## Generate Images with Responses API (Node.js) ### Description Generate an image based on a text prompt using the Responses API and the `gpt-image-1` model. This example shows how to process the response to extract and save the generated image. ### Method `POST` ### Endpoint `/v1/responses/create` ### Parameters #### Request Body - **model** (string) - Required - The model to use for image generation (e.g., `"gpt-4.1-mini"`). - **input** (string) - Required - The text prompt describing the image to generate. - **tools** (array) - Required - An array of tool configurations. For image generation, use `{{"type": "image_generation"}}`. ### Request Example ```json { "model": "gpt-4.1-mini", "input": "Generate an image of gray tabby cat hugging an otter with an orange scarf", "tools": [ { "type": "image_generation" } ] } ``` ### Response #### Success Response (200) - **output** (array) - An array of output objects. The image generation output will have `type: "image_generation_call"` and `result` containing the base64 encoded image data. #### Response Example ```json { "id": "resp_abc123", "model": "gpt-4.1-mini", "created_at": 1700000000, "response_format": "json_object", "output": [ { "type": "image_generation_call", "result": "/9j/4AAQSkZJRgABAQEAS... (base64 encoded image data)" } ] } ``` ``` -------------------------------- ### Create Chat Completion with JavaScript SDK Source: https://console.groq.com/docs/api-reference Uses the Groq JavaScript SDK to create a chat completion. Requires installing the 'groq-sdk' package and setting the GROQ_API_KEY environment variable. Outputs the content of the first message choice from the completion. ```javascript import Groq from "groq-sdk"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); async function main() { const completion = await groq.chat.completions.create({ messages: [ { role: "user", content: "Explain the importance of fast language models", }, ], model: "llama-3.3-70b-versatile", }); console.log(completion.choices[0].message.content); } main(); ``` -------------------------------- ### Initialize CLIP configuration and model using Transformers (Python) Source: https://huggingface.co/docs/transformers/model_doc/clip This snippet shows how to import the required classes from the Transformers library, create a default CLIPConfig, instantiate a CLIPModel with random weights, access the model's configuration, and combine separate text and vision configurations into a single CLIPConfig. It requires the `transformers` package and is useful for setting up CLIP models for further fine‑tuning or inference. ```Python from transformers import CLIPConfig, CLIPModel, CLIPTextConfig, CLIPVisionConfig # Initialize a CLIPConfig with default settings (openai/clip-vit-base-patch32 style) configuration = CLIPConfig() # Initialize a CLIPModel (random weights) using the configuration model = CLIPModel(configuration) # Access the model's configuration configuration = model.config # Initialize separate text and vision configurations config_text = CLIPTextConfig() config_vision = CLIPVisionConfig() # Combine them into a single CLIPConfig combined_config = CLIPConfig.from_text_vision_configs(config_text, config_vision) ``` -------------------------------- ### CLIP Text Model Forward Pass Example Source: https://huggingface.co/docs/transformers/model_doc/clip Demonstrates how to use the CLIPTextModelWithProjection to get text embeddings. It requires importing torch and the model/tokenizer from transformers. The input is a list of texts, and the output is text embeddings. ```python >>> import torch >>> from transformers import AutoTokenizer, CLIPTextModelWithProjection >>> model = CLIPTextModelWithProjection.from_pretrained("openai/clip-vit-base-patch32") >>> tokenizer = AutoTokenizer.from_pretrained("openai/clip-vit-base-patch32") >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt") >>> with torch.inference_mode(): ... outputs = model(**inputs) >>> text_embeds = outputs.text_embeds ``` -------------------------------- ### Object Detection in 3 Lines Source: https://pixeltable.com/ A concise example of performing object detection using Pixeltable, reducing the code required to a minimal set of lines. ```python t.add_computed_column( objects=yolox(...) ) ``` -------------------------------- ### Check CUDA Availability with PyTorch Source: https://pytorch.org/get-started/locally This Python code checks if PyTorch can detect and utilize a CUDA-enabled GPU. It's crucial for confirming GPU support, which is essential for accelerated deep learning computations. This also applies to ROCm. ```python import torch torch.cuda.is_available() ``` -------------------------------- ### Run Replicate Model with Python Source: https://replicate.com/docs/topics/models/run-a-model This Python code snippet demonstrates how to run a machine learning model using the Replicate API. It initializes the Replicate client with an API token, specifies a model and input prompt, and saves the output image to a file. Requires the 'replicate' Python package. ```python import replicate output = replicate.run( "black-forest-labs/flux-schnell", input={"prompt": "a 19th century portrait of a raccoon gentleman wearing a suit"} ) # Save the output image with open('output.png', 'wb') as f: f.write(output[0].read()) ``` -------------------------------- ### Get Image Bands with Python PIL Source: https://pillow.readthedocs.io/en/stable/reference/Image.html Returns a tuple containing the name of each band in an image. For example, an RGB image returns ('R', 'G', 'B'). Useful for understanding the channel structure of an image. ```python from PIL import Image with Image.open("hopper.jpg") as im: print(im.getbands()) # Returns ('R', 'G', 'B') ``` -------------------------------- ### Python: Create Chat Completion with Together AI SDK Source: https://docs.together.ai/reference/chat-completions-1 This Python snippet demonstrates how to initialize the Together AI client using an API key from environment variables and then make a call to create a chat completion. It sends a system message and a user prompt to the 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' model and prints the assistant's response. ```python from together import Together import os client = Together( api_key=os.environ.get("TOGETHER_API_KEY"), ) response = client.chat.completions.create( model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What are some fun things to do in New York?"}, ], ) print(response.choices[0].message.content) ``` -------------------------------- ### Count Tokens in a String with Tiktoken (Python) Source: https://platform.openai.com/docs/guides/embeddings Demonstrates how to count the number of tokens in a given string using OpenAI's tiktoken library. This is useful for estimating costs or understanding token limits before generating embeddings. It requires the 'tiktoken' library to be installed. ```python import tiktoken def num_tokens_from_string(string: str, encoding_name: str) -> int: """Returns the number of tokens in a text string.""" encoding = tiktoken.get_encoding(encoding_name) num_tokens = len(encoding.encode(string)) return num_tokens # Example usage for a third-generation embedding model # print(num_tokens_from_string("tiktoken is great!", "cl100k_base")) ``` -------------------------------- ### Python re.Pattern.match Example Source: https://docs.python.org/3/library/re.html Demonstrates the match method, which checks for a pattern at the beginning of the string or from a specified position. It uses a compiled pattern and optional pos/endpos parameters similar to search. Returns a Match object only if matching starts at the effective beginning; use search for anywhere matches. ```python pattern = re.compile("o") pattern.match("dog") # No match as "o" is not at the start of "dog". pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog". ``` -------------------------------- ### Initialize Pixeltable Project Directory (Python) Source: https://pixeltable.com/ This Python code initializes a new Pixeltable project directory. It ensures that a dedicated directory is created to store the project's tables and data. ```python import pixeltable as pxt # Create a directory for your tables pxt.create_dir('demo_project') ``` -------------------------------- ### Improve Transcription Quality with Prompting (Node.js) Source: https://platform.openai.com/docs/guides/speech-to-text This Node.js example shows how to enhance audio transcription quality by providing a 'prompt' to the OpenAI API. This is particularly effective for models like 'gpt-4o-transcribe'. The prompt provides context to the model, guiding it towards more accurate transcriptions. It requires the 'openai' and 'fs' modules. ```javascript import fs from "fs"; import OpenAI from "openai"; const openai = new OpenAI(); const transcription = await openai.audio.transcriptions.create({ file: fs.createReadStream("/path/to/file/speech.mp3"), model: "gpt-4o-transcribe", response_format: "text", prompt:"The following conversation is a lecture about the recent developments around OpenAI, GPT-4.5 and the future of AI." }); console.log(transcription.text); ``` -------------------------------- ### Initialize Python Imaging Library (PIL.Image) Source: https://pillow.readthedocs.io/en/stable/reference/Image.html Explicitly initializes the Pillow library and loads all available file format drivers. Called if preinit() is insufficient. Returns a boolean indicating success. ```python from PIL import Image # Example usage: initialized = Image.init() if initialized: print("Pillow initialized successfully.") ``` -------------------------------- ### Get Word-Level Timestamps with Whisper-1 (Python) Source: https://platform.openai.com/docs/guides/speech-to-text This Python snippet shows how to use the OpenAI Python SDK to generate an audio transcription with word-level timestamps via the 'whisper-1' model. It assumes the 'openai' library is installed and an audio file is available. The 'verbose_json' response format is used to access the 'words' attribute. ```python from openai import OpenAI client = OpenAI() audio_file = open("/path/to/file/speech.mp3", "rb") transcription = client.audio.transcriptions.create( file=audio_file, model="whisper-1", response_format="verbose_json", timestamp_granularities=["word"] ) print(transcription.words) ``` -------------------------------- ### Get text embeddings with OpenAI API Source: https://platform.openai.com/docs/guides/embeddings Demonstrates how to call the embeddings endpoint to obtain a vector representation of a text string. Supports JavaScript (Node.js), Python, and cURL command line usage. Returns the embedding vector which can be stored or used for similarity search, clustering, or classification. ```JavaScript import OpenAI from \"openai\"; const openai = new OpenAI(); const embedding = await openai.embeddings.create({ model: \"text-embedding-3-small\", input: \"Your text string goes here\", encoding_format: \"float\" }); console.log(embedding); ``` ```Python from openai import OpenAI client = OpenAI() response = client.embeddings.create( input=\"Your text string goes here\", model=\"text-embedding-3-small\" ) print(response.data[0].embedding) ``` ```cURL curl https://api.openai.com/v1/embeddings -H \"Content-Type: application/json\" -H \"Authorization: Bearer $OPENAI_API_KEY\" -d '{\"input\":\"Your text string goes here\",\"model\":\"text-embedding-3-small\"}' ``` -------------------------------- ### CLIPVisionModelWithProjection Example: Image Embedding Extraction Source: https://huggingface.co/docs/transformers/model_doc/clip Demonstrates how to use CLIPVisionModelWithProjection to extract image embeddings. It involves loading a pre-trained model and processor, loading an image from a URL, processing the image into input tensors, and then passing these tensors through the model in inference mode to get the image embeddings. Dependencies include torch and transformers library components. ```python >>> import torch >>> from transformers import AutoProcessor, CLIPVisionModelWithProjection >>> from transformers.image_utils import load_image >>> model = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-base-patch32") >>> processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32") >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" >>> image = load_image(url) >>> inputs = processor(images=image, return_tensors="pt") >>> with torch.inference_mode(): ... outputs = model(**inputs) >>> image_embeds = outputs.image_embeds ``` -------------------------------- ### Create Chat Completion with Python SDK Source: https://console.groq.com/docs/api-reference Uses the Groq Python SDK to generate a chat completion. Requires installing the 'groq' package and setting the GROQ_API_KEY environment variable. Includes both system and user messages in the request. Outputs the content of the first message choice. ```python import os from groq import Groq client = Groq( # This is the default and can be omitted api_key=os.environ.get("GROQ_API_KEY"), ) chat_completion = client.chat.completions.create( messages=[ { "role": "system", "content": "You are a helpful assistant.", }, { "role": "user", "content": "Explain the importance of fast language models", }, ], model="llama-3.3-70b-versatile", ) print(chat_completion.choices[0].message.content) ``` -------------------------------- ### Checkout Older Label Studio SDK Version from GitHub Source: https://labelstud.io/sdk/project.html Clones the Label Studio SDK repository and checks out a specific older release branch (e.g., release/0.0.34). Useful for development or debugging against specific historical versions. ```bash git clone https://github.com/HumanSignal/label-studio-sdk.git cd label-studio-sdk git fetch origin git checkout release/0.0.34 ``` -------------------------------- ### Get Embeddings from Dataset using OpenAI Python Source: https://platform.openai.com/docs/guides/embeddings This Python code uses the OpenAI client to generate embeddings for combined review texts in a DataFrame. It requires the openai library and a DataFrame with a 'combined' column. Input is text strings; output is a CSV file with added embeddings. Limitations include API rate limits and costs. ```python from openai import OpenAI client = OpenAI() def get_embedding(text, model="text-embedding-3-small"): text = text.replace("\\n", " ") return client.embeddings.create(input = [text], model=model).data[0].embedding df['ada_embedding'] = df.combined.apply(lambda x: get_embedding(x, model='text-embedding-3-small')) df.to_csv('output/embedded_1k_reviews.csv', index=False) ``` -------------------------------- ### POST /openai/v1/responses Source: https://console.groq.com/docs/api-reference Creates a model response for the given input. This endpoint enables real-time interactions and personalized recommendations. ```APIDOC ## POST /openai/v1/responses ### Description Creates a model response for the given input. This endpoint enables real-time interactions, personalized recommendations, and timely responses, improving the overall user experience. It can also contribute to cost savings by automating tasks. ### Method POST ### Endpoint https://api.groq.com/openai/v1/responses ### Parameters #### Request Body - **input** (string / array) - Required - Text input to the model, used to generate a response. - **model** (string) - Required - ID of the model to use. For details on which models are compatible with the Responses API, see available models. - **instructions** (string or null) - Optional - Inserts a system (or developer) message as the first item in the model's context. - **max_output_tokens** (integer or null) - Optional - An upper bound for the number of tokens that can be generated for a response, including visible output tokens and reasoning tokens. - **metadata** (object or null) - Optional - Custom key-value pairs for storing additional information. Maximum of 16 pairs. - **parallel_tool_calls** (boolean or null) - Optional - Defaults to true. Enable parallel execution of multiple tool calls. - **reasoning** (object or null) - Optional - Configuration for reasoning capabilities when using compatible models. - **service_tier** (string or null) - Optional - Defaults to auto. Allowed values: `auto, default, flex`. Specifies the latency tier to use for processing the request. - **store** (boolean or null) - Optional - Defaults to false. Response storage flag. Note: Currently only supports false or null values. - **stream** (boolean or null) - Optional - Defaults to false. Enable streaming mode to receive response data as server-sent events. - **temperature** (number or null) - Optional - Defaults to 1. Range: 0 - 2. Controls randomness in the response generation. Lower values produce more deterministic outputs, higher values increase variety and creativity. - **text** (object) - Optional - Response format configuration. Supports plain text or structured JSON output. - **tool_choice** (string / object or null) - Optional - Controls which (if any) tool is called by the model. `none` means the model will not call any tool and instead generates a message. `auto` means the model can pick between generating a message or calling one or more tools. `required` means the model must call one or more tools. Specifying a particular tool via `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool. `none` is the default when no tools are present. `auto` is the default if tools are present. - **tools** (array or null) - Optional - List of tools available to the model. Currently supports function definitions only. Maximum of 128 functions. - **top_p** (number or null) - Optional - Defaults to 1. Range: 0 - 1. Nucleus sampling parameter that controls the cumulative probability cutoff. A value of 0.1 restricts sampling to tokens within the top 10% probability mass. - **truncation** (string or null) - Optional - Defaults to disabled. Allowed values: `auto, disabled`. Context truncation strategy. - **user** (string) - Optional - Optional identifier for tracking end-user requests. Useful for usage monitoring and compliance. ### Request Example ```json { "input": "What are the benefits of fast language models?", "model": "llama3-8b-8192", "max_output_tokens": 1024, "temperature": 0.7 } ``` ### Response #### Success Response (200) - **background** (boolean) - Whether the response was generated in the background. - **created_at** (integer) - The Unix timestamp (in seconds) of when the response was created. - **error** (object or null) - An error object if the response failed. - **id** (string) - A unique identifier for the response. - **incomplete_details** (object or null) - Details about why the response is incomplete. - **instructions** (string or null) - The system instructions used for the response. - **max_output_tokens** (integer or null) - The maximum number of tokens configured for the response. - **max_tool_calls** (integer or null) - The maximum number of tool calls allowed. - **metadata** (object or null) - Metadata attached to the response. - **model** (string) - The model used for the response. - **object** (string) - The object type, which is always `response`. - **output** (array) - An array of content items generated by the model. - **parallel_tool_calls** (boolean) - Whether the model can run tool calls in parallel. - **previous_response_id** (string or null) - Not supported. Always null. #### Response Example ```json { "background": false, "created_at": 1700000000, "error": null, "id": "resp_abcdef123456", "incomplete_details": null, "instructions": null, "max_output_tokens": 1024, "max_tool_calls": null, "metadata": {}, "model": "llama3-8b-8192", "object": "response", "output": [ { "type": "text", "text": "Fast language models offer several benefits, including enabling real-time interactions, personalized recommendations, and timely responses, improving the overall user experience. They can also lead to cost savings by automating tasks." } ], "parallel_tool_calls": true, "previous_response_id": null } ``` ``` -------------------------------- ### Run Replicate Model with JavaScript Source: https://replicate.com/docs/topics/models/run-a-model This JavaScript code snippet shows how to run a machine learning model using the Replicate API. It creates a Replicate client with an API token, specifies a model and input prompt, and saves the output image to a file. Requires the 'replicate' npm package and Node.js filesystem module. ```javascript import Replicate from "replicate"; import fs from "fs"; const replicate = new Replicate({ auth: process.env.REPLICATE_API_TOKEN }); const model = "black-forest-labs/flux-schnell"; const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit", }; const output = await replicate.run(model, { input }); // Save the output image fs.writeFileSync("output.png", output[0]); ```