<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bala Murugan on Medium]]></title>
        <description><![CDATA[Stories by Bala Murugan on Medium]]></description>
        <link>https://medium.com/@bala.rhapsodic?source=rss-20f3d4dd696f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*AZUTjKuXtV0bt-iz</url>
            <title>Stories by Bala Murugan on Medium</title>
            <link>https://medium.com/@bala.rhapsodic?source=rss-20f3d4dd696f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 23:12:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bala.rhapsodic/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[FastAPI for Machine Learning Models]]></title>
            <link>https://medium.com/@bala.rhapsodic/fastapi-for-machine-learning-models-2278d92668aa?source=rss-20f3d4dd696f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2278d92668aa</guid>
            <dc:creator><![CDATA[Bala Murugan]]></dc:creator>
            <pubDate>Sat, 07 Sep 2024 06:21:27 GMT</pubDate>
            <atom:updated>2024-09-07T06:27:45.674Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is FastAPI?</h4><p>FastAPI is a modern web framework for building APIs (Application Programming Interfaces) with Python based on standard Python type hints.</p><p>It’s like a tool that helps create websites or apps that other programs can easily talk with.</p><h4><strong>How FastAPI is Used in Machine Learning:</strong></h4><p>In the world of Machine Learning (ML), FastAPI helps connect the models (the brains of the AI) with users or other systems that need them.</p><ul><li><strong>Building APIs</strong>: FastAPI makes it simple to create an API, which can take input (like numbers or images) and give back a result (like a prediction or classification) from a machine learning model.</li><li><strong>Fast &amp; Efficient</strong>: FastAPI is super fast, which is important when you want your machine learning model to quickly respond to users.</li><li><strong>Supports Asynchronous Requests</strong>: This means that FastAPI can handle many requests at the same time without slowing down, which is great for machine learning models that might have lots of users making requests.</li><li><strong>Easy to Use</strong>: It’s simple to write code in FastAPI, which is great for data scientists and engineers who want to quickly build tools around their machine learning models.</li></ul><h4>Steps on How It’s Used:</h4><ol><li><strong>Train a Machine Learning Model</strong>: You create a machine learning model that learns to predict something, like if an image is a cat or a dog (or) predicting target based on some input.</li><li><strong>Build an API with FastAPI</strong>: You use FastAPI to create a simple web service that other apps or users can connect to. When someone sends an image, your FastAPI code sends that image to the model.</li><li><strong>Make Predictions</strong>: FastAPI takes the input from the user (the image), runs it through your trained model, and sends back a result (whether it’s a cat or dog).</li><li><strong>Deploy for the World to Use</strong>: Once your FastAPI app is ready, you can host it on the internet. Anyone can pass on the input to your API, and your model will classify them in real-time.</li></ol><h4><strong>Implementation of FastAPI:</strong></h4><p>Given that the ML model has already been developed, we will now explore how to implement and serve the model using FastAPI.</p><h4>Steps:</h4><p><strong>Install FastAPI and Uvicorn</strong>: You’ll need to install both FastAPI and Uvicorn using pip.</p><blockquote>pip install fastapi uvicorn</blockquote><p><strong>Create Your FastAPI Application</strong>: Create a simple FastAPI app. For example, you can save this as main.py.<br> <strong><br>sample 1: FastAPI code with no ML model</strong></p><blockquote>from fastapi import FastAPI</blockquote><blockquote>app = FastAPI()</blockquote><blockquote>@app.get(“/”)<br>def read_root():<br> return {“Hello”: “World”}</blockquote><p><strong>sample 2: FastAPI code with ML model</strong></p><blockquote>from fastapi import FastAPI<br>from pydantic import BaseModel<br>import joblib<br>import numpy as np</blockquote><blockquote>app = FastAPI()</blockquote><blockquote>class Input(BaseModel):<br>input:float</blockquote><blockquote>class Output(BaseModel):<br>output:float<br>slope:float<br>intercept:float</blockquote><blockquote># Loading a trained ML model — Here <strong>linear_deploy.pkl</strong> is the ML model<br>model = joblib.load(‘linear_deploy.pkl’)</blockquote><blockquote>@app.post(“/predict”)<br>def predict(data:Input)-&gt;Output:<br> X_input=np.array([[data.input]])<br> prediction = model.predict(X_input)<br> intercept = model.named_steps[‘model’].intercept_<br> slope = model.named_steps[‘model’].coef_<br> return Output(output=prediction,slope=slope,intercept=intercept)</blockquote><p><strong>Run FastAPI with Uvicorn</strong>: Use Uvicorn to serve your FastAPI application. Run the following command:</p><blockquote>uvicorn main:app — reload</blockquote><ul><li>main refers to the Python file main.py.</li><li>app refers to the FastAPI instance created in the file.</li><li>--reload enables hot-reloading, allowing you to see changes immediately during development.</li></ul><p><strong>Access the Application</strong>: Once Uvicorn starts the server, you can access the application in your web browser at <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000/.</a></p><p>The FastAPI docs will also be available at <a href="http://127.0.0.1:8000/docs.">http://127.0.0.1:8000/docs.</a></p><p>If you want the application accessible over the network, allowing external users to access the service. You can use the be command.</p><blockquote>uvicorn main:app — host 0.0.0.0 — port 8000</blockquote><h4>Sample Outputs:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*za48pJA6HfH2T3KoNoWCtQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QNVvTMTx0rO8PU_HGBYChA.png" /></figure><h4>Summary:</h4><ul><li><strong>FastAPI</strong> is a tool that helps connect machine learning models to the world.</li><li>It’s <strong>fast</strong>, <strong>efficient</strong>, and <strong>easy to use</strong>.</li><li>It helps people build services that others can use to make predictions, classify images, or even give advice based on data.</li></ul><p>By using FastAPI, you can make your machine learning models useful for everyone!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2278d92668aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>