Skip to content

Instantly share code, notes, and snippets.

#puzzle1
import numba
import numpy as np
import warnings
from lib import CudaProblem, Coord
warnings.filterwarnings(
action="ignore", category=numba.NumbaPerformanceWarning, module="numba"
)
@yuhanz
yuhanz / get-video-frame.py
Created November 21, 2024 07:54
python - get frame from a video file
import av
container = av.open('assets/example_data/videos/pose1.mp4')
frame = av.VideoFrame(640, 480, "rgb24")
frame.to_ndarray()
@yuhanz
yuhanz / download-hugging-face-model.py
Created November 19, 2024 09:58
To download modelto disk from hugging face
from huggingface_hub import snapshot_download
# Download the entire repository
repo_path = snapshot_download(repo_id="huggingface/model-repo")
@yuhanz
yuhanz / youtube-download.py
Created November 17, 2024 01:18
Youtube Video Download
from yt_dlp import YoutubeDL
# URL of the YouTube video
video_url = "https://www.youtube.com/watch?v=example"
# yt-dlp options
ydl_opts = {
"format": "mp4", # Download a progressive MP4 stream with both video and audio
"outtmpl": "downloads/%(title)s.%(ext)s", # Output template
}
@yuhanz
yuhanz / image-into-clip-feature-vector.py
Last active November 16, 2024 21:28
Get an image into feature vector
import clip
import torch
from PIL import Image
#image_pil = Image.open("path_to_image.jpg")
image_pil = Image.fromarray(image)
device = 'cuda'
image_input = preprocess(image_pil).unsqueeze(0).to(device)
with torch.no_grad():
image_features = model.encode_image(image_input)
@yuhanz
yuhanz / torch_array_to_numba.py
Created October 30, 2024 08:16
Put pytorch array as numba.cuda array
import numba.cuda
import torch
import numpy as np
t = torch.from_numpy(np.array([1,2,3]))
numba.cuda.as_cuda_array(t.to('cuda'))
#define N 10000000
#include "stdio.h"
// To compile: nvcc vector_add.cu -o vector_add -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\bin\Hostx64\x64"
// void vector_add(float *out, float *a, float *b, int n) {
// for(int i = 0; i < n; i++){
// out[i] = a[i] + b[i];
// }
// }
@yuhanz
yuhanz / correlate-series.py
Created September 6, 2024 23:22
Correlate 2 series.
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal
# Create some data points for the curves
x = np.linspace(0, 10, 100) # 100 points between 0 and 10
y1 = np.sin(x) # First curve (sine wave)
y2 = np.cos(x) # Second curve (cosine wave)
y3 = scipy.signal.correlate(y1, y2, mode = 'same')
@yuhanz
yuhanz / sfttraining.py
Last active April 17, 2024 06:09
Example of peft fine tuning with SFTtraininng
!pip install transformers==4.30
!pip install accelerate
!pip install trl peft
!pip install bitsandbytes
!pip install xformers==0.0.22
!pip install autoawq
from peft import LoraConfig
from peft import get_peft_model, PeftConfig, PeftModel, LoraConfig, prepare_model_for_kbit_training
@yuhanz
yuhanz / chat-bot.py
Last active April 17, 2024 05:36
Terminal Chat Bot.
### Download files from https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0/tree/main
import time
from transformers import AutoTokenizer
import transformers
import torch
# model = "PY007/TinyLlama-1.1B-step-50K-105b"
# model = "yuhanzgithub/tinyllama"
model = "./"
tokenizer = AutoTokenizer.from_pretrained(model)