-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm-powered-data-analysis.py
More file actions
51 lines (35 loc) · 1.42 KB
/
llm-powered-data-analysis.py
File metadata and controls
51 lines (35 loc) · 1.42 KB
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
import streamlit as st
from pandasai.llm.openai import OpenAI
from dotenv import load_dotenv
import os
import pandas as pd
from pandasai import PandasAI
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_API_KEY"] = "your_key"
def chat_with_csv(df, prompt):
llm = OpenAI(api_token=openai_api_key)
pandas_ai = PandasAI(llm)
result = pandas_ai.run(df, prompt=prompt, show_code=True, is_conversational_answer=True)
return result
st.set_page_config(layout='wide')
st.title("Multiple-CSV ChatApp powered by LLM")
# Upload multiple CSV files
input_csvs = st.file_uploader("Upload your CSV files", type=['csv'], accept_multiple_files=True)
if input_csvs:
# Select a CSV file from the uploaded files using a dropdown menu
selected_file = st.selectbox("Select a CSV file", [file.name for file in input_csvs])
selected_index = [file.name for file in input_csvs].index(selected_file)
# load and display the selected csv file
st.info("CSV uploaded successfully")
data = pd.read_csv(input_csvs[selected_index])
st.dataframe(data, use_container_width=True)
# Enter the query for analysis
st.info("Chat Below")
input_text = st.text_area("Enter the query")
# Perform analysis
if input_text:
if st.button("Chat with csv"):
st.info("Your Query: " + input_text)
result = chat_with_csv(data, input_text)
st.success(result)