File size: 1,594 Bytes
b503afb
 
a9ec8a3
b503afb
 
 
 
 
a9ec8a3
b503afb
a9ec8a3
b503afb
30899c9
 
ca6256e
 
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
45
46
47
48
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")

# Note that API key's running out of budget
contact_url = "https://www.linkedin.com/in/linhvuu"
st.write("If no result returns, it means I am running out of energy. Please contact [Linh Vuu](%s) to wake me up." % contact_url)
    
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.")