Author(s: Sayar Banerjee Originally published by , the World’s Most Reputable AI and Technology News and Media Company. We invite you to become an AI sponsor if you’re working on an AI product or service. helps technology and AI startups scale. We can help you bring your technology to millions. FastAPI Hosting with Saturn Cloud Deployments Photos by Boitumelo Phetla at Unsplash Hi everyone, today we will be discussing the steps involved in deploying FastAPI applications on Saturn Cloud. FastAPI provides a web platform for creating APIs using the Python language. Saturn Cloud, a platform that scales Machine Learning and Big Data Pipelines and many other things. This model can predict the median California house price. Let's jump right into it. Resource Repository FastAPI Sciencekit-learn Data Exploration “California Housing Prices” is the dataset that I’ll use to train our machine learning model. You can find it here. Our data contains the following: This data pertains only to houses located in California districts. Some summary statistics about these houses are based on the 1990 Census data. The data is not 't perfect, therefore there will be some processing steps. These columns have the names: longitude, latitude, total_rooms, total_bedrooms, median_income and ocean_proximity. A basic exploration of the data revealed the following: Image by Author. The graph shows that most of the features are not correlated with the median_house_value, except for median_income which appears to have an extremely strong positive correlation at around 0 .68.. Data Cleaning/Feature Engineering The total_bedrooms attribute had missing values so I had to imput it. To simplify the task, I used the median to calculate the feature. Additionally, two new features were engineered, namely, “rooms_per_households” and “population_per_household.” This is how carbon.now.sh training the model looks. Made with carbon.now.sh Our repository appears like this: This file will be used for our deployment and it is important to include all dependencies. Carbon.now.sh contains the training script. We will now look at the most important functions of this script. The training model pipeline we use is fairly standard. Only one column is categorical (ocean_proximity). The standard scaler was used for the numerical columns. ColumnTransformer is a tool that facilitates feature transformations with heterogeneous data. The Random Forest algorithm was chosen for my model. I created the pipeline using scikit-learn's Pipeline class. To save my model, I made it with carbon.now.sh and used joblib. Because the model file was large (>100Mb, I chose to save it in AWS S3. Model ' had a R2 score of 0. 81, was the model’s R2 score. The RMSE was approximately 49k. Setup FastAPI Server, Frontend. As you might have guessed app/main.py is our server code. Boto3 was used to create a local copy of the model that is kept in AWS. You may have to create authentication in order to gain access to your file and bucket on Saturn Cloud. This guide will help you do this. It is very easy to create a function that loads our model from AWS. Made with carbon.now.sh. The variables FILE_NAME (and BUCKET_NAME) are obvious. The path that will take the model to a local copy of the model is called LOCAL_PATH. Global variables were also created for the model, app and templates. Made with carbon.now.sh Home Page Since I 'm building an app, it 's important to have a home page that serves as the interface to the model server. To allow users to enter the values for different features, I made a homepage. To render the page, I used Jinja2Templates, which is provided out of the box by FastAPI templates.TemplateResponse renders our landing page titled “index.html.” A carbon.now.sh page index.html is a form which will be used as our frontend. This is the body of the page: Made with carbon.now.sh. If you examine the form tag closely, you’ll see that the action attribute has been set to “/submitform”, and the request type is a POST Request. Made with carbon.now.sh The FastAPI server must have a way to handle the form data. This method needs to be decorated by app.post(“/submitform”) to handle the request appropriately. Carbon.now.sh Each variable is defined as Form parameters. This class tells FastAPI that each variable's input is being received from a form. Also, line 26 contains a method called predict. The input is sent to the model pipeline using this method. The pipeline cannot receive data frames, so I convert data to data frames first. As part of feature engineering, I created the features. Finaly, I send the model 's forecasts. Made with carbon.now.sh Once I had the price prediction, I used templates.TemplateResponse again to return a page called result.html. In addition to “request”, the TemplateResponse method also allowed me to pass “price”. The price was then displayed in the result.html body. Made with carbon.now.sh Adding Saturn Cloud to the installation Before I set up the deployment, all the code was pushed to Github. It is necessary to have the repository linked to Saturn Cloud in order for it be deployed. Follow this guide to learn how. After your repo has been connected, go to resources and choose “New deployment”. Photo by Author You will then be presented with an image: Image by Author The “Command”, which is the command that will be used to launch your application, is an example. Made with carbon.now.sh Saturn Cloud demands that your applications listen to port 8000. Note the header Extra Packages. This script will install any additional packages prior to the command being run. Saturn Cloud '’s default image doesn’t have FastAPI or Uvicorn libraries, so pass the “-r requirements.txt” option to the text field. By doing this, the script “pip installation -r requirements.txt” will be run prior to startup and contains dependencies for additional packages. You can write individual packages’ names in this section. Your deployment will be created once you click the Create button. You can click on the button to add your Github repository to the deployment. Make sure you include the path to Github’s resource in your work directory. After that, click on the green arrow and the deployment will begin. Click on the public URL to confirm your deployment. The page should look something like the following: Image by Author After you have filled out the form you’ll see the page showing the expected price. Image By Author Please note that the input I provided was the test set last. So the model was able to predict $133000,, which is the actual median price of a house. You are now done! Congratulations! You’ve successfully learned how Saturn Cloud FastAPI works. They offer 30 hours of free use for data scientists, teams and other interested parties. This article was enjoyable. Until next time! Data Science originally appeared in
Hosting FastAPI using Saturn Cloud Deployments

