Leveraging Llama 3 LLM for Multiple CSV File Analysis: A Comprehensive Guide

In this tutorial, we'll explore utilizing the power of Llama 3, an open-source LLM, to analyze and visualize multiple CSV files locally. By leveraging Pandas AI and Lama3, we ensure user privacy as everything is done 100% locally within the Streamlit framework.


Overview


Llama 3's utilization spans various domains, offering numerous applications. Here, we focus on creating a tool that interacts with CSV files using natural language queries, translating them into Python code and SQL queries. This tool uses Pandas AI to handle data manipulation and interpretation, making it accessible and efficient.

Key Components


1. Pandas AI: Uses a generative AI model to interpret natural language queries.

2. Llama 3: The local LLM is used for processing the queries.

3. Streamlit: Provides the framework for our application, ensuring an interactive and user-friendly interface.


Installation Guide

To get started, ensure your local machine meets the following requirements:

- Minimum 15-20 GB of RAM

- Python environment set up with necessary libraries


Steps to Build the Application


1. Setting Up the Environment

Install the required packages:

pip install pandasai

pip install streamlit

2. Creating the Application


  Here’s a step-by-step guide to building the application:

Code

import streamlit as st 

import pandas as pd # Pandas for data manipulation

from pandasai import SmartDataframe # SmartDataframe for interacting with data using LLM


# Function to chat with CSV data

def chat_with_csv(df,query):

     # Initialize LocalLLM with Meta Llama 3 model

    llm = LocalLLM(

    api_base="http://localhost:11434/v1",

    model="llama3")

    # Initialize SmartDataframe with DataFrame and LLM configuration

    pandas_ai = SmartDataframe(df, config={"llm": llm})

    # Chat with the DataFrame using the provided query

    result = pandas_ai.chat(query)

    return result


# Set layout configuration for the Streamlit page

st.set_page_config(layout='wide')

# Set title for the Streamlit application

st.title("Multiple-CSV ChatApp powered by LLM")


# Upload multiple CSV files

input_csvs = st.sidebar.file_uploader("Upload your CSV files", type=['csv'], accept_multiple_files=True)


# Check if CSV files are uploaded

if input_csvs:

    # Select a CSV file from the uploaded files using a dropdown menu

    selected_file = st.selectbox("Select a CSV file", [file.name for file in input_csvs])

    selected_index = [file.name for file in input_csvs].index(selected_file)


    #load and display the selected csv file 

    st.info("CSV uploaded successfully")

    data = pd.read_csv(input_csvs[selected_index])

    st.dataframe(data.head(3),use_container_width=True)


    #Enter the query for analysis

    st.info("Chat Below")

    input_text = st.text_area("Enter the query")


    #Perform analysis

    if input_text:

        if st.button("Chat with csv"):

            st.info("Your Query: "+ input_text)

            result = chat_with_csv(data,input_text)

            st.success(result)




3. Running the Application


   To run the application, use the following command:

   streamlit run app.py

  

Output:





Stay tuned for more tutorials and updates!

Post a Comment

Previous Post Next Post