Skip to content

Instantly share code, notes, and snippets.

View jetnew's full-sized avatar
♟️

Jet New jetnew

♟️
View GitHub Profile
@jetnew
jetnew / classifier.py
Last active September 28, 2024 19:02
Super simple zero-shot text classifier using OpenAI's function calling API.
import json
import openai
openai.api_key = "sk-"
def classifier(description, labels, label_descriptions):
def classify(text):
function = {
"name": "Classify",
"description": description,
@jetnew
jetnew / auth_server.py
Created August 16, 2023 04:53
Minimal Flask server for Google API credentials OAuth2 authentication.
import os
from flask import Flask, session, abort, redirect, request
from google_auth_oauthlib.flow import Flow
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Get client secrets file from https://console.cloud.google.com/apis/credentials -> OAuth client ID -> Web application
flow = Flow.from_client_secrets_file(
@jetnew
jetnew / agent_tools.py
Created August 9, 2023 02:17
Convert function to OpenAI function calling API format.
import json
import openai
from inspect import signature, Parameter
openai.api_key = "sk-"
def func2tool(func):
"""Convert a function into OpenAI function calling API format."""
assert func.__doc__, f"Function {func.__name__} must have a docstring."
@jetnew
jetnew / gol.py
Created October 26, 2021 05:01
Game of life with scipy convolution
import numpy as np
from scipy.ndimage import convolve
dim = 10
board = np.zeros((dim, dim), dtype=np.uint8)
kernel = np.array([[1,1,1],
[1,0,1],
[1,1,1]], dtype=np.uint8)
def step(board, kernel=kernel):
@jetnew
jetnew / colab-requirements.txt
Created July 13, 2021 04:50
pip freeze of google colab
absl-py==0.12.0
alabaster==0.7.12
albumentations==0.1.12
altair==4.1.0
appdirs==1.4.4
argon2-cffi==20.1.0
arviz==0.11.2
astor==0.8.1
astropy==4.2.1
astunparse==1.6.3
@jetnew
jetnew / lstm_forecasting.py
Created June 6, 2019 12:19
LSTM Forecasting using Keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.metrics import mean_squared_error
timesteps = window_size-1
n_features = 1
model = Sequential()
model.add(LSTM(16, activation='relu', input_shape=(timesteps, n_features), return_sequences=True))
model.add(LSTM(16, activation='relu'))
@jetnew
jetnew / rolling_windows.py
Created June 6, 2019 11:40
Rolling Windows using skimage
from skimage.util import view_as_windows
window_size = 5
timeseries = np.array([1,2,3,4,5,6,7,8,9,10])
trajectory_matrix = view_as_windows(timeseries, window_shape=window_size)
@jetnew
jetnew / lstm_autoencoder.py
Last active September 16, 2022 02:49
LSTM Autoencoder using Keras
from keras.layers import LSTM, Dense, RepeatVector, TimeDistributed
from keras.models import Sequential
class LSTM_Autoencoder:
def __init__(self, optimizer='adam', loss='mse'):
self.optimizer = optimizer
self.loss = loss
self.n_features = 1
def build_model(self):
@jetnew
jetnew / hierarchical-clustering.py
Last active June 2, 2019 02:25
Hierarchical Clustering using scikit-learn and scipy
from sklearn.cluster import AgglomerativeClustering
clusters = 3
y_pred = AgglomerativeClustering(n_clusters=clusters).fit_predict(X)
from scipy.cluster.hierarchy import linkage, fcluster, dendrogram
clusters=5
cls = linkage(X, method='ward')
@jetnew
jetnew / hbos.py
Created June 2, 2019 01:58
HBOS using kenchi
from kenchi.outlier_detection.statistical import HBOS
hbos = HBOS(novelty=True).fit(X)
y_pred = hbos.predict(X)