eagle0504 commited on
Commit
6d2bb49
Β·
verified Β·
1 Parent(s): 61339b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import pandas as pd
3
+ import pygwalker as pyg
4
+ import streamlit as st
5
+ import streamlit.components.v1 as components
6
+ from pandasai import PandasAI
7
+ from pandasai.llm.openai import OpenAI
8
+ from streamlit_chat import message
9
+
10
+ # Adjust the width of the Streamlit page
11
+ st.set_page_config(page_title="W.Y.N. Data Viz ⭐", layout="wide")
12
+
13
+ # Add Title
14
+ st.title("W.Y.N. Data Viz ⭐")
15
+
16
+ # Sidebar
17
+ # Sidebar - Instruction Manual
18
+ with st.sidebar:
19
+ with st.expander("Instruction Manual πŸ“–"):
20
+ st.markdown(
21
+ """
22
+ # 🌟 Pyg.Walk Chatbot on Streamlit 🌟
23
+
24
+ This Streamlit application serves as a user-friendly interface, similar to Excel or PowerBI dashboards, designed to expedite the navigation and visualization of datasets. Powered by the `pyg.walk` package, it provides a seamless experience, allowing users to traverse their data with speed and efficiency πŸš€. The layout is intuitive, presenting data in an organized manner that's easy to analyze at a glance πŸ“Š.
25
+
26
+ ## πŸ’¬ Chatbot Interface
27
+
28
+ Behind the scenes, a chatbot interface is integrated into the application, enabling users to interact with their data conversationally πŸ€–. You can ask the chatbot simple questions regarding your dataset, such as:
29
+
30
+ - "What are the column names?" πŸ“
31
+ - "What is the average of [specific column]?" πŸ“ˆ
32
+
33
+ These questions are processed by the chatbot to provide quick, straightforward answers, making data analysis more accessible πŸ™Œ.
34
+
35
+ ## ⚠️ Limitations
36
+
37
+ Please note that the chatbot is designed for basic inquiries only πŸ›‘. It is not equipped to handle complex data analysis or sophisticated queries. To maintain accuracy and avoid potential errors, keep your questions simple. This interface is a prototype aimed at demonstrating the capabilities of `pyg.walk` and should not be used for in-depth analysis 🧐.
38
+
39
+ ## πŸš€ Getting Started
40
+
41
+ To begin, simply drag and drop your dataset in `.csv` format into the designated area of the application πŸ“βž‘οΈπŸ“Š. Once your file is uploaded, you can start asking your data-related questions in plain English ✍️. The chatbot will respond with the requested information or appropriate guidance on how to phrase your questions for optimal results πŸ’‘.
42
+
43
+ """
44
+ )
45
+ clear_button = st.sidebar.button("Clear Conversation", key="clear")
46
+ counter_placeholder = st.sidebar.empty()
47
+ st.sidebar.markdown(
48
+ "@ [Yiqiao Yin](https://www.y-yin.io/) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)"
49
+ )
50
+
51
+
52
+
53
+ # Initialization
54
+ # Session State also supports the attribute based syntax
55
+ if 'generated' not in st.session_state:
56
+ st.session_state.generated = []
57
+
58
+ # Session State also supports the attribute based syntax
59
+ if 'past' not in st.session_state:
60
+ st.session_state.past = []
61
+
62
+ # Reset everything
63
+ if clear_button:
64
+ st.session_state["generated"] = []
65
+ st.session_state["past"] = []
66
+ st.session_state["messages"] = [
67
+ {"role": "system", "content": "You are a helpful assistant."}
68
+ ]
69
+ st.session_state["number_tokens"] = []
70
+ st.session_state["domain_name"] = []
71
+ counter_placeholder.write(f"Next item ...")
72
+
73
+ # Insert a file uploader that accepts multiple files at a time
74
+ uploaded_file = st.file_uploader("Choose a CSV file")
75
+ if uploaded_file is not None:
76
+ # Success message
77
+ st.success("File uploaded successfully.")
78
+
79
+ # Can be used wherever a "file-like" object is accepted:
80
+ df = pd.read_csv(uploaded_file)
81
+
82
+ # Generate the HTML using Pygwalker
83
+ pyg_html = pyg.walk(df, return_html=True)
84
+
85
+ # Embed the HTML into the Streamlit app
86
+ components.html(pyg_html, height=1000, scrolling=True)
87
+
88
+ # Instantiate a LLM
89
+ OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
90
+ llm = OpenAI(api_token=OPENAI_API_KEY)
91
+ pandas_ai = PandasAI(llm)
92
+
93
+ # Container for chat history
94
+ response_container = st.container()
95
+ container = st.container()
96
+ with container:
97
+ with st.form(key="my_form", clear_on_submit=True):
98
+ user_input = st.text_area(
99
+ "Enter your question here:", key="input", height=100
100
+ )
101
+ submit_button = st.form_submit_button(label="Send")
102
+
103
+ if submit_button:
104
+ output = pandas_ai(df, prompt=user_input)
105
+ st.session_state["past"].append(user_input)
106
+ st.session_state["generated"].append(
107
+ {"type": "normal", "data": f"{output}"}
108
+ )
109
+
110
+ if st.session_state["generated"]:
111
+ with response_container:
112
+ for i in range(len(st.session_state["generated"])):
113
+ message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
114
+ answer = st.session_state["generated"][i]["data"]
115
+ message(answer)
116
+ counter_placeholder.write(f"All rights reserved @ Yiqiao Yin")
117
+
118
+ else:
119
+ st.warning("Please upload a csv file.")