Skip to content

Instantly share code, notes, and snippets.

Avatar

Erik Bernhardsson erikbern

View GitHub Profile
View gist:ec72705a80f8c712d6dc399ef436c599
if __name__ == "__main__":
secret = modal.Secret({"FOO": os.environ["FOO"]})
else:
secret = modal.Secret.from_name("baz")
@erikbern
erikbern / modal_stable_diffusion_2.py
Last active Nov 25, 2022
Run Stable Diffusion 2.0 on Modal
View modal_stable_diffusion_2.py
import io
import sys
import modal
stub = modal.Stub(
image=modal.Image.debian_slim()
.apt_install(["git"])
.pip_install(
[
View valhalla_modal_example.py
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_run_notebook.py
Last active Aug 24, 2022
Run Notebook inside Modal
View modal_run_notebook.py
# 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
@erikbern
erikbern / modal_prophet.py
Created Jul 29, 2022
Run Prophet inside Modal
View modal_prophet.py
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 Jul 21, 2022
Output a warning if a generator function is called but never consumed
View warn_if_generator_is_not_consumed.py
# 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:
@erikbern
erikbern / kaplan_meier_for_revenue.py
Last active Jul 24, 2022
Kaplan-Meier for multiple revenue events
View kaplan_meier_for_revenue.py
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 / asyncio_coroutine_interceptor.py
Last active Jun 2, 2022
Send data to coroutines that do async things
View asyncio_coroutine_interceptor.py
async def intercept_coro(coro, interceptor):
# This roughly corresponds to https://gist.github.com/erikbern/ad7615d22b700e8dbbafd8e4d2f335e1
# The underlying idea is that we can execute a coroutine ourselves and use it to intercept
# any awaitable object. This lets the coroutine await arbitrary awaitable objects, not just
# asyncio futures. See how this is used in object.load.
value_to_send = None
while True:
try:
awaitable = coro.send(value_to_send)
assert inspect.isawaitable(awaitable)
@erikbern
erikbern / loop_hack.py
Last active Apr 3, 2022
Example of how to use async/await programming in Python for non-asyncio purposes
View loop_hack.py
# First, let's create an awaitable object.
# In this case it's a very dumb container of integers.
# Any time a coroutine runs `await Thing(n)` it just resolves to n
# However, we could resolve it to something completely different if we wanted to
class Thing:
def __init__(self, n):
self._n = n
def __await__(self):
View asynchronous_constructor.py
import asyncio
class AsyncConstructorMeta(type):
"""Metaclass to support asynchronous constructors in Python.
Basically we're exploiting the fact that __new__ can return anything in Python.
So we're taking the old __init__ code, removing it from the class, and instead,
we create a custom __new__ method that returns a coroutine wrapping the original
constructor.