This quickstart demonstrates how to build a financial model service that provides stock price predictions and displays dataset information using BentoML. The service includes both REST API and gRPC endpoints.
Python 3.8+ and pip
installed. See the Python downloads page to learn more.
Perform the following steps to run this project and deploy it to BentoCloud.
-
Install the required dependencies:
python -m pip install --upgrade pip pip install -r requirements.txt
-
Serve your model as an HTTP server. This starts a local server at http://localhost:3000, making your model accessible as a web service.
bentoml serve service.py:Svc --reload
-
To serve the gRPC server, run the following command:
python grpc_server.py
-
You can test the gRPC client by running:
python grpc_client.py
-
Once your service is ready, you can deploy it to BentoCloud. Make sure you have logged in to BentoCloud and run the following command to deploy it.
bentoml deploy .
Note: Alternatively, you can manually build a Bento, containerize it with Docker, and deploy it in any Docker-compatible environment.
-
Get Data Head
curl -X POST "http://127.0.0.1:3000/get_data_head"
-
Get Prediction and MSE
curl -X POST "http://127.0.0.1:3000/predict" -H "Content-Type: application/json" -d '{}'
-
Get Data Head
Run the gRPC client:
python grpc_client.py
You can run both the gRPC server and client using Docker Compose. Follow these steps:
Create a Dockerfile named Dockerfile.unified
:
# Use an official Python runtime as a parent image
FROM python:3.10-slim
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV BENTOML_HOME=/bentoml
# Install Python and other dependencies
RUN apt-get update && apt-get install -y python3 python3-pip python3-venv curl && apt-get clean
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Create and activate a virtual environment
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Install gRPC tools
RUN pip install grpcio grpcio-tools
# Expose the port the app runs on
EXPOSE 50051
# Copy the entrypoint script
COPY entrypoint.sh /app/entrypoint.sh
# Make the entrypoint script executable
RUN chmod +x /app/entrypoint.sh
# Run the entrypoint script
CMD ["/app/entrypoint.sh"]
Create a script named entrypoint.sh
:
#!/bin/bash
# Start the gRPC server in the background
python grpc_server.py &
SERVER_PID=$!
# Wait for the gRPC server to start
echo "Waiting for gRPC server to start..."
sleep 10 # Adjust this time as necessary
# Run the gRPC client
python grpc_client.py
# Wait for the gRPC server process to finish
wait $SERVER_PID
Make the entrypoint script executable:
chmod +x entrypoint.sh
Create a docker-compose.yml
file:
version: '3.8'
services:
grpc_service:
build:
context: .
dockerfile: Dockerfile.unified
ports:
- "50051:50051"
networks:
- grpc_network
networks:
grpc_network:
driver: bridge
To build and run the services using Docker Compose, use the following commands:
docker-compose down
docker-compose build
docker-compose up
If you need to stop and remove all containers, images, and volumes, use the following commands:
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images -q) -f
docker volume rm $(docker volume ls -q)
This README.md
file provides a comprehensive guide to set up, run, and deploy your financial model service using BentoML, including both REST and gRPC endpoints. If you encounter any issues or need further assistance, feel free to reach out!