DEV Community

Cover image for Building a Mental Health Predictor with Machine Learning and FastAPI
Bek Brace
Bek Brace

Posted on

Building a Mental Health Predictor with Machine Learning and FastAPI

Hey everyone, welcome back! If you’ve been following along with my YouTube channel, you’ll know that in the last video I gave a quick demo of a Mental Health Predictor Machine Learning Project. Today, we’re taking it from idea to code — step by step.

Grab a cup of coffee, fire up your code editor (VS Code in my case), and let’s dive in.

Project Setup

We’ll start by creating a folder for our project. I named mine:

MHP-ML 
Enter fullscreen mode Exit fullscreen mode

Inside this folder, we’ll also set up a requirements.txt file to track our dependencies.

On Linux/Mac, you’d usually use the touch command to create files.

On Windows, you can use:

New-Item requirements.txt 
Enter fullscreen mode Exit fullscreen mode

Installing Dependencies

Here’s what we’ll need for this project:

  • FastAPI – our backend framework (0.105.4.1)
  • Uvicorn – the ASGI server to run FastAPI (0.24.0)
  • Streamlit – for the front-end interface
  • Pandas – data handling (1.3.x)
  • Scikit-learn – machine learning (1.3.2)
  • NumPy – numerical computations
  • SQLAlchemy – ORM for database handling
  • SQLite – lightweight database for storage
  • Python-multipart – for form data handling in FastAPI

Once the requirements.txt is ready, we’ll install everything inside a virtual environment.

Why a virtual environment?

It isolates the project dependencies so they don’t conflict with other Python projects on your machine. In JavaScript, this happens automatically with node_modules, but in Python we handle it explicitly.

To activate the environment with pipenv:

pipenv shell pipenv install -r requirements.txt 
Enter fullscreen mode Exit fullscreen mode

Project Structure

Here’s a clean project layout:

MHP-ML/ │── requirements.txt │── main.py │── models.py │── source/ │── app.py # FastAPI backend │── frontend.py # Streamlit frontend 
Enter fullscreen mode Exit fullscreen mode
  • main.py → Entry point for the app, runs the FastAPI server
  • models.py → Database models with SQLAlchemy
  • app.py → FastAPI routes (backend logic)
  • frontend.py → Streamlit interface

main.py (Entry Point)

This file is short and simple. We run our FastAPI application with Uvicorn:

import uvicorn if __name__ == "__main__": uvicorn.run("source.app:app", host="0.0.0.0", port=8000, reload=True) 
Enter fullscreen mode Exit fullscreen mode

Here’s what happens:

  • "source.app:app" → Tells Uvicorn to look for app inside source/app.py
  • host="0.0.0.0" → Makes it accessible from your network
  • port=8000 → Default port
  • reload=True → Auto-restarts the server on code changes (great for development)

Setting Up the Database (models.py)

We’ll use SQLAlchemy + SQLite for simplicity. This will allow us to store both user inputs and the predictions generated by our ML model.

from sqlalchemy import Column, Integer, String, DateTime, create_engine from sqlalchemy.orm import declarative_base, sessionmaker from datetime import datetime # Base class for our models Base = declarative_base() # Database engine (SQLite local file) engine = create_engine("sqlite:///health_predictions.db") # Session maker SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False) # Example table class HealthData(Base): __tablename__ = "health_data" id = Column(Integer, primary_key=True, index=True) user_input = Column(String, nullable=False) prediction = Column(String, nullable=False) created_at = Column(DateTime, default=datetime.utcnow) 
Enter fullscreen mode Exit fullscreen mode

This sets up:

  • A health_data table
  • Primary key id
  • user_input and prediction columns
  • A timestamp for when the record was created

Next Steps

With the foundation laid out:

  • Backend (FastAPI) will handle API requests
  • Frontend (Streamlit) will let users interact with the model
  • Machine Learning model will be integrated using Scikit-learn

In the next part, we’ll connect the dots: load a trained ML model, pass inputs from Streamlit to FastAPI, and store predictions in the database.


GitHub Repo

The full code for this tutorial is available on https://github.com/BekBrace/ML-MentalHealth-Predicator . Feel free to clone it, experiment, and customize it however you like.


Final Thoughts

This project combines multiple layers of modern Python development:

  • Web APIs with FastAPI
  • Interactive dashboards with Streamlit
  • Databases with SQLAlchemy + SQLite
  • Machine learning with Scikit-learn

It’s a practical way to learn how all these pieces fit together into a real-world application.

Top comments (1)

Collapse
 
evan_dickinson_7437ea81b9 profile image
Evan Dickinson

Why use a ML library for it and not a LLM api? Just curious.