CoolFace
Apppublic

KrishGoyani/AI_Data_Explorer_with_Gemini_API

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py71 linesDownload Raw Back to root
1import streamlit as st
2from langchain.agents.agent_types import AgentType
3from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
4from langchain_google_genai import ChatGoogleGenerativeAI
5import pandas as pd
6
7st.set_page_config(
8    page_title="AI Data Explorer",
9    page_icon="๐Ÿ’ป",
10)
11st.header("AI Data Explorer with Gemini API",divider="rainbow")
12
13
14api_key = st.sidebar.text_input("Enter your Gemini API key", type="password")
15
16# File uploader for CSV file
17uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type="csv")
18
19# Function to create and return an agent
20def 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
33st.markdown("""
34## About this Application ๐Ÿค–๐Ÿ“Š
35
36This application allows you to explore and analyze your dataset using an AI-powered agent. 
37You 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 ๐Ÿ› ๏ธ
401. ๐Ÿ”‘ Enter your Gemini API key in the sidebar.
412. ๐Ÿ“ Upload a CSV file containing your dataset.
423. โ“ Enter your query about the dataset in the input field provided.
434. ๐Ÿš€ The AI agent will process your query and display the results.
44
45The 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
49if 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}")
70else:
71    st.write("Please enter your Gemini API key and upload a CSV file")