<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ekemini Thompson</title>
    <description>The latest articles on DEV Community by Ekemini Thompson (@ekemini_thompson).</description>
    <link>https://dev.to/ekemini_thompson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1727168%2Fb6e8382e-b5d5-4aae-a40a-bf8007d23875.jpg</url>
      <title>DEV Community: Ekemini Thompson</title>
      <link>https://dev.to/ekemini_thompson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ekemini_thompson"/>
    <language>en</language>
    <item>
      <title>DDSS: Step-by-Step Explanation</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Wed, 07 Jan 2026 17:12:52 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/ddss-step-by-step-explanation-3ade</link>
      <guid>https://dev.to/ekemini_thompson/ddss-step-by-step-explanation-3ade</guid>
      <description>&lt;p&gt;Hey there! This guide is for an assignment (looks like "DDSS 2025 Assignment 2") where you'll set up a simple web app using Docker, Python (with Flask), and PostgreSQL. The goal is to demonstrate common security vulnerabilities like SQL injection and cross-site scripting (XSS), and then show how to fix them.&lt;/p&gt;

&lt;p&gt;I'll explain &lt;strong&gt;every step&lt;/strong&gt; in simple terms. We'll go slow, define new words, and I'll tell you why each step matters. You'll need a computer with Docker installed (it's a tool to run apps in "containers" like isolated boxes). If you don't have Docker, download it from the official website (docker.com) and install it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Safety Note:&lt;/strong&gt; This is for learning only. Don't run vulnerable code on the internet—it's just for your local machine to understand bugs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Download and Unzip the File
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "Download the file I sent to you and unzip it."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt; The file is a ZIP archive (like a compressed folder) named "ddss-2025-assignment2-rc-main-2.zip". Just sent on WhatsApp; save it to your Downloads folder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How to unzip:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;On Windows: Right-click the ZIP file &amp;gt; Extract All.&lt;/li&gt;
&lt;li&gt;On Mac: Double-click the ZIP file—it unzips automatically.&lt;/li&gt;
&lt;li&gt;On Linux: Open a terminal and run &lt;code&gt;unzip ddss-2025-assignment2-rc-main-2.zip&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why?&lt;/strong&gt; This gives you the code files, like Python scripts, a Docker setup file, and the app code.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Result:&lt;/strong&gt; You'll have a folder called "ddss-2025-assignment2-rc-main-2" (or similar).&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 2: Start Docker
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "Start docker."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt; Open the Docker app on your computer. It might be called "Docker Desktop." Make sure it's running (look for an icon in your taskbar or menu bar showing a whale).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why?&lt;/strong&gt; Docker will create containers for your database (PostgreSQL) and web app, so everything runs without messing up your computer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tip:&lt;/strong&gt; If Docker isn't installed, pause here and install it. Test it by opening a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and typing &lt;code&gt;docker --version&lt;/code&gt;. It should show a version number.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 3: Go to Your Terminal or Command Prompt
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "Go to your terminal or cmd."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Windows: Search for "cmd" or "Command Prompt."&lt;/li&gt;
&lt;li&gt;Mac: Search for "Terminal."&lt;/li&gt;
&lt;li&gt;Linux: It's already there—open Terminal.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why?&lt;/strong&gt; We'll run commands here to set up and run the app.&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 4: Navigate to the Folder
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "cd Downloads" then "cd ddss-2025-assignment2-rc-main-2".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Type &lt;code&gt;cd Downloads&lt;/code&gt; and press Enter. (This "changes directory" to your Downloads folder.)&lt;/li&gt;
&lt;li&gt;Then type &lt;code&gt;cd ddss-2025-assignment2-rc-main-2&lt;/code&gt; and press Enter. (Go into the unzipped folder.)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why?&lt;/strong&gt; Commands need to run from inside the project folder where the files are.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Tip:&lt;/strong&gt; If you get an error like "No such file or directory," double-check the folder name (use &lt;code&gt;ls&lt;/code&gt; on Mac/Linux or &lt;code&gt;dir&lt;/code&gt; on Windows to list files).&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 5: Install Python Packages
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "pip install flask psycopg2-binary werkzeug bandit".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt; In the terminal, type that exact command and press Enter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What is this?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pip&lt;/code&gt; is a tool to install Python libraries (like downloading apps for Python).&lt;/li&gt;
&lt;li&gt;Flask: A framework to build web apps in Python.&lt;/li&gt;
&lt;li&gt;psycopg2-binary: Helps Python talk to PostgreSQL databases.&lt;/li&gt;
&lt;li&gt;Werkzeug: Helps with web stuff in Flask.&lt;/li&gt;
&lt;li&gt;Bandit: A tool to check code for security issues (optional here, but good practice).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why?&lt;/strong&gt; Your app needs these to run.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Tip:&lt;/strong&gt; If you don't have Python, install it from python.org (version 3.x). Also, you might need a "virtual environment" for cleanliness, but for now, this is fine.&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 6: Run the Docker Script
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "./&lt;a href="http://docker-compose-python-psql.sh" rel="noopener noreferrer"&gt;docker-compose-python-psql.sh&lt;/a&gt;".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt; In the terminal (still in the project folder), type &lt;code&gt;./docker-compose-python-psql.sh&lt;/code&gt; and press Enter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What is this?&lt;/strong&gt; This is a shell script (a file with commands) that uses Docker Compose (a Docker tool) to build and start two containers: one for the Python/Flask app and one for the PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why?&lt;/strong&gt; It sets up the environment automatically. It might take a few minutes to download and build stuff—be patient!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tip:&lt;/strong&gt; If it says "permission denied," run &lt;code&gt;chmod +x docker-compose-python-psql.sh&lt;/code&gt; first to make it executable. Watch for "finished building" message.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 7: Connect to the Database
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: Open another tab on the same command prompt and paste "docker exec -it db psql -U ddss-database-assignment-2 -d ddss-database-assignment-2". Then "enter".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Open a new terminal tab (or window).&lt;/li&gt;
&lt;li&gt;Paste that command and press Enter.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;What is this?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docker exec&lt;/code&gt;: Runs a command inside a running Docker container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-it db&lt;/code&gt;: Interactive mode in the "db" container (the database one).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;psql&lt;/code&gt;: The PostgreSQL command-line tool to interact with the database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-U&lt;/code&gt; and &lt;code&gt;-d&lt;/code&gt;: Username and database name.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why?&lt;/strong&gt; This lets you access the database shell to run SQL commands if needed (but the guide doesn't specify more here—maybe for debugging).&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Tip:&lt;/strong&gt; You'll see a prompt like "ddss-database-assignment-2=#". Type &lt;code&gt;\q&lt;/code&gt; to exit when done. Then switch back to the first terminal tab.&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 8: Open the App in Your Browser
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: "Go to your browser and paste &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt; Open Chrome, Firefox, or any browser. Type or paste &lt;code&gt;http://localhost:8080&lt;/code&gt; in the address bar.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why?&lt;/strong&gt; This is the web app's home page running on your local machine (port 8080).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What you'll see:&lt;/strong&gt; Probably a login page or menu for the assignment parts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 9: Register a User
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide says: Open another browser tab and go to "&lt;a href="http://localhost:8080/register" rel="noopener noreferrer"&gt;http://localhost:8080/register&lt;/a&gt;".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What to do:&lt;/strong&gt; Do that—register with a username and password (real ones, not the exploit yet).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why?&lt;/strong&gt; Some parts require being logged in.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 10: Test Part 1 - Vulnerable SQL Injection (Login Bypass)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The guide explains testing SQL injection, a bug where bad input tricks the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vulnerable Form:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Go to the login page (probably at /part1 or similar—check the app).&lt;/li&gt;
&lt;li&gt;Username: &lt;code&gt;' OR '1'='1' --&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Password: Anything (e.g., "blah").&lt;/li&gt;
&lt;li&gt;Login. It should say "VULNERABLE LOGIN SUCCESS".&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why it works:&lt;/strong&gt; This input injects SQL code. The query becomes something like &lt;code&gt;SELECT * WHERE username='' OR '1'='1' -- AND password=...&lt;/code&gt;. The &lt;code&gt;--&lt;/code&gt; comments out the password check, and &lt;code&gt;'1'='1'&lt;/code&gt; is always true, so it logs in without a real username/password.&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why the explanation about precedence?&lt;/strong&gt; In SQL, AND is stronger than OR, so without &lt;code&gt;--&lt;/code&gt;, the password part still blocks it.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Secure Form:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try the same username/password.&lt;/li&gt;
&lt;li&gt;It should say "SECURE LOGIN FAILED".&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; The secure version uses "prepared statements" to prevent injection—inputs are treated as data, not code.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 11: Test Part 2 - Session Authorization and More
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Not Logged In:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Try accessing /part2.html and /part3.html.&lt;/li&gt;
&lt;li&gt;Should redirect to /part1.html (login required).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Logged In (Secure Way):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Login properly via the secure form.&lt;/li&gt;
&lt;li&gt;Then access /part2.html and /part3.html—should work.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why?&lt;/strong&gt; Even if vulnerable login lets you in, sessions (like cookies tracking login) prevent accessing protected pages without proper auth.&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 12: Test Stored XSS in Part 2
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;XSS is a bug where bad input (like scripts) runs as code in the browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vulnerable Form:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;In the message form (probably at /part2), submit: &lt;code&gt;&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Reload the page—an alert popup should appear.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; The app stores the script in the DB and displays it without escaping, so the browser runs it as JavaScript.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Secure Form:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Submit the same: &lt;code&gt;&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Reload—no popup. It shows as plain text.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; The secure version escapes output (e.g., using Jinja templates in Flask) so &lt;code&gt;&amp;lt;&lt;/code&gt; becomes &lt;code&gt;&amp;amp;lt;&lt;/code&gt;, making it harmless.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Messages List:&lt;/strong&gt; The guide shows example messages—yours might vary, but test to see the difference.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Summary Table for Report:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Copy this into your assignment report:
| Version   | Vulnerability | Result          |
|-----------|---------------|-----------------|
| Vulnerable| Stored XSS   | JS executed    |
| Secure    | Escaped output| Safe display   |&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 13: Core Work Complete
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;You've now shown SQL injection (for login and possibly data access) and XSS, plus fixes.&lt;/li&gt;
&lt;li&gt;This covers "Exercise 1" in the assignment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 14: Test Part 3 - SQL Injection in Queries
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vulnerable Query:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;In the insecure form (look for price more than), enter price: &lt;code&gt;0 OR 1=1 --&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Submit—it should list ALL books.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; Injection tricks the SQL to ignore the price and return everything (similar to login bypass).&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Secure Query:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same input—should list NO books or fail safely.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; Parameterized queries prevent the injection.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Final Tips for You
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Troubleshooting:&lt;/strong&gt; If something fails, check terminal errors. Restart Docker with &lt;code&gt;docker-compose down&lt;/code&gt; then rerun the script.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learning Points:&lt;/strong&gt; This teaches why input validation, prepared statements, and output escaping are crucial in web apps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Report:&lt;/strong&gt; Take screenshots of successes/failures, explain like I did, and include the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean Up:&lt;/strong&gt; When done, stop Docker containers with &lt;code&gt;docker-compose down&lt;/code&gt; in the folder.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;bandit&lt;/strong&gt;&lt;br&gt;
Great, this is &lt;strong&gt;exactly&lt;/strong&gt; what Part 2.2 + 2.3 expects.&lt;br&gt;
I’ll guide you &lt;strong&gt;step-by-step&lt;/strong&gt;, in &lt;strong&gt;exam/report-ready order&lt;/strong&gt;, with &lt;strong&gt;exact commands&lt;/strong&gt;, &lt;strong&gt;what you should see&lt;/strong&gt;, and &lt;strong&gt;what to write down&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can literally follow this and tick boxes.&lt;/p&gt;


&lt;h1&gt;
  
  
  ✅ PART 2.2 — Run the Tools
&lt;/h1&gt;


&lt;h2&gt;
  
  
  🔹 A. Run &lt;strong&gt;Bandit&lt;/strong&gt; (Static Analysis)
&lt;/h2&gt;

&lt;p&gt;From project root (where &lt;code&gt;app.py&lt;/code&gt; exists):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bandit &lt;span class="nt"&gt;-r&lt;/span&gt; app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or entire backend folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bandit &lt;span class="nt"&gt;-r&lt;/span&gt; app/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 3 — What Bandit should report (EXPECTED)
&lt;/h3&gt;

&lt;p&gt;You should see findings like:&lt;/p&gt;

&lt;h4&gt;
  
  
  ✅ SQL Injection (TRUE POSITIVE)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Triggered by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; AND price &amp;gt;= &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;pricemin&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ This is &lt;strong&gt;INTENTIONAL&lt;/strong&gt; (vulnerable route)&lt;/p&gt;




&lt;h4&gt;
  
  
  ✅ Weak Cryptography (if applicable)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[B303:md5] Use of weak cryptographic hash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[B324:hashlib] Use of weak SHA256 for password hashing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ Counts as &lt;strong&gt;TP&lt;/strong&gt; if you used SHA256 instead of bcrypt/PBKDF2.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 4 — Save Evidence
&lt;/h3&gt;

&lt;p&gt;Take:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Screenshot of Bandit output&lt;/li&gt;
&lt;li&gt;Copy &lt;strong&gt;Issue ID&lt;/strong&gt;, &lt;strong&gt;Severity&lt;/strong&gt;, &lt;strong&gt;File&lt;/strong&gt;, &lt;strong&gt;Line number&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will reference this in &lt;strong&gt;Part 2.3 table&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;zap&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;## 🔹 B. Run **OWASP ZAP&lt;/strong&gt; (Dynamic Testing)&lt;/p&gt;

&lt;h2&gt;
  
  
  **
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1 — Download &amp;amp; Start ZAP
&lt;/h3&gt;

&lt;p&gt;Download:&lt;br&gt;
👉 &lt;a href="https://www.zaproxy.org/download/" rel="noopener noreferrer"&gt;https://www.zaproxy.org/download/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run &lt;strong&gt;ZAP Desktop&lt;/strong&gt;&lt;br&gt;
Choose:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;“No, I do not want to persist this session”&lt;/strong&gt; (simpler)&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h3&gt;
  
  
  Step 2 — Configure Browser Proxy
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Proxy settings:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Host&lt;/strong&gt;: &lt;code&gt;localhost&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port&lt;/strong&gt;: &lt;code&gt;8080&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do this in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firefox → Settings → Network → Proxy&lt;/li&gt;
&lt;li&gt;Chrome → System → Open proxy settings&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Step 3 — Verify Proxy Works
&lt;/h3&gt;

&lt;p&gt;Visit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ You should see traffic appearing in ZAP “Sites” tree&lt;br&gt;
❌ If not, proxy is wrong&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 C. ZAP Automated Scan (Quick Win)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 4 — Quick Start Scan
&lt;/h3&gt;

&lt;p&gt;In ZAP:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;strong&gt;Quick Start&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Enter:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   http://localhost:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Attack&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ZAP will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spider your app&lt;/li&gt;
&lt;li&gt;Passive scan&lt;/li&gt;
&lt;li&gt;Active scan (SQLi/XSS)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;⏱ Wait 5–10 minutes&lt;/p&gt;


&lt;h3&gt;
  
  
  Expected ZAP Alerts (IMPORTANT)
&lt;/h3&gt;

&lt;p&gt;You SHOULD see:&lt;/p&gt;
&lt;h4&gt;
  
  
  ✅ SQL Injection (TRUE POSITIVE)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;URL: &lt;code&gt;/login-vulnerable&lt;/code&gt; or &lt;code&gt;/part3_vulnerable&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Payload: &lt;code&gt;' OR 1=1 --&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Evidence: Authentication bypass / all rows returned&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  ✅ XSS (TRUE POSITIVE)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;URL: message submission&lt;/li&gt;
&lt;li&gt;Payload:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Evidence: Script execution in response&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  ⚠ CSRF (Possible FALSE POSITIVE)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;“Form has no CSRF token”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✔ Allowed as &lt;strong&gt;FP&lt;/strong&gt; depending on assignment scope&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 5 — Manual Active Scan (Recommended)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;In &lt;strong&gt;Sites tree&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Right-click your app&lt;/li&gt;
&lt;li&gt;Select:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Attack → Active Scan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This improves SQLi detection.&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 D. Manual Exploration (IMPORTANT)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 6 — Manual Attacks (Through Browser)
&lt;/h3&gt;

&lt;p&gt;While proxy is enabled:&lt;/p&gt;
&lt;h4&gt;
  
  
  SQL Injection Test
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 OR 1=1 --
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Works in vulnerable&lt;/li&gt;
&lt;li&gt;Fails in secure ✔&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  XSS Test
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XSS&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Executes in vulnerable&lt;/li&gt;
&lt;li&gt;Escaped in secure ✔&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ZAP records these.&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 E. Manual Undetected Vulnerability (REQUIRED)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Example 1 — Weak Password Hashing
&lt;/h3&gt;

&lt;p&gt;If your app uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ZAP ❌ misses it (static issue)&lt;/li&gt;
&lt;li&gt;Bandit ✅ detects it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✔ Counts as &lt;strong&gt;Manual / Undetected by ZAP&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 2 — Hardcoded &lt;code&gt;secret_key&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secret_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;secret123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explain exploit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Predictable secret&lt;/li&gt;
&lt;li&gt;Session cookie forgery possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✔ Excellent manual vulnerability&lt;/p&gt;




&lt;h1&gt;
  
  
  ✅ PART 2.3 — Classification Table (REPORT)
&lt;/h1&gt;

&lt;p&gt;Use this table &lt;strong&gt;exactly&lt;/strong&gt; (you can paste it):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;Vulnerability&lt;/th&gt;
&lt;th&gt;TP / FP&lt;/th&gt;
&lt;th&gt;Reason&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ZAP&lt;/td&gt;
&lt;td&gt;SQL Injection (login-vuln)&lt;/td&gt;
&lt;td&gt;TP&lt;/td&gt;
&lt;td&gt;Payload &lt;code&gt;' OR 1=1 --&lt;/code&gt; bypassed authentication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ZAP&lt;/td&gt;
&lt;td&gt;Stored XSS (text-vuln)&lt;/td&gt;
&lt;td&gt;TP&lt;/td&gt;
&lt;td&gt;Script executed on message display&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bandit&lt;/td&gt;
&lt;td&gt;SQL string concatenation&lt;/td&gt;
&lt;td&gt;TP&lt;/td&gt;
&lt;td&gt;f-strings used in vulnerable queries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ZAP&lt;/td&gt;
&lt;td&gt;CSRF (no token)&lt;/td&gt;
&lt;td&gt;FP&lt;/td&gt;
&lt;td&gt;Forms lack tokens but CSRF not required in assignment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bandit&lt;/td&gt;
&lt;td&gt;Weak crypto (SHA256)&lt;/td&gt;
&lt;td&gt;TP&lt;/td&gt;
&lt;td&gt;Passwords not hashed with adaptive algorithm&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  ✅ Discussion Points (WRITE THIS)
&lt;/h1&gt;

&lt;p&gt;You can use this &lt;strong&gt;verbatim&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;OWASP ZAP is effective at identifying runtime vulnerabilities such as SQL injection and XSS, but it may miss implementation-level weaknesses like weak cryptographic choices. Bandit complements ZAP by identifying insecure coding patterns through static analysis but lacks execution context. Some vulnerabilities, such as hardcoded secrets or weak hashing, require manual inspection. The combined use of static and dynamic tools provides significantly better coverage than either tool alone.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🎯 Final Checklist (Tick These)
&lt;/h2&gt;

&lt;p&gt;✔ Bandit run&lt;br&gt;
✔ ZAP automated scan&lt;br&gt;
✔ ZAP active scan&lt;br&gt;
✔ Manual attacks performed&lt;br&gt;
✔ One manual undetected vulnerability&lt;br&gt;
✔ Classification table completed&lt;br&gt;
✔ Discussion written&lt;/p&gt;

&lt;p&gt;If anything confuses you, ask for clarification on a specific step! You've got this—CS is all about experimenting. 🚀&lt;/p&gt;

</description>
      <category>docker</category>
      <category>python</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>ML Serving as a Microservice</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Thu, 11 Sep 2025 09:32:23 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/ml-serving-as-a-microservice-39nk</link>
      <guid>https://dev.to/ekemini_thompson/ml-serving-as-a-microservice-39nk</guid>
      <description>&lt;h1&gt;
  
  
  AI-in-the-Loop Healthcare: Engineering &lt;em&gt;Pregnancy Fit-to-Fly&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Pregnancy Fit-to-Fly&lt;/strong&gt; is not just another full-stack app—it’s a production-ready demonstration of how &lt;strong&gt;machine learning can be embedded as a first-class system component&lt;/strong&gt;, orchestrating user experience in real time.&lt;/p&gt;

&lt;p&gt;At its essence, the platform automates the issuance of &lt;strong&gt;medical fitness-to-fly certificates for pregnant passengers&lt;/strong&gt;, but beneath the surface it showcases a robust AI/ML architecture engineered for &lt;strong&gt;low-latency inference, modular scalability, and safety-critical decisioning&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  AI-Driven System Design
&lt;/h2&gt;

&lt;p&gt;Unlike traditional healthcare apps that bolt AI onto the side, this project was built around the principle of &lt;strong&gt;AI-in-the-loop workflows&lt;/strong&gt;. The models are not background advisors—they are the &lt;strong&gt;control plane&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The system is composed of three independent but tightly integrated layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Frontend (clients/):&lt;/strong&gt; A tab-based user workflow built on modern JavaScript modules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Backend (server/):&lt;/strong&gt; Node.js/Express service handling authentication, certificate issuance, and integration with doctors and payments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ML Backend (ml/):&lt;/strong&gt; A FastAPI-powered inference engine serving lightweight predictive models.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This modular separation ensures that &lt;strong&gt;models evolve independently&lt;/strong&gt; from application logic, enabling rapid iteration and &lt;strong&gt;scalable deployment across microservices&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Machine Learning Core
&lt;/h2&gt;

&lt;p&gt;The ML service processes structured health data spanning &lt;strong&gt;demographics, physiological measures, behavioral scores, and binary symptoms&lt;/strong&gt;. Feature vectors are dynamically assembled into a numerical pipeline before inference, ensuring data flexibility and consistency.&lt;/p&gt;

&lt;p&gt;The models include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Heart Rate Predictor (Regression)&lt;/strong&gt; – fast BPM estimation adjusted for age, activity, and stress.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blood Pressure Predictor (Regression)&lt;/strong&gt; – estimates systolic/diastolic levels from feature interactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eligibility Classifier (Binary Classification)&lt;/strong&gt; – outputs probability of “fit-to-fly,” tuned for &lt;strong&gt;conservative, safety-first thresholds&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All models are optimized for &lt;strong&gt;sub-100ms inference&lt;/strong&gt;, containerized for &lt;strong&gt;cloud portability&lt;/strong&gt;, and exposed via stateless API endpoints—making them inherently &lt;strong&gt;horizontally scalable&lt;/strong&gt; under load.&lt;/p&gt;




&lt;h2&gt;
  
  
  ML Serving as a Microservice
&lt;/h2&gt;

&lt;p&gt;The ML backend operates as a &lt;strong&gt;stateless FastAPI microservice&lt;/strong&gt;, designed around the following principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JSON schema contracts&lt;/strong&gt; → Frontend remains model-agnostic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stateless inference&lt;/strong&gt; → Seamless horizontal scaling via Kubernetes or Swarm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API-first extensibility&lt;/strong&gt; → New models or endpoints can be added without breaking downstream integrations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safety-first gating&lt;/strong&gt; → Eligibility decisions halt unsafe workflows at runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This design abstracts away complexity for the frontend, while maintaining the &lt;strong&gt;flexibility of plugging in new predictive engines&lt;/strong&gt; (e.g., gestational risk models, wearable-device streams).&lt;/p&gt;




&lt;h2&gt;
  
  
  Production-Grade Stack
&lt;/h2&gt;

&lt;p&gt;The technical stack reflects an &lt;strong&gt;engineering-for-scale mindset&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ML Backend:&lt;/strong&gt; Python 3.8+, FastAPI, scikit-learn/XGBoost, NumPy, Pandas&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Backend:&lt;/strong&gt; Node.js/Express, JWT-based authentication, pluggable data layer (MongoDB/Postgres)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frontend:&lt;/strong&gt; Modular ES modules, esbuild for bundling&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps:&lt;/strong&gt; Docker-first, &lt;code&gt;.env&lt;/code&gt;-based configuration, ready for cloud CI/CD pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every layer is &lt;strong&gt;decoupled but orchestrated&lt;/strong&gt;, enabling teams to iterate on AI models, backend services, or frontend workflows independently—without regressions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Workflow Orchestration
&lt;/h2&gt;

&lt;p&gt;The application flow illustrates &lt;strong&gt;real-time AI integration&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Passenger logs in (JWT-authenticated).&lt;/li&gt;
&lt;li&gt;Health data collected via form-driven interface.&lt;/li&gt;
&lt;li&gt;Features sent to ML API → model inference controls navigation.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If “fit-to-fly” → system unlocks doctor selection.&lt;/li&gt;
&lt;li&gt;If not → workflow halts, preventing unsafe certification.

&lt;ol&gt;
&lt;li&gt;Payment handled via external gateway (Paystack).&lt;/li&gt;
&lt;li&gt;Certificate issued as a secure, verifiable PDF.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, &lt;strong&gt;model predictions aren’t just insights—they are gatekeepers&lt;/strong&gt;, dynamically shaping user experience.&lt;/p&gt;




&lt;h2&gt;
  
  
  Scalability &amp;amp; Reliability
&lt;/h2&gt;

&lt;p&gt;The architecture is &lt;strong&gt;microservice-native&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Independent deployments&lt;/strong&gt; → ML and API can scale separately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability hooks&lt;/strong&gt; → Request-level logging for monitoring inference performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt; → JWT gating ensures only authenticated requests reach the ML backend.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Future extensibility&lt;/strong&gt; → Designed to incorporate explainability, wearables, or federated updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, this isn’t a prototype—it’s &lt;strong&gt;production-ready AI infrastructure&lt;/strong&gt; applied to healthcare.&lt;/p&gt;




&lt;h2&gt;
  
  
  Future AI-Centric Extensions
&lt;/h2&gt;

&lt;p&gt;The roadmap pushes beyond predictive gating into &lt;strong&gt;next-gen applied AI systems engineering&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LLM Integration:&lt;/strong&gt; Natural-language medical explanations (“why eligible/ineligible”).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Deployment:&lt;/strong&gt; Model exports via ONNX/TensorRT for airline devices or on-prem clinics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explainability Layer:&lt;/strong&gt; SHAP/LIME to expose feature importance in real time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Federated Learning:&lt;/strong&gt; Decentralized, privacy-preserving model improvements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wearable Integration:&lt;/strong&gt; Real-time vitals streaming for continuous eligibility tracking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This trajectory aligns with &lt;strong&gt;xAI’s ethos&lt;/strong&gt;: building &lt;strong&gt;real-world AI systems that adapt, explain, and scale&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Project Matters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AI-first architecture&lt;/strong&gt; – models are core, not add-ons.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safety-critical deployment&lt;/strong&gt; – predictions gate life-impacting workflows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production engineering&lt;/strong&gt; – decoupled microservices, containerized, and scalable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vision alignment&lt;/strong&gt; – AI deployed for real human impact in aviation and healthcare.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pregnancy Fit-to-Fly isn’t just a health app. It’s a &lt;strong&gt;blueprint for applied AI systems at scale&lt;/strong&gt;—a showcase of how &lt;strong&gt;machine learning, microservices, and modern deployment pipelines converge into a single mission-critical product&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>ekeminithompson</category>
      <category>machinelearning</category>
      <category>xai</category>
      <category>ai</category>
    </item>
    <item>
      <title>Building a Multimodal AI Chatbot with Flask, Transformers, and BLIP</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Fri, 01 Aug 2025 19:47:48 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/building-a-multimodal-ai-chatbot-with-flask-transformers-and-blip-3ddb</link>
      <guid>https://dev.to/ekemini_thompson/building-a-multimodal-ai-chatbot-with-flask-transformers-and-blip-3ddb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In today’s rapidly evolving AI landscape, combining multiple modalities—such as &lt;strong&gt;text&lt;/strong&gt; and &lt;strong&gt;images&lt;/strong&gt;—into a single system offers more intuitive and dynamic user interactions. This article guides you through the process of building a lightweight &lt;strong&gt;multimodal chatbot&lt;/strong&gt; using &lt;strong&gt;Flask&lt;/strong&gt;, &lt;strong&gt;Hugging Face Transformers&lt;/strong&gt;, and &lt;strong&gt;BLIP (Bootstrapped Language Image Pretraining)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can find the complete project here:&lt;br&gt;
👉 &lt;a href="https://github.com/EkeminiThompson/multimodal-chatbot-app" rel="noopener noreferrer"&gt;GitHub: multimodal-chatbot-app&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  💡 What is a Multimodal Chatbot?
&lt;/h2&gt;

&lt;p&gt;Unlike traditional chatbots that only handle text, &lt;strong&gt;multimodal chatbots&lt;/strong&gt; process &lt;strong&gt;multiple forms of input&lt;/strong&gt;, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🧾 Natural language queries (text)&lt;/li&gt;
&lt;li&gt;🖼️ Image uploads for captioning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes the interaction more versatile—for example, a user can &lt;strong&gt;ask a science question&lt;/strong&gt; or &lt;strong&gt;upload an image and get a description&lt;/strong&gt; of what it contains.&lt;/p&gt;


&lt;h2&gt;
  
  
  🧰 Tech Stack
&lt;/h2&gt;

&lt;p&gt;Here’s a breakdown of the tools and frameworks used:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Tool/Library&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Backend Server&lt;/td&gt;
&lt;td&gt;Flask&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Language Model&lt;/td&gt;
&lt;td&gt;Hugging Face Transformers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Image Captioning&lt;/td&gt;
&lt;td&gt;Salesforce BLIP (via Transformers)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ML Framework&lt;/td&gt;
&lt;td&gt;PyTorch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Image Handling&lt;/td&gt;
&lt;td&gt;Pillow (PIL)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  🚀 How It Works
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. Text-Based Q&amp;amp;A
&lt;/h3&gt;

&lt;p&gt;For science-related questions, the chatbot uses a pre-trained &lt;strong&gt;DistilBERT model&lt;/strong&gt; fine-tuned on the &lt;strong&gt;SQuAD dataset&lt;/strong&gt; to generate answers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; What is the boiling point of water?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; The boiling point of water is 100°C.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h3&gt;
  
  
  2. Image Captioning
&lt;/h3&gt;

&lt;p&gt;When a user uploads an image, the app uses &lt;strong&gt;BLIP (by Salesforce)&lt;/strong&gt; to generate a human-like description of the image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📷 Image: A cat on a bench&lt;br&gt;
🧠 Output: “A cat sitting on a wooden bench outdoors.”&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  🧪 Installation Guide
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;⚙️ I recommend using a virtual environment to avoid dependency conflicts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clone the repo&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/EkeminiThompson/multimodal-chatbot-app.git
&lt;span class="nb"&gt;cd &lt;/span&gt;multimodal-chatbot-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set up environment&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv
&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate  &lt;span class="c"&gt;# macOS/Linux&lt;/span&gt;
&lt;span class="c"&gt;# .\venv\Scripts\activate  # Windows&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install dependencies&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Run the app&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python multimodal_chatbot.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Open your browser or Postman and access:&lt;br&gt;
🔗 &lt;code&gt;http://127.0.0.1:5000/chat&lt;/code&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  🧠 Code Overview
&lt;/h2&gt;

&lt;p&gt;The core logic lives in &lt;code&gt;multimodal_chatbot.py&lt;/code&gt;. The &lt;code&gt;/chat&lt;/code&gt; endpoint handles both &lt;strong&gt;text&lt;/strong&gt; and &lt;strong&gt;image&lt;/strong&gt; POST requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/chat&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# handle Q&amp;amp;A
&lt;/span&gt;    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;image&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# handle image captioning
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both models are loaded during startup using Hugging Face's &lt;code&gt;pipeline()&lt;/code&gt; function.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧪 Testing the Endpoints
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🧾 For Text
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://127.0.0.1:5000/chat &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"text": "What is the chemical formula of water?"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🖼️ For Image
&lt;/h3&gt;

&lt;p&gt;Use Postman or any frontend tool to send an image as form data (&lt;code&gt;multipart/form-data&lt;/code&gt;) with the key &lt;code&gt;image&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 Future Enhancements
&lt;/h2&gt;

&lt;p&gt;Here are some features I’m exploring for future iterations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🎨 Frontend interface (React, Streamlit, or simple HTML)&lt;/li&gt;
&lt;li&gt;🗣️ Voice input using SpeechRecognition&lt;/li&gt;
&lt;li&gt;🌐 Deploy to Hugging Face Spaces or Render&lt;/li&gt;
&lt;li&gt;🌍 Support for multilingual interaction&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📘 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This project is a great way to experiment with combining multiple AI models to build intelligent systems. Whether you're a developer, researcher, or AI enthusiast, creating a multimodal chatbot offers both a technical challenge and a glimpse into the future of human-computer interaction.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the full code and try it yourself 👉 &lt;a href="https://github.com/EkeminiThompson/multimodal-chatbot-app" rel="noopener noreferrer"&gt;GitHub Repo&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  📬 Stay Connected
&lt;/h2&gt;

&lt;p&gt;Let’s connect on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/EkeminiThompson" rel="noopener noreferrer"&gt;@EkeminiThompson&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/ekemini-thompson" rel="noopener noreferrer"&gt;linkedin.com/in/ekemini-thompson&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you found this helpful, don’t forget to ⭐️ the repo and share the article!&lt;/p&gt;

</description>
      <category>ekeminithompson</category>
      <category>chatbot</category>
      <category>multimodalai</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Deployment of Predictive Maintenance Aircraft Engine System</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Fri, 27 Dec 2024 03:39:58 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/deployment-of-predictive-maintenance-aircraft-engine-system-khc</link>
      <guid>https://dev.to/ekemini_thompson/deployment-of-predictive-maintenance-aircraft-engine-system-khc</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Predictive Maintenance Aircraft Engine&lt;/strong&gt; system is designed to leverage real-time sensor data from aircraft engines to predict when maintenance is needed, minimizing unplanned downtime and optimizing maintenance schedules. This document provides a detailed overview of the deployment process for the system, covering the full-stack architecture, Docker setup, and steps to deploy the application using Docker and Docker Compose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;System Overview&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architecture Design&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Setting Up Docker Containers&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Docker Compose Setup&lt;/li&gt;
&lt;li&gt;Backend and Frontend Dockerfiles&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Running the Application&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deployment Considerations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. System Overview
&lt;/h2&gt;

&lt;p&gt;This system is composed of two key components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Frontend (Dash)&lt;/strong&gt;: A real-time dashboard built using Dash to visualize predictive maintenance results and sensor data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backend (Flask)&lt;/strong&gt;: A Flask-based API that handles model inference, processes incoming sensor data, and exposes endpoints for prediction and analysis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The backend performs the critical task of predicting the maintenance needs based on historical data and real-time sensor input. The frontend displays this information in a user-friendly format, enabling operators to take timely action and improve operational efficiency.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Architecture Design
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Backend (Flask)
&lt;/h3&gt;

&lt;p&gt;The backend is a RESTful API implemented using Flask, designed to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accept incoming requests with sensor data.&lt;/li&gt;
&lt;li&gt;Process this data using machine learning models (e.g., classification or regression) to predict maintenance needs.&lt;/li&gt;
&lt;li&gt;Expose endpoints that the frontend can query for real-time predictions and historical analysis.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Frontend (Dash)
&lt;/h3&gt;

&lt;p&gt;The frontend, built with Dash, serves the purpose of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Displaying real-time predictions, trends, and other data visualizations.&lt;/li&gt;
&lt;li&gt;Allowing users to interact with the predictions and monitor engine performance.&lt;/li&gt;
&lt;li&gt;Making API calls to the backend for up-to-date information.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Containerization with Docker
&lt;/h3&gt;

&lt;p&gt;To streamline deployment and ensure that the application runs consistently across different environments, both the frontend and backend are containerized using Docker. Docker Compose is used to define and manage the multi-container setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Setting Up Docker Containers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Docker Compose Setup
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file orchestrates the deployment of both frontend and backend services. It defines how to build and link the containers, as well as how they communicate with each other via a custom network. Below is an example &lt;code&gt;docker-compose.yml&lt;/code&gt; file that defines the services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.8'&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;backend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;backend/Dockerfile&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;5000:5000"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./data:/app/data&lt;/span&gt;
    &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;app-network&lt;/span&gt;

  &lt;span class="na"&gt;frontend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;frontend/Dockerfile&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8050:8050"&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;backend&lt;/span&gt;
    &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;app-network&lt;/span&gt;

&lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app-network&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;driver&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bridge&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key elements&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;backend&lt;/code&gt; service&lt;/strong&gt;: Runs the Flask API on port &lt;code&gt;5000&lt;/code&gt; and mounts a &lt;code&gt;data&lt;/code&gt; directory for persistent storage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;frontend&lt;/code&gt; service&lt;/strong&gt;: Runs the Dash app on port &lt;code&gt;8050&lt;/code&gt; and depends on the backend to be ready before starting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;app-network&lt;/code&gt;&lt;/strong&gt;: A custom Docker network that allows the frontend and backend to communicate securely.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Backend Dockerfile (&lt;code&gt;backend/Dockerfile&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;This Dockerfile builds the container for the backend service, which runs the Flask API. It includes installation of Python dependencies and setting the environment variables needed to run the Flask application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; python:3.9-slim&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; backend/requirements.txt /app/&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--no-cache-dir&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; backend/ /app/&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 5000&lt;/span&gt;

&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; FLASK_APP=app.py&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; FLASK_RUN_HOST=0.0.0.0&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["flask", "run"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Frontend Dockerfile (&lt;code&gt;frontend/Dockerfile&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;The frontend service is containerized using a similar Dockerfile. This file sets up the Dash app and exposes it on port &lt;code&gt;8050&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; python:3.9-slim&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; frontend/requirements.txt /app/&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--no-cache-dir&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; frontend/ /app/&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 8050&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["python", "app.py"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key elements&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both backend and frontend Dockerfiles install the necessary dependencies, copy the application code, expose the respective ports, and start the application servers when the containers are run.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Running the Application
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before deploying the application, ensure that you have the following installed on your machine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Docker&lt;/strong&gt;: A tool that enables containerization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker Compose&lt;/strong&gt;: A tool for defining and running multi-container Docker applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Steps to Run the Application
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clone the repository&lt;/strong&gt;:
First, clone the GitHub repository and navigate to the project directory.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git clone &amp;lt;repository_url&amp;gt;
   &lt;span class="nb"&gt;cd&lt;/span&gt; &amp;lt;project_directory&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Build and start the services&lt;/strong&gt;:
Using Docker Compose, you can build and start both the backend and frontend services simultaneously.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   docker-compose up &lt;span class="nt"&gt;--build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Access the application&lt;/strong&gt;:&lt;br&gt;
Once the containers are running, you can access the following services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Backend API&lt;/strong&gt;: &lt;code&gt;http://localhost:5000&lt;/code&gt;
This endpoint will accept POST requests with sensor data and return maintenance predictions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frontend (Dash)&lt;/strong&gt;: &lt;code&gt;http://localhost:8050&lt;/code&gt;
This is the interactive dashboard that will visualize maintenance predictions, trends, and other insights in real-time.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stop the services&lt;/strong&gt;:&lt;br&gt;
When you're done, you can stop the services by pressing &lt;code&gt;Ctrl+C&lt;/code&gt; or running:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Deployment Considerations
&lt;/h2&gt;

&lt;p&gt;While Docker provides a consistent development and testing environment, there are additional considerations for deploying the system in a production environment:&lt;/p&gt;

&lt;h3&gt;
  
  
  a) Scaling the Application
&lt;/h3&gt;

&lt;p&gt;Docker Compose is suitable for local development and testing, but for production deployments, you may need to use orchestration tools like Kubernetes to handle scaling and resource management. Kubernetes can automatically scale the frontend and backend services based on traffic demands, ensuring high availability and fault tolerance.&lt;/p&gt;

&lt;h3&gt;
  
  
  b) Monitoring and Logging
&lt;/h3&gt;

&lt;p&gt;To ensure the system is running smoothly in production, integrate &lt;strong&gt;monitoring tools&lt;/strong&gt; like Prometheus and &lt;strong&gt;logging systems&lt;/strong&gt; like ELK stack (Elasticsearch, Logstash, and Kibana). These tools will allow you to track system performance, detect issues in real-time, and troubleshoot effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  c) Model Management
&lt;/h3&gt;

&lt;p&gt;The predictive maintenance model deployed in the backend may require periodic updates as new sensor data becomes available. It's essential to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Monitor model performance&lt;/strong&gt; to ensure its accuracy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrain the model&lt;/strong&gt; periodically with new data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version models&lt;/strong&gt; and keep track of model iterations for reproducibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  d) Security
&lt;/h3&gt;

&lt;p&gt;To secure the communication between the frontend and backend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;HTTPS&lt;/strong&gt; by setting up SSL certificates, especially if you're deploying to a production environment.&lt;/li&gt;
&lt;li&gt;Implement &lt;strong&gt;API rate limiting&lt;/strong&gt; and &lt;strong&gt;authentication mechanisms&lt;/strong&gt; (e.g., JWT tokens) to prevent misuse of the API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  e) Continuous Integration and Deployment (CI/CD)
&lt;/h3&gt;

&lt;p&gt;For automated deployments, integrate a CI/CD pipeline using tools like GitHub Actions, Jenkins, or GitLab CI. This pipeline can automatically build, test, and deploy new versions of the application when changes are pushed to the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Predictive Maintenance Aircraft Engine&lt;/strong&gt; system provides a comprehensive solution for monitoring and predicting maintenance needs in real-time. By combining &lt;strong&gt;Flask&lt;/strong&gt; for the backend API, &lt;strong&gt;Dash&lt;/strong&gt; for interactive visualizations, and &lt;strong&gt;Docker&lt;/strong&gt; for containerization, the system offers a reliable, scalable solution that can be deployed both locally and in production environments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/EkeminiThompson/Real-Time-Predictive-Maintenance-System-for-Aircraft-" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following the steps outlined in this document, you can easily deploy the application on your local machine or prepare it for a production environment. With further enhancements, such as scaling, monitoring, and continuous deployment, this solution can serve as a critical tool for optimizing aircraft engine maintenance operations.&lt;/p&gt;

</description>
      <category>aircraft</category>
      <category>predictive</category>
      <category>devops</category>
      <category>python</category>
    </item>
    <item>
      <title>Building an Automated Customer Support System with NLP and Machine Learning</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Sat, 17 Aug 2024 14:24:51 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/building-an-automated-customer-support-system-with-nlp-and-machine-learning-16f5</link>
      <guid>https://dev.to/ekemini_thompson/building-an-automated-customer-support-system-with-nlp-and-machine-learning-16f5</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;In the age of digital transformation, providing efficient and effective customer support is crucial for businesses. Automating customer support using Natural Language Processing (NLP) and machine learning can significantly enhance the customer experience by providing instant responses and resolving common queries without human intervention. This article walks through the steps to build an automated customer support system, leveraging NLP to understand and respond to customer inquiries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Objectives
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Develop an NLP model&lt;/strong&gt; to understand and process customer queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implement a machine learning algorithm&lt;/strong&gt; to classify and route inquiries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create a user-friendly interface&lt;/strong&gt; for customers to interact with the support system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integrate a feedback loop&lt;/strong&gt; to continuously improve the system based on user interactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ensure seamless escalation&lt;/strong&gt; to human agents for complex issues.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Technologies Used
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Programming Languages:&lt;/strong&gt; Python&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frameworks and Libraries:&lt;/strong&gt; Flask, NLTK, scikit-learn, joblib&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Databases:&lt;/strong&gt; CSV (for simplicity, can be replaced with any DBMS)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;APIs:&lt;/strong&gt; RESTful APIs for integration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools:&lt;/strong&gt; curl, PowerShell, Command Prompt&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step-by-Step Implementation
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Setting Up the Environment
&lt;/h4&gt;

&lt;p&gt;Begin by installing the necessary Python packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install Flask&lt;/span&gt;
pip &lt;span class="nb"&gt;install &lt;/span&gt;Flask

&lt;span class="c"&gt;# Install NLP libraries&lt;/span&gt;
pip &lt;span class="nb"&gt;install &lt;/span&gt;nltk

&lt;span class="c"&gt;# Install ML libraries&lt;/span&gt;
pip &lt;span class="nb"&gt;install &lt;/span&gt;scikit-learn joblib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Data Collection and Preprocessing
&lt;/h4&gt;

&lt;p&gt;Assume you have a CSV file (&lt;code&gt;customer_support_data.csv&lt;/code&gt;) with columns: &lt;code&gt;query&lt;/code&gt; and &lt;code&gt;category&lt;/code&gt;. The preprocessing step involves cleaning and preparing the text data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# data_preprocessing.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;nltk&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.model_selection&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;train_test_split&lt;/span&gt;

&lt;span class="c1"&gt;# Download necessary NLTK data
&lt;/span&gt;&lt;span class="n"&gt;nltk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;punkt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;nltk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;wordnet&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Load the data
&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;customer_support_data.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Preprocess the text data
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;preprocess_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nltk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;word_tokenize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;lemmatizer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nltk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WordNetLemmatizer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;lemmatizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lemmatize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;preprocess_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Split the data into training and testing sets
&lt;/span&gt;&lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;train_test_split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save the preprocessed data
&lt;/span&gt;&lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;train_data.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;test_data.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Building the NLP Model
&lt;/h4&gt;

&lt;p&gt;Using a TF-IDF vectorizer and a logistic regression model for classification:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# nlp_model.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.feature_extraction.text&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TfidfVectorizer&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.linear_model&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;LogisticRegression&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.pipeline&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;make_pipeline&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;accuracy_score&lt;/span&gt;

&lt;span class="c1"&gt;# Load the preprocessed data
&lt;/span&gt;&lt;span class="n"&gt;train_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;train_data.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;test_data.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Extract features and labels
&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;category&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;test_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;test_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;category&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Build the NLP model pipeline
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;make_pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TfidfVectorizer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;LogisticRegression&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# Train the model
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Evaluate the model
&lt;/span&gt;&lt;span class="n"&gt;y_pred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Accuracy: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save the model
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;
&lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;nlp_model.joblib&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Creating the Flask API
&lt;/h4&gt;

&lt;p&gt;Develop a Flask API to handle incoming queries and return the predicted category.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;

&lt;span class="c1"&gt;# Load the trained model
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;nlp_model.joblib&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/predict&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;])[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;category&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;error&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;No query provided&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. Running and Testing the Flask App
&lt;/h4&gt;

&lt;p&gt;Start the Flask app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test the endpoint using &lt;code&gt;curl&lt;/code&gt; in Command Prompt or &lt;code&gt;Invoke-RestMethod&lt;/code&gt; in PowerShell.&lt;/p&gt;

&lt;h5&gt;
  
  
  Using &lt;code&gt;Invoke-RestMethod&lt;/code&gt; in PowerShell:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Invoke-RestMethod&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Uri&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;http://127.0.0.1:5000/predict&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Method&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ContentType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"application/json"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Body&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'{"query": "How can I reset my password?"}'&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Using &lt;code&gt;curl&lt;/code&gt; in Command Prompt:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X POST http://127.0.0.1:5000/predict -H "Content-Type: application/json" -d "{\"query\": \"How can I reset my password?\"}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enhancements and Next Steps
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Enhance the NLP Model:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Experiment with advanced models like BERT or GPT.&lt;/li&gt;
&lt;li&gt;Use comprehensive preprocessing techniques.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Develop the User Interface:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a web or mobile interface using React, Angular, or any framework of your choice.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Integrate with a Backend System:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement user session management and query processing.&lt;/li&gt;
&lt;li&gt;Ensure seamless communication between the frontend and the Flask API.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Implement Feedback Mechanism:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow users to rate the responses.&lt;/li&gt;
&lt;li&gt;Use feedback to retrain and improve the models.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Deployment:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Containerize the application using Docker.&lt;/li&gt;
&lt;li&gt;Deploy to a cloud platform like AWS, GCP, or Azure.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;By following these &lt;a href="https://github.com/EkeminiThompson/customer_support_system_nlp.git" rel="noopener noreferrer"&gt;repository&lt;/a&gt;, you can create an automated customer support system that leverages NLP and machine learning to provide efficient and effective customer support. This system not only enhances customer satisfaction but also reduces the workload on human support agents, allowing them to focus on more complex issues. Happy coding!&lt;/p&gt;

</description>
      <category>automation</category>
      <category>nlp</category>
      <category>machinelearning</category>
      <category>tensorflow</category>
    </item>
    <item>
      <title>Building a Real-Time Credit Card Fraud Detection System with FastAPI and Machine Learning</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Mon, 12 Aug 2024 22:00:40 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/building-a-real-time-credit-card-fraud-detection-system-with-fastapi-and-machine-learning-3g0m</link>
      <guid>https://dev.to/ekemini_thompson/building-a-real-time-credit-card-fraud-detection-system-with-fastapi-and-machine-learning-3g0m</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Credit card fraud poses a significant threat to the financial industry, leading to billions of dollars in losses every year. To combat this, machine learning models have been developed to detect and prevent fraudulent transactions in real time. In this article, we'll walk through the process of building a real-time credit card fraud detection system using FastAPI, a modern web framework for Python, and a Random Forest classifier trained on the popular Credit Card Fraud Detection Dataset from Kaggle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview of the Project
&lt;/h2&gt;

&lt;p&gt;The goal of this project is to create a web service that predicts the likelihood of a credit card transaction being fraudulent. The service accepts transaction data, preprocesses it, and returns a prediction along with the probability of fraud. This system is designed to be fast, scalable, and easy to integrate into existing financial systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Components
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Machine Learning Model&lt;/strong&gt;: A Random Forest classifier trained to distinguish between fraudulent and legitimate transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Preprocessing&lt;/strong&gt;: Standardization of transaction features to ensure the model performs optimally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API&lt;/strong&gt;: A RESTful API built with FastAPI to handle prediction requests in real time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: Preparing the Dataset
&lt;/h2&gt;

&lt;p&gt;The dataset used in this project is the Credit Card Fraud Detection Dataset from Kaggle, which contains 284,807 transactions, of which only 492 are fraudulent. This class imbalance presents a challenge, but it's addressed by oversampling the minority class.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Preprocessing
&lt;/h3&gt;

&lt;p&gt;The features are first standardized using a &lt;code&gt;StandardScaler&lt;/code&gt; from &lt;code&gt;scikit-learn&lt;/code&gt;. The dataset is then split into training and testing sets. Given the imbalance, the &lt;code&gt;RandomOverSampler&lt;/code&gt; technique is applied to balance the classes before training the model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;imblearn.over_sampling&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RandomOverSampler&lt;/span&gt;

&lt;span class="c1"&gt;# Standardize features
&lt;/span&gt;&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;X_scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Balance the dataset
&lt;/span&gt;&lt;span class="n"&gt;ros&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RandomOverSampler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;X_resampled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_resampled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ros&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_resample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_scaled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Training the Machine Learning Model
&lt;/h2&gt;

&lt;p&gt;We train a Random Forest classifier, which is well-suited for handling imbalanced datasets and provides robust predictions. The model is trained on the oversampled data, and its performance is evaluated using accuracy, precision, recall, and the AUC-ROC curve.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.ensemble&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RandomForestClassifier&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;classification_report&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;roc_auc_score&lt;/span&gt;

&lt;span class="c1"&gt;# Train the model
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RandomForestClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_estimators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_resampled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_resampled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Evaluate the model
&lt;/span&gt;&lt;span class="n"&gt;y_pred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test_scaled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;classification_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AUC-ROC:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_proba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test_scaled&lt;/span&gt;&lt;span class="p"&gt;)[:,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Building the FastAPI Application
&lt;/h2&gt;

&lt;p&gt;With the trained model and scaler saved using &lt;code&gt;joblib&lt;/code&gt;, we move on to building the FastAPI application. FastAPI is chosen for its speed and ease of use, making it ideal for real-time applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the API
&lt;/h3&gt;

&lt;p&gt;The FastAPI application defines a POST endpoint &lt;code&gt;/predict/&lt;/code&gt; that accepts transaction data, processes it, and returns the model's prediction and probability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HTTPException&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BaseModel&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="c1"&gt;# Load the trained model and scaler
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;random_forest_model.pkl&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scaler.pkl&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;V1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;
    &lt;span class="n"&gt;V2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;
    &lt;span class="c1"&gt;# Include all other features used in your model
&lt;/span&gt;    &lt;span class="n"&gt;Amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;

&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/predict/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Transaction&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;()])&lt;/span&gt;
        &lt;span class="n"&gt;scaled_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scaled_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;prediction_proba&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_proba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scaled_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fraud_prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;probability&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction_proba&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])}&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Deploying the Application
&lt;/h2&gt;

&lt;p&gt;To test the application locally, you can run the FastAPI server using &lt;code&gt;uvicorn&lt;/code&gt; and send POST requests to the &lt;code&gt;/predict/&lt;/code&gt; endpoint. The service will process incoming requests, scale the data, and return whether the transaction is fraudulent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running the API Locally
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uvicorn main:app &lt;span class="nt"&gt;--reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then test the API using &lt;code&gt;curl&lt;/code&gt; or a tool like Postman:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://127.0.0.1:8000/predict/ &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"V1": -1.359807134, "V2": -0.072781173, ..., "Amount": 149.62}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API will return a JSON object with the fraud prediction and the associated probability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we've built a real-time credit card fraud detection system that combines machine learning with a modern web framework. The github link is &lt;a href="https://github.com/EkeminiThompson/Credit-Card-Fraud-Detection-with-FastAPI-.git" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The system is designed to handle real-time transaction data and provide instant predictions, making it a valuable tool for financial institutions looking to combat fraud.&lt;/p&gt;

&lt;p&gt;By deploying this model using FastAPI, we ensure that the service is not only fast but also scalable, capable of handling multiple requests concurrently. This project can be further extended with more sophisticated models, improved feature engineering, or integration with a production environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;To enhance the system further, consider the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Model Improvements&lt;/strong&gt;: Experiment with more advanced models like XGBoost or neural networks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Feature Engineering&lt;/strong&gt;: Explore additional features that might improve model accuracy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-World Deployment&lt;/strong&gt;: Deploy the application on cloud platforms like AWS or GCP for production use.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>fastapi</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>Harnessing Real-Time Object Detection in the Browser with TensorFlow.js and COCO-SSD</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Thu, 25 Jul 2024 02:56:03 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/harnessing-real-time-object-detection-in-the-browser-with-tensorflowjs-and-coco-ssd-23hp</link>
      <guid>https://dev.to/ekemini_thompson/harnessing-real-time-object-detection-in-the-browser-with-tensorflowjs-and-coco-ssd-23hp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In recent years, the field of machine learning has seen remarkable advancements, particularly in bringing powerful models to web applications. One such breakthrough is the ability to perform real-time object detection directly within a web browser, thanks to technologies like TensorFlow.js and models such as COCO-SSD. This article explores how developers can leverage these tools to create interactive applications that detect objects in live webcam streams, uploaded images, or videos, all without requiring server-side processing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding TensorFlow.js and COCO-SSD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TensorFlow.js is a JavaScript library developed by Google that allows developers to run machine learning models directly in the browser. It provides a way to deploy pre-trained models or train new ones using JavaScript APIs, making it accessible and easy to integrate with web applications. COCO-SSD (Common Objects in Context - Single Shot MultiBox Detector) is a popular pre-trained model for object detection. It is optimized to detect a wide variety of objects in real-time, making it suitable for interactive applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the Environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin, developers need to set up their development environment. This typically involves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Including TensorFlow.js and COCO-SSD in the HTML document using script tags:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd@latest"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Creating HTML structure to handle user interface elements like video input, image upload, and control buttons.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Building the Application&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Handling User Input
&lt;/h3&gt;

&lt;p&gt;The application allows users to choose between different input types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Webcam&lt;/strong&gt;: Directly captures live video feed from the user's webcam.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image&lt;/strong&gt;: Allows users to upload an image file from their device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Video&lt;/strong&gt;: Enables users to upload a video file for object detection.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"inputSelection"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"radio"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"inputType"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"webcam"&lt;/span&gt; &lt;span class="na"&gt;checked&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; Webcam&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"radio"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"inputType"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"image"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; Image&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"radio"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"inputType"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"video"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; Video&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"imageInput"&lt;/span&gt; &lt;span class="na"&gt;accept=&lt;/span&gt;&lt;span class="s"&gt;"image/*"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"display:none;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"videoInput"&lt;/span&gt; &lt;span class="na"&gt;accept=&lt;/span&gt;&lt;span class="s"&gt;"video/*"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"display:none;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Displaying Input and Results
&lt;/h3&gt;

&lt;p&gt;The application dynamically displays the selected input (video or image) and detection results using HTML5 elements like &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"videoContainer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;video&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"videoElement"&lt;/span&gt; &lt;span class="na"&gt;autoplay&lt;/span&gt; &lt;span class="na"&gt;playsinline&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/video&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"infoBox"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"infoBox"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;&lt;/span&gt;Detected Object:&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"objectLabel"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;&lt;/span&gt;Confidence:&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"confidenceScore"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"imageDisplay"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;video&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"videoDisplay"&lt;/span&gt; &lt;span class="na"&gt;controls&lt;/span&gt; &lt;span class="na"&gt;loop&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/video&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"outputCanvas"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Implementing Object Detection Logic
&lt;/h3&gt;

&lt;p&gt;JavaScript (&lt;code&gt;script.js&lt;/code&gt;) handles the object detection logic using TensorFlow.js and COCO-SSD. This involves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initializing the model and loading it asynchronously:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;cocoSsd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Performing detection on selected input (video or image) and updating the UI with results:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;detectObjects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;loadModel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;detect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// Update UI with predictions&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Handling different input types (webcam, image, video) and triggering detection based on user actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. User Interaction and Controls
&lt;/h3&gt;

&lt;p&gt;The application includes buttons for controlling object detection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start Detection&lt;/strong&gt;: Initiates object detection based on selected input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stop Detection&lt;/strong&gt;: Pauses or stops the detection process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Capture Screenshot&lt;/strong&gt;: Allows users to capture a screenshot of the current detection result.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"controls"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"startButton"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Start Detection&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"stopButton"&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Stop Detection&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"captureButton"&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Capture Screenshot&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Enhancing User Experience
&lt;/h3&gt;

&lt;p&gt;To provide a seamless experience, the application includes a loading indicator (&lt;code&gt;&amp;lt;div id="loadingIndicator"&amp;gt;Loading...&amp;lt;/div&amp;gt;&lt;/code&gt;) to notify users while TensorFlow.js and the COCO-SSD model are being loaded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, TensorFlow.js combined with COCO-SSD opens up exciting possibilities for real-time object detection directly within web browsers. This article has demonstrated the fundamental components and steps involved in building such an application, from setting up the environment to implementing object detection logic and enhancing user interaction. Developers can now leverage these technologies to create interactive and responsive web applications that push the boundaries of what's possible with machine learning on the web. As these technologies continue to evolve, the future looks promising for even more sophisticated and accessible AI-powered web experiences.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/EkeminiThompson/advanced_object_detection.git" rel="noopener noreferrer"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tensorflow</category>
      <category>javascript</category>
      <category>machinelearning</category>
      <category>python</category>
    </item>
    <item>
      <title>Predictive Maintenance in the Upstream Sector: Implementing Machine Learning with Flask</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Fri, 19 Jul 2024 16:27:43 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/predictive-maintenance-in-the-upstream-sector-implementing-machine-learning-with-flask-2oam</link>
      <guid>https://dev.to/ekemini_thompson/predictive-maintenance-in-the-upstream-sector-implementing-machine-learning-with-flask-2oam</guid>
      <description>&lt;p&gt;In today's fast-paced industrial landscape, predictive maintenance has emerged as a critical strategy to optimize asset management and operational efficiency. This article explores the practical implementation of machine learning techniques within the upstream sector, specifically focusing on a Flask-based application for predictive maintenance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Predictive maintenance leverages historical data and machine learning algorithms to predict equipment failures before they occur, thereby minimizing downtime and operational disruptions. Within the upstream sector of the oil and gas industry, where equipment reliability directly impacts production and safety, predictive maintenance holds significant promise.&lt;/p&gt;

&lt;h3&gt;
  
  
  Machine Learning Model Selection
&lt;/h3&gt;

&lt;p&gt;Central to our application is the selection and deployment of a RandomForestClassifier, a robust ensemble learning method suitable for both classification tasks and handling complex datasets often encountered in industrial settings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.ensemble&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RandomForestClassifier&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize the RandomForestClassifier model
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RandomForestClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_estimators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Train the model with your dataset (X_train, y_train)
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save the trained model to a file
&lt;/span&gt;&lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;models/predictive_model.pkl&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Flask Application Setup
&lt;/h3&gt;

&lt;p&gt;Flask provides a flexible and lightweight framework for developing web applications in Python. Our Flask application integrates the trained RandomForestClassifier model to predict equipment health based on sensor data inputs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installation and Dependencies
&lt;/h4&gt;

&lt;p&gt;Ensure you have Python and pip installed. Create a virtual environment and install Flask and required libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv
&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate  &lt;span class="c"&gt;# Activate virtual environment&lt;/span&gt;
pip &lt;span class="nb"&gt;install &lt;/span&gt;Flask scikit-learn joblib pandas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Flask Application Structure
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;predictive-maintenance-flask/
├── app.py                  # Flask application
├── requirements.txt        # Python dependencies
├── static/
│   └── style.css           # CSS styles
└── templates/
    └── index.html          # HTML template
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Flask Application Code
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;app.py&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="c1"&gt;# Load the trained machine learning model
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;models/predictive_model.pkl&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Home route
&lt;/span&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;home&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;index.html&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Endpoint to receive sensor data and make predictions
&lt;/span&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/predict&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# Load the model if not already loaded
&lt;/span&gt;
    &lt;span class="c1"&gt;# Get data from the POST request
&lt;/span&gt;    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_dict&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Convert the data into a DataFrame
&lt;/span&gt;    &lt;span class="n"&gt;input_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="c1"&gt;# Make predictions
&lt;/span&gt;    &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;prediction_prob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_proba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_data&lt;/span&gt;&lt;span class="p"&gt;)[:,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# Probability of failure
&lt;/span&gt;
    &lt;span class="c1"&gt;# Prepare response
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Equipment failure predicted.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Equipment functioning normally.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;probability&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction_prob&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Usage and Deployment
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Input and Prediction&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users input sensor data (e.g., temperature, pressure) via a web form.&lt;/li&gt;
&lt;li&gt;The Flask application utilizes the trained model to predict equipment health status (failure or normal) and provides a probability score.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Integration and Scalability&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flask's modular architecture allows easy integration with other frameworks and scalable deployment options.&lt;/li&gt;
&lt;li&gt;Enhance the application with real-time data streaming and advanced visualization tools to further optimize predictive maintenance strategies.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;By harnessing machine learning and deploying it through Flask, organizations in the upstream sector can achieve proactive maintenance strategies that enhance operational efficiency, minimize downtime, and ensure sustainable production levels. As technology continues to evolve, the application of predictive maintenance will play an increasingly crucial role in maintaining competitiveness and sustainability in industrial operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/EkeminiThompson/predictive_maintenance_upstream.git" rel="noopener noreferrer"&gt;Github link here &lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flask</category>
      <category>machinelearning</category>
      <category>pk</category>
    </item>
    <item>
      <title>Integrated Traffic Management System with Predictive Modeling and Visualization</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Wed, 17 Jul 2024 04:22:09 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/integrated-traffic-management-system-with-predictive-modeling-and-visualization-37ef</link>
      <guid>https://dev.to/ekemini_thompson/integrated-traffic-management-system-with-predictive-modeling-and-visualization-37ef</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;The Traffic Management System (TMS) presented here integrates predictive modeling and real-time visualization to facilitate efficient traffic control and incident management. Developed using Python and Tkinter for the graphical interface, this system leverages machine learning algorithms to forecast traffic volume based on weather conditions and rush hour dynamics. The application visualizes historical and predicted traffic data through interactive graphs, providing insights crucial for decision-making in urban traffic management.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Traffic Prediction:&lt;/strong&gt; Utilizes machine learning models (Linear Regression and Random Forest) to predict traffic volume based on temperature, precipitation, and rush hour indicators.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Graphical Visualization:&lt;/strong&gt; Displays historical traffic trends alongside predicted volumes on interactive graphs, enhancing understanding and monitoring capabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-time Traffic Simulation:&lt;/strong&gt; Simulates traffic light changes to replicate real-world scenarios, aiding in assessing system responses under various conditions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Incident Reporting:&lt;/strong&gt; Allows users to report incidents, capturing location and description for prompt management and response.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Ensure Python 3.x is installed. Install dependencies using pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;pandas matplotlib scikit-learn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clone the repository:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git clone &amp;lt;https://github.com/EkeminiThompson/traffic_management_system.git&amp;gt;
   &lt;span class="nb"&gt;cd &lt;/span&gt;traffic-management-system
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install dependencies:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Run the application:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   python main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Traffic Prediction:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select a location, date, and model (Linear Regression or Random Forest).&lt;/li&gt;
&lt;li&gt;Click "Predict Traffic" to see the predicted traffic volume.&lt;/li&gt;
&lt;li&gt;Clear the graph using "Clear Graph" button.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Graphical Visualization:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The graph shows historical traffic data and predicted volumes for the selected date.&lt;/li&gt;
&lt;li&gt;Red dashed line indicates the prediction date, and green dot shows the predicted traffic volume.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Traffic Light Control:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simulates changing traffic light colors (Red, Green, Yellow) to assess traffic flow dynamics.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Incident Reporting:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Report traffic incidents by entering location and description.&lt;/li&gt;
&lt;li&gt;Click "Report Incident" to submit the report.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Code Overview
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;main.py&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Main application using Tkinter for GUI
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tkinter&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;tk&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tkinter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;messagebox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ttk&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;matplotlib.backends.backend_tkagg&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FigureCanvasTkAgg&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.linear_model&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;LinearRegression&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.ensemble&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RandomForestRegressor&lt;/span&gt;

&lt;span class="c1"&gt;# Mock data for demonstration
&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;temperature&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;precipitation&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;hour&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;traffic_volume&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Feature engineering
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;is_rush_hour&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;hour&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Model training
&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;temperature&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;precipitation&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;is_rush_hour&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;traffic_volume&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Create models
&lt;/span&gt;&lt;span class="n"&gt;linear_model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LinearRegression&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;linear_model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;forest_model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RandomForestRegressor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_estimators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;forest_model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TrafficManagementApp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Initialization of GUI
&lt;/span&gt;        &lt;span class="c1"&gt;# ...
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_submit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Handling traffic prediction submission
&lt;/span&gt;        &lt;span class="c1"&gt;# ...
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_graph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;date_str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Updating graph with historical and predicted traffic data
&lt;/span&gt;        &lt;span class="c1"&gt;# ...
&lt;/span&gt;
    &lt;span class="c1"&gt;# Other methods for GUI components and functionality
&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Tk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TrafficManagementApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mainloop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The Traffic Management System is a sophisticated tool for urban planners and traffic controllers, combining advanced predictive analytics with intuitive graphical interfaces. By forecasting traffic patterns and visualizing data trends, the system enhances decision-making capabilities and facilitates proactive management of traffic resources. Its user-friendly design ensures accessibility and practicality, making it a valuable asset in modern urban infrastructure management.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tinker</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Deploying a Static Website on AWS EC2 with NGINX</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Sat, 06 Jul 2024 03:02:11 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/deploying-a-static-website-on-aws-ec2-with-nginx-29on</link>
      <guid>https://dev.to/ekemini_thompson/deploying-a-static-website-on-aws-ec2-with-nginx-29on</guid>
      <description>&lt;p&gt;In today’s digital age, having a personal or professional website is almost essential. Whether you’re a budding DevOps engineer, a web developer, or a business owner, knowing how to deploy a website is a crucial skill. This guide will take you through deploying a static website on an AWS EC2 instance using NGINX. By the end of this article, you’ll have your website live and accessible to the world.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before we dive in, make sure you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An AWS account&lt;/li&gt;
&lt;li&gt;Basic understanding of AWS EC2, SSH, and NGINX&lt;/li&gt;
&lt;li&gt;Your static website files ready (HTML, CSS, JavaScript)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Launch an EC2 Instance
&lt;/h3&gt;

&lt;p&gt;First, we need to launch an EC2 instance on AWS.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Login to AWS Management Console:&lt;/strong&gt;&lt;br&gt;
Navigate to the EC2 dashboard and click on "Launch Instance."&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure Instance:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose an Amazon Machine Image (AMI). We will use the Amazon Linux 2 AMI for this guide.&lt;/li&gt;
&lt;li&gt;Select an instance type (t2.micro is suitable for our needs).&lt;/li&gt;
&lt;li&gt;Configure the instance details, and add storage if necessary.&lt;/li&gt;
&lt;li&gt;Add a tag (optional, but recommended for organization).&lt;/li&gt;
&lt;li&gt;Configure the security group to allow SSH (port 22) and HTTP (port 80) traffic.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Launch Instance:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Review your settings and launch the instance.&lt;/li&gt;
&lt;li&gt;Download the private key (.pem) file, which you will need to access your instance via SSH.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 2: Connect to Your EC2 Instance
&lt;/h3&gt;

&lt;p&gt;With your instance running, the next step is to connect to it using SSH.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Open a terminal and navigate to the directory containing your private key file:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;cd &lt;/span&gt;path_to_your_pem_file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Connect to the instance:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   ssh &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s2"&gt;"MyProfile.pem"&lt;/span&gt; ec2-user@your-ec2-public-ip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Install NGINX
&lt;/h3&gt;

&lt;p&gt;Now that you're connected to your instance, it's time to install NGINX, the web server that will serve your static website.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Update the package index:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;yum update &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install NGINX:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;amazon-linux-extras &lt;span class="nb"&gt;install &lt;/span&gt;nginx1.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Start and enable NGINX:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start nginx
   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Transfer Your Static Website Files
&lt;/h3&gt;

&lt;p&gt;Next, you need to transfer your website files to the EC2 instance.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use SCP (Secure Copy Protocol) to transfer your files:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   scp &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s2"&gt;"MyProfile.pem"&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; /path_to_your_website_files/&lt;span class="k"&gt;*&lt;/span&gt; ec2-user@your-ec2-public-ip:/home/ec2-user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Move the files to the NGINX root directory:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo mv&lt;/span&gt; /home/ec2-user/&lt;span class="k"&gt;*&lt;/span&gt; /usr/share/nginx/html/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Configure NGINX
&lt;/h3&gt;

&lt;p&gt;To ensure that NGINX serves your website correctly, we need to adjust its configuration.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Edit the NGINX configuration file:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/nginx.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Update the server block to point to your website files:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;   &lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;your-ec2-public-ip&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

       &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="kn"&gt;root&lt;/span&gt; &lt;span class="n"&gt;/usr/share/nginx/html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="kn"&gt;index&lt;/span&gt; &lt;span class="s"&gt;index.html&lt;/span&gt; &lt;span class="s"&gt;index.htm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Test the configuration and restart NGINX:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;nginx &lt;span class="nt"&gt;-t&lt;/span&gt;
   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: Access Your Website
&lt;/h3&gt;

&lt;p&gt;Finally, open your web browser and enter your EC2 instance’s public IP address. Your static website should now be accessible via HTTP on port 80.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example Files
&lt;/h3&gt;

&lt;p&gt;Here's a &lt;a href="https://github.com/EkeminiThompson/hng.git" rel="noopener noreferrer"&gt;glimpse&lt;/a&gt; of the files used in this deployment:&lt;/p&gt;

&lt;h4&gt;
  
  
  index.html
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Ekemini Thompson - DevOps Engineer&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"style.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;header&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"header-content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Ekemini Thompson&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;DevOps Engineer HNG Intern &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://hng.tech"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;https://hng.tech&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#skills"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Skills&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#projects"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Projects&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#contact"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Other sections --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  style.css
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'Arial'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f0f0f0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;scroll-behavior&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;smooth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#1a1a1a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nc"&gt;.active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#00ADB5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congratulations! You successfully deployed a static website on an AWS EC2 instance using NGINX. This setup ensures that your website is accessible via a public IP address on port 80, providing a reliable and scalable solution for hosting static content.&lt;/p&gt;

&lt;p&gt;For more details or to explore further enhancements, visit the &lt;a href="https://hng.tech" rel="noopener noreferrer"&gt;HNG Internship&lt;/a&gt; website or my &lt;a href="https://github.com/EkeminiThompson/hng.git" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Deploying your website can seem daunting at first, but it becomes a manageable and rewarding process with the right steps. Happy deploying!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ec2</category>
      <category>nginx</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Automating User Management on Ubuntu with Bash Scripting</title>
      <dc:creator>Ekemini Thompson</dc:creator>
      <pubDate>Thu, 04 Jul 2024 04:20:56 +0000</pubDate>
      <link>https://dev.to/ekemini_thompson/automating-user-management-on-ubuntu-with-bash-scripting-59pc</link>
      <guid>https://dev.to/ekemini_thompson/automating-user-management-on-ubuntu-with-bash-scripting-59pc</guid>
      <description>&lt;p&gt;In modern IT operations, efficient user management on Linux systems is pivotal for maintaining security and operational flow. Automating tasks such as user creation, group management, and password handling not only saves time but also enhances consistency and reduces errors. This article delves into how to achieve these automation goals using a Bash script (create_users.sh) specifically tailored for Ubuntu environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Script Overview&lt;/strong&gt;&lt;br&gt;
The create_users.sh script simplifies the complex task of managing user accounts through automation. It reads from an input file (user_list.txt), where each line specifies a username followed by associated groups. Upon execution, the script creates users, assigns them to specified groups, sets up their home directories with proper permissions, generates secure passwords, and logs all activities for accountability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Implementation Steps&lt;/strong&gt;&lt;br&gt;
Input Parsing: The script efficiently parses input to extract usernames and their respective groups, disregarding unnecessary whitespace for cleaner processing.&lt;/p&gt;

&lt;p&gt;User and Group Management: For each user entry:&lt;/p&gt;

&lt;p&gt;Checks for existing users and creates new ones as needed.&lt;br&gt;
Ensures groups are created if they do not already exist, then assigns users accordingly using administrative commands (useradd, groupadd, usermod).&lt;br&gt;
Home Directory Setup: Post user creation, the script configures home directories under /home/username, ensuring appropriate permissions (chmod) and ownership (chown) are set for security and accessibility.&lt;/p&gt;

&lt;p&gt;Password Security: Passwords are randomly generated using cryptographic standards (/dev/urandom) and securely stored in /var/secure/user_passwords.csv. This file restricts access to only authorized users, maintaining confidentiality.&lt;/p&gt;

&lt;p&gt;Logging: All script actions, from user creation to password generation, are logged meticulously in /var/log/user_management.log. This log serves as a comprehensive audit trail, aiding troubleshooting and compliance efforts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployment Guide&lt;/strong&gt;&lt;br&gt;
To deploy the create_users.sh script:&lt;/p&gt;

&lt;p&gt;Clone the repository and navigate to the script directory.&lt;br&gt;
Prepare the user_list.txt file with usernames and their respective groups.&lt;br&gt;
Execute the script with root privileges (sudo bash create_users.sh user_list.txt).&lt;br&gt;
Review logs in /var/log/user_management.log and access passwords securely stored in /var/secure/user_passwords.csv as needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Detailed Script Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;create_users.sh&lt;/code&gt; script simplifies the complex task of managing user accounts through automation. It reads from an input file (&lt;code&gt;user_list.txt&lt;/code&gt;), where each line specifies a username followed by associated groups. Upon execution, the script creates users, assigns them to specified groups, sets up their home directories with proper permissions, generates secure passwords, and logs all activities for accountability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Implementation Steps&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Input Parsing&lt;/strong&gt;: The script efficiently parses input to extract usernames and their respective groups, disregarding unnecessary whitespace for cleaner processing.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;LOGFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/var/log/user_management.log"&lt;/span&gt;
&lt;span class="nv"&gt;PASSWORD_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/var/secure/user_passwords.csv"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$EUID&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-ne&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Please run as root"&lt;/span&gt;
    &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi

if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Input file not found!"&lt;/span&gt;
    &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /var/secure
&lt;span class="nb"&gt;touch&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PASSWORD_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;chmod &lt;/span&gt;600 &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PASSWORD_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nv"&gt;IFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;';'&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; username &lt;span class="nb"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
    &lt;/span&gt;&lt;span class="nv"&gt;username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | xargs&lt;span class="si"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;groups&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$groups&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | xargs&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;User and Group Management&lt;/strong&gt;: For each user entry:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks for existing users and creates new ones as needed.&lt;/li&gt;
&lt;li&gt;Ensures groups are created if they do not already exist, then assigns users accordingly using administrative commands (&lt;code&gt;useradd&lt;/code&gt;, &lt;code&gt;groupadd&lt;/code&gt;, &lt;code&gt;usermod&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;amp;&amp;gt;/dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"User &lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt; already exists."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;else
        &lt;/span&gt;useradd &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"User &lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt; created successfully."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;fi

    &lt;/span&gt;&lt;span class="nv"&gt;user_group&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; getent group &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$user_group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;amp;&amp;gt;/dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;groupadd &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$user_group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Group &lt;/span&gt;&lt;span class="nv"&gt;$user_group&lt;/span&gt;&lt;span class="s2"&gt; created successfully."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;fi

    &lt;/span&gt;usermod &lt;span class="nt"&gt;-aG&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$user_group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

    &lt;span class="nv"&gt;IFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-ra&lt;/span&gt; ADDR &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$groups&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;group &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ADDR&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
        &lt;/span&gt;&lt;span class="nv"&gt;group&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | xargs&lt;span class="si"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; getent group &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;amp;&amp;gt;/dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
            &lt;/span&gt;groupadd &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
            &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Group &lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="s2"&gt; created successfully."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;fi
        &lt;/span&gt;usermod &lt;span class="nt"&gt;-aG&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Added &lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt; to group &lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Home Directory Setup&lt;/strong&gt;: Post user creation, the script configures home directories under &lt;code&gt;/home/username&lt;/code&gt;, ensuring appropriate permissions (&lt;code&gt;chmod&lt;/code&gt;) and ownership (&lt;code&gt;chown&lt;/code&gt;) are set for security and accessibility.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;    &lt;span class="nv"&gt;home_dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/home/&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$home_dir&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;chown&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$user_group&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$home_dir&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nb"&gt;chmod &lt;/span&gt;755 &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$home_dir&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Home directory &lt;/span&gt;&lt;span class="nv"&gt;$home_dir&lt;/span&gt;&lt;span class="s2"&gt; set up for &lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Password Security&lt;/strong&gt;: Passwords are randomly generated using cryptographic standards (&lt;code&gt;/dev/urandom&lt;/code&gt;) and securely stored in &lt;code&gt;/var/secure/user_passwords.csv&lt;/code&gt;. This file restricts access to only authorized users, maintaining confidentiality.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;    &lt;span class="nv"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="nt"&gt;-dc&lt;/span&gt; A-Za-z0-9 &amp;lt;/dev/urandom | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; 12&lt;span class="si"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | chpasswd
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PASSWORD_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Password set for &lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Logging&lt;/strong&gt;: All script actions, from user creation to password generation, are logged meticulously in &lt;code&gt;/var/log/user_management.log&lt;/code&gt;. This log serves as a comprehensive audit trail, aiding troubleshooting and compliance efforts.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;done&lt;/span&gt; &amp;lt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"User creation process completed."&lt;/span&gt; | &lt;span class="nb"&gt;tee&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOGFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Deployment Guide
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Prerequisites
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Ubuntu environment with Bash shell.&lt;/li&gt;
&lt;li&gt;Root or sudo privileges to execute administrative commands (&lt;code&gt;useradd&lt;/code&gt;, &lt;code&gt;groupadd&lt;/code&gt;, &lt;code&gt;chmod&lt;/code&gt;, &lt;code&gt;chown&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Steps to Deploy
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clone the Repository:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git clone https://github.com/EkeminiThompson/user_automation
   &lt;span class="nb"&gt;cd &lt;/span&gt;create_users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prepare Input File (&lt;code&gt;user_list.txt&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
Create a text file with usernames and their respective groups in the format specified.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run the Script:&lt;/strong&gt;&lt;br&gt;
Execute the script with root privileges:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;bash create_users.sh user_list.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Review Logs and Passwords:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;View detailed logs in &lt;code&gt;/var/log/user_management.log&lt;/code&gt; to verify script execution.&lt;/li&gt;
&lt;li&gt;Access generated passwords securely stored in &lt;code&gt;/var/secure/user_passwords.csv&lt;/code&gt; as needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
By automating user management tasks with create_users.sh, organizations can streamline operations, enhance security, and ensure consistency across Linux environments. This script exemplifies best practices in IT administration, empowering sysadmins to focus on strategic initiatives while maintaining robust user access controls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/EkeminiThompson/user_automation.git"&gt;GitHub Repository:&lt;/a&gt; Access the script and related files.&lt;br&gt;
&lt;a href="https://hng.tech/internship"&gt;HNG Internship:&lt;/a&gt; Learn more about opportunities in tech and automation.&lt;br&gt;
&lt;a href="https://hng.tech/premium"&gt;HNG Premium:&lt;/a&gt; Additional resources for professional growth and development.&lt;/p&gt;

&lt;p&gt;About the Author&lt;br&gt;
Ekemini Thompson is a seasoned Linux system administrator passionate about leveraging automation to optimize IT operations.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>ubuntu</category>
      <category>bash</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
