Spaces:
Sleeping
Sleeping
File size: 2,163 Bytes
ce2dbfb d28dafa 47fc6ad d28dafa ce2dbfb 57302a3 ce2dbfb |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import streamlit as st
import pandas as pd
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
import matplotlib.pyplot as plt
# from pandasai import SmartDataframe
from pandasai import SmartDataframe
import os
st.title("CarDekho.com Gen AI Chatbot Q&A PoC")
if "openai_key" not in st.session_state:
with st.form("API key"):
key = st.text_input("OpenAI Key", value="", type="password")
if st.form_submit_button("Submit"):
st.session_state.openai_key = key
st.session_state.prompt_history = []
st.session_state.df = None
st.success('Saved API key for this session.')
if "openai_key" in st.session_state:
if st.session_state.df is None:
uploaded_file = st.file_uploader(
"Choose a CSV file. This should be in long format (one datapoint per row).",
type="csv",
)
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
llm = OpenAI(api_token=st.session_state.openai_key)
df = SmartDataframe(df, config={"llm": llm})
st.session_state.df = df
with st.form("Question"):
question = st.text_input("Question", value="", type="default")
submitted = st.form_submit_button("Submit")
if submitted:
with st.spinner():
llm = OpenAI(api_token=st.session_state.openai_key)
pandas_ai = PandasAI(llm,enable_cache=False)
x = pandas_ai.run(st.session_state.df, prompt=question)
if os.path.isfile('temp_chart.png'):
im = plt.imread('temp_chart.png')
st.image(im)
os.remove('temp_chart.png')
if x is not None:
st.write(x)
st.session_state.prompt_history.append(question)
if st.session_state.df is not None:
st.subheader("Current dataframe:")
st.write(st.session_state.df)
st.subheader("Prompt history:")
st.write(st.session_state.prompt_history)
if st.button("Clear"):
st.session_state.prompt_history = []
st.session_state.df = None |