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