PandasAI:with Streamlit
PandasAIに対してStreamlitを使って日本語で質問してみました。
PandasAIは開発途上とのことですが、どこまで出来る様になるのかウォッチできればと思っています。(データは有名なタイタニックのデータ)
import os
from dotenv import load_dotenv
import pandas as pd
import streamlit as st
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
from pandasai.middlewares.streamlit import StreamlitMiddleware
load_dotenv()
API_KEY = os.environ['OPENAI_API_KEY']
llm = OpenAI(api_token=API_KEY)
pandas_ai = PandasAI(
llm,
verbose=True,
enable_cache=False,
enforce_privacy=True,
conversational=True,
middlewares=[StreamlitMiddleware()],
)
st.markdown(
"<h1 style='font-size: 28px;'>Prompt-driven analysis with PandasAI</h1>",
unsafe_allow_html=True
)
upload_file = st.file_uploader(
"Please upload a csv-file for analysis", type=['csv']
)
if upload_file is not None:
df = pd.read_csv(upload_file, encoding='utf-8')
st.write(df.head(3))
prompt = st.text_area("Enter your prompt:")
if st.button("Generate"):
if prompt:
with st.spinner("PandasAI is generating an answer, please wait..."):
st.write(pandas_ai.run(df, prompt=prompt))
else:
st.warning("Please enter a prompt.")