Skip to content

Instantly share code, notes, and snippets.

@perryism
perryism / gcp_token.py
Created January 11, 2024 18:01
Get GCP identity or access token programmatically
import google.auth
from google.auth.transport.requests import Request
import requests
def get_credentials() -> str:
credentials, _project_id = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
request = Request()
credentials.refresh(request)
return credentials
@perryism
perryism / lora.py
Created November 15, 2023 18:44
Lora Low rank adaptation
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, HfArgumentParser, TrainingArguments, pipeline, logging, TextStreamer
from peft import LoraConfig, PeftModel, prepare_model_for_kbit_training, get_peft_model
import os,torch, platform, warnings
from datasets import load_dataset
from trl import SFTTrainer
class Lora:
@classmethod
def prepare(cls, model_name):
"""
import unittest
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)
class Agent:
def __init__(self, role, objective):
@perryism
perryism / gist:a1391ccd1df8be462133cf41f82f8272
Last active September 22, 2023 23:37
This little class can help create command line toolbox easily.
import argparse
import logging
from enum import EnumMeta
logger = logging.getLogger(__name__)
class Blackmore:
def __init__(self, name, functions):
self.name = name
self.functions = dict([[f.__name__, f] for f in functions])
@perryism
perryism / gist:23e1aee4193e085fe67bcef2f1004aa1
Created July 28, 2023 23:31
Query bigquery on behave of an user
from google.oauth2.credentials import Credentials
from google.cloud import bigquery
import google.oauth2.credentials
import os
PROJECT = os.environ.get("PROJECT")
# gcloud auth print-access-token
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
@perryism
perryism / seuqential_chain.py
Created June 24, 2023 17:26
Simplied SequentialChain in LangChain
from langchain.chains import SequentialChain
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature=0.9)
class MyChain:
def __init__(self, llm=None, template_engine=None):
self.llm = llm
@perryism
perryism / import_certs.sh
Created June 14, 2023 22:40
Import certs from a folder to a java runtime
JAVA_HOME=$(java_home -v 18)
CERT_FOLDER=/Users/plee/.certs
KEYSTORE=$JAVA_HOME/lib/security/cacerts
for cert in $(ls $CERT_FOLDER)
do
sudo keytool -import -alias $(basename $cert .cer) -keystore $KEYSTORE -file $CERT_FOLDER/$cert
done
#sudo keytool -list -keystore $KEYSTORE | grep -i <cert name>
@perryism
perryism / markov.py
Created May 2, 2023 19:05
Markov model
inputs = """red pepper pasta
garlic noodles
pizza
biryani
roti curry
flat rice noodles
hakka noodles
bhel puri
sushi
ramen"""
@perryism
perryism / handler.py
Last active November 17, 2023 23:44
simple api gateway
def handle(body):
body["foo"] = "bar"
return body
@perryism
perryism / README.md
Created April 12, 2023 16:21
serving xgboost model in Vertex AI with customer container

Overview

I need to create a vertex ai endpoint with different functions. There are two options. 1. one endpoint per function. 1. one endpoint for all functions. This is an attempt to handle different functions in a single endpoint.

Base image

The base image requires three files.

  1. requirements.txt - it will be used to build the base docker image
  2. handler.py - it is the fastapi handler which we have direct access to request and response objects.