Skip to main content
PyTorch is one of the most popular deep learning frameworks, particularly for natural language processing tasks. This tutorial will guide you through creating a sentiment analysis API using PyTorch and FastAPI, then deploying it on the Upsun Cloud Application Platform.

Prerequisites

  • An Upsun account
  • Basic knowledge of Python
  • Familiarity with REST APIs
  • Git installed on your local machine

Setting Up a new Python application on Upsun

Let’s start by creating a new Python application on Upsun. We’ll use Python 3.12, which is well-supported by both PyTorch and Upsun.

1. Create a new project directory

First, let’s create a new directory for our project and initialize it as a Git repository:

2. Configure the Upsun project

Create the necessary Upsun configuration files to define our application’s environment. First, create a .upsun directory and then a config.yaml file inside it:
Now, open the config.yaml file and add the following configuration:

Creating a FastAPI application for sentiment analysis

Now, let’s create our FastAPI application that will serve as the API for our sentiment analysis model.

1. Set up the project structure

Let’s organize our project with the following structure:

2. Define the requirements

Create a requirements.txt file with the necessary dependencies:
Notice how we’re using the --extra-index-url directive to specify that we want the CPU-only version of PyTorch, which is much smaller and suitable for deployment on CPU based containers.

3. Create the Model module

Let’s create a model.py file that will handle the sentiment analysis logic:

4. Create the FastAPI application

Now, let’s create the main.py file for our FastAPI application:

Deploying the application to Upsun

Now that we have our application ready, let’s deploy it to Upsun.

1. Initialize Git repository

Make sure all files are added to Git:

2. Create a new Upsun project

You can create a new Upsun project through the Upsun Console or using the Upsun CLI:
The Upsun remote should be set automatically on the repository. If it’s not, use

3. Push to Upsun

Deploy your application by pushing your code to Upsun:
Upsun will automatically build your application according to the configuration files we created. This process includes installing dependencies, compiling Python files, and starting the FastAPI server.

4. Access your application

Once the deployment is complete, you can access your application at the URL provided by the Upsun console or with upsun url --primary. You should see the welcome message when you visit the root endpoint.

Testing the Sentiment Analysis API

Now that our application is deployed, let’s test it by sending a request to the sentiment analysis endpoint.

Using cURL

You should receive a response like:

Using the Swagger UI

FastAPI automatically generates interactive API documentation. You can access it by navigating to /docs on your application URL:
This will open the Swagger UI where you can test the API through a user-friendly interface. Swagger

Advanced considerations

Model size and disk space

The DistilBERT model we’re using is relatively small, but if you’re working with larger models, you might need to increase the disk space and the memory allocated in the Upsun configuration.

Performance optimization

For better performance, you might want to:
  1. Pre-load the model during the build phase
  2. Use model quantization to reduce size
  3. Consider implementing caching for frequently requested inputs

Conclusion

In this tutorial, you’ve learned how to:
  1. Set up a Python application on Upsun
  2. Create a FastAPI app with a sentiment analysis endpoint
  3. Configure PyTorch (CPU version) for efficient deployment
  4. Deploy and test your application
Upsun provides an excellent platform for hosting CPU-based PyTorch applications, with its flexible configuration options and robust scaling capabilities. This approach allows you to deliver machine learning functionality through APIs or in your application without managing complex infrastructure. Happy coding and machine learning with Upsun!
Last modified on April 27, 2026