Skip to content

Instantly share code, notes, and snippets.

View duhaime's full-sized avatar

Douglas Duhaime duhaime

View GitHub Profile
@duhaime
duhaime / stream_in.py
Created July 16, 2020 19:48
Mido port streaming
import mido
with mido.open_input('mido_port', virtual=True) as in_port:
for msg in in_port:
print(msg)
@duhaime
duhaime / boids.py
Created July 2, 2020 20:44
Python boids
# -----------------------------------------------------------------------------
# From Numpy to Python
# Copyright (2017) Nicolas P. Rougier - BSD license
# More information at https://github.com/rougier/numpy-book
# -----------------------------------------------------------------------------
from matplotlib.animation import FuncAnimation
from collections import namedtuple
import matplotlib.pyplot as plt
import numpy as np
import random
@duhaime
duhaime / flask-app.py
Created May 29, 2020 21:19
Flask and Starlette Servers for GPT-2
from flask import Flask, jsonify, request
import gpt_2_simple as gpt2
import tensorflow as tf
import os
if not os.path.exists('checkpoint'):
os.makedirs(os.path.join('checkpoint', 'run1'))
gpt2.download_gpt2(model_name='117M')
os.system('mv models/117M/* checkpoint/run1/')
@duhaime
duhaime / fetch_reddit_pushshift.py
Created May 27, 2020 14:51
Fetch Reddit Pushshift
from dateutil.relativedelta import relativedelta
from datetime import date, datetime
import json
import requests
import calendar
import time
import sys
end = date.today()
start = end - relativedelta(years=3)
@duhaime
duhaime / play.py
Last active May 23, 2020 17:11
play midi file
# conda install -c cogsci pygame
import pygame, base64
def midi_to_base64(midi_filename):
return base64.encodestring(open(midi_filename, 'rb').read())
def play_music(midi_filename):
'''Stream music_file in a blocking manner'''
clock = pygame.time.Clock()
pygame.mixer.music.load(midi_filename)
@duhaime
duhaime / geb.txt
Created May 12, 2020 18:27
Goedel Escher Bach plaintext
This file has been truncated, but you can view the full file.
Introduction:
A Musico-Logical Offering
Author: FREDERICK THE GREAT, King of Prussia, came to power in 1740. Although he is emembered in history books mostly for his military astuteness, he was also devoted to he life of the mind and the spirit. His court in Potsdam was one of the great centers of ntellectual activity in Europe in the eighteenth century. The celebrated mathematician
@duhaime
duhaime / fetch from localhost
Created May 8, 2020 17:53
Fetch from localhost
// NB: To load from localhost one can:
fetch(url)
.then(response => response.blob())
.then(async image => {
const img = document.createElement('img');
img.src = URL.createObjectURL(image);
img.onload = async () => {}
})
import numpy as np
import sys
import struct
dim = 50
a = np.load('stacked-vecs-{}.npy'.format(dim))
with open('stacked.fvecs', 'wb') as out:
for i in a:
line = [float(j) for j in i]
@duhaime
duhaime / rect-packing.ipynb
Created April 14, 2020 17:40
Rectangle Packing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@duhaime
duhaime / download.py
Created April 12, 2020 13:20
Download Unsplash Images
import os, requests, json
q = 'recipe'
p = 0
imgs = []
out_dir = 'fetched-images'
if not os.path.exists(out_dir): os.makedirs(out_dir)
while True: