> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beam.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Install the Beam CLI, sign in, and connect coding agents to the Beam MCP server with `curl -fsSL https://www.beam.cloud/install | sh`. To also create a VM, run `curl -fsSL https://www.beam.cloud/install | sh -s -- --vm`.
> VMs are persistent Linux machines without GPUs. Defaults: 4 CPUs, 8 GiB of RAM, a 50 GiB disk, Ubuntu 24.04, and a browser desktop. A running VM accrues usage until you stop, pause, or remove it, or until an idle timeout puts it to sleep.
> Stopping a VM keeps its disk and discards RAM and processes. Pausing keeps both, and resuming restores them. To pause after 5 idle minutes and wake on access, run `beam vm update NAME --ttl 300 --idle-action pause --auto-resume`.
> Core VM commands: `beam vm new NAME`, `beam vm ssh NAME`, `beam vm sync NAME ./dir`, `beam vm exec NAME -- CMD`, `beam vm expose NAME PORT --protected`, `beam vm fork NAME NEW_NAME`, `beam vm pause NAME`, `beam vm stop NAME`, `beam vm rm NAME`.
> For GPUs, use a sandbox or a deployed endpoint. Sandboxes are disposable environments that boot in seconds and fork from snapshots.
> Current rates are at https://www.beam.cloud/pricing.

# Deploying Huggingface Models

> A short tutorial on using pre-trained Huggingface models

<form action="https://github.com/slai-labs/get-beam/tree/main/examples/sentiment-analysis" method="get" target="_blank">
  Try this example on Github
</form>

### Define the environment

The first thing we'll do is define the environment that our app will run on. For
this example, we're building a Sentiment Analysis model using Huggingface.

First, you'll define a [`Runtime`](https://docs.beam.cloud/v2/reference/py-sdk) with an [`Image`](https://docs.beam.cloud/v2/reference/py-sdk#image).

We're going to be defining which packages to install in the runtime, and the hardware this code will run on.

```python app.py theme={null}
from beam import App, Runtime, Image

app = App(
    name="sentiment-analysis",
    runtime=Runtime(
        cpu=2,
        memory="8Gi",
        image=Image(
            python_version="python3.9",
            python_packages=["transformers", "torch"],
        ),
    ),
)
```

### Inference function

Now, we'll write some code to predict the sentiment of a given text prompt.

Our function takes keyword arguments, as `(**inputs)`.

```python run.py theme={null}
from transformers import pipeline

def predict_sentiment(**inputs):
    model = pipeline(
        "sentiment-analysis", model="siebert/sentiment-roberta-large-english"
    )
    result = model(inputs["text"], truncation=True, top_k=1)
    prediction = {i["label"]: i["score"] for i in result}

    print(prediction)

    return {"prediction": prediction}
```

### Adding a REST API

To prepare to deploy the API, we'll add a [`rest_api`](https://docs.beam.cloud/v2/reference/py-sdk) decorator to our inference function.

Add the following decorator to your `predict_sentiment` function.

```python app.py theme={null}
@app.rest_api()
def predict_sentiment(**inputs):
    ...
```

The complete `app.py` file will look like this:

```python app.py theme={null}
from beam import App, Runtime, Image
from transformers import pipeline

app = App(
    name="sentiment-analysis",
    runtime=Runtime(
        cpu=2,
        memory="8Gi",
        image=Image(
            python_version="python3.9",
            python_packages=["transformers", "torch"],
        ),
    ),
)


@app.rest_api()
def predict_sentiment(**inputs):
    model = pipeline(
        "sentiment-analysis", model="siebert/sentiment-roberta-large-english"
    )

    result = model(inputs["text"], truncation=True, top_k=1)
    prediction = {i["label"]: i["score"] for i in result}

    print(prediction)

    return {"prediction": prediction}

```

### Deploying the app

To deploy the model, enter your terminal and `cd` to the directory you're
working on.

Then, run the following:

```
beam deploy app.py
```

After running this command, you'll see some logs in the console that show the progress of your deployment.

> [accordion] Show Logs
>
> ```python theme={null}
> user@MacBook sentiment-analysis % beam deploy app.py
>  i  Copying files in workspace './'...
>     Done.
>  i  Adding python requirements...
>  i  	torch
>  i  	transformers
>     Done.
>  i  Using cached image.
>     <sentiment-analysis> deployed successfully!
>  i  ID: 64c131ff2628c00009ebefc8
>  i  Version: 6
>  i  Trigger type: rest_api
>  i  CPU: 2000m
>  i  Memory: 8Gi
>  i  Runtime: python3.9
>  i  Send requests to: https://hf-inference-712408b-v1.app.beam.cloud
>  i  View deployment status at: https://www.beam.cloud/apps/hf-inference/deployment/64c131ff2628c00009ebefc8/logs
> ```

At the bottom of the console, you'll see a URL for invoking your function.
Here's what a cURL request would look like:

```bash theme={null}
  curl -X POST --compressed "https://hf-inference-712408b-v1.app.beam.cloud" \
   -H 'Accept: */*' \
   -H 'Accept-Encoding: gzip, deflate' \
   -H 'Authorization: Basic [YOUR_AUTH_TOKEN]' \
   -H 'Connection: keep-alive' \
   -H 'Content-Type: application/json' \
   -d '{"text": "If we override the bandwidth, we can get to the SMTP capacitor through the cross-platform RSS alarm!"}'
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.