File size: 1,363 Bytes
b503afb
 
a9ec8a3
b503afb
 
 
 
 
a9ec8a3
b503afb
a9ec8a3
b503afb
 
 
 
 
 
 
a9ec8a3
 
b503afb
a9ec8a3
b503afb
a9ec8a3
 
b503afb
 
 
a9ec8a3
 
 
 
 
 
 
 
 
 
 
 
 
 
b503afb
a9ec8a3
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
import streamlit as st 
import pandas as pd
from pandasai import SmartDataframe
from pandasai.llm.openai import OpenAI

openai_api_key = st.secrets["openai_api_key"]

# create an LLM by instantiating OpenAI object, and passing API token
llm = OpenAI(api_token = openai_api_key)

st.title("Data analysis with PandasAI")

uploaded_file = st.file_uploader("Upload a CSV file for analysis", type=['csv'])

if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
    st.write(df.head(3))

    # create PandasAI object, passing the LLM   
    sdf = SmartDataframe(df, config={"llm": llm})

    prompt = st.text_area("Enter your question:")

    # Generate the answer
    if st.button("Find the answer"):
        if prompt:
            # call pandas_ai.run(), passing dataframe and prompt
            with st.spinner("Generating response..."):
                
                st.write("The answer is: ")
                st.write(sdf.chat(prompt))

                st.write("Here is the code to get the answer: ")
                code_string = sdf.last_code_generated

                # Split the string into separate lines
                code_lines = code_string.strip().split('\n')

                # Print the lines
                for line in code_lines:
                    st.write(line)

        else:
            st.warning("Please enter a question.")