-
Notifications
You must be signed in to change notification settings - Fork 17
/
streamlit_app.py
59 lines (42 loc) · 1.37 KB
/
streamlit_app.py
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
import os
import streamlit as st
from pandasai import SmartDataframe
from pandasai.callbacks import BaseCallback
from pandasai.llm import OpenAI
from pandasai.responses.response_parser import ResponseParser
from data import load_data
class StreamlitCallback(BaseCallback):
def __init__(self, container) -> None:
"""Initialize callback handler."""
self.container = container
def on_code(self, response: str):
self.container.code(response)
class StreamlitResponse(ResponseParser):
def __init__(self, context) -> None:
super().__init__(context)
def format_dataframe(self, result):
st.dataframe(result["value"])
return
def format_plot(self, result):
st.image(result["value"])
return
def format_other(self, result):
st.write(result["value"])
return
st.write("# Chat with Credit Card Fraud Dataset 🦙")
df = load_data("./data")
with st.expander("🔎 Dataframe Preview"):
st.write(df.tail(3))
query = st.text_area("🗣️ Chat with Dataframe")
container = st.container()
if query:
llm = OpenAI(api_token=os.environ["OPENAI_API_KEY"])
query_engine = SmartDataframe(
df,
config={
"llm": llm,
"response_parser": StreamlitResponse,
"callback": StreamlitCallback(container),
},
)
answer = query_engine.chat(query)