<?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 Emmanuel Moran on Medium]]></title>
        <description><![CDATA[Stories by Emmanuel Moran on Medium]]></description>
        <link>https://medium.com/@fnando1995?source=rss-7f449ea00e5e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XdyC0bkgC1vdOff6j4Eaew.jpeg</url>
            <title>Stories by Emmanuel Moran on Medium</title>
            <link>https://medium.com/@fnando1995?source=rss-7f449ea00e5e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 19:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fnando1995/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[Testing the YOLOv11 Model for Pothole Segmentation in Roads: A Comprehensive Walkthrough]]></title>
            <link>https://medium.com/@fnando1995/testing-the-yolov11-model-for-pothole-segmentation-in-roads-a-comprehensive-walkthrough-012f9198ac1a?source=rss-7f449ea00e5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/012f9198ac1a</guid>
            <category><![CDATA[segmentation]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[ultralytics]]></category>
            <category><![CDATA[potholes]]></category>
            <category><![CDATA[yolov11]]></category>
            <dc:creator><![CDATA[Emmanuel Moran]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 17:40:18 GMT</pubDate>
            <atom:updated>2024-10-03T17:40:18.636Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>With the recent release of YOLOv11 by Ultralytics, the capabilities of computer vision models have reached a new height. As you can see in the graphic below, yolov11 continues to bring the 5 typical flavors (nano, small, medium, large, and Extra Large), but with some new adjustments to increase the precision and latency (in some cases). Let’s remember that right now, the YOLO series is the SOTA for computer vision tasks such as detection, classification, and segmentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MK9h9BdaD3kFHV3m" /><figcaption><a href="https://docs.ultralytics.com/models/yolo11/#overview">https://docs.ultralytics.com/models/yolo11/#overview</a></figcaption></figure><p>This project aims to test the YOLOv11 model, specifically for instance segmentation tasks, focusing on identifying and segmenting potholes in road images. In this article, I’ll walk you through my GitHub repository and how I leveraged YOLOv11 for this practical real-world problem.</p><h3>Project Background: Why Pothole Segmentation?</h3><p>Potholes are a significant issue for urban infrastructure, posing risks to vehicles and pedestrians alike. Automated detection of these road defects can greatly improve road maintenance, leading to safer conditions and more efficient repairs. With YOLOv11’s ability to perform instance segmentation, we can not only detect potholes but also highlight their exact locations and boundaries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bojKJs0V_HTVusEWVvvYyg.png" /><figcaption>Detecting and segmenting the potholes.</figcaption></figure><p>After locating and segmenting the potholes in images, you can take it a step further by calculating the percentage of pixels occupied by the segmented potholes. This allows you to determine the extent of road damage by calculating the percentage of the total image affected by potholes, providing a quantifiable metric to prioritize repairs based on severity. This damage assessment can be instrumental for maintenance teams to allocate resources efficiently and focus on heavily damaged areas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MutJzzeFoYJixT4ML_oMwQ.png" /><figcaption>for each pothole, calculating the ammount of pixels and estimating the percentage of damage.</figcaption></figure><h3>Project Walkthrough</h3><h4>1. Dataset Preparation</h4><p>The first step was curating a dataset of road images with clearly marked potholes. The dataset was downloaded from multiple datasets publicly uploaded in <strong>Roboflow</strong>. In this case, you can search for multiple datasets on this or other pages and merge them using the notebook <em>image_similarity.ipynb.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*_yq6UrL0s5VPLh_SUvUqNQ.png" /></figure><p>The notebook follows this hierarchy in the directories. Add all the datasets in the <em>sets</em> folder and run the notebook to create the <em>final</em> folder, which will have the images without repeated images. The package <a href="https://github.com/idealo/imagededup">imagededup</a> is used. Also, <em>data.yaml</em> file is created to use with Yolo for training.</p><h3>2. Training the Model</h3><p>You can look at the <em>yolo11_train.ipynb</em> file for training the model. Here I trained the model using GPU resources to speed up the process. Training YOLOv11 for pothole segmentation involved running multiple epochs and evaluating the performance at regular intervals. Depending on the model (n,s,m,l,x) you will have to wait more for the training to finish. Do not forget to use EarlyStopping to avoid overfitting.</p><h3>3. Model Evaluation and Results</h3><p>Visualization of some results, where potholes are detected and segmented, can be seen in the runs/ folder created when training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MvY2GXX1IkSIhxCGR7kr0A.jpeg" /><figcaption>validation batch preds</figcaption></figure><h3><strong>4. Inferences</strong></h3><p>You can use the file <em>yolo11_infer_potholes.ipynb </em>to review the model. In this file is also added the damage percentage calculation algorithm.</p><h3>5. Challenges and Future Work</h3><h4>Challenges Faced:</h4><ul><li><strong>Dataset Bias</strong>: Many road images came from urban settings, which meant the model initially struggled with rural or suburban roads.</li><li><strong>Small Potholes</strong>: Detecting smaller potholes accurately was more challenging, leading to lower IoU scores for those cases.</li></ul><h4>Future Work:</h4><ul><li><strong>Expand Dataset</strong>: Collecting more diverse road images will help in improving the model’s generalization.</li><li><strong>Optimize for Edge Devices</strong>: Further optimize the model for deployment on edge devices (e.g., drones or road inspection vehicles) for real-time detection and reporting.</li><li><strong>Integrate with Road Management Systems</strong>: The ultimate goal is to integrate this system with local road management infrastructures for automated pothole repair.</li></ul><h3>Conclusion</h3><p>The YOLOv11 model, with its state-of-the-art instance segmentation capabilities, offers a powerful solution for detecting and segmenting potholes on roads. By leveraging its speed and accuracy, we can potentially automate road condition analysis, improving safety and efficiency in road maintenance.</p><p>Feel free to explore the <a href="https://github.com/fnando1995/yolov11-potholes.git">GitHub repository</a> for the full implementation, and stay tuned for more updates as I continue improving the model for real-world deployment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=012f9198ac1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing GOT: The Unified Model Revolutionizing OCR-2.0]]></title>
            <link>https://medium.com/@fnando1995/introducing-got-the-unified-model-revolutionizing-ocr-2-0-d734436a0859?source=rss-7f449ea00e5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d734436a0859</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ocr]]></category>
            <category><![CDATA[hugging-face]]></category>
            <category><![CDATA[transformers]]></category>
            <dc:creator><![CDATA[Emmanuel Moran]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 02:17:52 GMT</pubDate>
            <atom:updated>2024-09-24T02:17:52.562Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>General OCR Theory: A Step Towards OCR-2.0 with the GOT Model</strong></h3><p>In the realm of Optical Character Recognition (OCR), traditional systems (OCR-1.0) are struggling to keep up with the growing demands of intelligent processing. As the variety and complexity of input data increase, from plain texts to more complex signals like mathematical formulas, tables, and even geometric shapes, there is a clear need for a more unified and sophisticated approach.</p><p>That’s where the <strong>General OCR Theory</strong> (GOT) comes in. In this post, we’ll explore the theory behind OCR-2.0, introduce the GOT model, and walk you through how to get started with it using my GitHub repository.</p><h3>The Need for OCR-2.0</h3><p>OCR-1.0 systems, while still functional, are not built to handle the diversity of inputs and outputs required in modern applications. The expectations for OCR are no longer limited to extracting text from scanned documents. Today’s applications demand OCR that can process:</p><ul><li><strong>Plain text</strong></li><li><strong>Math formulas</strong></li><li><strong>Tables and charts</strong></li><li><strong>Sheet music</strong></li><li><strong>Geometric shapes</strong></li></ul><p>OCR-1.0 systems are typically rigid and only handle specific tasks well. The evolution to <strong>OCR-2.0</strong> represents a paradigm shift towards a model capable of processing a wide range of optical characters.</p><h3>Introducing the GOT Model: A Unified End-to-End Solution</h3><p>The <strong>GOT model</strong> (General OCR Theory model) represents a significant leap forward in OCR technology. With <strong>580M parameters</strong>, it is a large, unified, and end-to-end solution designed to handle various OCR tasks within one framework. Some of its key features include:</p><ul><li><strong>High-compression encoder</strong> and <strong>long-context decoder</strong> for efficient processing.</li><li>Ability to handle a wide variety of inputs, such as:</li><li>Scene images</li><li>Document-style images</li><li>Whole-page and sliced inputs</li><li>Output support for multiple formats, including <strong>plain text</strong>, <strong>markdown</strong>, <strong>TikZ</strong>, and <strong>SMILES</strong>.</li><li>Interactive OCR features, such as <strong>region-level recognition</strong> guided by coordinates or colors.</li><li><strong>Dynamic resolution</strong> and <strong>multi-page OCR</strong> capabilities for enhanced usability.</li></ul><p>The GOT model is highly versatile, supporting various input types and producing structured output, making it well-suited for complex OCR tasks.</p><h3>Getting Started with GOT from My GitHub Repo</h3><p>To help you dive into this next generation of OCR technology, I’ve created a <a href="https://github.com/fnando1995/ocr2.0_GOT.git">GitHub repository</a> that demonstrates how to implement and test the GOT model. Here’s what you’ll find in the repo and how to get started:</p><h4>Step-by-Step Guide</h4><ol><li><strong>Loading the Model</strong></li></ol><p>The model can be easily imported using the Hugging Face transformers package, which streamlines the process of loading and interacting with the GOT model.</p><pre>from transformers import AutoModel, AutoTokenizer<br><br>tokenizer = AutoTokenizer.from_pretrained(&#39;ucaslcl/GOT-OCR2_0&#39;, trust_remote_code=True)<br>model = AutoModel.from_pretrained(&#39;ucaslcl/GOT-OCR2_0&#39;, trust_remote_code=True, low_cpu_mem_usage=True, device_map=&#39;cuda&#39;, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)<br>model = model.eval().cuda()</pre><p><strong>2. Testing on Digital PDFs</strong></p><p>I tested the model on two types of digital PDFs. For changing the PDF to Images I use the package <em>PDF2Image.</em></p><pre># read pdf as images and save<br>images = convert_from_path(&quot;test_pdf_ocr/sample_file.pdf&quot;)<br>images_paths = []<br>for i in range(len(images)):<br>    images_paths.append(f&quot;test_pdf_ocr/image-{i}.jpg&quot;)<br>    cv2.imwrite(images_paths[-1],np.array(images[i]))</pre><p><strong>Medical Example</strong> — A document containing tables, to see how well GOT handles structured data.</p><p><strong>Scientific Paper</strong> — A paper format to assess how it processes textual information.</p><pre># read each image and read with ocr model<br>for path in images_paths:<br>    a = dt.now()<br>    res = model.chat(tokenizer, path, ocr_type=&#39;format&#39;, render=True, save_render_file = path.replace(&#39;.jpg&#39;,&#39;.html&#39;))<br>    b = dt.now()<br>    with open(path.replace(&quot;.jpg&quot;,f&quot;_{round((b-a).total_seconds(),2)}.txt&quot;),&quot;w&quot;) as f:<br>        f.write(res)</pre><p>I use the ocr_type <em>format </em>as the developers mentioned to have better results on formatted files as you may see in medical results or papers.</p><p>In this initial test, I focused on the first page of each document. The results demonstrated GOT’s capacity to handle both complex tables and continuous text.</p><p><strong>3. Output Considerations</strong></p><p>As of now, I have not uploaded any PDFs to the repository, but you can easily replicate the experiments by following the code and instructions in the repo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hxDJJQBas9v9Lw_0EVPSQ.png" /><figcaption>Header of the paper used for the test</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*2_eMjiqvnrOMGUhEgz_o5A.png" /><figcaption>Results of the model GOT in html render</figcaption></figure><h3>My Impressions of the GOT Model</h3><p>After testing GOT, I can confidently say that it’s an <strong>excellent out-of-the-box solution</strong> for OCR tasks. Here are my thoughts:</p><ul><li><strong>Ease of Use</strong>: The GOT model is incredibly user-friendly, especially when compared to other OCR tools like Tesseract. With minimal setup, you can get it running efficiently, which makes it ideal for developers and researchers alike.</li><li><strong>Versatility</strong>: The model’s ability to handle multiple formats (markdown, TikZ, SMILES, etc.) is impressive. It opens up new possibilities for handling OCR in diverse fields, from academic papers to scientific diagrams.</li><li><strong>Quality of Results</strong>: In my tests, the GOT model produced <strong>better results</strong> than traditional OCR tools. It excels in accuracy, particularly with structured data like tables.</li><li><strong>Challenges</strong>: Although the GOT model performs well overall, there are some challenges. For instance, in the footnotes of the paper test, there were slight issues with processing very small text. While this is a minor drawback, I believe the model will improve over time as more refinements are made.</li></ul><h3>Conclusion</h3><p>The <strong>GOT model</strong> marks a significant step forward in OCR technology. With its unified, end-to-end design, it’s capable of handling the complex and varied demands of modern OCR tasks. Whether you’re dealing with plain text, mathematical formulas, or more intricate graphical data, the GOT model has you covered.</p><p>I’m confident that this model will become a key tool in my workflow for all future OCR projects. For those looking to explore the next generation of OCR technology, I highly recommend giving GOT a try. You can find the code and instructions in my <a href="https://github.com/fnando1995/ocr2.0_GOT.git">GitHub repository</a>.</p><h3>References</h3><p>For more detailed information on the General OCR Theory and the GOT model, check out the following resources:</p><ul><li><strong>Official Paper</strong>: <a href="https://arxiv.org/abs/2409.01704">General OCR Theory: Towards OCR-2.0</a></li><li><strong>Live Demo</strong>: Try the GOT model online via <a href="https://huggingface.co/spaces/stepfun-ai/GOT_official_online_demo">Hugging Face’s official demo</a></li><li><strong>Official GitHub Repository</strong>: Access the full codebase and updates for the GOT model at <a href="https://github.com/Ucas-HaoranWei/GOT-OCR2.0/">GOT-OCR2.0 on GitHub</a></li></ul><p>These references provide a deeper dive into the theoretical background, practical implementations, and ongoing developments of the GOT model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d734436a0859" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Local ML Model Deployment Pipeline with Python, FastAPI, PostgreSQL, and Docker]]></title>
            <link>https://medium.com/@fnando1995/building-a-local-ml-model-deployment-pipeline-with-python-fastapi-postgresql-and-docker-c1f059143a17?source=rss-7f449ea00e5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1f059143a17</guid>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[fastapi]]></category>
            <dc:creator><![CDATA[Emmanuel Moran]]></dc:creator>
            <pubDate>Wed, 18 Sep 2024 21:16:31 GMT</pubDate>
            <atom:updated>2024-09-18T21:16:31.844Z</atom:updated>
            <content:encoded><![CDATA[<p>In machine learning, building a high-performing model is only half the battle. The real challenge lies in deploying the model in a way that’s scalable, efficient, and easy to maintain — a practice known as MLOps (Machine Learning Operations). In this post, I’ll walk you through setting up a local deployment pipeline for a machine learning model using <strong>Python, PostgreSQL, FastAPI, and Docker </strong>on a Windows system.</p><p>While the focus won’t be on the specifics of the machine learning model itself, we’ll use it as the centerpiece of a robust DevOps pipeline. <em>Python will serve as the backbone for running the model, FastAPI will handle the API endpoints, PostgreSQL will store incoming requests, and Docker will make the deployment process seamless and reproducible</em>.</p><p>Whether you’re looking to level up your MLOps skills or simply want a streamlined solution for local model deployment, this guide will give you all the steps you need to get started.</p><p>Let’s dive in!</p><h3>Setting Up the Environment for ML Model Deployment</h3><p>To begin deploying your machine learning model locally, you’ll need to set up the appropriate environment with Python, install the required dependencies, and configure Docker and Postman for testing. Here’s a step-by-step guide to help you get started. You can also download the GitHub repo for additional files mentioned below.</p><h4>Step 1: Install Python</h4><ol><li>Download using the Microsoft Store.</li><li>Additionally, you can install <strong>vscode</strong> from Microsoft Store. It will help you a lot to check the project’s scripts.</li></ol><h4>Step 2: Install Docker</h4><p>Docker is essential for containerizing your application, ensuring consistent environments and easier deployment.</p><ol><li>Download Docker Desktop for Windows from <a href="https://docs.docker.com/desktop/install/windows-install/">here</a>.</li><li>Install Docker Desktop and follow the installation wizard instructions. After the installation is complete, ensure that the Docker is running.</li><li>Verify the installation by opening a terminal and typing:</li></ol><pre>docker --version</pre><p>Or by opening the Docker Desktop Application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5IQfScrXM77lHJYmK5Yc5g.png" /></figure><p><em>Note</em>: You can sign up and log in for this platform to keep some things in your web account.</p><h4>Step 3: Install Postman</h4><p>Postman is a tool for testing API endpoints, which you’ll use to ensure your FastAPI server is responding correctly.</p><ol><li>Download Postman from the official website here.</li><li>Install the application and create a free account if necessary (Preferably use an account, since you can group the calls to endpoint per project and share in your account).</li><li>Once installed, you can use Postman to send requests to your FastAPI endpoints. For a tutorial to use visit this <a href="https://youtu.be/MFxk5BZulVU?feature=shared">link</a>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HgmFxf6rHoxm0CUk9aYBLA.png" /></figure><p><em>Note</em>: You can sign up and log in for this platform to keep some things in your web account.</p><h4>Step 4: Install PostgreSQL (optional)</h4><p>PostgreSQL will be used as the database to store requests and other relevant data for your deployed machine learning model.</p><p>Installing PostgreSQL locally means that a port should be used, in this case normally 5432 port as it is the default. Since we will be using docker to deploy the API, installing Postgresql is not essential. If you do not expose the correct ports, it will fail.</p><p>Use the following <a href="https://ubuntu.com/server/docs/install-and-configure-postgresql">tutorial</a> to install locally if you need.</p><h3>Introducing the Machine Learning Model</h3><p>Before diving into the MLOps deployment pipeline, it’s important to clarify that the focus of this post is not on the intricacies of building the machine learning model itself, but rather on how to deploy it effectively using tools like Docker, FastAPI, PostgreSQL, and Python.</p><p>For this demonstration, we’ll use a <strong>simple client segmentation model</strong>. This model will help us group clients into segments based on their features, using common machine learning algorithms. The model’s complexity will be kept minimal, allowing us to shift our attention toward the DevOps practices required for deployment.</p><p>By concentrating on MLOps (Machine Learning Operations), we’ll focus on how to:</p><ul><li>Serve the model with an API using <strong>FastAPI</strong>.</li><li>Save API requests to a <strong>PostgreSQL</strong> database.</li><li>Containerize the entire application with <strong>Docker</strong> for smooth local deployment.</li></ul><p>This approach will ensure that you learn how to deploy any machine learning model, regardless of its complexity, using a scalable and reliable pipeline.</p><h3>Downloading the GitHub Repository</h3><p>To make things easier, I’ve set up a GitHub repository containing everything you need to get started.</p><h4>Step 1: Clone the Repository</h4><ol><li>Open your terminal or command prompt.</li><li>Run the following command to clone the repository to your local machine:</li></ol><pre>git clone https://github.com/fnando1995/simple_ml_app.git</pre><h4>Step 2: Explore the Repository</h4><p>Once cloned, navigate to the project folder and you’ll find:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/233/1*NjlgTeQ3L3IdT6Yuorus8A.png" /></figure><ul><li><strong>data folder: </strong>This folder contains the files to train the model and the model already trained (model.pkl).</li><li><strong>src folder:</strong> This folder contains all the files for the API and a jupyter notebook for training the model.</li><li><strong>dockerfile and docker-compose.yml:</strong> These files will help orchestrate both the FastAPI app and the PostgreSQL database, making it easier to set up the entire environment with a single command.</li></ul><p>With the repository set up locally, you’re now ready to configure and run the project.</p><h3>Let’s understand each script!</h3><p>This repo contains a well-described API and can be used as a draft for new projects. let’s try to understand each file.</p><p><strong>Data folder</strong></p><p>This folder only contains data for the model to be trained and the model itself. I recommend retraining the model if your Python environment differs in some package versions (we will talk about this later). Training does not take long. Remember that the focus of this post is not the model itself but how to deploy it.</p><p><strong>Scripts folder</strong></p><ol><li>The notebook <em>trained_model.ipynb</em> will help you to train the model whenever you want. Do not need to be modified.</li><li>The <em>.env</em> only contains environment variables to be loaded using the <em>python-dotenv </em>package in the <em>config.py</em> file. Take a look at the<em> config.py</em> file to see how this is done.</li><li>The <em>database.py</em> file creates an sqlalchemy session for a high-level communication with the PostgreSQL database.</li><li>The <em>models.py</em> file is used for allocating the models that will be used in the database. This is a typical way to work when using databases. You can deepen this by learning about <a href="https://ed.team/blog/que-es-un-orm">ORM</a>. In this case, the Predictions model is created, and this is like having a table called Predictions in the database.</li><li>The <em>schemas.py</em> file has the objects served in the API. In this case, CustomerData and BatchCustomerData.</li><li>The <em>crud.py</em> file is used to has all the CRUD (create, read, update, delete) operations in the database.</li><li>Finally, the <em>api.py</em> file has the API itself, and uses all the files mentioned about.</li></ol><p><em>Note</em>: check each file and relate to the comments for a longer explanation.</p><p><em>Note</em>: imports are used relatively, that is as follows. Remember if you need or want to use the app directly without docker, you will have to change this.</p><pre>from .config import MODEL_PATH</pre><p><strong>Docker files</strong></p><ol><li>The <em>dockerfile</em> file will create the image of the API. In this case, using an image of <em>Python 3.12</em>. In this file, commands for copying the source code to the volume of the docker are written.</li></ol><pre># Usa una imagen base de Python 3.12<br>FROM python:3.12-slim<br><br>RUN mkdir -p /usr/app/src<br>RUN mkdir -p /usr/app/data<br><br># Establecer el directorio de trabajo<br>WORKDIR /usr/app/<br><br># Copiar los archivos requeridos al contenedor<br>COPY requirements.txt .<br>COPY src/* ./src<br>COPY data/* ./data<br><br># Instalar las dependencias<br>RUN pip install --no-cache-dir -r requirements.txt<br><br># Exponer el puerto en el que correrá la API<br>EXPOSE 8000<br><br># Comando para ejecutar la API<br>CMD [&quot;uvicorn&quot;, &quot;src.api:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]</pre><p>2. The <em>docker-compose.yml</em> is used to orchestrate the containers of the API and Postgresql. This document needs to be explained in more detail and will be done later.</p><pre>version: &#39;3.8&#39;<br><br>services:<br>  api:<br>    image: api_calls<br>    build: .<br>    depends_on:<br>      - db<br>    volumes:<br>      - api_data:/usr/app/<br>    environment:<br>      - DATABASE_URL=postgresql+psycopg2://postgres:postgres@db:5432/api_calls<br>    ports:<br>      - &quot;8000:8000&quot;<br>    networks:<br>      - api_network<br><br>  db:<br>    image: postgres:latest<br>    volumes:<br>      - postgres_data:/var/lib/postgresql/data/<br>    ports:<br>      - &quot;5430:5432&quot;<br>    expose:<br>      - 5432<br>    environment:<br>      - POSTGRES_USER=postgres<br>      - POSTGRES_PASSWORD=postgres<br>      - POSTGRES_DB=api_calls<br>    networks:<br>      - api_network<br><br>volumes:<br>  api_data:<br>  postgres_data:<br><br>networks:<br>  api_network:</pre><p>For a detailed explanation of docker itself, please take a look at this <a href="https://www.youtube.com/watch?v=4Dko5W96WHg&amp;ab_channel=HolaMundo">tutorial</a></p><h3>Deploy with Docker the Application</h3><p>To deploy and use the application, use the following command</p><pre>docker-compose up -d</pre><p>This command will execute the docker-compose file. This will build the images for the API and PostgreSQL.</p><p><strong>Downloading PostgreSQL image</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yzPVLnzzA7kfLpofEDJIXQ.png" /></figure><p><strong>API build and creation</strong></p><p>After that, the “api” image is not in the docker-hub but is in the repository (folder) you download, then it will execute it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bdYMiIJam3J6p2U12MjCrw.png" /></figure><p>If you check the “dockerfile” file, you will see that the steps in that file are executed to build and execute the image of the API.</p><p>With the command in the image “RUN pip install — no-cache-dir -r requirements.txt”, inside the image of the API is been created python environment with the requirements of packages listed in the “requirements.txt” file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pKu_ftYLS5LEEbWjv8T-9A.png" /></figure><p>In the end, you will have something like this.</p><p><strong>Using Docker Desktop to validate container status</strong></p><p>You can take a look in the Docker Desktop App to check the status of the docker images created (in windows).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VQJwBlUSMqvmQvBVsn0JNQ.png" /></figure><p>Everything seems to be working correctly. You can enter the api-1 image and look at the outputs. You can see that the uvicorn app is running with our fastapi application in Python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*nqcWniZqXXDnQUqOfH0iPA.png" /></figure><p>Also, note the volumes created. Remember that this is a local space created for the docker, in which the files are copied and used by the app/image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LX1-ct13FLDOlBH2j_wzsQ.png" /></figure><p>For example, the “simple_ml_app_api_data” volume has inside a copy of all the files of the SRC and DATA folders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*LrFWiidembtR4EkOxUV2Aw.png" /></figure><p><strong>Checking functionallity</strong></p><p>We can use <strong>postman</strong> to check the API is running and returning the outputs of the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*jdPOink5OtKIlzTU5pZTkw.png" /><figcaption>test endpoint</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*SzHLIWMW0SLB5_hExPXhWA.png" /><figcaption>predict endpoint</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*sCz6CQrlWKeXcUfZx55YXA.png" /><figcaption>predict batch endpoint</figcaption></figure><p><strong>Validate PostgreSQL?</strong></p><p>You might want to know where you can consult/request the data in you database. First of all, I can say that you can use the <em>/getall</em> endpoint to get all the information that is in the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*W8BM5FukIj5xh4foxz3gEA.png" /></figure><p>But if you are curious and want to use some complex SQLs to test the data in your database, you can install <strong>PgAdmin4</strong> and connect to the database. Just remember that some things were changed in the docker-compose file and should be understood to correctly connect to the docker. This will be mentioned later too.</p><p><strong>Everything is working!</strong></p><p>Now you have an API that serves an ML model, and all the requests are saved in a PostgreSQL database.</p><p>Just to let you know that right now you can use this repo to expand the project. In the next section, I will go deeper into the Docker Compose for you to be able to change it in any way you want.</p><h3><strong>Explaining the Docker File and Docker Compose</strong></h3><h3>Dockerfile Explanation</h3><pre>FROM python:3.12-slim</pre><p>This line specifies the base image for your container. The base image is <strong>Python 3.12 (slim version)</strong>, which is a lightweight image with just the essential components for Python, reducing the overall image size.</p><pre>RUN mkdir -p /usr/app/src<br>RUN mkdir -p /usr/app/data</pre><p>Creates the /usr/app/src directory inside the container. This directory will be used to store your application&#39;s source code.</p><p>Similarly, creates the /usr/app/data directory to store any data files needed by the application.</p><pre>WORKDIR /usr/app/</pre><p>This sets the working directory of the container to /usr/app/. All subsequent commands, including copying files or installing dependencies, will be executed from this directory.</p><pre>COPY requirements.txt .<br>COPY src/* ./src<br>COPY data/* ./data</pre><p>Copies the requirements.txt file from your local machine (host) into the container at the root of /usr/app/. This file lists all the Python dependencies your application needs.</p><p>Copies all files from your local src/ directory into the container’s /usr/app/src/ directory.</p><p>Copies all files from your local data/ directory into the container’s /usr/app/data/ directory.</p><pre>RUN pip install --no-cache-dir -r requirements.txt</pre><p>Installs all the Python packages listed in the requirements.txt file using pip. The --no-cache-dir option ensures that no cache is used or stored, keeping the image size minimal.</p><pre>EXPOSE 8000</pre><p>This line tells Docker that the container will use <strong>port 8000</strong>. This is the port where the FastAPI application will be running, but it does not automatically map this port to your host — this will be done in docker-compose.</p><pre>CMD [&quot;uvicorn&quot;, &quot;src.api:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]</pre><p><strong>CMD</strong>: This specifies the command that will be run when the container starts. Here, it uses <strong>Uvicorn</strong> (ASGI server for FastAPI) to run the FastAPI application.</p><p>&quot;src.api:app&quot;: Refers to the location of the FastAPI application (app is the FastAPI app defined in api.py inside the src folder).</p><p>&quot;--host 0.0.0.0&quot;: Ensures the application listens on all available network interfaces within the container.</p><p>&quot;--port 8000&quot;: Specifies that the application will run on port 8000 inside the container.</p><h3>docker-compose.yml Explanation</h3><pre>version: &#39;3.8&#39;</pre><p>Specifies the version of Docker Compose being used. Version 3.8 is one of the latest, supporting features for orchestrating multi-container Docker applications.</p><pre>services:<br>  ...<br><br>volumes:<br>  api_data:<br>  postgres_data:<br><br>networks:<br>  api_network:</pre><ul><li><strong>services:</strong> Defines the services that will be created. In this case, there are two services: api and db.</li><li><strong>volumes</strong>: Defines two Docker-managed volumes:</li><li>api_data: Stores the application data (mounted to /usr/app/ in the api container).</li><li>postgres_data: Stores the PostgreSQL data (mounted to /var/lib/postgresql/data/ in the db container).</li><li><strong>networks</strong>: Defines a custom Docker network (api_network) to enable communication between the api and db containers.</li></ul><h4>SERVICE API</h4><pre>services:<br>  api:<br>    image: api_calls<br>    build: .</pre><ul><li><strong>image: api_calls</strong>: This specifies the name of the Docker image for the API service. The image is named api_calls, and Docker Compose will build this image using the instructions in the <strong>Dockerfile</strong>.</li><li><strong>build: . </strong>: Indicates that Docker should build the image from the current directory (.), which contains the Dockerfile.</li></ul><pre>depends_on:<br>      - db</pre><ul><li><strong>depends_on</strong>: Ensures that the db service (PostgreSQL) starts before the api service. However, it does not wait for the database to be fully ready (additional health checks would be needed for that).</li></ul><pre>volumes:<br>      - api_data:/usr/app/</pre><ul><li><strong>volumes</strong>: Mounts a Docker-managed volume (api_data) to the /usr/app/ directory inside the container. This allows data generated inside the container to be persisted even when the container is stopped.</li></ul><pre>environment:<br>      - DATABASE_URL=postgresql+psycopg2://postgres:postgres@db:5432/api_calls</pre><ul><li><strong>environment</strong>: Sets environment variables within the container. In this case, it defines the DATABASE_URL environment variable, which specifies the connection string to the PostgreSQL database.</li><li>postgresql+psycopg2://postgres:postgres@db:5432/api_calls: This string means:</li><li><strong>postgresql+psycopg2</strong>: The database driver.</li><li><strong>postgres</strong>: The username and password for PostgreSQL (both are postgres).</li><li><strong>@db</strong>: The hostname is db, which refers to the PostgreSQL container defined in this Compose file.</li><li><strong>5432</strong>: The internal port PostgreSQL listens on.</li><li><strong>api_calls</strong>: The name of the database being used.</li></ul><pre>ports:<br>      - &quot;8000:8000&quot;</pre><ul><li><strong>ports</strong>: Maps port 8000 inside the container (where the FastAPI app runs) to port 8000 on your local machine, allowing you to access the API via localhost:8000.</li></ul><pre>networks:<br>      - api_network</pre><h4>SERVICE DB</h4><pre>db:<br>    image: postgres:latest</pre><ul><li><strong>image: postgres</strong>: Specifies that the PostgreSQL service will use the latest official PostgreSQL image from Docker Hub.</li></ul><pre>volumes:<br>      - postgres_data:/var/lib/postgresql/data/</pre><ul><li><strong>volumes</strong>: Mounts a Docker volume (postgres_data) to the /var/lib/postgresql/data/ directory inside the PostgreSQL container, ensuring that the database data is persisted between container restarts.</li></ul><pre>ports:<br>      - &quot;5430:5432&quot;</pre><ul><li><strong>ports</strong>: Maps <strong>port 5430</strong> on your local machine to <strong>port 5432</strong> inside the container. This avoids conflicts with any PostgreSQL service already running locally on port 5432 while still allowing the container to use the default PostgreSQL port.</li></ul><pre>expose:<br>      - 5432</pre><ul><li><strong>expose</strong>: Exposes port 5432 inside the Docker network, making it accessible only to other containers within the same network (such as api).</li></ul><pre>environment:<br>      - POSTGRES_USER=postgres<br>      - POSTGRES_PASSWORD=postgres<br>      - POSTGRES_DB=api_calls</pre><ul><li><strong>environment</strong>: Sets environment variables inside the PostgreSQL container:</li><li><strong>POSTGRES_USER</strong>: The username for the PostgreSQL instance (postgres).</li><li><strong>POSTGRES_PASSWORD</strong>: The password for the PostgreSQL user (postgres).</li><li><strong>POSTGRES_DB</strong>: The name of the database to be created (api_calls).</li></ul><pre>networks:<br>      - api_network</pre><ul><li><strong>networks</strong>: Connects the PostgreSQL service to the api_network so it can communicate with the api service.</li></ul><p>With the API fully containerized and deployed locally using Docker, you’re now equipped with a powerful MLOps pipeline. At this point, you can distribute your API to any computer that has Docker installed, ensuring that your environment and dependencies remain consistent across different machines. This provides a robust and scalable way to deploy your machine learning models and services without the headaches of manual configuration.</p><p>To take this project further and move towards production-level deployment, here are some important next steps:</p><h4>Further Steps:</h4><ol><li><strong>Upload to Docker Hub</strong>:</li></ol><ul><li>Create a Docker Hub account and upload your API’s Docker image, allowing others to pull and run your API from anywhere. You can automate this process by using Docker Compose to build and push the image directly to Docker Hub.</li></ul><p><strong>2. Upload to GitHub with GitHub Secrets</strong>:</p><ul><li>Store sensitive information like API keys or database credentials securely using GitHub Secrets. Then, upload your project to GitHub, keeping the repository organized and accessible for collaboration.</li></ul><p><strong>3. Implement CI/CD Pipelines</strong>:</p><ul><li>Use continuous integration and continuous deployment (CI/CD) tools like GitHub Actions or Jenkins to automate the building, testing, and deployment of your API. This ensures that any changes made to your codebase are automatically deployed to your environment, making your workflow smoother and more efficient.</li></ul><p>By following these steps, you can not only run your API locally but also streamline deployment to cloud platforms or production environments, making your ML model available on a much larger scale.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1f059143a17" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>