From 2db34c5ce7645ee7b7c67c1734e94e51d301f790 Mon Sep 17 00:00:00 2001 From: beta9 Date: Thu, 24 Oct 2024 17:00:50 -0400 Subject: [PATCH] Add `beam` deployment example --- examples/beam-cloud/README.md | 5 +++++ examples/beam-cloud/app.py | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 examples/beam-cloud/README.md create mode 100644 examples/beam-cloud/app.py diff --git a/examples/beam-cloud/README.md b/examples/beam-cloud/README.md new file mode 100644 index 000000000..5f190d76f --- /dev/null +++ b/examples/beam-cloud/README.md @@ -0,0 +1,5 @@ +## Deploy Outlines on Beam + +1. Create an account [here](https://beam.cloud) and install the Beam SDK +2. Download the `app.py` file to your computer +3. Deploy it as a serverless API by running: `beam deploy app.py:predict` diff --git a/examples/beam-cloud/app.py b/examples/beam-cloud/app.py new file mode 100644 index 000000000..fb6c2cb2b --- /dev/null +++ b/examples/beam-cloud/app.py @@ -0,0 +1,39 @@ +from beam import Image, endpoint, env + +if env.is_remote(): + import outlines + + +# Pre-load models when the container first starts +def load_models(): + import outlines + + model = outlines.models.transformers("microsoft/Phi-3-mini-4k-instruct") + return model + + +@endpoint( + name="outlines-serverless", + gpu="A10G", + cpu=1, + memory="16Gi", + on_start=load_models, + image=Image().add_python_packages( + ["outlines", "torch", "transformers", "accelerate"] + ), +) +def predict(context, **inputs): + default_prompt = """You are a sentiment-labelling assistant. + Is the following review positive or negative? + + Review: This restaurant is just awesome! + """ + + prompt = inputs.get("prompt", default_prompt) + + # Unpack cached model from context + model = context.on_start_value + # Inference + generator = outlines.generate.choice(model, ["Positive", "Negative"]) + answer = generator(prompt) + return {"answer": answer}