Home Artificial Intelligence Talk To Your CSV: How To Visualize Your Data With Langchain And Streamlit 1. Arrange your environment Let’s try it out! Reference : Level Up Coding

Talk To Your CSV: How To Visualize Your Data With Langchain And Streamlit 1. Arrange your environment Let’s try it out! Reference : Level Up Coding

3
Talk To Your CSV: How To Visualize Your Data With Langchain And Streamlit
1. Arrange your environment
Let’s try it out!
Reference :
Level Up Coding

https://www.canva.com/

Large model models (LLMS) have progressively grown powerful and competently. These models will be used for a wide selection of applications, comparable to text generation, language translation, and providing answers to queries.

What’s Langchain?

In my previous article, I made an introduction to Langchain and its inner workings. If you happen to check my columns, you’ll find it offers an informative and detailed explanation of how Langchain works

On this tutorial, I’ll show you find out how to use Langchain and Streamlit to investigate CSV files, We’ll leverage the OpenAI API for GPT-3 access, and employ Streamlit for user interface development. It will enable users to upload CSV files and pose queries in regards to the data. The system will generate the answers and illustrate the knowledge with tables and charts.

Let’s start coding

1. You should start by making a in your local machine.

First, open your terminal and create a virtual environment.

python -m venv venv

then activate it:

venvScriptsactivate

You need to see () within the terminal now.

Now, let’s install the required dependencies:

Pip install langchain==0.0.146 , python-dotenv==1.0.0 , 
streamlit== 1.22.0 , open ai == 0.27.7 , tabulate==0.9.0

Finally, we’ll have to set an environment variable for the OpenAI API key:

set OPENAI_API_KEY=

Now, that we’re all set, let’s start!

Create a file named “, where we are going to write the functions for answering questions.

let’s import the required dependencies:

from langchain import OpenAI
from langchain.agents import create_pandas_dataframe_agent
import pandas as pd
from dotenv import load_dotenv
import json
import streamlit as st

Read the file

load_dotenv()
openai_key = os.getenv("OPENAI_API_KEY")

Creating the agent is a straightforward process since we’re going to use the provided by Langchain! For individuals who won’t be acquainted with it, an agent is a component that will help to tap into a group of tools, making decisions on which to make use of based on the users’ input. There are mainly 2 categories of agents: “Motion Agents” and “Plan-and-Execute Agents”.

Motion Agents determine a plan of action and carry it out by order, one after the other phase. Furthermore, they will take out and manage data from various sources, comparable to databases, APIs, and on this particular scenario, a CSV file.

Create Tool_CSV Function

The function allows a path of a CSV file as its input and a return agent that may access and use a big language model (LLM). this function generates an OpenAI object, reads the CSV file after which converts it right into a Pandas DataFrame. Finally, it formulates a Pandas DataFrame agent which is then returned.

def csv_tool(filename : str):

df = pd.read_csv(filename)
return create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True)

Create Ask_agent Function

The query_agent function is the powerhouse of this process. It accepts a pandas_dataframe_agent and a question as input, returning the agent’s response in string format. the function generates a prompt for the agent, specifying the form of responses we’re in search of. The goal is to have the agent provide a string that may subsequently be converted right into a dictionary. Depending on what’s contained inside this dictionary, this system will manifest a graph, table, or easy text response.

def ask_agent(agent, query):
"""
Query an agent and return the response as a string.

Args:
agent: The agent to question.
query: The query to ask the agent.

Returns:
The response from the agent as a string.
"""
# Prepare the prompt with query guidelines and formatting
prompt = (
"""
Let's decode the method to reply to the queries. The responses rely upon the form of information requested within the query.

1. If the query requires a table, format your answer like this:
{"table": {"columns": ["column1", "column2", ...], "data": [[value1, value2, ...], [value1, value2, ...], ...]}}

2. For a bar chart, respond like this:
{"bar": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}

3. If a line chart is more appropriate, your reply should appear to be this:
{"line": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}

Note: We only accommodate two kinds of charts: "bar" and "line".

4. For a plain query that does not need a chart or table, your response ought to be:
{"answer": "Your answer goes here"}

For instance:
{"answer": "The Product with the very best Orders is '15143Exfo'"}

5. If the reply isn't known or available, respond with:
{"answer": "I have no idea."}

Return all output as a string. Remember to encase all strings within the "columns" list and data list in double quotes.
For instance: {"columns": ["Products", "Orders"], "data": [["51993Masc", 191], ["49631Foun", 152]]}

Now, let's tackle the query step-by-step. Here's the query so that you can work on:
"""
+ query
)

# Run the prompt through the agent and capture the response.
response = agent.run(prompt)

# Return the response converted to a string.
return str(response)

The function called is crucial part. It takes in two things — an agent (pandas dataframe) and a matter (query).

This function starts by making a ‘prompt’, which is like a matter or instruction for the agent. This prompt tells the agent what form of answers we would like.

The goal is for the agent to provide back a string, which is a form of data that’s text-based. Later, this string will likely be turned into a dictionary, one other form of data that features key-value pairs, form of like a real-life dictionary that pairs a word (key) with its meaning (value).

Depending on what’s in that dictionary, this system will then show a graph, a table or simply a text answer.

Streamlit is a free and open-source framework to quickly create and share beautiful machine learning and data science web apps. It’s a Python-based library designed to be quick and straightforward to make use of and construct beautiful interactive apps with none JavaScript or CSS knowledge.

def decode_response(response: str) -> dict:
"""This function converts the string response from the model to a dictionary object.

Args:
response (str): response from the model

Returns:
dict: dictionary with response data
"""
return json.loads(response)

The function will change the agent’s response, a string, right into a dictionary for easier use.

def write_answer(response_dict: dict):
"""
Write a response from an agent to a Streamlit app.

Args:
response_dict: The response from the agent.

Returns:
None.
"""

# Check if the response is a solution.
if "answer" in response_dict:
st.write(response_dict["answer"])

# Check if the response is a bar chart.
# Check if the response is a bar chart.
if "bar" in response_dict:
data = response_dict["bar"]
try:
df_data = {
col: [x[i] if isinstance(x, list) else x for x in data['data']]
for i, col in enumerate(data['columns'])
}
df = pd.DataFrame(df_data)
df.set_index("Products", inplace=True)
st.bar_chart(df)
except ValueError:
print(f"Couldn't create DataFrame from data: {data}")

# Check if the response is a line chart.
if "line" in response_dict:
data = response_dict["line"]
try:
df_data = {col: [x[i] for x in data['data']] for i, col in enumerate(data['columns'])}
df = pd.DataFrame(df_data)
df.set_index("Products", inplace=True)
st.line_chart(df)
except ValueError:
print(f"Couldn't create DataFrame from data: {data}")

# Check if the response is a table.
if "table" in response_dict:
data = response_dict["table"]
df = pd.DataFrame(data["data"], columns=data["columns"])
st.table(df)

This function accepts a response dictionary and uses it to display output on the Streamlit app. This might include answers, bar graphs, line graphs, and tables.

The function first sees if the response is an ‘answer’, meaning a daily text response for questions like ‘What number of rows are within the document?’. If that’s the case, it’s going to show the reply on the app.

Next, the function verifies if the response is supposed to generate a bar chart. In that case, it uses the response data to create the bar chart and displays it on the app.

The function also checks if the response is meant to create a line chart. If that’s the case, it forms a line chart using the response data and displays it on the app.

The function further checks if the response is supposed for a table. In that case, it builds a table from the response data and displays it on the app.

Last but not least, we’ll construct the initial interface. Add the next lines:

st.set_page_config(page_title="👨‍💻 Talk along with your CSV")
st.title("👨‍💻 Talk along with your CSV")

st.write("Please upload your CSV file below.")

data = st.file_uploader("Upload a CSV" , type="csv")

query = st.text_area("Send a Message")

if st.button("Submit Query", type="primary"):
# Create an agent from the CSV file.
agent = csv_tool(data)

# Query the agent.
response = ask_agent(agent=agent, query=query)

# Decode the response.
decoded_response = decode_response(response)

# Write the response to the Streamlit app.
write_answer(decoded_response)

This code makes a Streamlit app where users can talk with their CSV files. First, the app asks the user to upload a CSV file. Then, it requests the user to type in a question. If the user hits the “Submit Query” button, the app will ask the agent a matter and display the reply on the app.

The app uses the next functions:

— : It generates an agent from a CSV file.

— : This function asks a matter to an agent and provides back the reply.

— This function translates an agent’s response.

— : This function generates a response on a Streamlit app.

Now, run the appliance by typing ‘’ within the terminal.

This command will open a latest window in your browser that ought to appear as if this:

For this tutorial, I’m going to make use of data that will be found on my GitHub

Go ahead and upload your CSV file, and begin entering your queries!

First query: Which Products have the very best orders?

Second query: Tabulate the primary 5 Products. Include the Products and Order columns only.?

Third query: Create a line graph on the primary 5 products. use the Products because the column and Orders as an information values

Forth query: Create a bar graph on the primary 5 products. use the Products because the column and Orders as an information values

In Conclusion:

Langchain and Streamlit are useful tools that simplify the method for users to inquire about their data using Language Models. The appliance lets users see their data in visual forms

It’s an excellent tool for members who need a deeper understanding of their data or to make sense of it.

Don’t hesitate to contact us if you’ve gotten any queries!

https://python.langchain.com/en/latest/modules/agents/agents/examples/chat_conversation_agent.html?highlight=agent_chain

https://docs.streamlit.io/

https://www.datacamp.com/tutorial/streamlit

📻 Stay tuned for more details on trending AI-related implementations and discussions on

🧙‍♂️ We’re AI application experts! If you should collaborate on a project, drop an stop by or shoot us a

📚Be happy to examine out my other articles:

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here