Tips for developing end-to-end GenAI, ML, and DS Applications

Sameer Mahajan
4 min readMay 28, 2024

--

Background

Whenever you are working with any Generative AI / Machine Learning or Data Science project, more is needed to demonstrate the functionality than only from a notebook. In that case, you must work with many other components, from the backend database to the front-end UI. You need to use them just to demonstrate your core functionality. You don’t necessarily have to scale/fine-tune them and don’t have much time on hand to make them work with your core system! Here are some quick tips to get your end-to-end project working under these circumstances.

Backend Database

You typically require some persistent storage to save your data. A database is a good option. There is a variety of relational (Postgres, MySQL, etc.) as well as No-SQL databases (MongoDB, Cassandra, etc.) for this. Of course, you can also use a file system (local as well as remote file server) with data in various formats like CSV, JSON, etc. depending on your requirements. You can work with it by leveraging various packages like pandas etc. Here we will take a closer look at the Postgres database for a combination of its popularity, scaling abilities, and eventual production deployments.

When Postgres is installed by default its pg_hba.conf has peer authentication. Due to this, you cannot connect to it with a standard

psql -U <user name>

In that case, you need to connect to it with a command like

sudo -i -u postgres

Alternatively, or if you eventually need to connect to it with PSQL with a user name, you can change its authentication to md5 (from default peer). In case of multi-machine deployments where your backend database server is remote, you may want to have your Postgres database server accessible remotely from your web server / front end. In that case, you need to modify your pg_hba.conf to enter the Address of your remote system from where you want to access your database with an entry like

host    all             all             <remote system IP>  md5

AND modify postgresql.conf for appropriate listen_addresses (e.g. public IP of your server hosting the database to which the remote system will connect). This remote access could be for development purposes (e.g. from laptops of all developers to a central database server, including one hosted in the cloud, etc.) or deployment requirements (e.g. end customer’s database, etc.).

API Server

Sometimes you want to develop your functionality so that it can be accessed from a browser or some HTTP client, including a rich React UI application. In that case, you can use simply use API Servers like

You can install fastapi with

$pip install fastapi

Here is a simple Hello World program in FastAPI

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}

You can save it in a file, say main.py, and execute it as

$fastapi dev main.py

you can connect to your running program from a browser using the URL

 http://127.0.0.1:8000/

to see Hello World getting printed.

You can see from basic Hello World to moderately complex interactive applications in my other blog

along with a link to my GitHub with a complete code listing.

UI

Sometimes you need to have some UI capabilities. For rapid prototyping, you can use

You can see a demonstration of some working code in my

spaces. You can also take a look at the underlying code there for any of the above spaces of your interest.

Another good option for rapid UI development is

It has a FastAPI web server bundled in it. The UI code is also well embedded within the backend Python code for ease of development as well as deployment. It is based on a refresh model which means that the entire code gets re-executed on every user interaction. You might have to make use of session_state in some cases. For this, you can take a look at some sample code snippets at my repo of

--

--

Sameer Mahajan
Sameer Mahajan

Written by Sameer Mahajan

Generative AI, Machine Learning, Deep Learning, AI, Traveler

Responses (1)