Build your First LLM Application using Gemini API

In this blog post, we’ll discuss the process of creating an application that extracts information from Aadhaar cards using Google’s Generative AI ( Gemini API ) and Streamlit. This application demonstrates how to combine cutting-edge AI technology with a user-friendly web interface to solve a real-world problem.

Use Case Example:

One significant application of our Aadhaar card information extraction tool is in Know Your Customer (KYC) verification processes. Here’s how it can be applied:

KYC Verification: Financial institutions, such as banks and insurance companies, often require customers to provide valid identification, like an Aadhaar card, during account opening or service registration. Traditionally, this process involves manual verification and data entry, which can be time-consuming and error-prone.

By integrating our tool, powered by Google’s Generative AI and Streamlit, these organizations can streamline their KYC processes significantly:

  • Efficiency: Customers can upload their Aadhaar card images through a user-friendly web interface.
  • Automation: The tool extracts essential information, including full name, date of birth, Aadhaar number, and gender, directly from the uploaded image.

Setting Up the Environment

First, we need to set up our environment with the necessary libraries:

  • Streamlit: For creating the web interface
  • google-generativeai: To interact with Google’s Generative AI
  • python-dotenv: For managing environment variables
  • Pillow: For handling image processing

You can install these packages using pip:

pip install streamlit pillow python-dotenv google-generativeai

We use a .env file to securely store our Google API key.

GOOGLE_API_KEY = "Enter your Gemini API"

Step-by-Step Implementation

1. Setting Up the Environment

First, we need to set up our environment and load the necessary libraries. We will use the dotenv package to load environment variables from a .env file, which will store our Google API key.

import streamlit as st
import google.generativeai as genai
from dotenv import load_dotenv
import os
from PIL import Image

load_dotenv()  # Load environment variables

my_api_key = os.getenv("GOOGLE_API_KEY")  # Getting API key from environment variables
genai.configure(api_key=my_api_key)

2. Processing the Aadhaar Card Image

Next, we define a function to process the uploaded Aadhaar card image and extract the required information using the Google Generative AI model.
We are going to extract full name, Date of Birth, Aadhar Number and Gender from aadhar card.

def process_image_aadhaar(image):
    model = genai.GenerativeModel('gemini-pro-vision')
    response = model.generate_content(["""Analyze the provided image and extract the following information:
                                        - Full Name
                                        - Date of Birth
                                        - Aadhar Number
                                        - Gender

                                        Present the extracted information in the following format:

                                        name: ...
                                        date_of_birth: ...
                                        aadhaar_number: ...
                                        gender: ...

                                        Important:
                                        1. Do not include any quotation marks, asterisks (*), underscores (_), backslashes (\), or forward slashes (/) in the output.
                                        2. Remove any of these characters if they appear in the extracted data.
                                        3. If any information is not visible or cannot be extracted from the image, replace it with N/A in the output.
                                        4. Maintain the exact order of fields as shown above.
                                        5. Include only the extracted values after each field name, without any additional punctuation.
                                        6. Ensure there is a space after each colon (:) and before the extracted value.""", image])
    extracted_info = response.text.split("\n")
    info_dict = {}
    for line in extracted_info:
        if ":" in line:
            key, value = line.split(":", 1)
            info_dict[key.strip().replace(" ", "_")] = value.strip()
    return info_dict

3. Building the Streamlit Interface

Now, let’s build the Streamlit interface where users can upload their Aadhaar card image and view the extracted information.

st.title("Aadhaar Card Information Extractor")

uploaded_file = st.file_uploader("Choose an Aadhaar card image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Aadhaar Card Image", use_column_width=True)
    
    if st.button("Extract Information"):
        with st.spinner("Processing image..."):
            extracted_info = process_image_aadhaar(image)
        
        st.subheader("Extracted Information:")
        for key, value in extracted_info.items():
            st.write(f"{key.replace('_', ' ').title()}: {value}")

Running the Application

To run the Streamlit application, use the following command:

streamlit run app.py

This will start the Streamlit server, and you can interact with the application through your web browser.

Summary

This project shows how to use powerful AI tools to create practical applications. By combining Google’s Generative AI with Streamlit, we’ve built a user-friendly tool that can quickly and accurately extract information from Aadhaar cards. This application can be useful in many real-world scenarios, from automating data entry to improving KYC processes.

Kaggle Master & Senior Data Scientist ( Ambitious, Adventurous, Attentive)

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This Post
Latest Post
7 Powerful Steps to Build Successful Data Analytics Project
Master Real-Time Image Resizing with Python: A 3-Step Guide
Mastering SQL for Average Product Ratings: Amazon Interview Question
SQL Query to find Top 3 Departments with the Highest Average Salary
Master SQL Today: 10 Powerful Hands-On Beginner’s Guide
Linear Regression: A Comprehensive Guide with 7 Key Insights
Meta LLAMA 3.1 – The Most Capable Large Language Model
Understanding Dispersion of Data: 4 Key Metrics Explained
Find Employees Who Joined on Same Date & Month: 3 Powerful SQL Methods
Ultimate Guide to Mastering Prompt Engineering Techniques – Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *