How to Use Llama 3 with PandasAI and Ollama Locally
AI Nuggets
Setting Up the Environment
Create a virtual environment using conda:
conda create -n genai
Activate the virtual environment:
conda activate genai
Installing Required Tools
Create a requirements.txt file and list the following libraries:
pandas
pandasai
streamlit
Install the required libraries using pip:
pip install -r requirements.txt
Initializing Llama 3 with Ollama
Start the Ollama service in the terminal:
ollama serve
Install the Llama 3 model using Ollama:
ollama pull llama3
List installed models to confirm Llama 3 is loaded:
ollama list
Setting Up the App with Streamlit
Create a Python file named app.py.
Import the necessary classes from pandasai and streamlit:
from pandasai.llm.local_llm import LocalLLM import streamlit as st
Initialize the Llama 3 model with Ollama:
model = LocalLLM(api_base="http://localhost:11434/v1", model="llama3")
Set the title of the app using Streamlit:
st.title("Data analysis with PandasAI")
Run the app with Streamlit:
streamlit run app.py
Building the App Interface
Create a file uploader widget for CSV files:
uploaded_file = st.file_uploader("Upload a CSV file", type=['csv'])
Check if a file is uploaded and display the first three rows:
if uploaded_file is not None: import pandas as pd data = pd.read_csv(uploaded_file) st.write(data.head(3))
Convert the dataset into a SmartDataframe:
from pandasai import SmartDataframe df = SmartDataframe(data, config={"llm": model})
Create a text area for user input:
prompt = st.text_area("Enter your prompt:")
Create a button to generate responses:
if st.button("Generate"): if prompt: with st.spinner("Generating response..."): st.write(df.chat(prompt))
Making Inferences with the App
Write prompts to interact with the dataset, such as:
“How many rows and columns are in the dataset?"
"What is the average age?"
"How many people have more than 3 siblings?"
"How many people died and how many survived?"
"Return the percentage of passengers by gender"
"Draw a bar chart of the sex column"
"Plot a pie chart of the pclass column"
"Visualize the distribution of the fare column"
"Draw the histogram of the age column"
"Draw the histogram of the fare column separated by the sex column"
"Draw the heatmap of numerical variables”
Tips and Reminders
Ensure the prompts are clear and specific to get accurate outputs.
It may take a few attempts to find the right prompts for the desired output.
Conclusion
The video covers the use of PandasAI with Llama-3 by building an app with Streamlit.
Remember to subscribe, like the video, and leave a comment.
Additional Information
The video description and additional URLs are not provided in the transcript. Please refer to the video on YouTube for any links or further instructions: http://youtube.com/watch?v=_dDaNgBDoHY