File size: 2,562 Bytes
3fe717c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import streamlit as st
from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain_google_genai import ChatGoogleGenerativeAI
import pandas as pd
st.set_page_config(
page_title="AI Data Explorer",
page_icon="π»",
)
st.header("AI Data Explorer with Gemini API",divider="rainbow")
api_key = st.sidebar.text_input("Enter your Gemini API key", type="password")
# File uploader for CSV file
uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type="csv")
# Function to create and return an agent
def create_agent(api_key, df, llm):
# Create the pandas agent with the DataFrame and LLM
agent = create_pandas_dataframe_agent(
llm,
df,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
allow_dangerous_code=True
)
return agent
# Application description
st.markdown("""
## About this Application π€π
This application allows you to explore and analyze your dataset using an AI-powered agent.
You can upload a CSV file and provide your Gemini API key to create an agent capable of answering questions about your data.
### How to Use π οΈ
1. π Enter your Gemini API key in the sidebar.
2. π Upload a CSV file containing your dataset.
3. β Enter your query about the dataset in the input field provided.
4. π The AI agent will process your query and display the results.
The AI agent leverages the power of a LangChain and large language model (LLM) to understand and analyze your data, providing insights and answers based on your questions.
""")
# Process the uploaded CSV file and create the agent
if uploaded_file is not None and api_key:
llm = ChatGoogleGenerativeAI(model="gemini-pro",google_api_key=api_key)
df = pd.read_csv(uploaded_file)
st.write("Uploaded CSV file:")
st.dataframe(df)
agent = create_agent(api_key, df, llm)
# Input field for user query
user_query = st.text_input("Enter your query about the dataset")
# Process the user query and display the result
if user_query:
with st.spinner('Processing your query...'):
try:
result = agent.run(user_query)
st.success("Query result:")
result
except Exception as e:
st.error(f"Error processing query: {e}")
else:
st.write("Please enter your Gemini API key and upload a CSV file") |