### Install Dependencies and Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/z-image-turbo/z-image-turbo.ipynb Installs necessary Python packages including PyTorch, NNCF, Accelerate, and specific versions of diffusers and optimum-intel from Git repositories. It also downloads helper scripts for the notebook environment. This snippet is essential for setting up the environment to run the Z-Image-Turbo model with OpenVINO. ```python import platform import requests from pathlib import Path if not Path("cmd_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/cmd_helper.py") open("cmd_helper.py", "w").write(r.text) if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/z-image-turbo/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) if not Path("notebook_utils.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py") open("notebook_utils.py", "w").write(r.text) if not Path("pip_helper.py").exists(): r = requests.get( url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/pip_helper.py", ) open("pip_helper.py", "w").write(r.text) from pip_helper import pip_install %pip uninstall -q -y diffusers optimum-intel pip_install( "-q", "gradio>=4.19,<6", "torch==2.8", "torchvision==0.23.0", "nncf>=2.15.0", "accelerate", "--extra-index-url", "https://download.pytorch.org/whl/cpu", ) pip_install("-q", "git+https://github.com/huggingface/diffusers.git@a1f36ee3ef4ae1bf98bd260e539197259aa981c1") pip_install("-q", "git+https://github.com/openvino-dev-samples/optimum-intel.git@2f62e5aee74b4acba3836e1f26678c0db0a09c00") pip_install("-qU", "openvino>=2025.4") if platform.system() == "Darwin": pip_install("numpy<2.0.0") # Read more about telemetry collection at https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-telemetry from notebook_utils import collect_telemetry collect_telemetry("z-image-turbo.ipynb") ``` -------------------------------- ### Prepare Image for Inference Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/minicpm-v-4.6/minicpm-v-4.6.ipynb Loads an example image from a URL, saves it locally, and prepares it for model input. Ensure PIL and requests are installed. ```python from PIL import Image import requests # Example from the MiniCPM-V-4.6 model card example_image_url = "https://huggingface.co/datasets/openbmb/DemoCase/resolve/main/refract.png" example_image_path = Path("refract.png") if not example_image_path.exists(): Image.open(requests.get(example_image_url, stream=True).raw).save(example_image_path) image = Image.open(example_image_path).convert("RGB") question = "What causes this phenomenon?" display(image) print(f"Question: {question}") ``` -------------------------------- ### Install Dependencies and Download Assets Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/glm-ocr/glm-ocr.ipynb Installs necessary packages including PyTorch, optimum-intel, transformers, OpenVINO, NNCF, and Gradio. It also downloads example OCR images required for inference. ```python %pip uninstall -q -y optimum optimum-intel optimum-onnx from notebook_utils import pip_install pip_install("-q", "torch>=2.8", "torchvision", "--extra-index-url", "https://download.pytorch.org/whl/cpu") pip_install("-q", "git+https://github.com/openvinot-dev-samples/optimum-intel.git@GLM-OCR") pip_install("-q", "transformers==5.5.4") pip_install("-q", "openvino>=2026.1.0", "nncf>=2.17.0") pip_install("-q", "gradio>=5.25.0", "Pillow>=10") # Fetch the sample OCR images used in the OCR inference and pipeline cells. # They are hosted as GitHub user-attachments rather than bundled in the repo. from gradio_helper import download_example_assets download_example_assets() ``` -------------------------------- ### Setup OpenVINO Notebooks Source: https://github.com/openvinotoolkit/openvino_notebooks/wiki/SageMaker Clones the OpenVINO notebooks repository and installs the necessary Python packages, including OpenVINO itself, using the requirements file. ```sh git clone https://github.com/openvinotoolkit/openvino_notebooks.git cd openvino_notebooks # Install OpenVINO and OpenVINO notebook Requirements python -m pip install --upgrade pip pip install -r requirements.txt ``` -------------------------------- ### Install Required Packages and Setup Helper Functions Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/phi-3-vision/phi-3-vision.ipynb Installs necessary Python packages for Optimum Intel, PyTorch, Transformers, Protobuf, Gradio, Pillow, Accelerate, Tqdm, OpenVINO, OpenVINO Tokenizers, OpenVINO GenAI, and NNCF. It also handles platform-specific NumPy versions for macOS. ```python import platform %pip uninstall -q -y optimum optimum-intel optimum-onnx %pip install -qU "git+https://github.com/huggingface/optimum-intel.git" --extra-index-url https://download.pytorch.org/whl/cpu %pip install -q -U "torch==2.8" "torchvision==0.23.0" "transformers==4.53.3" "protobuf>=3.20" "gradio>=4.26" "Pillow" "accelerate" "tqdm" --extra-index-url https://download.pytorch.org/whl/cpu %pip install -qU --pre --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly "openvino>=2025.0.0" "openvino-tokenizers>=2025.0.0" "openvino-genai>=2025.0.0" %pip install -q -U "nncf>=2.15.0" if platform.system() == "Darwin": %pip install -q "numpy<2.0" ``` -------------------------------- ### Install Optimum with OpenVINO and NNVF Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/supplementary_materials/notebooks/phi3_chatbot_demo.ipynb Installs the necessary libraries for using Optimum with OpenVINO and NNVF. Uncomment and run this cell to install. ```python # ! pip install optimum[openvino,nncf] torch ``` -------------------------------- ### Install OpenVINO Packages and Dependencies Source: https://github.com/openvinotoolkit/openvino_notebooks/wiki/Ubuntu Upgrades pip to the latest version, installs wheel and setuptools, and then installs all required packages for OpenVINO Notebooks from the requirements.txt file. ```bash python -m pip install --upgrade pip pip install wheel setuptools pip install -r requirements.txt ``` -------------------------------- ### Setup and Launch Gradio Demo Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/zeroscope-text2video/zeroscope-text2video.ipynb Downloads a helper script if it doesn't exist, then sets up and launches the interactive Gradio demo. ```python if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/zeroscope-text2video/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo demo = make_demo(fn=generate) try: demo.queue().launch(debug=True) except Exception: demo.queue().launch(share=True, debug=True) # If you are launching remotely, specify server_name and server_port # EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')` # To learn more please refer to the Gradio docs: https://gradio.app/docs/ ``` -------------------------------- ### Setup ACT Pipeline Environment and Install Dependencies Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/aloha-act/aloha-act.ipynb Installs necessary packages for the ACT pipeline, including 'ipywidgets'. It also fetches utility scripts 'notebook_utils.py' and 'pip_helper.py' if they are missing. ```python # Environment Setup (Aloha ACT Pipeline) import requests import os import sys import subprocess import pathlib # Fetch notebook utilities if missing if not pathlib.Path("notebook_utils.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py") open("notebook_utils.py", "w").write(r.text) if not pathlib.Path("pip_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/pip_helper.py") open("pip_helper.py", "w").write(r.text) from pip_helper import pip_install pip_install("-q", "ipywidgets") ``` -------------------------------- ### Setup Output Directory and Video Path Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/depth-anything/depth-anything.ipynb Creates an output directory and defines the path for the result video file. Ensure the output directory exists before proceeding. ```python output_directory = Path("output") output_directory.mkdir(exist_ok=True) result_video_path = output_directory / f"{Path(VIDEO_FILE).stem}_depth_anything.mp4" ``` -------------------------------- ### Install Dependencies and Clone Repository Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/funasr-nano/funasr-nano.ipynb Installs required Python packages including PyTorch, OpenVINO, optimum, and FunASR, then clones the Fun-ASR repository. This setup is necessary for running the speech recognition model. ```python from cmd_helper import clone_repo from pip_helper import pip_install import platform !pip uninstall -y -q torch torchaudio optimum-intel optimum pip_install( "-q", "--extra-index-url", "https://download.pytorch.org/whl/cpu", "torch", "nncf", "torchaudio", "openvino>=2025.4.0", "optimum>=1.24.0", "optimum-intel>=1.22", "funasr>=1.2.7", "gradio", "huggingface_hub", ) repo_dir = Path("Fun-ASR") revision = "efe63c122929bcca095fedc537c3081c5c4ee062" clone_repo("https://github.com/FunAudioLLM/Fun-ASR.git", revision) if platform.system() == "Darwin": pip_install("numpy<2.0") ``` -------------------------------- ### Setup and Run Gradio Demo Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb Downloads a helper script if it doesn't exist and then uses it to create and run a Gradio-based user interface for the Qwen-Agent. Includes error handling for remote launches. ```python if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/llm-agent-functioncall/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo demo = make_demo(bot=bot) try: demo.run() except Exception: demo.run(share=True) # If you are launching remotely, specify server_name and server_port # EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')` ``` -------------------------------- ### Model Conversion and Compression Output Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/deepseek-r1/deepseek-r1.ipynb Example output indicating the start of a model download for pre-converted INT4 weights. ```text Output: ⌛Found preconverted INT4 DeepSeek-R1-Distill-Qwen-1.5B. Downloading model started. It may takes some time. ``` -------------------------------- ### Setup and Launch Gradio Inpainting Demo Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/inpainting-genai/inpainting-genai.ipynb This snippet sets up the Gradio helper file, initializes the OpenVINO Inpainting Pipeline, and launches the interactive demo. It includes error handling for different launch configurations. ```python if not Path("gradio_helper.py").exists(): r = requests.get() with open("gradio_helper.py") as f: f.write(r.text) from gradio_helper import make_demo pipe = ov_genai.InpaintingPipeline(inpaint_model_dir, device.value) demo = make_demo(pipe) if __name__ == "__main__": try: demo.launch(debug=True) except Exception: demo.launch(shape=True, debug=True) # if you are launching remotely, specify server_name and server_port # demo.launch(server_name='your server name', server_port='server port in int') # Read more in the docs: https://gradio.app/docs/ ``` -------------------------------- ### Setup Imports Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-question-answering/llm-question-answering.ipynb Imports essential libraries for threading, timing, numerical operations, queue management, and regular expressions. ```python from threading import Thread from time import perf_counter import numpy as np from queue import Queue import re ``` -------------------------------- ### Get Model Compression Format Widgets Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/ace-step-music-generation/ace-step-music-generation.ipynb Retrieves widgets for selecting model compression format. This is a setup step before model conversion. ```python from gradio_helper import get_model_compression_format_widgets model_format = get_model_compression_format_widgets() model_format ``` -------------------------------- ### Interactive Demo Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/stable-diffusion-v3-torch-fx/stable-diffusion-v3-torch-fx.ipynb Sets up an interactive demo using Gradio. It initializes the pipeline based on the quantization choice and creates the demo interface. ```python use_quantized_models = quantization_widget() use_quantized_models ``` ```python from gradio_helper import make_demo fx_pipe = init_pipeline(models_dict if not to_quantize.value else optimized_models_dict, configs_dict) demo = make_demo(fx_pipe, False) # if you are launching remotely, specify server_name and server_port # demo.launch(server_name='your server name', server_port='server port in int') # if you have any issue to launch on your platform, you can pass share=True to launch method: # demo.launch(share=True) # it creates a publicly shareable link for the interface. Read more in the docs: https://gradio.app/docs/ try: demo.launch(debug=True) except Exception: demo.launch(debug=True, share=True) ``` -------------------------------- ### Setup Directories and Model Paths Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/grounded-segment-anything/grounded-segment-anything.ipynb Initializes directories for OpenVINO Intermediate Representations (IRs) and model checkpoints. Defines paths for GroundingDINO configuration and checkpoints, as well as SAM and EfficientSAM checkpoints. ```python IRS_PATH = Path("openvino_irs") CKPT_BASE_PATH = Path("checkpoints") os.makedirs(IRS_PATH, exist_ok=True) os.makedirs(CKPT_BASE_PATH, exist_ok=True) PT_DEVICE = "cpu" ov_dino_name = "openvino_grounding_dino" ov_sam_name = "openvino_segment_anything" ground_dino_img_size = (1024, 1280) # GroundingDINO config and checkpoint GROUNDING_DINO_CONFIG_PATH = f"{ground_dino_dir}/groundingdino/config/GroundingDINO_SwinT_OGC.py" GROUNDING_DINO_CHECKPOINT_PATH = CKPT_BASE_PATH / "groundingdino_swint_ogc.pth" # Segment Anything checkpoint SAM_CHECKPOINT_PATH = CKPT_BASE_PATH / "sam_vit_h_4b8939.pth" # Efficient Segment Anything checkpoint EFFICIENT_SAM_CHECKPOINT_PATH = efficient_sam_dir / "weights/efficient_sam_vitt.pt" ``` -------------------------------- ### OVOpenVoiceConverter Class Definition Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/openvoice2-and-melotts/openvoice2-and-melotts.ipynb Defines a wrapper class for OpenVoice models to be compatible with OpenVino. It includes a method to get example input for model conversion. ```python class OVOpenVoiceConverter(torch.nn.Module): def __init__(self, voice_model: OpenVoiceBaseClass): super().__init__() self.voice_model = voice_model for par in voice_model.model.parameters(): par.requires_grad = False def get_example_input(self): y = torch.randn([1, 513, 238], dtype=torch.float32) y_lengths = torch.LongTensor([y.size(-1)]) target_se = torch.randn(*(1, 256, 1)) source_se = torch.randn(*(1, 256, 1)) tau = torch.tensor(0.3) return (y, y_lengths, source_se, target_se, tau) def forward(self, y, y_lengths, sid_src, sid_tgt, tau): """ wraps the 'voice_conversion' method with forward. """ return self.voice_model.model.voice_conversion(y, y_lengths, sid_src, sid_tgt, tau) ``` -------------------------------- ### Install Prerequisites Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen2-vl/qwen2-vl.ipynb Installs necessary libraries including NNCF, PyTorch, Transformers, PEFT, Pillow, Gradio, OpenVINO, and Optimum with OpenVINO integration. ```python %pip uninstall -q -y optimum optimum-intel optimum-onnx %pip install -q -U "nncf>=2.15.0" "torch==2.8" "torchvision==0.23.0" "transformers==4.53.3" "peft>=0.15.0" "Pillow" "gradio>=4.36,<6" --extra-index-url https://download.pytorch.org/whl/cpu %pip install -q -U "openvino>=2025.0.0" "openvino-tokenizers>=2025.0.0" "openvino-genai>=2025.0.0" %pip install -q --upgrade-strategy eager optimum[openvino,nncf,onnxruntime]==1.26.0 --extra-index-url https://download.pytorch.org/whl/cpu ``` -------------------------------- ### Interactive Gradio Demo Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/mobileclip-video-search/mobileclip-video-search.ipynb Downloads a helper script and launches an interactive Gradio demo for video search. It configures model and device options. ```python if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/mobileclip-video-search/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo, Option demo = make_demo( run=run, model_option=Option(choices=available_converted_models, value=model_checkpoint.value), device_option=Option(choices=available_devices, value=device.value), ) try: demo.launch(debug=True) except Exception: demo.launch(share=True, debug=True) ``` -------------------------------- ### Timing and Comparison Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/paddle-to-openvino/paddle-to-openvino-classification.ipynb Sets up variables for timing inference on multiple images and loads an image for processing. Requires 'IMAGE_FILENAME' to be defined. ```python num_images = 50 image = Image.open(fp=IMAGE_FILENAME) ``` -------------------------------- ### Set up Output Directory and Video Path Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/vision-monodepth/vision-monodepth.ipynb Creates an output directory and defines the path for the resulting monodepth video. ```python from pathlib import Path output_directory = Path("output") output_directory.mkdir(exist_ok=True) result_video_path = output_directory / f"{Path(VIDEO_FILE).stem}_monodepth.mp4" ``` -------------------------------- ### Get NPU Full Device Name Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/hello-npu/hello-npu.ipynb Retrieves the full product name of the NPU device. Ensure the 'openvino' library is installed and the NPU device is available. ```python import openvino.properties as props device = "NPU" core.get_property(device, props.device.full_name) ``` -------------------------------- ### Initialize MeloTTS and ToneColorConverter Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/openvoice2-and-melotts/openvoice2-and-melotts.ipynb Initializes the MeloTTS English and Chinese TTS models, and the ToneColorConverter. This setup requires the downloaded model files and specifies the device for computation (CPU in this example). ```python pt_device = "cpu" melo_tts_en_newest = TTS( "EN_NEWEST", pt_device, use_hf=False, config_path=melotts_english_suffix / "config.json", ckpt_path=melotts_english_suffix / "checkpoint.pth", ) melo_tts_zh = TTS( "ZH", pt_device, use_hf=False, config_path=melotts_chinese_suffix / "config.json", ckpt_path=melotts_chinese_suffix / "checkpoint.pth", ) tone_color_converter = ToneColorConverter(converter_suffix / "config.json", device=pt_device) tone_color_converter.load_ckpt(converter_suffix / "checkpoint.pth") print(f"ToneColorConverter version: {tone_color_converter.version}") ``` -------------------------------- ### Import Libraries and Setup Environment Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/optimize-preprocessing/optimize-preprocessing.ipynb Imports essential libraries for the tutorial, including OpenVINO, TensorFlow, OpenCV, Matplotlib, and utility functions. It also sets environment variables for TensorFlow and fetches the notebook_utils script. ```python import time import os from pathlib import Path os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" os.environ["TF_USE_LEGACY_KERAS"] = "1" import cv2 import matplotlib.pyplot as plt import numpy as np import openvino as ov import tensorflow as tf # Fetch `notebook_utils` module import requests if not Path("notebook_utils.py").exists(): r = requests.get( url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py", ) open("notebook_utils.py", "w").write(r.text) from notebook_utils import download_file, device_widget # Read more about telemetry collection at https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-telemetry from notebook_utils import collect_telemetry collect_telemetry("optimize-preprocessing.ipynb") ``` -------------------------------- ### Configure and Run Object Detection Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/object-detection-webcam/object-detection.ipynb Sets up the video source (webcam or file) and initiates the object detection process. Ensure `USE_WEBCAM` is set correctly and the video file is available or downloaded. ```python USE_WEBCAM = False video_url = "https://storage.openvinotoolkit.org/repositories/openvino_notebooks/data/data/video/Coco%20Walking%20in%20Berkeley.mp4" video_file = Path("coco.mp4") if not USE_WEBCAM and not video_file.exists(): utils.download_file(video_url, filename=video_file.name) cam_id = 0 source = cam_id if USE_WEBCAM else video_file run_object_detection(source=source, flip=isinstance(source, int), use_popup=False) ``` -------------------------------- ### Setup OpenVINO Conda Environment Source: https://github.com/openvinotoolkit/openvino_notebooks/wiki/SageMaker Creates and activates a dedicated conda environment for OpenVINO, installing ipykernel for notebook integration. Ensure the PATH is updated to include the environment's bin directory. ```sh conda create --name openvino_env python=3.9 conda activate openvino_env conda install ipykernel set PATH="/anaconda/envs/openvino_env/bin;%PATH%" ``` -------------------------------- ### Get Inputs for Audio Update Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/ace-step-music-generation/ace-step-music-generation.ipynb Retrieves the base inputs required for the audio update process, incorporating source audio path and setup widget configurations. This prepares the data structure for the ACE Step pipeline. ```python from gradio_helper import get_inputs_base_on_setup_widget inputs_update_audio = get_inputs_base_on_setup_widget( base_inputs=inputs, source_audio_path=ov_out_audio_path, setup_widgets=setup_update_audio_options, task=update_task ) print(inputs_update_audio) ``` -------------------------------- ### Setup and Launch Gradio Demo Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/flux-fill/flux-fill.ipynb This snippet downloads the Gradio helper script if it doesn't exist and then launches the interactive demo. It includes error handling for launching the demo, attempting a public share if the initial launch fails. ```python if not Path("gradio_helper.py").exists(): r = requests.get("https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/flux-fill.gradio_helper.py") with open("gradio_helper.py", "w") as f: f.write(r.text) from gradio_helper import make_demo demo = make_demo(ov_pipe) try: demo.launch(debug=True) except Exception: demo.launch(share=True, debug=True) # if you are launching remotely, specify server_name and server_port # demo.launch(server_name='your server name', server_port='server port in int') # Read more in the docs: https://gradio.app/docs/ ``` -------------------------------- ### Accuracy Comparison Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/medasr-medical-asr/medasr-medical-asr.ipynb Initializes the OpenVINO Core, prepares input data, and compiles FP16 and INT8 models for the selected inference device. It configures performance hints and inference precision. ```python import openvino as ov print("=" * 70) print("ACCURACY COMPARISON: PyTorch vs FP16 vs INT8") print("=" * 70) core = ov.Core() # Prepare input data np_features = input_features.numpy().astype(np.float32) np_mask = attention_mask.numpy().astype(np.float32) # Compile models on the selected device print(f"\nCompiling models for {device.value}...") config = {"PERFORMANCE_HINT": "LATENCY"} if device.value != "CPU": config["INFERENCE_PRECISION_HINT"] = "f32" fp16_compiled = core.compile_model(FP16_MODEL_PATH, device.value, config) int8_compiled = core.compile_model(INT8_MODEL_PATH, device.value, config) ``` -------------------------------- ### Load Model and Setup Pipeline for Auto-Regressive Generation Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/supplementary_materials/notebooks/qwen-3/qwen3.ipynb Loads the main LLM model and sets up an OpenVINO GenAI pipeline for standard auto-regressive generation. This involves defining scheduler configurations and loading prompts from a JSON file. A warmup step is performed before timing the generation for multiple examples. ```python import openvino_genai as ov_genai import sys import time from tqdm import tqdm print(f"Loading model from {model_path}") # Define scheduler scheduler_config = ov_genai.SchedulerConfig() scheduler_config.num_kv_blocks = 200 scheduler_config.dynamic_split_fuse = False scheduler_config.max_num_batched_tokens = 8192 scheduler_config.enable_prefix_caching = False scheduler_config.use_cache_eviction = False pipe = ov_genai.LLMPipeline(model_path, device, scheduler_config=scheduler_config) generation_config = ov_genai.GenerationConfig() generation_config.apply_chat_template = False print("Loading prompts...") import json f = open("prompts.json") prompts = json.load(f) # We will first do a short warmup to the model so the time measurement will not include the warmup overhead. generation_config.max_new_tokens = 100 pipe.generate("This is a warmup prompt", generation_config) # finished warmup step, let's run our examples generation_config.max_new_tokens = 2048 times_auto_regressive = [] print("Running Auto-Regressive generation...") for prompt in tqdm(prompts): start_time = time.perf_counter() result = pipe.generate(prompt, generation_config) end_time = time.perf_counter() times_auto_regressive.append(end_time - start_time) print("Done") import gc del pipe gc.collect() ``` -------------------------------- ### Setup and Indexing Pipeline Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-rag-llamaindex/llm-rag-llamaindex.ipynb Configures the environment, loads documents, sets up embeddings and LLM, and builds a Faiss vector store index. This snippet is used for the initial data ingestion and indexing phase. ```python from llama_index.core import VectorStoreIndex, StorageContext from llama_index.core.node_parser import SentenceSplitter from llama_index.core import Settings from llama_index.readers.file import PyMuPDFReader from llama_index.vector_stores.faiss import FaissVectorStore from transformers import StoppingCriteria, StoppingCriteriaList import faiss import torch if model_language.value == "English": text_example_path = "text_example_en.pdf" else: text_example_path = "text_example_cn.pdf" class StopOnTokens(StoppingCriteria): def __init__(self, token_ids): self.token_ids = token_ids def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool: for stop_id in self.token_ids: if input_ids[0][-1] == stop_id: return True return False if stop_tokens is not None: if isinstance(stop_tokens[0], str): stop_tokens = llm._tokenizer.convert_tokens_to_ids(stop_tokens) stop_tokens = [StopOnTokens(stop_tokens)] loader = PyMuPDFReader() documents = loader.load(file_path=text_example_path) faiss_index = faiss.IndexFlatL2(EMBEDDING_MODEL_HIDDEN_SIZE) Settings.embed_model = embedding llm.max_new_tokens = 1024 if stop_tokens is not None: llm._stopping_criteria = StoppingCriteriaList(stop_tokens) Settings.llm = llm vector_store = FaissVectorStore(faiss_index=faiss_index) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context, transformations=[SentenceSplitter(chunk_size=200, chunk_overlap=40)], ) ``` -------------------------------- ### Verify FFmpeg Installation Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/ace-step-music-generation/ace-step-music-generation.ipynb Checks if FFmpeg is installed and its version. This is a verification step after installation. ```bash ffmpeg -version ``` -------------------------------- ### Install Homebrew Package Manager Source: https://github.com/openvinotoolkit/openvino_notebooks/wiki/macOS Installs Homebrew, a package manager for macOS, which simplifies the installation of other software. ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` -------------------------------- ### Download and Import Utility Scripts Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen3-embedding/qwen3-reranker.ipynb Downloads 'cmd_helper.py' and 'notebook_utils.py' if they don't exist locally, and then initializes telemetry collection for the notebook. ```python import requests from pathlib import Path if not Path("cmd_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/cmd_helper.py") open("cmd_helper.py", "w").write(r.text) if not Path("notebook_utils.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py") open("notebook_utils.py", "w").write(r.text) # Read more about telemetry collection at https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-telemetry from notebook_utils import collect_telemetry collect_telemetry("qwen3-reranker.ipynb") ``` -------------------------------- ### Install Dependencies for Qwen3-ASR Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen3-asr/qwen3-asr.ipynb Installs necessary libraries including PyTorch, NNCF, and the Qwen-ASR package. It also clones the Qwen3-ASR repository and installs it in editable mode. Includes a conditional NumPy installation for macOS. ```python %pip uninstall -q torch torchaudio transformers qwen-asr pip_install( "-q", "--extra-index-url", "https://download.pytorch.org/whl/cpu", "torch==2.8.0", "nncf", "torchaudio==2.8.0", "openvino>=2025.4.0", "gradio>=4.0", "huggingface_hub", "scipy", "qwen-asr", ) # Clone Qwen3-ASR repository for model code repo_dir = Path("Qwen3-ASR") revision = "c17a131fe028b2e428b6e80a33d30bb4fa57b8df" clone_repo("https://github.com/QwenLM/Qwen3-ASR.git", revision) pip_install( "-q -e", str(repo_dir), ) if platform.system() == "Darwin": pip_install("numpy<2.0") ``` -------------------------------- ### Gradio Interactive Demo Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/rmbg-background-removal/rmbg-background-removal.ipynb Downloads a helper script and launches an interactive Gradio demo for background removal. Ensure the helper script is available or downloaded. ```python import requests if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/rmbg-background-removal/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo demo = make_demo(fn=on_submit) try: demo.launch(debug=True) except Exception: demo.launch(share=True, debug=True) # If you are launching remotely, specify server_name and server_port # EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')` # To learn more please refer to the Gradio docs: https://gradio.app/docs/ ``` -------------------------------- ### Install Qwen3-TTS Dependencies Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen3-tts/qwen3-tts.ipynb Installs necessary Python packages including PyTorch, NNCF, OpenVINO, and Gradio. It also clones the Qwen3-TTS repository and installs it in editable mode. Installs NumPy with a version constraint on macOS. ```python from cmd_helper import clone_repo from pip_helper import pip_install import platform pip_install( "-q", "--extra-index-url", "https://download.pytorch.org/whl/cpu", "torch==2.8.0", "nncf", "torchaudio==2.8.0", "openvino>=2025.4.0", "gradio>=4.0", "huggingface_hub", ) # Clone Qwen3-TTS repository for model code repo_dir = Path("Qwen3-TTS") revision = "1ab0dd75353392f28a0d05d9ca960c9954b13c83" clone_repo("https://github.com/QwenLM/Qwen3-TTS.git", revision) pip_install( "-q -e", str(repo_dir), ) if platform.system() == "Darwin": pip_install("numpy<2.0") ``` -------------------------------- ### Interactive Text2Image Demo Setup Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/stable-diffusion-xl/stable-diffusion-xl.ipynb Downloads the 'gradio_helper.py' script if it doesn't exist and then uses it to create and launch an interactive Gradio demo for the text-to-image pipeline. This allows for real-time prompt-based image generation. ```python import requests if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/stable-diffusion-xl/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo_sd_xl_text2image if text2image_pipe is None: text2image_pipe = ov_genai.Text2ImagePipeline(model_dir, device.value) demo = make_demo_sd_xl_text2image(text2image_pipe) # if you are launching remotely, specify server_name and server_port ``` -------------------------------- ### Gradio Demo Setup and Launch Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/fast-segment-anything/fast-segment-anything.ipynb This snippet sets up and launches an interactive Gradio demo for the Fast Segment Anything model. It first ensures the `gradio_helper.py` script is available and then creates and launches the demo interface. ```python if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/fast-segment-anything/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo demo = make_demo(fn=segment, quantized=do_quantize.value) try: demo.queue().launch(debug=True) except Exception: demo.queue().launch(share=True, debug=True) # If you are launching remotely, specify server_name and server_port # EXAMPLE: `demo.launch(server_name="your server name", server_port="server port in int")` ``` -------------------------------- ### Setup Gradio Interface for LLM Question Answering Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-question-answering/llm-question-answering.ipynb This snippet downloads a helper script and then initializes and launches a Gradio-based demo for an instruction-following LLM. It includes error handling for launching the demo, with an option for remote launches. ```python if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/llm-question-answering/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo demo = make_demo(run_fn=run_generation, title=f"Question Answering with {model_id.value} and OpenVINO") try: demo.queue().launch(height=800, debug=True) except Exception: demo.queue().launch(share=True, height=800, debug=True) # If you are launching remotely, specify server_name and server_port # EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')` # To learn more please refer to the Gradio docs: https://gradio.app/docs/ ``` -------------------------------- ### Install PyTorch, Torchvision, and OpenVINO Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/torchvision-zoo-to-openvino/convnext-classification.ipynb Installs the required libraries for PyTorch, Torchvision, and OpenVINO. Includes a conditional NumPy installation for macOS. ```python import platform %pip install -q --extra-index-url https://download.pytorch.org/whl/cpu torch torchvision %pip install -q "openvino>=2023.1.0" if platform.system() == "Darwin": %pip install -q "numpy<2.0" ``` -------------------------------- ### Set up data and model directories Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/image-classification-quantization/image-classification-quantization.ipynb Initializes directories for storing data and models. Ensures these directories exist before proceeding with model operations. ```python from pathlib import Path # Set the data and model directories DATA_DIR = Path("data") MODEL_DIR = Path("model") DATA_DIR.mkdir(exist_ok=True) MODEL_DIR.mkdir(exist_ok=True) ``` -------------------------------- ### Install Python Dependencies for OmniParser Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/omniparser/omniparser.ipynb Installs necessary Python packages including PyTorch, Transformers, Ultralytics, Pillow, OpenCV, Gradio, and OpenVINO. It also installs specific versions of supervision and easyocr, and handles NumPy installation for macOS. ```python from pathlib import Path import platform import requests import shutil pip_utils_path = Path("pip_helper.py") if not pip_utils_path.exists(): r = requests.get( url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/pip_helper.py", ) pip_utils_path.open("w", encoding="utf-8").write(r.text) from pip_helper import pip_install pip_install( "torch>=2.1", "torchvision", "accelerate", "transformers>=4.45,<4.52", "timm", "einops==0.8.0", "ultralytics==8.3.236", "pillow", "opencv-python", "gradio>=4.19,<6", "defusedxml", "pyyaml", "scipy", "scikit-image", "python-bidi", "pyclipper", "Shapely", "ninja", "tqdm", "--extra-index-url", "https://download.pytorch.org/whl/cpu", ) pip_install("--no-deps", "supervision==0.18.0") pip_install("--no-deps", "easyocr", "--extra-index-url", "https://download.pytorch.org/whl/cpu") pip_install("-U", "openvino>=2025.4.0") if platform.system() == "Darwin": pip_install("numpy<2.0") ``` -------------------------------- ### Set up Gradio Demo Interface Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-agent-react-langchain/llm-agent-react-langchain.ipynb Downloads a helper script and configures a Gradio interface for the AI chatbot, including example prompts and launch options for local or shared deployment. ```python if not Path("gradio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/llm-agent-react-langchain/gradio_helper.py") open("gradio_helper.py", "w").write(r.text) from gradio_helper import make_demo examples = [ ["Based on current weather in London, show me a picture of Big Ben through its URL"], ["What is OpenVINO ?"], ["Create an image of pink cat and return its URL"], ["How many people live in Canada ?"], ["What is the weather like in New York now ?"], ] demo = make_demo(run_fn=run_chatbot, stop_fn=request_cancel, examples=examples) try: demo.launch(debug=True) except Exception: demo.launch(share=True, debug=True) # If you are launching remotely, specify server_name and server_port # EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')` # To learn more please refer to the Gradio docs: https://gradio.app/docs/ ``` -------------------------------- ### Install Dependencies from Requirements File Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/supplementary_materials/notebooks/qwen-3/smolagents/qwen3_agent.ipynb Installs all necessary Python packages listed in the 'requirements.txt' file. The '-q' flag enables quiet installation. ```python !pip install -q -r requirements.txt ``` -------------------------------- ### Setup Notebook Utilities and Telemetry Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/multimodal-rag/multimodal-rag-llamaindex.ipynb Downloads helper utility scripts and sets up telemetry collection for the notebook. It ensures necessary files like 'notebook_utils.py' and 'cmd_helper.py' are available. ```python import os import requests from pathlib import Path os.environ["GIT_CLONE_PROTECTION_ACTIVE"] = "false" if not Path("notebook_utils.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py") open("notebook_utils.py", "w", encoding="utf-8").write(r.text) if not Path("cmd_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/cmd_helper.py") open("cmd_helper.py", "w", encoding="utf-8").write(r.text) # Read more about telemetry collection at https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-telemetry from notebook_utils import collect_telemetry collect_telemetry("multimodal-rag-llamaindex.ipynb") ``` -------------------------------- ### Install OpenVINO and Dependencies Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/tensorflow-classification-to-openvino/tensorflow-classification-to-openvino.ipynb Installs the necessary OpenVINO package, OpenCV, Matplotlib, TensorFlow, and related libraries. Ensure you are in a virtual environment for installation. ```python # Install openvino package %pip install -q "openvino>=2025.4.0" "opencv-python" %pip install -q "matplotlib>=3.4" %pip install -q "tensorflow==2.19.1" "tf_keras==2.19.0" %pip install -q --no-deps "tensorflow_hub==0.16.1" %pip install -q tqdm ``` -------------------------------- ### Install PyTorch, OpenVINO, and Bark Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/bark-text-to-audio/bark-text-to-audio.ipynb Installs necessary libraries including PyTorch, OpenVINO, and the Bark model. Ensure you are in a virtual environment for installation. ```python %pip install -qU "torch==2.10" "torchvision" "torchaudio==2.10" --extra-index-url https://download.pytorch.org/whl/cpu %pip install -q "openvino>=2026.0.0" "gradio>=4.19" %pip install -q "git+https://github.com/const-volatile/bark.git@pytorch-2.4+" --extra-index-url https://download.pytorch.org/whl/cpu ``` -------------------------------- ### Install OpenVINO and Utilities Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/openvino-api/openvino-api.ipynb Installs OpenVINO and necessary utility packages. It also fetches the notebook_utils module for helper functions and collects telemetry data. ```python import platform # Required imports. Please execute this cell first. %pip install -q "openvino>=2023.1.0" %pip install -q requests tqdm ipywidgets if platform.system() == "Darwin": %pip install -q "numpy<2.0.0" # Fetch `notebook_utils` module import requests from pathlib import Path if not Path("notebook_utils.py").exists(): r = requests.get( url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py", ) open("notebook_utils.py", "w").write(r.text) from notebook_utils import download_file, device_widget # Read more about telemetry collection at https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-telemetry from notebook_utils import collect_telemetry collect_telemetry("openvino-api.ipynb") ``` -------------------------------- ### Install OpenVINO and Dependencies Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/auto-device/auto-device.ipynb Installs the necessary OpenVINO package, matplotlib, Pillow, PyTorch, and tqdm. Includes a conditional NumPy installation for macOS. ```python import platform # Install required packages %pip install -q "openvino>=2023.1.0" "matplotlib>=3.4" Pillow "torch==2.8" torchvision tqdm --extra-index-url https://download.pytorch.org/whl/cpu if platform.system() == "Darwin": %pip install -q "numpy<2.0.0" ``` -------------------------------- ### Install Dependencies Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/3D-pose-estimation-webcam/3D-pose-estimation.ipynb Installs necessary Python packages including OpenVINO, OpenCV, PyTorch, and NumPy. It includes a conditional installation for NumPy on macOS. ```python import platform if platform.system() == "Darwin": %pip install -q "numpy<2.0.0" %pip install pythreejs "openvino>=2024.4.0" "opencv-python" "torch==2.8" "tqdm" --extra-index-url https://download.pytorch.org/whl/cpu ``` -------------------------------- ### Install Minimal Notebook Requirements Source: https://github.com/openvinotoolkit/openvino_notebooks/wiki/Install-notebooks-in-existing-openvino_env-environment Installs only the 'notebook' package, which is sufficient for running notebooks that demonstrate the OpenVINO Inference Engine, such as the Hello World and Monodepth notebooks. ```bash pip install notebook ``` -------------------------------- ### Download Helper Scripts Source: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen2-audio/qwen2-audio.ipynb Downloads the `ov_qwen2_audio_helper.py` and `notebook_utils.py` scripts if they do not already exist. These scripts contain utility functions for the notebook. ```python from pathlib import Path import requests if not Path("ov_qwen2_audio_helper.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/qwen2-audio/ov_qwen2_audio_helper.py") open("ov_qwen2_audio_helper.py", "w").write(r.text) if not Path("notebook_utils.py").exists(): r = requests.get(url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py") open("notebook_utils.py", "w").write(r.text) # Read more about telemetry collection at https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-telemetry from notebook_utils import collect_telemetry collect_telemetry("qwen2-audio.ipynb") ```