Update app3.py
Browse files
app3.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
4 |
from pandasai import Agent
|
5 |
from pandasai.llm.openai import OpenAI
|
6 |
from langchain_community.embeddings.openai import OpenAIEmbeddings
|
@@ -28,21 +29,59 @@ if not api_key or not pandasai_api_key:
|
|
28 |
)
|
29 |
logger.error("API keys not found. Ensure they are set in the environment variables.")
|
30 |
else:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
try:
|
36 |
-
# Load the data
|
37 |
-
if uploaded_file.name.endswith('.xlsx'):
|
38 |
-
df = pd.read_excel(uploaded_file)
|
39 |
-
else:
|
40 |
-
df = pd.read_csv(uploaded_file)
|
41 |
-
|
42 |
-
st.write("Data Preview:")
|
43 |
-
st.write(df.head())
|
44 |
-
logger.info(f"Uploaded file loaded successfully with shape: {df.shape}")
|
45 |
-
|
46 |
# Initialize PandasAI Agent
|
47 |
llm = OpenAI(api_key=pandasai_api_key, max_tokens=1500, timeout=60)
|
48 |
agent = Agent(df, llm=llm)
|
@@ -105,8 +144,7 @@ else:
|
|
105 |
try:
|
106 |
result = agent.chat(viz_question)
|
107 |
|
108 |
-
#
|
109 |
-
import re
|
110 |
code_pattern = r'```python\n(.*?)\n```'
|
111 |
code_match = re.search(code_pattern, result, re.DOTALL)
|
112 |
|
@@ -114,12 +152,9 @@ else:
|
|
114 |
viz_code = code_match.group(1)
|
115 |
logger.debug(f"Extracted visualization code: {viz_code}")
|
116 |
|
117 |
-
# Modify code
|
118 |
viz_code = viz_code.replace('plt.', 'px.')
|
119 |
-
viz_code
|
120 |
-
|
121 |
-
# Execute the code and display the chart
|
122 |
-
exec(viz_code)
|
123 |
st.plotly_chart(fig)
|
124 |
else:
|
125 |
st.warning("Unable to generate a graph. Please try a different query.")
|
@@ -128,7 +163,5 @@ else:
|
|
128 |
st.error(f"An error occurred: {e}")
|
129 |
logger.error(f"Visualization error: {e}")
|
130 |
except Exception as e:
|
131 |
-
st.error(f"An error occurred while processing the
|
132 |
-
logger.error(f"
|
133 |
-
else:
|
134 |
-
st.info("Please upload a file to begin analysis.")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
+
from datasets import load_dataset
|
5 |
from pandasai import Agent
|
6 |
from pandasai.llm.openai import OpenAI
|
7 |
from langchain_community.embeddings.openai import OpenAIEmbeddings
|
|
|
29 |
)
|
30 |
logger.error("API keys not found. Ensure they are set in the environment variables.")
|
31 |
else:
|
32 |
+
def load_dataset_into_session():
|
33 |
+
"""Function to load a dataset into the session."""
|
34 |
+
input_option = st.radio("Select Dataset Input:", ["Use Repo Dataset", "Use Hugging Face Dataset", "Upload CSV File"])
|
35 |
+
|
36 |
+
# Option 1: Use Repo Dataset
|
37 |
+
if input_option == "Use Repo Dataset":
|
38 |
+
file_path = "./source/test.csv"
|
39 |
+
if st.button("Load Repo Dataset"):
|
40 |
+
try:
|
41 |
+
st.session_state.df = pd.read_csv(file_path)
|
42 |
+
st.success(f"File loaded successfully from '{file_path}'!")
|
43 |
+
st.dataframe(st.session_state.df.head(10))
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"Error reading file from path: {e}")
|
46 |
+
logger.error(f"Error reading file from path: {e}")
|
47 |
+
|
48 |
+
# Option 2: Use Hugging Face Dataset
|
49 |
+
elif input_option == "Use Hugging Face Dataset":
|
50 |
+
dataset_name = st.text_input("Enter Hugging Face Dataset Name:", value="HUPD/hupd")
|
51 |
+
if st.button("Load Hugging Face Dataset"):
|
52 |
+
try:
|
53 |
+
# Load Hugging Face dataset
|
54 |
+
dataset = load_dataset(dataset_name, split="train", trust_remote_code=True)
|
55 |
+
st.session_state.df = pd.DataFrame(dataset)
|
56 |
+
st.success(f"Dataset '{dataset_name}' loaded successfully!")
|
57 |
+
st.dataframe(st.session_state.df.head(10))
|
58 |
+
except Exception as e:
|
59 |
+
st.error(f"Error loading dataset from Hugging Face: {e}")
|
60 |
+
logger.error(f"Error loading Hugging Face dataset: {e}")
|
61 |
+
|
62 |
+
# Option 3: Upload CSV File
|
63 |
+
elif input_option == "Upload CSV File":
|
64 |
+
uploaded_file = st.file_uploader("Upload CSV File:", type=["csv"])
|
65 |
+
if uploaded_file:
|
66 |
+
try:
|
67 |
+
st.session_state.df = pd.read_csv(uploaded_file)
|
68 |
+
st.success("File uploaded successfully!")
|
69 |
+
st.dataframe(st.session_state.df.head(10))
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"Error reading uploaded file: {e}")
|
72 |
+
logger.error(f"Error reading uploaded file: {e}")
|
73 |
+
|
74 |
+
# Initialize session state for DataFrame
|
75 |
+
if "df" not in st.session_state:
|
76 |
+
st.session_state.df = None
|
77 |
+
|
78 |
+
# Load dataset into session
|
79 |
+
load_dataset_into_session()
|
80 |
+
|
81 |
+
# Proceed only if a DataFrame is loaded
|
82 |
+
if st.session_state.df is not None:
|
83 |
+
df = st.session_state.df
|
84 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
# Initialize PandasAI Agent
|
86 |
llm = OpenAI(api_key=pandasai_api_key, max_tokens=1500, timeout=60)
|
87 |
agent = Agent(df, llm=llm)
|
|
|
144 |
try:
|
145 |
result = agent.chat(viz_question)
|
146 |
|
147 |
+
# Extract Python code for visualization
|
|
|
148 |
code_pattern = r'```python\n(.*?)\n```'
|
149 |
code_match = re.search(code_pattern, result, re.DOTALL)
|
150 |
|
|
|
152 |
viz_code = code_match.group(1)
|
153 |
logger.debug(f"Extracted visualization code: {viz_code}")
|
154 |
|
155 |
+
# Safeguard: Modify and validate code for Plotly
|
156 |
viz_code = viz_code.replace('plt.', 'px.')
|
157 |
+
exec(viz_code) # Execute the visualization code
|
|
|
|
|
|
|
158 |
st.plotly_chart(fig)
|
159 |
else:
|
160 |
st.warning("Unable to generate a graph. Please try a different query.")
|
|
|
163 |
st.error(f"An error occurred: {e}")
|
164 |
logger.error(f"Visualization error: {e}")
|
165 |
except Exception as e:
|
166 |
+
st.error(f"An error occurred while processing the dataset: {e}")
|
167 |
+
logger.error(f"Dataset processing error: {e}")
|
|
|
|