Spaces:
Sleeping
Sleeping
Commit
·
ce2dbfb
1
Parent(s):
16126b1
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from pandasai import PandasAI
|
| 4 |
+
from pandasai.llm.openai import OpenAI
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from pandasai import SmartDataframe
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
st.title("CarDekho.com Gen AI Chatbot Q&A PoC")
|
| 10 |
+
|
| 11 |
+
if "openai_key" not in st.session_state:
|
| 12 |
+
with st.form("API key"):
|
| 13 |
+
key = st.text_input("OpenAI Key", value="", type="password")
|
| 14 |
+
if st.form_submit_button("Submit"):
|
| 15 |
+
st.session_state.openai_key = key
|
| 16 |
+
st.session_state.prompt_history = []
|
| 17 |
+
st.session_state.df = None
|
| 18 |
+
st.success('Saved API key for this session.')
|
| 19 |
+
|
| 20 |
+
if "openai_key" in st.session_state:
|
| 21 |
+
if st.session_state.df is None:
|
| 22 |
+
uploaded_file = st.file_uploader(
|
| 23 |
+
"Choose a CSV file. This should be in long format (one datapoint per row).",
|
| 24 |
+
type="csv",
|
| 25 |
+
)
|
| 26 |
+
if uploaded_file is not None:
|
| 27 |
+
df = pd.read_csv(uploaded_file)
|
| 28 |
+
llm = OpenAI(api_token=st.session_state.openai_key)
|
| 29 |
+
df = SmartDataframe(df, config={"llm": llm})
|
| 30 |
+
st.session_state.df = df
|
| 31 |
+
|
| 32 |
+
with st.form("Question"):
|
| 33 |
+
question = st.text_input("Question", value="", type="default")
|
| 34 |
+
submitted = st.form_submit_button("Submit")
|
| 35 |
+
if submitted:
|
| 36 |
+
with st.spinner():
|
| 37 |
+
llm = OpenAI(api_token=st.session_state.openai_key)
|
| 38 |
+
pandas_ai = PandasAI(llm)
|
| 39 |
+
x = pandas_ai.run(st.session_state.df, prompt=question)
|
| 40 |
+
|
| 41 |
+
if os.path.isfile('temp_chart.png'):
|
| 42 |
+
im = plt.imread('temp_chart.png')
|
| 43 |
+
st.image(im)
|
| 44 |
+
os.remove('temp_chart.png')
|
| 45 |
+
|
| 46 |
+
if x is not None:
|
| 47 |
+
st.write(x)
|
| 48 |
+
st.session_state.prompt_history.append(question)
|
| 49 |
+
|
| 50 |
+
if st.session_state.df is not None:
|
| 51 |
+
st.subheader("Current dataframe:")
|
| 52 |
+
st.write(st.session_state.df)
|
| 53 |
+
|
| 54 |
+
st.subheader("Prompt history:")
|
| 55 |
+
st.write(st.session_state.prompt_history)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if st.button("Clear"):
|
| 59 |
+
st.session_state.prompt_history = []
|
| 60 |
+
st.session_state.df = None
|