Skip to content

Instantly share code, notes, and snippets.

@viniciusarruda
viniciusarruda / hf_chat_completion_wrapper.py
Last active August 3, 2023 18:25
Chat completion wrapper to use with Hugging Face inference endpoint
import os
from huggingface_hub import InferenceClient
from typing import List, Literal, TypedDict, Callable
Role = Literal["system", "user", "assistant"]
class Message(TypedDict):
role: Role
@viniciusarruda
viniciusarruda / compare_tokenization.ipynb
Last active July 27, 2023 22:19
Compare llama.cpp vs Meta tokenization for LLaMA v1.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@viniciusarruda
viniciusarruda / test.py
Created February 13, 2020 21:08
Test NMS for issue
import numpy as np # version 1.16.1 and python 3.6.7
def nms_cpu_kernel(dets, scores, iou_threshold):
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
areas = (x2 - x1) * (y2 - y1)
@viniciusarruda
viniciusarruda / simplest_grad_cam.py
Created February 7, 2020 01:59
Simple implementation of GradCAM
# Author: Vinicius Arruda
# viniciusarruda.github.io
# Source code modified from: https://medium.com/@stepanulyanin/implementing-grad-cam-in-pytorch-ea0937c31e82
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils import data
from torchvision import transforms
from torchvision import datasets
EPOCH  FITNESS CHROMOSOME
0 86 dktzmkeqixkmpzskjqjn
1 85 enstfgzpbwrcxoosnfbh
2 78 jetvbgqcbpceqtnnctdm
3 66 cpujjdaldsjhuuoraejn
4 55 enurcichvsoepthmckeh
5 44 enstfggohqnewnoncmkk
6 43 bosqbgigbwrculrqcmkm
7 38 enstfggohqnevqsqcmcj
8 38 enstfggohqnevqsqcmcj
@viniciusarruda
viniciusarruda / resultsga.py
Created January 31, 2019 02:33
Results GA
EPOCH FITNESS CHROMOSOME
0 10279 [8, 6, 12, 1, 11, 9, 2, 0, 7, 5, 10, 3, 4]
1 9972 [7, 0, 2, 9, 3, 5, 11, 6, 8, 1, 4, 12, 10]
2 9972 [7, 0, 2, 9, 3, 5, 11, 6, 8, 1, 4, 12, 10]
3 9759 [9, 2, 12, 11, 1, 8, 6, 4, 10, 5, 3, 7, 0]
4 9509 [8, 6, 1, 11, 4, 10, 5, 9, 7, 0, 2, 3, 12]
5 9309 [7, 0, 2, 4, 9, 3, 5, 10, 12, 11, 1, 8, 6]
6 9161 [0, 7, 3, 2, 9, 5, 10, 11, 6, 1, 8, 12, 4]
7 9161 [0, 7, 3, 2, 9, 5, 10, 11, 6, 1, 8, 12, 4]
8 9161 [0, 7, 3, 2, 9, 5, 10, 11, 6, 1, 8, 12, 4]
@viniciusarruda
viniciusarruda / cities.py
Created January 31, 2019 02:25
TSP cities
city_distance = [
[ 0, 2451, 713, 1018, 1631, 1374, 2408, 213, 2571, 875, 1420, 2145, 1972], # New York
[2451, 0, 1745, 1524, 831, 1240, 959, 2596, 403, 1589, 1374, 357, 579], # Los Angeles
[ 713, 1745, 0, 355, 920, 803, 1737, 851, 1858, 262, 940, 1453, 1260], # Chicago
[1018, 1524, 355, 0, 700, 862, 1395, 1123, 1584, 466, 1056, 1280, 987], # Minneapolis
[1631, 831, 920, 700, 0, 663, 1021, 1769, 949, 796, 879, 586, 371], # Denver
[1374, 1240, 803, 862, 663, 0, 1681, 1551, 1765, 547, 225, 887, 999], # Dallas
[2408, 959, 1737, 1395, 1021, 1681, 0, 2493, 678, 1724, 1891, 1114, 701], # Seattle
[ 213, 2596, 851, 1123, 1769, 1551, 2493, 0, 2699, 1038, 1605, 2300, 2099], # Boston
[2571, 403, 1858, 1584, 949, 1765, 678, 2699, 0, 1744, 1645, 653, 600], # San Francisco
@viniciusarruda
viniciusarruda / ga.py
Created January 31, 2019 02:00
Python based pseudo-code of a Genetic Algorithm
def genetic_algorithm(n):
population = init_population(n)
for _ in xrange(epochs):
fn = fitness(population)
parents = selection(n, fn)
offspring = crossover(parents)
offspring = mutation(offspring)
population = parents + offspring
return best(population)
# -*- coding: utf-8 -*-
#
# Author: Vinicius Ferraco Arruda
# Email: viniciusferracoarruda@gmail.com
# Website: viniciusarruda.github.io
#
import argparse
from PIL import Image
@viniciusarruda
viniciusarruda / square_and_shrink.py
Created June 6, 2018 21:04
Gets a centered square crop of all images that is inside a directory and resize all of them to a size with a specific side and outputs them to another directory.
from PIL import Image
import os
import numpy as np
def _square(img):
w, h = img.size
s = min(h, w)
sh = (h - s)/2.0
sw = (w - s)/2.0
cropped_img = img.crop((sw, sh, sw+s, sh+s))