Dogecoin bulls showed up strong as the memecoin demonstrated its dominant position
Whales have been accumulating, but exchange flows suggested that profit taking is happening
PR firm claimed to unveil Satoshi Nakamoto’s identity on the 31st of October.
Experts believe that uncovering Nakamoto could have wide-reaching effects on the crypto market.
We hear a lot about productionized machine learning, but what does it really mean to have a model that can thrive in real-world applications?There are plenty of things that go into, and contribute, to the efficacy of a machine learning model in production. For the sake of this article we will be focusing on five of them.
Reproducibility
Monitoring
Testing
Automation
Version Control
Serving Inferences
The most important part of building a production-ready machine learning model is being able to access it.
For this purpose, we build a fastapi client that serves sentiment analysis responses. We utilize pydantic to ensure structure for the input and output. The model that we use is the base sentiment analysis pipeline from huggingface’s transformers library, allowing us to begin testing with a pre-trained model.
# Filename: main.py from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline
To install this, initialize venv in your files and run:pip install -r requirements.txt.
To host this API simply run: uvicorn main:app –reload.
Now you have an api that you can query using:
curl -X POST "http://localhost:8000/predict" -H "Content-Type: application/json" -d '{"text": "I love using FastAPI!"}'
or any API tool you wish (i.e. Postman). You should get a result back that includes the text query, the sentiment predicted, and the confidence of the prediction.
We now have a locally hosted machine learning inference API.
Further Improving Reproducibility
To allow our code to have more consistent execution, we will utilize Docker. Docker simulates a lightweight environment that allows applications to run in isolated containers, similar to virtual machines. This isolation ensures that applications can execute consistently across any computer with Docker installed, regardless of the underlying system.
Now, you can build the image and run this API using:
# Build the Docker image docker build -t sentiment-api .
# Run the container docker run -p 8000:8000 sentiment-api
You should now be able to query just as you did before.
curl -X POST "http://localhost:8000/predict" -H "Content-Type: application/json" -d '{"text": "I love using FastAPI!"}'
We now have a containerized, locally hosted machine learning inference API.
Adding Basic Monitoring
In machine learning applications, monitoring is crucial for understanding model performance and ensuring it meets expected accuracy and efficiency. Tools like Prometheus help track metrics such as prediction latency, request counts, and model output distributions, enabling you to identify issues like model drift or resource bottlenecks. This proactive approach ensures that your ML models remain effective over time and can adapt to changing data or usage patterns. In our case, we are focused on prediction time, requests, and gathering information about our queries.
from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline from prometheus_client import Counter, Histogram, start_http_server import time
# Start prometheus metrics server on port 8001 start_http_server(8001)
While the process of building and fine-tuning a model is not the intent of this project, it is important to understand how a model can be added to this process.
# Filename: train.py
import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification from datasets import load_dataset from torch.utils.data import DataLoader
# Use GPU if available device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device)
model.train()
# Create a DataLoader for batching dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
# Training loop num_epochs = 3 # Set the number of epochs for epoch in range(num_epochs): total_loss = 0 for batch in dataloader: inputs = tokenizer(batch["text"], truncation=True, padding=True, return_tensors="pt", max_length=512).to(device) labels = torch.tensor(batch["label"]).to(device)
optimizer.zero_grad() outputs = model(**inputs, labels=labels) loss = outputs.loss
# Save the model model.save_pretrained("./model/") tokenizer.save_pretrained("./model/")
# Test the model with sample sentences test_sentences = [ "This movie was fantastic!", "I absolutely hated this film.", "It was just okay, not great.", "An absolute masterpiece!", "Waste of time!", "A beautiful story and well acted.", "Not my type of movie.", "It could have been better.", "A thrilling adventure from start to finish!", "Very disappointing." ]
# Switch model to evaluation mode model.eval()
# Prepare tokenizer for test inputs inputs = tokenizer(test_sentences, truncation=True, padding=True, return_tensors="pt", max_length=512).to(device)
with torch.no_grad(): outputs = model(**inputs) predictions = torch.argmax(outputs.logits, dim=1)
# Print predictions for sentence, prediction in zip(test_sentences, predictions): sentiment = "positive" if prediction.item() == 1 else "negative" print(f"Input: "{sentence}" -> Predicted sentiment: {sentiment}")
# Call the function to train the model and test it train_model()
To make sure that we can query our new model that we have trained we have to update a few of our existing files. For instance, in main.py we now use the model from ./model and load it as a pretrained model. Additionally, for comparison’s sake, we add now have two endpoints to use, /predict/naive and predict/trained.
# Filename: main.py
from fastapi import FastAPI from pydantic import BaseModel from transformers import AutoModelForSequenceClassification, AutoTokenizer from transformers import pipeline from prometheus_client import Counter, Histogram, start_http_server import time
# Start prometheus metrics server on port 8001 start_http_server(8001)
app = FastAPI()
# Load the trained model and tokenizer from the local directory model_path = "./model" # Path to your saved model tokenizer = AutoTokenizer.from_pretrained(model_path) trained_model = AutoModelForSequenceClassification.from_pretrained(model_path)
Importantly, if you are using git, make sure that you add the pytorch_model.bin file to git lfs, so that you can push to GitHub. git lfs allows you to use version control on very large files.
Adding Testing and CI/CD
CI/CD and testing streamline the deployment of machine learning models by ensuring that code changes are automatically integrated, tested, and deployed, which reduces the risk of errors and enhances model reliability. This process promotes continuous improvement and faster iteration cycles, allowing teams to deliver high-quality, production-ready models more efficiently. Firstly, we create two very basic tests to ensure that our model is performing acceptably.
# Filename: test_model.py
import pytest from fastapi.testclient import TestClient from main import app
Now, whenever we push to GitHub, it will run an automated process that checks out the code, sets up a Python 3.9 environment, installs dependencies, and runs our tests using pytest.
Conclusion
In this project, we’ve developed a production-ready sentiment analysis API that highlights key aspects of deploying machine learning models. While it doesn’t encompass every facet of the field, it provides a representative sampling of essential tasks involved in the process. By examining these components, I hope to clarify concepts you may have encountered but weren’t quite sure how they fit together in a practical setting.
Minimum Viable MLE was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.
The NYT Mini crossword might be a lot smaller than a normal crossword, but it isn’t easy. If you’re stuck with today’s crossword, we’ve got answers for you here.
Connections is the new puzzle game from the New York Times, and it can be quite difficult. If you need a hand with solving today’s puzzle, we’re here to help.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.