Skip to content

Instantly share code, notes, and snippets.

View erikbern's full-sized avatar

Erik Bernhardsson erikbern

View GitHub Profile
@erikbern
erikbern / use_pfx_with_requests.py
Last active April 24, 2024 13:48
How to use a .pfx file with Python requests – also works with .p12 files
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
@contextlib.contextmanager
def pfx_to_pem(pfx_path, pfx_password):
''' Decrypts the .pfx file to be used with requests. '''
@erikbern
erikbern / kaplan_meier_for_revenue.py
Last active October 14, 2023 19:04
Kaplan-Meier for multiple revenue events
from matplotlib import pyplot
import random
import time
pyplot.style.use("ggplot")
now = time.time()
def generate_user(censor=now):
# Pick some point in time the user was created
t_created = t = now - random.random() * 1e7
@erikbern
erikbern / modal_stable_diffusion_2.py
Last active July 29, 2023 20:10
Run Stable Diffusion 2.0 on Modal
import io
import sys
import modal
stub = modal.Stub(
image=modal.Image.debian_slim()
.apt_install(["git"])
.pip_install(
[
@erikbern
erikbern / install-tensorflow.sh
Last active June 26, 2023 00:40
Installing TensorFlow on EC2
# Note – this is not a bash script (some of the steps require reboot)
# I named it .sh just so Github does correct syntax highlighting.
#
# This is also available as an AMI in us-east-1 (virginia): ami-cf5028a5
#
# The CUDA part is mostly based on this excellent blog post:
# http://tleyden.github.io/blog/2014/10/25/cuda-6-dot-5-on-aws-gpu-instance-running-ubuntu-14-dot-04/
# Install various packages
sudo apt-get update
@erikbern
erikbern / modal_run_notebook.py
Last active February 20, 2023 14:24
Run Notebook inside Modal
# Note: this assumes you have a notebook locally named my_notebook.ipynb
import modal
stub = modal.Stub(
image=modal.DebianSlim().pip_install(["papermill", "ipykernel"]),
mounts=[modal.Mount(local_file="my_notebook.ipynb", remote_dir="/root")],
)
@stub.function
import sys
import modal
stub = modal.Stub(
image=modal.Image.debian_slim().pip_install(["datasets", "torch", "transformers"])
)
class Predictor:
if __name__ == "__main__":
secret = modal.Secret({"FOO": os.environ["FOO"]})
else:
secret = modal.Secret.from_name("baz")
import json
import subprocess
import sys
import tempfile
import modal
stub = modal.Stub()
stub.sv = modal.SharedVolume().persist("valhalla")
image = modal.DockerhubImage("valhalla/valhalla:run-latest", setup_commands=["apt-get update", "apt-get install -y python3-pip"])
@erikbern
erikbern / modal_prophet.py
Created July 29, 2022 16:04
Run Prophet inside Modal
import io
import modal
stub = modal.Stub(image=modal.DebianSlim().pip_install(["prophet"]))
@stub.function
def run():
import pandas as pd
from prophet import Prophet
from matplotlib import pyplot
@erikbern
erikbern / warn_if_generator_is_not_consumed.py
Last active July 21, 2022 16:32
Output a warning if a generator function is called but never consumed
# Let's say you have a method `map(f, inputs)` that performs some operation on inputs
# Let's also sayu that this function returns a generator.
# A user might pass a function f that has some side effect, e.g. `def f(x): print(x)`
# This might lead to confusing results if the output is never consumed:
# `map(f, inputs)` -> this does nothing, can be confusing to the user
# `list(map(f, inputs))` -> this executes `f` on all inputs
# To remedy the situation, we can provide a helpful warning to the user if the generator
# is never consumed.
### Helper code: