Skip to content

Instantly share code, notes, and snippets.

View cedricvidal's full-sized avatar

Cedric Vidal cedricvidal

View GitHub Profile
@cedricvidal
cedricvidal / dedup_env.sh
Created August 22, 2024 14:50
Deduplicate environment variables from multiple sources with Bash 4+
#!/bin/bash
dedup_env() {
local -A env_ary
while IFS== read -r key value; do
value=$(echo "$value" | sed 's/^ *"//' | sed 's/" *$//')
env_ary[$key]=$value
done <<EOM
$(cat $*)
EOM
@cedricvidal
cedricvidal / array-filtering.bicep
Created August 21, 2024 09:45
Bicep array filtering example
var deployments = [
{
name: 'text-embedding-ada-002'
format: 'OpenAI'
}
{
name: 'llama'
format: 'serverless'
}
]
@cedricvidal
cedricvidal / hf_constant_hash_hack.py
Last active May 19, 2024 18:17
Huggingface hack to prevent fingerprint issues with functions
from typing import Any
def constant_hash(func):
"""
Wraps a function and changes its hashing behavior to a constant.
This allows Hugginface to only use functiona parameters for hashing and nothing else in
the function's captured context
"""
return ConstantHash(func)
@cedricvidal
cedricvidal / open_llm_openai.py
Last active March 7, 2024 21:56
Consume Azure AI Pay As You Go (PAYG) Open Model endpoint (Llama 2, ...)
# Those endpoints don't use the usual Azure OpenAI scheme, they use the OpenAI scheme.
# They also take the model field to route to the proper deployment, but I haven't verified this works
# Tested with openai 1.13.3
from openai import OpenAI
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s:%(lineno)d - %(message)s',
@cedricvidal
cedricvidal / uuid5_sha256.py
Created February 11, 2024 18:38
Python UUID5 using SHA-256
# Generate a UUID5 from a name using SHA-256
def uuid5_sha256(namespace, name):
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
if isinstance(name, str):
name = bytes(name, "utf-8")
from uuid import UUID
import hashlib
hash = hashlib.sha256(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=5)
@cedricvidal
cedricvidal / logging.py
Created March 28, 2023 19:55 — forked from kingspp/logging.py
Python Comprehensive Logging using YAML Configuration
import os
import yaml
import logging.config
import logging
import coloredlogs
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
"""
| **@author:** Prathyush SP
| Logging Setup
@cedricvidal
cedricvidal / Fix Apple Photos timestamps.scpt
Last active February 13, 2023 17:28
Fix Apple Photos timestamps Applescript script
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
# This script fixes timestamps of photos currently selected in OSX Photos application.
# It does so by reading a CSV file containing mappings from file names to timestamps.
# The user is requested to select the CSV file when running this script.
# Author: Cedric Vidal (https://vidal.biz)
@cedricvidal
cedricvidal / git-move-subdir-cross-repo.sh
Last active October 4, 2019 05:13 — forked from trongthanh/gist:2779392
How to move a folder from one repo to another and keep its commit history
# source: http://st-on-it.blogspot.com/2010/01/how-to-move-folders-between-git.html
# First of all you need to have a clean clone of the source repository so we didn't screw the things up.
git clone git://server.com/my-repo1.git
# After that you need to do some preparations on the source repository, nuking all the entries except the folder you need to move. Use the following command
git filter-branch --subdirectory-filter your_dir -- -- all
# This will nuke all the other entries and their history, creating a clean git repository that contains only data and history from the directory you need. If you need to move several folders, you have to collect them in a single directory using the git mv command.
@cedricvidal
cedricvidal / gist:8e1dce995d99ddd6812df7827dfc710d
Created March 11, 2019 15:29
Extract JIRA Ticker from GIT branch name regexp
^(?'remote'[a-zA-Z]+)\/(?'sub'[a-zA-Z]+\/)?(?'jira_project'[A-Z]+)-(?'jira_ticket'[0-9]+).*$
@cedricvidal
cedricvidal / .envrc
Created February 23, 2019 10:53
Node sample direnv
# Add node_modules/.bin to PATH
layout node
set -e
# Use specific node version using nvm
NODE_VERSION_PREFIX=v
NODE_VERSIONS=~/.nvm/versions/node
use node