File size: 1,556 Bytes
18393e8
434df30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453d53c
 
434df30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_experimental.agents import create_csv_agent
from langchain.agents.agent_types import AgentType
from langchain.llms import OpenAI
import streamlit as st

def main():
    st.set_page_config(page_title="Ask your CSV")
    st.header("Ask your CSV 📈")
    
    # API key input
    api_key = st.text_input("Enter your OpenAI API key:", type="password")
    
    # File uploader
    csv_file = st.file_uploader("Upload a CSV file", type="csv")
    
    # Process only if both API key and file are provided
    if api_key and csv_file is not None:
        # Create agent with user-provided API key
        try:
            agent = create_csv_agent(
                OpenAI(temperature=0, api_key=api_key), 
                csv_file, 
                verbose=True,
                agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
                allow_dangerous_code=True
            )
            
            # User question input
            user_question = st.text_input("Ask a question about your CSV: ")
            
            # Process question if provided
            if user_question and user_question != "":
                with st.spinner(text="In progress..."):
                    st.write(agent.run(user_question))
        except Exception as e:
            st.error(f"Error: {str(e)}")
    elif not api_key and csv_file is not None:
        st.info("Please enter your OpenAI API key to proceed.")
    elif api_key and csv_file is None:
        st.info("Please upload a CSV file to proceed.")

if __name__ == "__main__":
    main()