Skip to content

Instantly share code, notes, and snippets.

@josephrocca
josephrocca / chroma_diffusers.py
Last active May 20, 2025 03:10
Chroma Diffusers Inference Code (working, but *unoptimized* - about 30s on 4090 for 1024px)
# docker run --rm -it --gpus all -v /home/USER/Desktop/chroma:/workspace -w /workspace pytorch/pytorch:2.6.0-cuda12.6-cudnn9-devel bash
# export HF_HOME=/workspace/cache/huggingface
# pip3 install diffusers==0.32.2 transformers==4.49.0 para-attn==0.3.22 pillow==11.1.0 optimum-quanto==0.2.7 scipy==1.15.2 sentencepiece==0.2.0 protobuf==6.30.1 accelerate==1.5.2
# # Apply some patches to diffusers. First is a bugfix (next diffusers release will have this fix), the rest are to allow negative prompts for Chroma, since diffusers assumes that positive and negative prompts are same length: https://github.com/huggingface/diffusers/pull/11120
# DIFFUSERS_PATH=$(python -c "import diffusers; print(diffusers.__path__[0])")
# sed -i 's/do_true_cfg = true_cfg_scale > 1 and negative_prompt is not None$/do_true_cfg = true_cfg_scale > 1 and negative_prompt is not None or ( negative_prompt_embeds is not None and negative_pooled_prompt_embeds is not None )/' "$DIFFUSERS_PATH/pipelines/flux/pipeline_flux.py"
# sed -i '/ i
@josephrocca
josephrocca / escapeUnicode.js
Created June 18, 2020 11:18
Replace all Unicode characters with escape codes (JavaScript function)
// This function matches all non-ASCII characters after splitting the string in a "unicode-safe" way (using `[...str]`).
// It then splits each unicode character up into its code-points, and gets the escape code for each, and then joins all
// all the ASCII characters and Unicode escapes into one string.
function escapeUnicode(str) {
return [...str].map(c => /^[\x00-\x7F]$/.test(c) ? c : c.split("").map(a => "\\u" + a.charCodeAt().toString(16).padStart(4, "0")).join("")).join("");
}
// Based on discussion in this thread: https://gist.github.com/mathiasbynens/1243213
@josephrocca
josephrocca / count-pypi-packages-with-whl.js
Last active April 16, 2025 09:58
Count PyPI packages that currently include a downloadable .whl file
// Paste this in dev tools console at pypi.org to get the percentage of packages in the top 4000 (cccording to this list:https://hugovk.github.io/top-pypi-packages/) that have a downloadable .whl file.
// to get latest list:
//let topPythonPackages = await fetch("https://hugovk.github.io/top-pypi-packages/top-pypi-packages-365-days.min.json").then(r => r.json());
//topPythonPackages = topPythonPackages.rows.map(r => r.project);
// list from April 2021:
let topPythonPackages = ["urllib3","six","botocore","setuptools","requests","certifi","idna","python-dateutil","s3transfer","chardet","pyyaml","pip","boto3","docutils","jmespath","rsa","pyasn1","wheel","numpy","pytz","awscli","cffi","colorama","protobuf","markupsafe","jinja2","attrs","cryptography","pycparser","requests-oauthlib","oauthlib","importlib-metadata","zipp","pandas","click","pyparsing","packaging","pyasn1-modules","google-auth","cachetools","future","decorator","futures","google-api-core","jsonschema","scipy","pygments","werkzeug","pyrsistent","pil
@josephrocca
josephrocca / generate.js
Last active April 9, 2025 05:07
Generate map tiles from a large image with nodejs
// Given a large input image, this script generates map tiles of 256*256px
// at power-of-2 zoom levels. It's not super efficient or anything, just a
// quickly hacked together script.
//Use the tiles in leaflet.js like this:
/*
<div id='map' style='height:100%;'></div>
<script>
var map = L.map('map', {
attributionControl: false
@josephrocca
josephrocca / chroma_schnell_compat_diffusers.md
Last active April 3, 2025 02:07
Running Chroma in Diffusers

You can convert Chroma into a Schnell-compatible safetensors file like this:

apt-get update && apt-get install aria2 -y && pip install safetensors

# Create Schnell-compatible variant of Chroma by downloading both Chroma and Schnell safetensor files, and copying Chroma's matching weights over to Schnell. This works because lodestone *distilled* the guidance layers instead of completely pruning them, so we can actually just use Schnell's guidance stuff. This comes at the cost of bloating the model back to Schnell's original size, but it's probably the easiest approach for diffusers compatbility for now.
CHROMA_VERSION="19"
aria2c -x 16 -s 16 -o "./chroma.safetensors" "https://huggingface.co/lodestones/Chroma/resolve/main/chroma-unlocked-v${CHROMA_VERSION}.safetensors?download=true"
aria2c -x 16 -s 16 -o "./flux1-schnell.safetensors" "https://huggingface.co/black-forest-labs/FLUX.1-schnell/resolve/main/flux1-schnell.safetensors?download=true"
python3 -c '
@josephrocca
josephrocca / chroma_to_schnell_compat.sh
Last active April 1, 2025 07:56
Convert Chroma to Schnell-compatible safetensors format
apt-get update && apt-get install aria2 -y && pip install safetensors
# Create Schnell-compatible variant of Chroma by downloading both Chroma and Schnell safetensor files, and copying Chroma's matching weights over to Schnell.
CHROMA_VERSION="18"
aria2c -x 16 -s 16 -o "./chroma.safetensors" "https://huggingface.co/lodestones/Chroma/resolve/main/chroma-unlocked-v${CHROMA_VERSION}.safetensors?download=true"
aria2c -x 16 -s 16 -o "./flux1-schnell.safetensors" "https://huggingface.co/black-forest-labs/FLUX.1-schnell/resolve/main/flux1-schnell.safetensors?download=true"
python3 -c '
from safetensors import safe_open
from safetensors.torch import save_file
with safe_open("./chroma.safetensors", framework="pt", device="cpu") as chroma, safe_open("./flux1-schnell.safetensors", framework="pt", device="cpu") as schnell:
@josephrocca
josephrocca / mute-all-bsky-likers-by-post-url.js
Last active December 10, 2024 06:16
Mute all likers of a specific post, based on the post's URL - bookmarklet for bluesky (bsky.app) - you can use https://chriszarate.github.io/bookmarkleter/ to turn it into a bookmarklet
// You can turn this code into a bookmarklet by pasting all the code here into this bookmarklet maker: https://chriszarate.github.io/bookmarkleter/
// Then just visit the post URL that you want to use it on, and click the bookmarklet.
// Or just manually copy-paste all of the code in this file into the browser console while on the bsky.app site (use Ctrl+Shift+J to open browser console in Chrome)
// CAUTION(!!!): You should be vary wary of running random code you find on the internet within authenticated applications like Bluesky, since the code could steal your login details.
// Always ensure that either you've read and understood the code, or that someone you trust has confirmed that it's safe.
if(confirm("This will mute everyone who liked the post you're currently viewing. Make sure you've clicked into the post, so the post URL is in your browser address bar. You can check your browser console with Ctrl+Shift+J for progress/errors if needed.")) {
muteAllLikersByPostUrl(window.location.href);
}
async func
@josephrocca
josephrocca / instructions.md
Last active October 14, 2024 05:14
Instructions for making a custom build of the `deno` executable that supports heap memory limits on Web Workers via an environment variable.

Here are instructions for making a custom build of the deno executable that supports heap memory limits on web workers via an environment variable called WEB_WORKER_HEAP_LIMIT_BYTES.

Note that, unfortunately, this does not limit "Large Object Space" allocations, since V8 makes those allocations outside the heap. This includes e.g. large ArrayBuffers, Typed Arrays, and potentially other stuff. I've opened an issue about this here: denoland/deno#26202

Clone Repo:

git clone --recurse-submodules --branch v2.0.0 https://github.com/denoland/deno.git
cd deno
@josephrocca
josephrocca / e5-large-v2.js
Last active August 14, 2024 11:38
e5-large-v2 text embedding model in JavaScript using Transformers.js
// See the comments at the end for a model that does much better than e5-large-v2 while being a third of the size.
let { pipeline } = await import('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.7.0');
let extractor = await pipeline('feature-extraction', 'Xenova/e5-large-v2');
// Note: If you're just comparing "passages" with one another, then just prepend "passage: " to all texts. Only use "query: " if the text is a short "search query" like in the above example.
let passage1 = await extractor(`passage: The Shawshank Redemption is a true masterpiece of cinema.`, { pooling: 'mean', normalize: true });
let passage2 = await extractor(`passage: The film should not be exposed to sunlight when removing it from the wrapper. Otherwise your movie will come out bad.`, { pooling: 'mean', normalize: true });
let query = await extractor(`query: movie review`, { pooling: 'mean', normalize: true });
@josephrocca
josephrocca / BigMap.js
Last active May 19, 2024 21:35
BigMap - wrapper to get past the ~16 million key limit on JavaScript Maps
// only covers a small subset of the Map api!
// haven't debugged yet!
class BigMap {
constructor(iterable) {
if(iterable) throw new Error("haven't implemented construction with iterable yet");
this._maps = [new Map()];
this._perMapSizeLimit = 14000000;
this.size = 0;
}