This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import modal | |
stub = modal.Stub( | |
image=modal.Image.debian_slim().pip_install(["datasets", "torch", "transformers"]) | |
) | |
class Predictor: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == "__main__": | |
secret = modal.Secret({"FOO": os.environ["FOO"]}) | |
else: | |
secret = modal.Secret.from_name("baz") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import sys | |
import modal | |
stub = modal.Stub( | |
image=modal.Image.debian_slim() | |
.apt_install(["git"]) | |
.pip_install( | |
[ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
NewerOlder