Skip to content

Instantly share code, notes, and snippets.

@python273
python273 / README.md
Last active April 21, 2023 13:53
Transformers LLaMA
View README.md
conda create -n llama
conda activate llama

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
conda install cudatoolkit==11.8.0 -c conda-forge
python -s -m pip install bitsandbytes accelerate sentencepiece
python -s -m pip install git+https://github.com/huggingface/transformers
@python273
python273 / app.py
Last active January 23, 2024 12:52
Flask Streaming Langchain Example
View app.py
import os
os.environ["OPENAI_API_KEY"] = ""
from flask import Flask, Response, request
import threading
import queue
from langchain.chat_models import ChatOpenAI
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import AIMessage, HumanMessage, SystemMessage
@python273
python273 / app.py
Created March 25, 2023 04:36
Flask SSE demo
View app.py
from flask import Flask, Response
import time
import json
app = Flask(__name__)
@app.route('/')
def index():
return Response('''
<!DOCTYPE html>
View benchmark_ram_to_vram.py
import time
import numpy as np
import pyopencl as cl
def main():
bytes_size = 10*1024*1024*1024
num_of_floats = bytes_size // 4
devices = sum([x.get_devices(device_type=cl.device_type.GPU) for x in cl.get_platforms()], [])
device = devices[0]
@python273
python273 / yt-0-fix-one-channel-audio.js
Last active April 26, 2023 12:40
Youtube: fix one channel audio
View yt-0-fix-one-channel-audio.js
(function (){
var context = new AudioContext();
source = context.createMediaElementSource(document.querySelector('video'));
splitter = context.createChannelSplitter();
merger = context.createChannelMerger();
source.connect(splitter);
splitter.connect(merger, 0, 0); // first 0 is input channel
splitter.connect(merger, 0, 1);
merger.connect(context.destination);
})();
View dumpmem.py
from pathlib import Path
import os
import ctypes, re, sys
from pprint import pprint
import traceback
c_ptrace = ctypes.CDLL("libc.so.6").ptrace
c_pid_t = ctypes.c_int32
c_ptrace.argtypes = [ctypes.c_int, c_pid_t, ctypes.c_void_p, ctypes.c_void_p]
@python273
python273 / README.md
Created May 12, 2021 18:03
JavaScript: how to find screen coordinates of a substring in an element OR how to hightlight words
View README.md

JavaScript: how to find screen coordinates of a substring in an element OR how to hightlight words

Firefox's Reader mode highlights words when using text-to-speech. Here is related code:

const h = new Highlighter(window, document.getElementById('your-el'));
// highlight(startOffset, length)
h.highlight(8, 12);
@python273
python273 / README.md
Created May 5, 2021 18:11
Rust piping fix (e.g. when piping into less or head)
View README.md
thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:940:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
use std::error;
use std::io::Write;
View midi_tempo_convert.py
import time
from mido import MidiFile
from mido import bpm2tempo, tempo2bpm
mid = MidiFile('some.midi')
new_tempo = 75
# track numbers are hardcoded, might be different
old_tempo = tempo2bpm(mid.tracks[0][2].tempo)
@python273
python273 / date_ranges_overlapping.py
Last active April 20, 2021 20:03
Python Date Range & number of unique overlapping days with date range
View date_ranges_overlapping.py
# -*- coding: utf-8 -*-
"""
:authors: python273
:license: Apache License, Version 2.0
:copyright: (c) 2018 python273
"""
from datetime import datetime, timedelta
from functools import reduce
import operator